Very weird behaviour of lcd.print while reading a serial port

I made a program for displaying the CI-V output of an ICOM HAM radio transceiver data port.
It outputs most of the time blocks of 7 or 11 bytes when a button is moved
I can show them on display, all works.

Now I want to switch on the built in LED when byte3 is high. This does not work...
For debugging I print this byte3 a second time on the display on the second line and suddenly it is not the same any more but always 1 !!!

I am totally puzzled what is going on here

program extract: (byte3 is an INT)

if (readCounter == 3){
lcd.setCursor(0,0); // set the LCD cursor position
byte3=(byteread);
lcd.print(byte3);

lcd.setCursor(0,1); // set the LCD cursor position
lcd.print(byte3); // print again: THIS IS NOT WORKING !!!! always 1
if (byte3==001){digitalWrite(13, LOW);} //This also not working
if (byte3==000){digitalWrite(13, HIGH);}

full program enclosed

CI-V_monitor.ino (4.43 KB)

aren't you reading ASCII characters from the serial interface? the value 000 is NULL and 001 SOH

as far as I know mySerial.read() returns an integer.

but even then it does not explain why lcd.print(byte3) displays two different values on the screen...

i believe lcd.print () takes ASCII character strings (e.g. lcd.print("bytes:");). that may explain why your print of byte3 is odd.

okay, but...

I have two lines in code

lcd.setCursor(0,0);
lcd.print (byte3);
lcd.setCursor(0,1);
lcd.print (byte3);

and they display a different character on my display !!!

whatever type byte3 might be, they should print the same on the screen but they don't !!!!!

this is just too crazy to be possible but it is what happens !!!!!

Show the portion of the code where byte3 is declared and set to its value. This is sounding like a local variable that is being used out of scope.

the full code is enclosed with the first message. And now again...

byte3 is globally declared.

CI-V_monitor.ino (4.43 KB)

i don't know how a NULL or SOH character is processed by the LCD.

can you try lcd.print("1") and lcd.print ("0");

but you miss the point...

The purpose is to have output 13 switch on a certain specifiec character in the byte3

as this did not work I tried printing this byte3 first again one more time to show myself what could be wrong and then I discovered apparently the content of int byte3 changed...

on7wp:
The purpose is to have output 13 switch on a certain specifiec character in the byte3

instead of testing for 001 or 000, test for '1' or '0'

   if (byte3=='1'){
        digitalWrite(13, LOW);
    }     //This also not working
    else {
        digitalWrite(13, HIGH);
    }

on7wp:
the full code is enclosed with the first message. And now again...

byte3 is globally declared.

Ah, sorry, did not see it. Can't look at the file on this phone anyway.

if (byte3=='1'){
digitalWrite(13, LOW);
} //This also not working
else {
digitalWrite(13, HIGH);
}

already tried this before, not working either.... :frowning:

do you know the value of byte3?

can you create a string with it's value and display it on the LCD?

char s [20];
sprintf(s, " byte3 %02x", byte3);
lcd.setCurso(1,0);
lcd.print(s);

Put your code in a post between code tags [code] and [/code].

Post a link to the documentation that describes the output of radio.

OP's code

/********************************************************

Description: CI-V monitor by ON7WP

********************************************************/

 

#include <LiquidCrystal.h>
#include <SoftwareSerial.h> // for comms to IC7000
#define BAUD_RATE 9600     // CI-V speed
#define TRX_address (0x34)  // HEX $34 = Icom IC-R7100
//#define TRX_address ((byte)00)  // $00: Icom universal address (works for all radios).

// serial connection CI-V
// RX = Icom radio to Arduino  : to pin 2 via resistor 4k7
// TX = Arduino to Icom radio  : to pin 3 via diode 1N4148, with pull up 10k to Vcc (5V) on tip of 3.5 mm connector

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel (using LCD keypad shield)
SoftwareSerial mySerial = SoftwareSerial(2, 3); // (RX, TX)

int readCounter; // counts the number of bytes received from the radio
int byte3;
int byte4;
int byte5;
int byte6;
int byte7;
int byte8;
int byte9;
int byte10;
int byte11;
int byte12;

void setup(){

lcd.begin(16, 2); // start the library

  pinMode(13, OUTPUT); digitalWrite(13, LOW); // force LED (pin 13) to turn off.
  pinMode(2, INPUT);  // CI-V serial communication from radio
  pinMode(3, OUTPUT); // CI-V serial communication to radio
  
  mySerial.begin(BAUD_RATE);
  mySerial.listen();  
  while (mySerial.available()) mySerial.read(); 

  lcd.setCursor(0, 0);             // set the LCD cursor position
  lcd.print("waiting for CI-V");
  lcd.setCursor(0, 1);             // set the LCD cursor position
  lcd.print(" ON7WP Analyser");
  delay(1000);                     // display startup screen
  lcd.clear();                     // clear the LCD 
}
void loop(){

                                   // state to be decoded:
                                   // $1C  $00 $01 is Transmit On 
                                   // $1C  $00 $00 is Transmit Off   

                                   // now read info from radio
  int nbChar = mySerial.available();  
  if (nbChar > 0) {
     lcd.clear(); // clear the LCD
     lcd.setCursor(8,1);
     lcd.print("bytes:");
     lcd.setCursor(14,1); // set the LCD cursor position
     lcd.print(nbChar);
      for (int readCounter = 0; readCounter < nbChar ; readCounter++) {
      int byteread = mySerial.read();

// first two bytest are start bytes, not interesting
// next two bytes are "to" and "from" ID's so also not interesting
// so we start with the fourth byte value into an integer 000-255

      if (readCounter == 3){
          lcd.setCursor(0,0); // set the LCD cursor position
          byte3=(byteread);
          lcd.print(byte3);

           lcd.setCursor(0,1); // set the LCD cursor position
           lcd.print(byte3);                           // print again for debug reasons: THIS IS NOT WORKING !!!!
           if (byte3==001){digitalWrite(13, LOW);}     //This also not working
           if (byte3==000){digitalWrite(13, HIGH);} 

          
          }
      if (readCounter == 4){
          lcd.setCursor(0,0); // set the LCD cursor position
          lcd.print(byteread);
          byte4=(byteread);
          }
      if (readCounter == 5){
          lcd.setCursor(3,0); // set the LCD cursor position
          lcd.print(byteread);
          byte5=(byteread);
          }
      if (readCounter == 6){
          lcd.setCursor(6,0); // set the LCD cursor position
          lcd.print(byteread);
          byte6=(byteread);
          }
      if (readCounter == 7){
          lcd.setCursor(9,0); // set the LCD cursor position
          lcd.print(byteread);
          byte7=(byteread);
          }
      if (readCounter == 8){
          lcd.setCursor(12,0); // set the LCD cursor position
          lcd.print(byteread);
          byte8=(byteread);
          }
      if (readCounter == 9){
          lcd.setCursor(0,1); // set the LCD cursor position
          lcd.print(byteread);
          byte9=(byteread);
          }
     if (readCounter == 10){
          lcd.setCursor(3,1); // set the LCD cursor position
          lcd.print(byteread);
          byte10=(byteread);
         }
      if (readCounter == 11){
          lcd.setCursor(6,1); // set the LCD cursor position
          lcd.print(byteread);
          byte11=(byteread);
          }
      if (readCounter == 12){
          lcd.setCursor(9,1); // set the LCD cursor position
          lcd.print(byteread);
          byte12=(byteread);
          }
      }
    }
  }

Are you sure you are actually seeing byte3 on the LCD display on both lines 0 and line 1? You are also displaying byte4 at the same position on line 0 of the LCD, so there is not going to be time for you to actually see the contents of byte3 displayed before it is overwritten with the value of byte4.

I'm really surprised the code ever has time to accumulate 4 bytes in the input, it should be consuming them as soon as they are received.