Start a new topic

Arduino Sketch code to debug and receive Data from Nextion

 I put here a code to permit seeing value and position of a slidder bar, like a debug.

Even using the cleaning code to reset buffer I face problem.
The cause is nextion interprets 'serial.print' as part of some code about to come.

The problem I face is when a slider containing two digits sends value I did not receive the number ID, so I need to remove the '//' on lines in the code, but this action causes another problem, I did not receive the value of the slidder... 

  byte valor[10];
  byte i = 0;
  byte flag = 0;

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

void loop() { 

    if (Serial.available()) {

        i++;
        valor[i] =  Serial.read();
        if (valor[(i)] == 255) flag++;  
        if (flag == 3) {

          if (valor[1] == 101) {
            Serial.print("AJUSTE=");          
            Serial.println(valor[3]);
            Serial.print("code_c");            
            //Serial.write(0xFF);
            //Serial.write(0xFF);
            //Serial.write(0xFF);
            }          

          if (valor[1] == 113) {
            Serial.print("valor=");          
            Serial.println(valor[2]);
            Serial.print("code_c");            
            Serial.write(0xFF);
            Serial.write(0xFF);
            Serial.write(0xFF);
            }      
          i = 0;
          flag = 0;

          if (valor[3] == 11) {
            Serial.print("code_c");            
            Serial.write(0xFF);
            Serial.write(0xFF);
            Serial.write(0xFF);
            Serial.print("get f32.val");
            Serial.write(0xFF);
            Serial.write(0xFF);
            Serial.write(0xFF);  
            }
          } 
      }
  }

 


serial.println is adding extra unwanted line-ending characters


1 person likes this


@Christian


Your debug 'Serial.print' has to be separate from Nextion, you cannot have both on the same connection.

Nextion requires an exclusive port of it's own - usually Serial1 on Arduino Mega.


There are other errors in your code. You should increment 'i' after the Serial.read or use this syntax which works the same way.... 

valor[i++] =  Serial.read(); 

If you don't then valor[0] could have any old value stored there, which is not good coding practice.


I presented some non-library solutions to catching slider values on Arduino here...

http://support.iteadstudio.com/support/discussions/topics/11000011886


If it is you intention to code your project without the use of the Itead library, then you should approach things differently to the 'library' way of dong things. Return codes have no real place in any such project :)


1 person likes this

Hi @indev2 !

thank you very much for your time, patience and knowledge. You sent me to good ideas.

Don't worry about valor[0] because I wont never use this value, my first slider is valor[1].

@Christian

On line 15 you have:

if (valor[(i)] == 255) flag++;

Do you intentionally write [(i)] instead of [i]?

Login or Signup to post a comment