Integer variable inside if statement changing randomly.

Hi,
I am currently trying to program between two APC220 radio transceivers. And when I write an if statement that triggers when the correct integer is received the integer always changes so the if statement is always triggered.

The transmitter: (This seems to work)

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
const int switchPin = 2;

int state = LOW;
int lastButtonState = HIGH;
int ledState = LOW;
int reading;
int x = 2;


void setup()
{
  Serial.begin(9600);
  pinMode(switchPin, INPUT);
}

void loop() {
  reading = digitalRead(switchPin);

 if (reading != lastButtonState){
  lastDebounceTime = millis();
  lastButtonState = reading;
 }

if ((millis() - lastDebounceTime) > debounceDelay){
  if(state != lastButtonState){
    state = lastButtonState;
    if (state == HIGH) {
    Serial.print(2);}

    }
  }
  }

The receiver: In this code the y integer changes to five when I run the code, even though I cant see how this is possible. With this code it prints a 2 and a 5 when the button on the transmitter is clicked
(I used y=5 in this test just to make sure it wasn't working)

int x=1;
char data;


void setup()
{
  Serial.begin(9600);
}


void loop() {
  while(Serial.available()>0){
    data = Serial.read();
    int y = int(data)-48;
    Serial.println(y);
    if (y = 5){
      Serial.println(y);
    }
}}

I'm new to Arduino (and coding as a whole) so sorry if it's a simple mistake that I've made.

    if (y = 5)

Whoops. Not how you compare 2 values

Oh, flip.
I didn't notice this
Should be ==
Thanks a lot.

You are not alone in making that mistake, but spotting mistakes in your own code can be difficult sometimes. Good luck with this and further projects and thanks for using code tags

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.