Nextion Baud Rate help please

I am using a Nextion display with an Arduino Mega (a genuine one).
They are communicating with each other fine, but I would like to increase the baud rate.
I can change the Nextion baud rate with the Nextion editor but not with the Arduino.

I have tried
Serial2.begin(38400);
&
nexSerial.begin(38400);
and it still communicates at 9600.

Using this it still communicates at 9600
Serial2.print("baud=38400");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);

the only way I can change the baud rate of the display is with the Nextion editor

If i put Serial2.end(); the serial connection still works.

It's like none of the serial commands are working.
I have tried 2 different Megas and the result is the same.
I think the problem with the Mega not the display

Martyn

g0thy:
I am using a Nextion display with an Arduino Mega (a genuine one).
They are communicating with each other fine, but I would like to increase the baud rate.
I can change the Nextion baud rate with the Nextion editor but not with the Arduino.

I have tried

Serial2.begin(38400);

&
nexSerial.begin(38400);



And it still communicates at 9600.

Using this it still communicates at 9600


Serial2.print("baud=38400");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);




The only way I can change the baud rate of the display is with the Nextion editor

If I put Serial2.end(); the serial connection still works.

It's like none of the serial commands are working.
I have tried 2 different Megas and the result is the same.
I think the problem with the Mega not the display

Martyn

Martyn,
I've never tried changing the baud rate 'on the fly' but in the first of your examples you are changing it at the Arduino end but not at the Nextion end, and in the second you are changing it at the Nextion not the Arduino. You have to change both and you have to change the Nextion first then the Arduino (Because if you change the Arduino first it will be sending to the Nextion at a different rate to what it expects, so it won't understand the instruction).

I've not checked this but I'd think this or something similar would do it:

Serial2.begin(9600);
Serial2.print("baud=38400");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.begin(38400);
Serial2.begin(9600);
Serial2.print("baud=38400");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.begin(38400);

Sorry I thought of something after I posted that. Not sure but maybe need a delay before

Serial2.begin(38400);

Reason is that without a delay you are trying to change the baud rate of the serial port while it is still sending the previous data. I don't know how it behaves when you do this but I suspect it's not good.

Thanks Perry

I had tried changing the Nextion baud rate using serial print. That worked but I couldn’t change the Arduino Mega baud rate so they wouldn’t communicate - the Mega still at 9600.
I could also change the Nextion Baud using the nextion editor and that worked.

My problem is I don;t seem to be able to change the Mega baud rate. I have monitored the serial connection with an oscilloscope and there is no change from the Mega.

oddly, commands like Serial2.end() doesn’t work either.

Is like the Mega serial commands don’t work.

I will try putting a delay in next week.

Martyn

g0thy:
Thanks Perry

I had tried changing the Nextion baud rate using serial print. That worked but I couldn't change the Arduino Mega baud rate so they wouldn't communicate - the Mega still at 9600.
I could also change the Nextion Baud using the nextion editor and that worked.

My problem is I don;t seem to be able to change the Mega baud rate. I have monitored the serial connection with an oscilloscope and there is no change from the Mega.

oddly, commands like Serial2.end() doesn't work either.

Is like the Mega serial commands don't work.

I will try putting a delay in next week.

Martyn

Hi Martyn,
I can't help with the Mega baud rate, I have never had one, sorry.

I am a bit curious as to why you don't just set the baud rate at the start and leave it at that.

Perry

Hello Martyn

I’ve been playing to try to get an answer for you. I have a Nextion with 2 text boxes with objname as T0 and T1. The code below sends odd numbers to T0 at 9600 baud and even number to T1 at 19200 baud, I have checked this on an oscilloscope and it does keep changing baud rate. Ignore the mess of delays and millis, I put this together in a hurry from some other code used to test something else, I just wanted something that proved it worked, not something neat and tidy. Play around with this, change the delays, see if they are needed at all.

This is on a Wemos SAMD21

unsigned long lastmillis0 = millis();
unsigned long lastmillis1 = millis();
uint8_t testout = 0;
void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  SerialUSB.begin(9600);
  Serial.begin(9600);
}

void loop() {

  if (lastmillis0 + 500 < millis()) {
    lastmillis0 = millis();
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }
  if (lastmillis1 + 2000 < millis()) {
    lastmillis1 = millis();

    ++testout;
    Serial.print("baud=9600");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    delay(50);
    Serial.begin(9600);

    Serial.print("T0.txt=\"");
    Serial.print(testout);
    Serial.print("\"");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);

    delay(1000);

    ++testout;
    Serial.print("baud=19200");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    delay(50);
    Serial.begin(19200);

    Serial.print("T1.txt=\"");
    Serial.print(testout);
    Serial.print("\"");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
}

I have tried setting the baud rate at void setup() but it still won’t work.

maybe I will have to try using a different arduino board, i guess it might be something that I don’t understand about the Mega’s serial port.

Off for christmas so will try again when I return

thanks for your assistance Perry

m

g0thy:
I have tried setting the baud rate at void setup() but it still won’t work.

maybe I will have to try using a different arduino board, i guess it might be something that I don’t understand about the Mega’s serial port.

Off for christmas so will try again when I return

thanks for your assistance Perry

m

Hi Martyn,

Can you clarify what you are doing? The default baud rate on the Nextion is 9600. If you change it on the Arduino it won’t work because the Nextion will still be at 9600. The experimental code I posted sends a change of rate to the Nextion, waits a short time then makes a matching change on the Arduino. Please post your code and I’ll look for you.

Enjoy Christmas!
Perry

The Nextion display baud rate changes fine, but I can’t get the arduino rate to change - I guess I am doing something stupid.
I have monitored the serial out from the Arduino Mega, but it doesn’t change even when I try. No error is flagged by the Arduino IDE.

m

ps - this isn’t homework - I work in a research institute and want it to control an experiment.

NextionLedControl_02_EPROM_read_changed_03.ino (14.8 KB)

Here is a view of the Nextion Display

m

Nextion display.doc (59.5 KB)

Sorry to be a pain but can you please read How to use this forum, particularly item 7.

If you post your code here using link tags it makes it so much easier to help you.

Thanks.

Sorry about that Perry.
Here’s the first half. m

// NE320818
// Touch Screen LED Controller
//NX8048T070
//Arduino Mega

//06/12/18 09:44z

#include "Nextion.h"
#include "NexNumber.h"  //not included in Nextion.h so imported from github
#include <EEPROM.h>
#include <Wire.h>   // for the DaC
#include <Adafruit_MCP4725.h>  //Better library than Sparkfun!!

//
//#define DEFAULTBAUDRATE 57600

Adafruit_MCP4725 dac;

#define MCP4725_ADDR 0x60   //for Sparkfun, AdaFruit = 0x62
#define nexSerial Serial2


 
//Declare Nextion Objects

//NexText t0 = NexText(0, 1, "t0");

//setUp variables 
int(count) = 0;
boolean (loopCount) = false;
boolean (ledState) = false;
byte(value) = 0;  //used for downloading EEPROM values
byte(num_n0) = EEPROM.read(0);
byte(num_n1) = EEPROM.read(1);
byte(num_n2) = EEPROM.read(2);
byte(num_n3) = EEPROM.read(3);
byte(num_n4) = EEPROM.read(4);
byte(num_n5) = EEPROM.read(5);
byte(num_n6) = EEPROM.read(6);
byte(num_n7) = EEPROM.read(7);
byte(num_n8) = EEPROM.read(8); 
byte(num_n9) = EEPROM.read(9);
byte(num_n10) = EEPROM.read(10);
byte(num_n11) = EEPROM.read(11);
byte(num_n12) = 0;//EEPROM.read(12);
byte(num_n13) = 0;//EEPROM.read(13);
byte(num_n14) = 0;//EEPROM.read(14);
byte(num_n15) = 0;

byte(mode) = 0;
byte(loops) = 0;
/*    //What mode are we in? 
 * 0 = LED Off
 * 1 = LED On
 * 2 = LED Cycle Stop
 * 3 = LED Cycle Start, # of pulses not reached
 * 4 = LED Cycle Start, # of pulses HAS reached, waiting for Cycle Time
 */
unsigned long onWidth = 0;
unsigned long offWidth = 0;
word noPulses = 0;
word pulseCount = 0;
unsigned long cycleCount = 0;
word Cycle = 0;
word onCount = 0;
word offCount = 0;
unsigned long previousMillis = 0;
unsigned long cycleMillis = 0;
unsigned long cycleInterval = 0;
int interval = 0;   // orig interval = onWidth;
boolean outState = false;
unsigned long CycleCount = 0; //32 bit for 0-999s = 999000ms = 0xF3E58ms, needs 20 bits



NexNumber n0 = NexNumber(1, 1, "n0");
NexNumber n1 = NexNumber(1, 2, "n1");
NexNumber n2 = NexNumber(1, 3, "n2");
NexNumber n3 = NexNumber(1, 4, "n3");
NexNumber n4 = NexNumber(1, 5, "n4");
NexNumber n5 = NexNumber(1, 6, "n5");
NexNumber n6 = NexNumber(1, 7, "n6");
NexNumber n7 = NexNumber(1, 8, "n7");
NexNumber n8 = NexNumber(1, 9, "n8");
NexNumber n9 = NexNumber(1, 10, "n9");
NexNumber n10 = NexNumber(1, 11, "n10");
NexNumber n11 = NexNumber(1, 12, "n11");
NexNumber n12 = NexNumber(1, 13, "n12");
NexNumber n13 = NexNumber(1, 14, "n13");
NexNumber n14 = NexNumber(1, 15, "n14");
NexNumber n15 = NexNumber(1, 54, "n15");

NexRadio r0 = NexRadio(1, 16, "r0");
NexRadio r1 = NexRadio(1, 17, "r1");
NexRadio r2 = NexRadio(1, 18, "r2");
NexRadio r3 = NexRadio(1, 68, "r3");
NexRadio r4 = NexRadio(1, 69, "r4");

NexButton b0 = NexButton(1, 24, "b0");
NexButton b1 = NexButton(1, 25, "b1");
NexButton b2 = NexButton(1, 26, "b2");
NexButton b3 = NexButton(1, 27, "b3");
NexButton b4 = NexButton(1, 28, "b4");
NexButton b5 = NexButton(1, 29, "b5");
NexButton b6 = NexButton(1, 30, "b6");
NexButton b7 = NexButton(1, 31, "b7");
NexButton b8 = NexButton(1, 32, "b8");
NexButton b9 = NexButton(1, 33, "b9");
NexButton b10 = NexButton(1, 34, "b10");
NexButton b11 = NexButton(1, 35, "b11");
NexButton b12 = NexButton(1, 36, "b12");
NexButton b13 = NexButton(1, 37, "b13");
NexButton b14 = NexButton(1, 38, "b14");
NexButton b15 = NexButton(1, 39, "b15");
NexButton b16 = NexButton(1, 40, "b16");
NexButton b17 = NexButton(1, 41, "b17");
NexButton b18 = NexButton(1, 42, "b18");
NexButton b19 = NexButton(1, 43, "b19");
NexButton b20 = NexButton(1, 44, "b20");
NexButton b21 = NexButton(1, 45, "b21");
NexButton b22 = NexButton(1, 46, "b22");
NexButton b23 = NexButton(1, 47, "b23");
NexButton b24 = NexButton(1, 48, "b24");
NexButton b25 = NexButton(1, 49, "b25");
NexButton b26 = NexButton(1, 50, "b26");
NexButton b27 = NexButton(1, 51, "b27");
NexButton b28 = NexButton(1, 52, "b28");
NexButton b29 = NexButton(1, 53, "b29");
NexButton b30 = NexButton(1, 59, "b30");
NexButton b31 = NexButton(1, 60, "b31");
// don't need b32 or b33
NexButton b34 = NexButton(1, 64, "b34");
NexButton b35 = NexButton(1, 65, "b35");
NexButton b36 = NexButton(1, 66, "b36");
NexButton b37 = NexButton(1, 67, "b37");
NexButton b38 = NexButton(1, 70, "b70");
//Touch List
 NexTouch *nex_Listen_List[] = 
{
  &b0,  &b1, &b2, &b3, &b4, &b5, &b6, &b7, &b8, &b9,
  &b10, &b11, &b12, &b13, &b14, &b15, &b16, &b17, &b18, &b19,
  &b20, &b21, &b22, &b23, &b24, &b25, &b26, &b27, &b28, &b29,
  &b30, &b31, &b34, &b35, &b36, &b37, &b38, NULL 
};

void onTotal()
{
 onWidth = (num_n2 +(num_n1*10) + (num_n0 * 100));

}

void offTotal()
{
 offWidth = (num_n5 +(num_n4*10) + (num_n3 * 100));  
}

void noTotal()
{
 noPulses = (num_n8 +(num_n7*10) + (num_n6 * 100));   
}

void cycleTotal()
{
 Cycle = (num_n11 +(num_n10*10) + (num_n9 * 100));  
}

void b0PopCallback(void *ptr) //On Width Hundreds +
{if (num_n0 < 9)
  {num_n0 = (num_n0 + 1); }  
else
 {num_n0 = 0; }
n0.setValue(num_n0); 
onTotal();
}

void b1PopCallback(void *ptr) //On Width Tens +
{if (num_n1 < 9)
  {num_n1 = (num_n1 + 1); }
else
 {num_n1 = 0; }
n1.setValue(num_n1);
onTotal();   
}

void b2PopCallback(void *ptr) //On Width Units +
{if (num_n2 < 9)
  {num_n2 = (num_n2 + 1); }
else
 {num_n2 = 0; }
n2.setValue(num_n2); 
 onTotal();
}

void b3PopCallback(void *ptr) //On Width Hundreds -
{ if (num_n0 > 0)
  {num_n0 = (num_n0 - 1); }
else
 {num_n0 = 9; }
n0.setValue(num_n0); 
 onTotal();
}

void b4PopCallback(void *ptr) //On Width Tens -
{ if (num_n1 > 0)
  {num_n1 = (num_n1 - 1); }
else
 {num_n1 = 9; }
n1.setValue(num_n1);
onTotal(); 
}

void b5PopCallback(void *ptr) //On Width Units -
{ if (num_n2 > 0)
  {num_n2 = (num_n2 - 1); }
else
 {num_n2 = 9; }
n2.setValue(num_n2);
onTotal();   
}

void b6PopCallback(void *ptr) //Off Width Hundreds +
{if (num_n3 < 9)
  {num_n3 = (num_n3 + 1); }
else
 {num_n3 = 0; }
n3.setValue(num_n3);
offTotal();  
}

void b7PopCallback(void *ptr) //Off Width Tens +
{if (num_n4 < 9)
  {num_n4 = (num_n4 + 1); }
else
 {num_n4 = 0; }
n4.setValue(num_n4);   
offTotal();
}

void b8PopCallback(void *ptr) //Off Width Units +
{if (num_n5 < 9)
  {num_n5 = (num_n5 + 1); }
else
 {num_n5 = 0; }
n5.setValue(num_n5);
offTotal();   
}

void b9PopCallback(void *ptr) //Off Width Hundreds -
{ if (num_n3 > 0)
  {num_n3 = (num_n3 - 1); }
else
 {num_n3 = 9; }
n3.setValue(num_n3); 
offTotal(); 
}

void b10PopCallback(void *ptr) //Off Width Tens -
{ if (num_n4 > 0)
  {num_n4 = (num_n4 - 1); }
else
 {num_n4 = 9; }
n4.setValue(num_n4); 
offTotal(); 
}

void b11PopCallback(void *ptr)  //Off Width Units -
{ if (num_n5 > 0)
  {num_n5 = (num_n5 - 1); }
else
 {num_n5 = 9; }
n5.setValue(num_n5); 
offTotal(); 
}

void b12PopCallback(void *ptr)  //# Pulses Hundreds +
{if (num_n6 < 9)
  {num_n6= (num_n6 + 1); }
else
 {num_n6 = 0; }
n6.setValue(num_n6); 
noTotal();  
}

void b13PopCallback(void *ptr)  //# Pulses Tens +
{if (num_n7 < 9)
  {num_n7 = (num_n7 + 1); }
else
 {num_n7 = 0; }
n7.setValue(num_n7); 
noTotal();  
}

void b14PopCallback(void *ptr)  //#Pulses Units +
{ if (num_n8 < 9)
  {num_n8 = (num_n8 + 1); }
else
 {num_n8 = 0; }
n8.setValue(num_n8);
noTotal();  
}

void b15PopCallback(void *ptr)  //# Pulses Hundreds -
{ if (num_n6 > 0)
  {num_n6 = (num_n6 - 1); }
else
 {num_n6 = 9; }
n6.setValue(num_n6); 
noTotal(); 
}

void b16PopCallback(void *ptr)  //# Pulses Tens -
{ if (num_n7 > 0)
  {num_n7 = (num_n7 - 1); }
else
 {num_n7 = 9; }
n7.setValue(num_n7); 
noTotal(); 
}

void b17PopCallback(void *ptr)  //# Pulses Units -
{ if (num_n8 > 0)
  {num_n8 = (num_n8 - 1); }
else
 {num_n8 = 9; }
n8.setValue(num_n8); 
noTotal(); 
}

void b18PopCallback(void *ptr)  //Cycle Hundreds +
{if (num_n9 < 9)
  {num_n9 = (num_n9 + 1); }
else
 {num_n9 = 0; }
n9.setValue(num_n9); 
cycleTotal();  
}

void b19PopCallback(void *ptr)  //Cycle Tens +
{if (num_n10 < 9)
  {num_n10 = (num_n10 + 1); }
else
 {num_n10 = 0; }
n10.setValue(num_n10);  
cycleTotal(); 
}

void b20PopCallback(void *ptr)  //Cycle Units +
{if (num_n11 < 9)
  {num_n11 = (num_n11 + 1); }
else
 {num_n11 = 0; }
n11.setValue(num_n11);   
cycleTotal();
}

and the second half

m.

void b21PopCallback(void *ptr)  //Cycle Hundreds -
{ if (num_n9 > 0)
  {num_n9 = (num_n9 - 1); }
else
 {num_n9 = 9; }
n9.setValue(num_n9); 
cycleTotal(); 
}

void b22PopCallback(void *ptr) //Cycle Tens -
{ if (num_n10 > 0)
  {num_n10 = (num_n10 - 1); }
else
 {num_n10 = 9; }
n10.setValue(num_n10);
cycleTotal();  
}

void b23PopCallback(void *ptr)  //Cycle Units -
{  if (num_n11 > 0)
  {num_n11 = (num_n11 - 1); }
else
 {num_n11 = 9; }
n11.setValue(num_n11);
cycleTotal();
}

void b24PopCallback(void *ptr)  //Current Hundreds +
{if (num_n12 < 9)
  {num_n12 = (num_n12 + 1); }
else
 {num_n12 = 0; }
n12.setValue(num_n12); 

D2Aload();
}

void b25PopCallback(void *ptr)  //Current Tens +
{if (num_n13 < 9)
  {num_n13 = (num_n13 + 1); }
else
 {num_n13 = 0; }
n13.setValue(num_n13); 

D2Aload();  
}

void b26PopCallback(void *ptr)  //Current Units +
{if (num_n14 < 9)
  {num_n14 = (num_n14 + 1); }
else
 {num_n14 = 0; }
n14.setValue(num_n14);

D2Aload();
}

void b27PopCallback(void *ptr)  //Current Hundreds -
{ if (num_n12 > 0)
  {num_n12 = (num_n12 - 1); }
else
 {num_n12 = 9; }
n12.setValue(num_n12);

D2Aload();
}

void b28PopCallback(void *ptr)  //Current Tens -
{ if (num_n13 > 0)
  {num_n13 = (num_n13 - 1); }
else
 {num_n13 = 9; }
n13.setValue(num_n13);  

D2Aload();
}

void b29PopCallback(void *ptr)  //Current Units -
{  if (num_n14 > 0)
  {num_n14 = (num_n14 - 1); }
else
 {num_n14 = 9; }
n14.setValue(num_n14); 

D2Aload();
}

//****************Control Buttons***************

void b30PopCallback(void *ptr)  //Stop
{
if ((mode == 2) || (mode == 3))
{r3.setValue(1);
r4.setValue(0);
mode = 2;
}
}

void b31PopCallback(void *ptr)  //Start
{
  if ((mode == 2) || (mode == 3))
{r3.setValue(0);
r4.setValue(1);
mode = 3;
noPulses = 0;   //MIGHT NEED TO BE SEET TO = 1
} 
}

void b34PopCallback(void *ptr)  //LED Off
{  
mode = 0;  
r0.setValue(1);
r1.setValue(0);
r2.setValue(0);
r3.setValue(1);
r4.setValue(0);
}

void b35PopCallback(void *ptr)  //LED On
{  
mode = 1;  
r0.setValue(0);
r1.setValue(1);
r2.setValue(0);
r3.setValue(1);
r4.setValue(0);
}

void b36PopCallback(void *ptr)  //LED Cycle
{
mode = 2;
r0.setValue(0);
r1.setValue(0);
r2.setValue(1);
r3.setValue(1);
r4.setValue(0);  
}

//****************Control Buttons ~END***************

void b37PopCallback(void *ptr)  //Store Variables in EEPROM
//load these variables on reset or startup
{  
  EEPROM.update(0, num_n0);
  EEPROM.update(1, num_n1);
  EEPROM.update(2, num_n2);
  EEPROM.update(3, num_n3);
  EEPROM.update(4, num_n4);
  EEPROM.update(5, num_n5);
  EEPROM.update(6, num_n6);
  EEPROM.update(7, num_n7);
  EEPROM.update(8, num_n8);
  EEPROM.update(9, num_n9);
  EEPROM.update(10, num_n10);
  EEPROM.update(11, num_n11);
  EEPROM.update(12, num_n12);
  EEPROM.update(13, num_n13);
  EEPROM.update(14, num_n14);
  
}


void b38PopCallback(void *ptr)  //Load EEPROM valuse
{  
//Try Installing EEPROM values  ££$IT  WORKS""£$%%^
n0.setValue(num_n0);
n1.setValue(num_n1);
n2.setValue(num_n2);
n3.setValue(num_n3);
n4.setValue(num_n4);
n5.setValue(num_n5);
n6.setValue(num_n6);
n7.setValue(num_n7);
n8.setValue(num_n8);
n9.setValue(num_n9);
n10.setValue(num_n10);
n11.setValue(num_n11);
n12.setValue(num_n12);
n13.setValue(num_n13);
n14.setValue(num_n14); 
D2Aload();
}

void D2Aload()   // DAC out ~1.2V when set to 999mA
{
int sum = 0;    //Local Variable
sum = (num_n14 + (num_n13*10) + (num_n12*100));
dac.setVoltage(sum, false);
}


void cycleCheck() //check if incremented 1s and check if reached Cycle Time
{
 // 
}




void setup() {


// Serial.begin(4800);
//nexSerial.flush();
//nexSerial.begin(4800);
//Serial1.begin(4800);

//Serial2.flush(); 
//Serial2.begin(4800);          //Serial2.begin(9600); // Increase Baud if possible ZZ not working - still 9600  ZZ
Serial2.end();            //This Don't Work!!!!!

Wire.begin();   //Start the Wire driver
pinMode(LED_BUILTIN, OUTPUT);
pinMode(8, OUTPUT);   //LED Drive, 1= On, 0 = Off
pinMode(9, OUTPUT); 
pinMode(10, OUTPUT); 
//pinMode(11, OUTPUT); 
//pinMode(12, OUTPUT);
//pinMode(13, OUTPUT); 


dac.begin(MCP4725_ADDR);


/* would appear not - needs more looking into*/
//delay(1000); //let the display settle down
//Serial2.print("baud=38400");
//Serial2.write(0xff);
//Serial2.write(0xff);
//Serial2.write(0xff);
//Serial2.end();
//
//Serial2.begin(9600);



D2Aload();    //set DaC to 0V
num_n12 = EEPROM.read(12);
num_n13 = EEPROM.read(13);
num_n14 = EEPROM.read(14);


  nexInit();
b0.attachPop(b0PopCallback, &b0);
b1.attachPop(b1PopCallback, &b1);
b2.attachPop(b2PopCallback, &b2);
b3.attachPop(b3PopCallback, &b3);
b4.attachPop(b4PopCallback, &b4);
b5.attachPop(b5PopCallback, &b5);
b6.attachPop(b6PopCallback, &b6);
b7.attachPop(b7PopCallback, &b7);
b8.attachPop(b8PopCallback, &b8);
b9.attachPop(b9PopCallback, &b9);
b10.attachPop(b10PopCallback, &b10);
b11.attachPop(b11PopCallback, &b11);
b12.attachPop(b12PopCallback, &b12);
b13.attachPop(b13PopCallback, &b13);
b14.attachPop(b14PopCallback, &b14);
b15.attachPop(b15PopCallback, &b15);
b16.attachPop(b16PopCallback, &b16);
b17.attachPop(b17PopCallback, &b17);
b18.attachPop(b18PopCallback, &b18);
b19.attachPop(b19PopCallback, &b19);
b20.attachPop(b20PopCallback, &b20);
b21.attachPop(b21PopCallback, &b21);
b22.attachPop(b22PopCallback, &b22);
b23.attachPop(b23PopCallback, &b23);
b24.attachPop(b24PopCallback, &b24);
b25.attachPop(b25PopCallback, &b25);
b26.attachPop(b26PopCallback, &b26);
b27.attachPop(b27PopCallback, &b27);
b28.attachPop(b28PopCallback, &b28);
b29.attachPop(b29PopCallback, &b29);
b30.attachPop(b30PopCallback, &b30);
b31.attachPop(b31PopCallback, &b31);

b34.attachPop(b34PopCallback, &b34);
b35.attachPop(b35PopCallback, &b35);
b36.attachPop(b36PopCallback, &b36);
b37.attachPop(b37PopCallback, &b37);
b38.attachPop(b38PopCallback, &b37);

}

void loop() {

nexLoop(nex_Listen_List); //Look for commands from Nextion
offTotal();
onTotal();
noTotal();
cycleTotal();

if (mode == 0)
{
  digitalWrite(8, LOW);
  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(10, LOW);
}
else if (mode == 1)
{
  digitalWrite(8, HIGH);
  digitalWrite(LED_BUILTIN, HIGH);
  digitalWrite(10, HIGH);
}
else if (mode == 2)
{
  digitalWrite(8, LOW);
  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(10, LOW);
}
else if (mode ==3)
{
//"!"!"!""! Now Generate Pulses !"!"!"!"!//

//* start with cycling output */

//  onWidth = On time in ms // offWidth = Off time in ms

digitalWrite(8, ledState);
digitalWrite(10, ledState);

word currentMillis = millis();
unsigned long cycleMillis = millis();

if((unsigned long) (currentMillis - previousMillis) >= interval) {
  if (!ledState){ 
//
  interval = onWidth;
  }
  else
  {interval = offWidth;
  

  //Count Pulses
pulseCount = (pulseCount + 1);}
  ledState = !(ledState);
previousMillis = currentMillis;

if (pulseCount >= noPulses)
{
  mode = 4;
//  r3.setValue(1);
//  r4.setValue(0);
  pulseCount = 0; 
  digitalWrite(8, LOW);
digitalWrite(10, LOW);

cycleCheck;
}
}
}
else if (mode == 4)
{
// this just to test >> n15.setValue(4);        //n15.setValue(cycleMillis/1000);  // use to test >>> can't go below 10ms while active  // can't divide - must add  

cycleCheck;

}
}

Hi Martyn,

Thanks that's better, thank you.

I have 2 problems in helping you, I don't know anything about the Nextion libraries as I have never needed or used them, and seeing in all your code the bits relevant to our discussion. That said...

Is this where you were experimenting with my suggestions?:

//delay(1000); //let the display settle down
//Serial2.print("baud=38400");
//Serial2.write(0xff);
//Serial2.write(0xff);
//Serial2.write(0xff);
//Serial2.end();
//
//Serial2.begin(9600);

If so there are 2 things wrong with this code:
The 1000ms delay is in the wrong place, it needs to be after you send the change of baud rate to the Nextion to allow time for the Arduino to send the data to the serial port, so after Serial2.end(); (I'm not even sure you need that).
You then have Serial2.begin(9600); but you you have just told the Nextion you want the baud rate to be 38400. Try Serial2.begin(38400); instead. So, like this:

Serial2.print("baud=38400");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.end();            //I'm not sure you need this, I didn't when I was playing to test this
delay(1000);             //Give the Arduino time to finish sending the above data
Serial2.begin(38400);

I don't undestand why you are using 4800 baud, the default for the Nextion is 9600.

// Serial.begin(4800);
//nexSerial.flush();
//nexSerial.begin(4800);
//Serial1.begin(4800);

If you are still struggling then I suggest you create a new project just for playing with the Nextion, get it to do work as you want then use what you have learned in your main project. Trying to separate the Nextion stuff from the rest of the project makes finding the problem so much more difficult. When I create something I have separate tabs for each block of code that naturally belongs together, so for example, I am working on a Nextion controlled DAB radio, I have 2 tabs, one for everything to do with controlling the DAB radio and one for everything to do with sending data to and receiving data from the Nextion. Makes finding and fixing stuff a lot easier.

I had tried the faster baud rate in the first text box and the third box.
I have also tried other values of baud to see if they work and had been playing around with them, that's why some of them look odd. Saying that, the Mega still communicates with the Nextion at 9600 whatever value I set the Mega to, that's why it looks odd - I am unable to change the Mega baud rate from 9600.

I can change the Nextion baud rate, but then cannot get it to communicate as the Mega is still at 9600 (whatever I do).

Thanks for the idea - I will try just doing some simple stuff with the Nextion and Mega.
Will order a different Ardino - maybe the serial port is easier to control. I will have to sort out the serial port as the Nextion Arduino libraries are written for Mega Serial Port 2 and have to be adjusted to work with other ports and boards.

m

No worries Martyn,

If I had a Mega I'd try it for you, but I don't. When you find the answer please post it here as I am curious to know, and I'm sure it will help someone else too.

Perry

I have been able modify the programme to change the Baud rate.

If I comment out the "nexInit();" section with the button attach parameters (b0 to b38), just before void loop() I can change the Baud rate. I don't expect the buttons on the Nextion to work.
I need to look into the Nextion Arduino libraries as these might take control of the baud rate.

I will post once I have found out more

Martyn

ps TBC!!!

Thank you Martyn, makes sense I guess. I can't help with the libraries, sorry.

Sorted!!
I had to edit the NexHardware.cpp file as it sets the Arduino Baud rate to 9600.
I now have it running at 115200 baud.

Thanks for your assistance

Martyn

OK, good :slight_smile: