When I use this code to read the sent values, I can see values which are wrong displayed in the LCD screen, I can't understand how did these values pass the different switch cases since they are wrong....
The updated value takes time to get updated, & before it does, numbers like 2304 2559 255 2313 are displayed... (updated sent value is 9)
here is the code :
#include<SCServo.h>
SCSCL sc;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display
int save[4];
int Servo_=0;
int ndx = 0;
int foundValue = 0;
int cursor = 0;
void setup() {
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
Serial.begin(9600);
// Serial.println("<Arduino is ready>");
// Serial1.begin(1000000);
// sc.pSerial = &Serial1;
}
void loop() {
if (Serial.available() > 0) { //at least 4 bytes were received
ndx++;
byte rc;
rc = Serial.read();
// cursor += lcd.print(sum); //<---should be 'rc' not 'foundvalue'!
// cursor += lcd.print(" ");
lcd.clear();
switch (ndx) {
case 1:
if (rc != 0xFF) --ndx; //reset index count if first 2 received bytes is NOT 0xFF
if (ndx < 0) ndx = 0;
break;
case 2:
if (rc != 0xFF ) --ndx;
break;
case 3:
if (rc == 0xFF) {
--ndx;
}
else {
foundValue = rc;
foundValue <<= 8;
save[2] = rc;
}
break;
case 4:
save[3] = rc;
foundValue |= rc;
ndx = 0;
int sum = save[2] * 256 + save[3];
lcd.setCursor(0, 0);
lcd.print(foundValue);
break;
}
}
delay(50); //Wait for 1s
}```