Skip to main content
Arduino

Return to Answer

You have to invalidate the ‘state’state variable when SerialSerial is not available, otherwise you will produce strange behaviours.

if(Serial1.available() > 0){ state = Serial1.read(); } else { state =-1; // not valid }

if (Serial1.available() > 0) {
 state = Serial1.read();
} else {
 state = -1; // not valid
}

Also, you have to check ‘1’ and ‘0’serial values will be characters and, not 1 and 0integers, so compare with character values. ‘if (state == ‘1’), e.g.,

if (state == '1')

You have to invalidate the ‘state’ variable when Serial is not available, otherwise you will produce strange behaviours.

if(Serial1.available() > 0){ state = Serial1.read(); } else { state =-1; // not valid }

Also, you have to check ‘1’ and ‘0’ characters and not 1 and 0 values. ‘if (state == ‘1’)..

You have to invalidate the state variable when Serial is not available, otherwise you will produce strange behaviours.

if (Serial1.available() > 0) {
 state = Serial1.read();
} else {
 state = -1; // not valid
}

Also, serial values will be characters, not integers, so compare with character values, e.g.,

if (state == '1')
Source Link
leoc7
  • 603
  • 4
  • 12

You have to invalidate the ‘state’ variable when Serial is not available, otherwise you will produce strange behaviours.

if(Serial1.available() > 0){ state = Serial1.read(); } else { state =-1; // not valid }

Also, you have to check ‘1’ and ‘0’ characters and not 1 and 0 values. ‘if (state == ‘1’)..’

AltStyle によって変換されたページ (->オリジナル) /