jluarduino:
All right, I got it working buttery smooth and without any hiccups.
I changed the timer interval in the Nextion to 50 millis.
I used the third example in the page you linked, thanks alot.
Here is my final code:
#include "Nextion.h"
NexWaveform s0 = NexWaveform(1, 1, "s0");
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void setup() {
nexInit();
Serial.begin(9600);
Serial.println("");
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(A0, INPUT);
}
void loop() {
unsigned long curntMillis = millis();
if(Serial.available()<1){
progBar();
}
recvWithStartEndMarkers();
useNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void useNewData() {
if (newData == true) {
String nexDat = receivedChars;
String xval = getValue(nexDat, ',', 0);
String yval = getValue(nexDat, ',', 1);
String zval = getValue(nexDat, ',', 2);
if(xval == "as"){digitalWrite(11, LOW);}
if(xval == "af"){digitalWrite(11, HIGH);}
if(yval == "bs"){digitalWrite(10, LOW);}
if(yval == "bf"){digitalWrite(10, HIGH);}
if(zval == "cs"){digitalWrite(9, LOW);}
if(zval == "cf"){digitalWrite(9, HIGH);}
newData = false;
//Serial.println(receivedChars);
}
}
void progBar() {
int val = analogRead(0);
val = map(val, 0, 1023, 0, 100);
Serial.print("j0.val=");
Serial.print(val/2);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
s0.addValue(0, val);
if (val > 50){
Serial.print("p2.pic=57");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}else if (val < 50){
Serial.print("p2.pic=56");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
There are many ways to "code" serial data collection - ranging from simple to what I call "convoluted".
Eventually it works , but why complicating things ( Google "KISS" ) ?
Check Resources -> Serial for functions collecting data from Serial buffer using passed parameters such as checking for terminating character, expected length of data to receive and also (optionally) returning timeout error of -1.
PS
OT
If String causes "problems" in Arduino - could it be Arduino "problem"?
It would not be first Arduino IDE anomaly.
Did not Arduino IDE / .ino had issues with #define?