Readling Serial communication from RF links

Hi!

Im trying to turn 4 LEDs on when pressing 4 buttons. The thing is that the LEDs and buttons is separated. They are connected to 2 different Arduino boards. They connect using wireless RF links.

I managed to make the two boards communicated perfectly. When pressing one of the buttons on the first Arduino, the other Arduino receives what it should. The LEDs just aren’t lighting up.

Transmitter code:

#include <VirtualWire.h>

const char *red = "r";
const char *green = "g";
const char *yellow = "y";
const char *green2 = "G";


const int greenButton = 2;
const int yellowButton = 3;
const int redButton = 4;
const int greenButton2 = 5;

void setup()
{
  Serial.begin(9600);	  
  Serial.println("setup");

  //Button pin setup
  pinMode(greenButton, INPUT);
  pinMode(yellowButton, INPUT);
  pinMode(redButton, INPUT);
  pinMode(greenButton2, INPUT);

  // Tranmitter setup
  vw_setup(4000);	 
  vw_set_tx_pin(7);


}

void loop()
{

  if (digitalRead(redButton) == HIGH){
    digitalWrite(13, HIGH); 
    vw_send((byte *)red, strlen(red));
    vw_wait_tx(); 
    delay(200);
  }
  else if (digitalRead(greenButton) == HIGH) {
    digitalWrite(13, HIGH); 
    vw_send((byte *)green, strlen(green));
    vw_wait_tx(); 
    delay(200);
  }
  else if (digitalRead(yellowButton) == HIGH){
    digitalWrite(13, HIGH); 
    vw_send((byte *)yellow, strlen(yellow));
    vw_wait_tx(); 
    delay(200); 
  }
  else if (digitalRead(greenButton2) == HIGH){
    digitalWrite(13, HIGH); 
    vw_send((byte *)green2, strlen(green2));
    vw_wait_tx(); 
    delay(200);
  }
  else{
    digitalWrite(13, LOW); 
  }
}

Receiver code:

#include <VirtualWire.h>
long msg;

int greenLED = 8;
int yellowLED = 9;
int redLED = 10;
int greenLED2 = 11;

void setup()
{
  Serial.begin(9600);	
  Serial.println("Setup");

  pinMode(greenLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED2, OUTPUT);
  digitalWrite(greenLED, LOW); 
  digitalWrite(yellowLED, LOW);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED2, LOW);

  vw_set_rx_pin(7); 

  vw_setup(4000);	 
  vw_rx_start();       
}

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++)
    {
      char c = (buf[i]);
      Serial.print(c);
      Serial.print("");

    }
    Serial.println("");
  }

  if (Serial.available() > 0){
    msg = Serial.read();
    if (msg == 'g'){
      digitalWrite(greenLED, HIGH);
    }
    if (msg =='r'){
      digitalWrite(redLED, HIGH);
    }
    if (msg == 'y'){
      digitalWrite(yellowLED, HIGH);
    }
    if (msg == 'G'){
      digitalWrite(greenLED2, HIGH);

    }
  }

}

I can read in the Serial Monitor that the receiving Arduino is receiving what i should (y, g, r, G). Also, when I manually send these characters to the serial monitor the LEDs light up. But when pressing the buttons they won’t?

But when pressing the buttons they won’t?

Thats because your LEDs are only looking at the serial monitor.

Replace

if (Serial.available() > 0)
{
msg = Serial.read();
if (msg == ‘g’){
digitalWrite(greenLED, HIGH);
}
if (msg ==‘r’){
digitalWrite(redLED, HIGH);
}
if (msg == ‘y’){
digitalWrite(yellowLED, HIGH);
}
if (msg == ‘G’){
digitalWrite(greenLED2, HIGH);
}
}

with this

if (Serial.available() > 0)
{
    msg = Serial.read();
}

if (msg == 'g' || c == 'g')
{
      digitalWrite(greenLED, HIGH);
}

if (msg =='r' || c == 'r'){
      digitalWrite(redLED, HIGH);
}

if (msg == 'y' || c == 'y')
{
      digitalWrite(yellowLED, HIGH);
}

if (msg == 'G' || c == 'G')
{
      digitalWrite(greenLED2, HIGH);
}

HazardsMind:

But when pressing the buttons they won't?

Thats because your LEDs are only looking at the serial monitor.

Replace

if (Serial.available() > 0)
{
msg = Serial.read();
if (msg == 'g'){
digitalWrite(greenLED, HIGH);
}
if (msg =='r'){
digitalWrite(redLED, HIGH);
}
if (msg == 'y'){
digitalWrite(yellowLED, HIGH);
}
if (msg == 'G'){
digitalWrite(greenLED2, HIGH);
}
}

with this

if (Serial.available() > 0)

{
    msg = Serial.read();
}

if (msg == 'g' || c == 'g')
{
      digitalWrite(greenLED, HIGH);
}

if (msg =='r' || c == 'r'){
      digitalWrite(redLED, HIGH);
}

if (msg == 'y' || c == 'y')
{
      digitalWrite(yellowLED, HIGH);
}

if (msg == 'G' || c == 'G')
{
      digitalWrite(greenLED2, HIGH);
}

Thank you very much! This worked.
I can't figure out how to change the code, so the LED will stay HIGH as long as the button is pressed, and then, when the button is released, the LED is LOW. Is this even possible with the RF Links?

Yea, add an else statement to turn them back LOW when nothing is pressed or comes in.

Example:
if (msg == 'g' || c == 'g')
{
digitalWrite(greenLED, HIGH);
}
else
digitalWrite(greenLED, LOW);