Let's Make Robots!

[SOLVED] toggle ON/OFF remote LEDs

I can't believe I'm just about to ask this... but it has been a nasty weekend and despite all I've managed some progress. So yeah, now a feel a little silly at throwing the towel at some LED toggling.

So, I have a prototyped remote (TX) that send X, Y info from a joystick plus four buttons. The information is transmitted as an array in the shape of:

[XXX, YYY, A, B, C, D]

A, B, C, D are either 0 or 1 meaning that either a button has been pressed or not really. On the TX I've checked and currently everything is fine it sends all values over to the RX unit, which retrieves them well. The problem is that the A, B, C, D are not being processed the way I would like them to be.


Whenever I press a button X a "1" is sent in the respective position in the array. Then at the RX that 1 should either turn ON the respective LED IF it's OFF or turn it OFF if it's ALREADY ON. However, the further I've managed to go is either turn ON a LED (once) if it's OFF at the beginning, and currently I can turn 1 LED OFF (that I set ON purposedly at setup) but only once. So, yeah please take a look at my code below and see where my "toggling" effect is failing.


#include <Servo.h> 
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"

int commands[6];
int iOldPos, iNewPos = 0;    // servo positions

long previousMillis = 0;      // timer for servo
const int interval = 30;      // interval to update servo position

Servo rudder;

// pretty debug LEDs
const int b1 = 2;
const int b2 = 3;
const int b3 = 4;
const int b4 = 5;

// LED's state
int b1state = LOW;
int b2state = LOW;
int b3state = LOW;
int b4state = LOW;
RF24 radio(8, 9);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup(void)
  pinMode(b1, OUTPUT);
  pinMode(b2, OUTPUT);
  pinMode(b3, OUTPUT);
  pinMode(b4, OUTPUT);

  digitalWrite(b3, HIGH);   // DEBUG

void loop(void)
  if ( radio.available() )
    // Dump the payloads until we've gotten everything
    bool done = false;
    while (!done)
      // Fetch the payload, and see if this was the last one.
      done = radio.read( commands, sizeof(commands) );

      int X = commands[0];
      int Y = commands[1];

    if (X > 485 && X < 525 )    // joystick is centered
      iNewPos = 87;     // 87 = servo in center position
    if (X < 490)              
      iNewPos = (map(X, 484, 140, 88, 130));
    if (X > 530)
      iNewPos = (map(X, 526, 830, 86, 35));

    unsigned long currentMillis = millis();
    // Issue command only if desired position changes and interval is over
    if(iOldPos != iNewPos && currentMillis - previousMillis > interval) {      
      previousMillis = currentMillis;
      iOldPos = iNewPos;
      rudder.write(iNewPos);     // tell servo to go to position in variable 'pos' 

    } // close if millis-land
    if (commands[2] == 1){
      if(b1state == LOW){
      b1state == HIGH; }
      else{ b1state == LOW; }     
    digitalWrite(b1, b1state); 
    if (commands[3] == 1){
      if(b2state == LOW){
      b2state == HIGH; }
      else{ b2state == LOW; }     
    digitalWrite(b2, b2state);     
    if (commands[4] == 1){
      if(b3state == LOW){
      b3state == HIGH; }
      else{ b3state == LOW; }
    digitalWrite(b3, b3state);   
    if (commands[5] == 1){
      if(b4state == LOW){
      b4state == HIGH; }
      else{ b4state == LOW; }
    digitalWrite(b4, b4state);   
    //} // close if millis-land (secundary test)

    //}  // close while(!done)  
  }    // close if(radio.available() )
  else { Serial.println("No radio available"); }
} // close loop()

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
if (commands[2] == 1){
      if(b1state == LOW){
      b1state == HIGH; }
      else{ b1state == LOW; }     
    digitalWrite(b1, b1state); 

Shouldn't this be:

if (commands[2] == 1){       if(b1state == LOW){       b1state = HIGH; }       else{ b1state = LOW; }     digitalWrite(b1, b1state);     }

Again you've come to my rescue \o/ Yes indeed it was that simple basic mistake, easy to make but hard to notice with my weary eyes.


Thanks again :D

Looks like there are even more of this mistake. Use the == in the if statement only but not inside the clojure.

Another thought, remove the Serial.print statements from the while(!done) loop.

Just a thought but you could debounce the buttons and see if that makes a difference.

Well, I've already tried that, at least by including them in the servo debounce routine. To no effect :(