I am using an Xbee transmitter (Master) to control two Xbee receivers (Left and Right).
The Master sends a message once a button is either pressed or released to either the Left or Right receivers. The Left or Right receivers receive the message and turn on or off the LEDs. However the LEDs are flickering on and off quickly because the Master is continuously sending the message " ‘A’, ‘A’, ‘A’, etc".
In my mind there are two possible solutions that could work, however I need help understand how exactly the issue can be fixed.
The first possible solution is get the Master to only print the message once until the button state changes. How would I go about doing this?
This is the Master code:
const int buttonPinBrake = 4; // BRAKE
const int buttonPinLeft = 3; // LEFT
const int buttonPinRight = 2; // RIGHT
int buttonState = 0;
void setup() {
pinMode(buttonPinBrake, INPUT_PULLUP);
pinMode(buttonPinLeft, INPUT_PULLUP);
pinMode(buttonPinRight, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPinBrake);
if(buttonState == LOW){
Serial.println("A"); //turn on the LED
delay(10);
}
if(buttonState == HIGH){
Serial.println("B"); //turn LED off
delay(10);
}
buttonState = digitalRead(buttonPinLeft);
if(buttonState == LOW){
Serial.println("C"); //turn on the LED
delay(10);
}
if(buttonState == HIGH){
Serial.println("D"); //turn LED off
delay(10);
}
buttonState = digitalRead(buttonPinRight);
if(buttonState == LOW){
Serial.println("E"); //turn on the LED
delay(10);
}
if(buttonState == HIGH){
Serial.println("F"); //turn LED off
delay(10);
}
}
The second possible solution is that once the Left or Right receives the message ‘A’ to turn on the red LEDs it will not listen to what the message the Master is sending until it gives the receivers a different message then ‘A’.
This is the code for Left receiver:
#include <FastLED.h>
#define DATA_PIN 3
#define NUM_LEDS 29
CRGB leds[NUM_LEDS];
char msg = ' '; //contains the message from Master
void setup() {
Serial.begin(9600);
pinMode(DATA_PIN,OUTPUT);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}
void loop() {
while(Serial.available()) {
msg=Serial.read();
if(msg=='A') {
leds[0] = CRGB::Red;
FastLED.show();
delay(0);
}
if(msg=='B') {
leds[0] = CRGB::Black;
FastLED.show();
delay(0);
}
if(msg=='C') {
leds[0] = CRGB::Orange;
FastLED.show();
delay(0);
}
if(msg=='D') {
leds[0] = CRGB::Black;
FastLED.show();
delay(0);
}
}
}
Does anybody have any advice as to what I should do or where I should look? I have been looking through the forum and googling every possible solution I can think of, but nothing works. Cheers!