I'm working on a project that I have two sensors, two Arduinos (a YUN and an UNO), two LED's, and an RF transmitter/receiver
It's a pretty simple procedure: Activate a force sensor, send it via RF to the receiver and light up the corresponding LED. So my design looks like this -
Sensor 1 and Sensor 2 ---> Arduino YUN ---> RF Transmitter --> Wireless
RF Receiver ---> Arduino UNO ---> LED 1 and LED 2
So if Sensor 1 is activated LED 1 lights up and if Sensor 2 is activated LED 2 lights up.
So far I am able to send the signal and distinguish between each sensor by viewing the serial monitor. If I hit sensor 1, the Uno receives sensor 1's signal. Same with sensor 2. I'm just stuck on how to get the LED's to activate. I tried Serial.read() code so whenever I send Sensor 1 or Sensor 2, it would read the serial then digitalWrite the led high. But that didn't work. Any advice would be much appreciated. Here's my code if you would like to look at it.
Transmitter:
// Transmitter Arduino Yun
#include <VirtualWire.h>
int pin0 = 0;
int pin1 = 1;
void setup()
{
vw_set_ptt_inverted(true); // Required by the RF module
vw_setup(2000); // bps connection speed
Serial.begin (9600);
vw_set_tx_pin(3); // Arduino pin to connect the transmitter data pin
}
void loop()
{
int force = analogRead(pin0); // analog reading from the Force sense resistor
int force1 = analogRead(pin1); // analog reading from the Force1 sense resistor
const char *msg = "Sensor 1";
const char *ms = "Sensor 2";
if (force > 100) {
vw_send((uint8_t *)msg, strlen(msg));
digitalWrite(led_pin, HIGH);
vw_wait_tx(); // We wait to finish sending the message
delay(200); // We wait to send the message again
}
if (force1 > 100) {
vw_send((uint8_t *)ms, strlen(ms));
digitalWrite(led_pin1, HIGH);
vw_wait_tx(); // We wait to finish sending the message
delay(200); // We wait to send the message again
}
}
And the Receiver:
//Reciever
#include <VirtualWire.h>
const int led_pin = 3;
const int led_pin1 = 4;
char data[20];
void setup()
{
pinMode (3,OUTPUT);
pinMode (4, OUTPUT);
Serial.begin(9600); //Debugging
vw_set_ptt_inverted(true); //Required
vw_setup(2000); //bps
vw_set_rx_pin(2);
vw_rx_start(); //Start receiver
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen))
{
int i;
for (i = 0; i < buflen; i++)
{
Serial.write(buf[i]); // The received data is stored in the buffer
// and sent through the serial port to the computer
}
}
if (Serial.available()) {
char ser = Serial.read();
if(ser == 'Sensor 1') {
digitalWrite(led_pin, HIGH);
}
else if(ser == 'Sensor 2') {
digitalWrite(led_pin1, HIGH);
}
}
}
Thanks again for any insight.
-
\$\begingroup\$ Why are you only reading a single character? Why are you attempting to compare with a (valid yet suspect) multiple-character character? \$\endgroup\$Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2014年04月03日 00:50:37 +00:00Commented Apr 3, 2014 at 0:50
-
\$\begingroup\$ Sorry, I don't understand. I'm a beginner to Arduino. \$\endgroup\$user35653– user356532014年04月03日 02:06:38 +00:00Commented Apr 3, 2014 at 2:06
-
\$\begingroup\$ Has this question got anything to do with RF, wireless, transmitter or receiver tags or is it just about driving an LED? \$\endgroup\$Andy aka– Andy aka2014年04月03日 14:32:34 +00:00Commented Apr 3, 2014 at 14:32
-
\$\begingroup\$ Yes, it does involve the rf transmitter and receiver. I'm sending the sensor signal over rf and I didn't know if the fact that the leds not lighting up had to do with my rf code. \$\endgroup\$user35653– user356532014年04月03日 23:10:52 +00:00Commented Apr 3, 2014 at 23:10
1 Answer 1
you are comparing a single character, which may be 'S' which is the first character in teh string "Sensor 1", with, again, A string literal which you have declared as a character.
In C programming a single quote ' mark means that its an ASCII character, like '&' or 's'. If you want a string, you use doublequote, "Sensor" and even single character strings work "S".
What you need to do is set up a char buffer as an empty "string" for the received data.
char rxbuffer[64]; //64 byte long char buffer!
memset (rxbuffer,0,64); //this clears the bytes to 0. otherwise could be garbage data
64 is arbitrary, but means it can hold 63 character long string, and a null terminator at the end.
then you should use the arduino Serial.readBytesUntil() function. http://arduino.cc/en/Serial/ReadBytesUntil
This would looke like:
//you need to include this at the top!!
#include <string.h>
if(Serial.available()){
Serial.readBytesUntil('0円', rxbuffer, 64);
//use the C++ standard function call strstr to search buffer for matchin
//input string
//was it sensor 1 command?
if( strstr(rxbuffer, "Sensor 1") ){
digitalWrite(LED1, HIGH);
}else{
//maybe it was sensor 2?
if( strstr(rxbuffer, "Sensor 2") ){
digitalWrite(LED2, HIGH);
}
} //end if it wasnt first sensor message
}//end serial available, processing message after read
So there you go, that should work. change the LED pin numbers/names though
-
\$\begingroup\$ your string comparison of: if(ser == "Sensor 1" ) is okay, but may fail if random extra stuff happens to also come in the message. my use of strstr is a built in function that DOES work with Arduino (i use it for my remote sensor networking and Synapse integration) with similar style of message. in my case, I look for "SETUP 1" hehe. \$\endgroup\$KyranF– KyranF2014年04月03日 03:46:31 +00:00Commented Apr 3, 2014 at 3:46
-
\$\begingroup\$ I tried researching the string.h library but couldn't find it. Did you download this library or is it already on the arduino? \$\endgroup\$user35653– user356532014年04月07日 14:19:55 +00:00Commented Apr 7, 2014 at 14:19
-
\$\begingroup\$ Did you try to include it the way I showed? The proper c++ library is called Cstrings \$\endgroup\$KyranF– KyranF2014年04月07日 14:58:35 +00:00Commented Apr 7, 2014 at 14:58
-
\$\begingroup\$ Here is the reference. cplusplus.com/reference/cstring/strstr/?kw=strstr \$\endgroup\$KyranF– KyranF2014年04月07日 15:01:55 +00:00Commented Apr 7, 2014 at 15:01
-
\$\begingroup\$ But like I show you include string.h to get all those functions \$\endgroup\$KyranF– KyranF2014年04月07日 15:03:37 +00:00Commented Apr 7, 2014 at 15:03
Explore related questions
See similar questions with these tags.