I have a code, here is an excerpt. full code shouldn't matter here,,, for some reason my if statement isn't working.
I want to switch one led on and the other off at the same time,, store the on/off state in a variable, and use the variable to determine the if statement to be used.
I have already referenced the sample of turning on and off an led,, but I don't seem to be having any errors... I don't have an else,, just an if,, followed by an if,, but that doesn't seem to be where the problem lies,, it just doesn't even use the first if statement.
if(lfs_button == HIGH)
{
Serial.println(lfs_button);
if (lfs_led = 0)
{
digitalWrite(lf_on, HIGH);
digitalWrite(rf_on, LOW);
lfs_led = 1;
rfs_led = 0;
Serial.println(lfs_led);
}
system
January 29, 2017, 8:22pm
#2
if (lfs_led = 0)
oops
. full code shouldn't matter here
It may well do - who are you to say?
For all we know lfs_button could be a pin number that is never going to equal one.
I caught it
if ==, not if = ,
ayway, sure…
#include<SPI.h>
#include<RF24.h>
#define joyStick_x A0
#define joyStick_y A1
#define joyStick_z A2
#define leftFrontSelect 4
#define rightFrontSelect 2
const int lf_on = 5;
const int rf_on = 3;
int lfs_button = 0;
int rfs_button = 0;
int lfs_led = 0;
int rfs_led = 0;
RF24 radio (7,8);
byte addresses [][10] = {"1Node","2Node"};
struct dataStruct
{
int Xposition;
int Yposition;
int Zposition;
int lf_select;
int rf_select;
} myData;
void setup() {
// put your setup code here, to run once:
pinMode(leftFrontSelect, INPUT);
pinMode(rightFrontSelect, INPUT);
pinMode(lf_on, OUTPUT);
pinMode(rf_on, OUTPUT);
digitalWrite(lf_on, LOW);
digitalWrite(rf_on, LOW);
Serial.begin(115200);
radio.begin();
radio.setChannel(108);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.startListening();
}
void loop() {
// put your main code here, to run repeatedly:
lfs_button = digitalRead(leftFrontSelect);
rfs_button = digitalRead(rightFrontSelect);
if(lfs_button == HIGH)
{
Serial.println(lfs_button);
if (lfs_led == 0)
{
digitalWrite(lf_on, HIGH);
digitalWrite(rf_on, LOW);
lfs_led = 1;
rfs_led = 0;
Serial.println(lfs_led);
}
}
if(rfs_button == HIGH)
{
if(rfs_led == 0)
{
digitalWrite(rf_on, HIGH);
digitalWrite(lf_on, LOW);
rfs_led = 1;
rfs_led = 0;
}
}
radio.stopListening();
myData.Xposition = analogRead(joyStick_x);
myData.Yposition = analogRead(joyStick_y);
myData.Zposition = analogRead(joyStick_z);
myData.lf_select = lf_on;
myData.rf_select = rf_on;
radio.write(&myData, sizeof(myData));
radio.startListening();
}