At the bottom of an instruction page I put 4 boxes (buttons) to select baud rate. I have a scale set at 4800. If the Mega 2560 Serial1 happens to be set to 2400 and I "click" 4800, everything works ok. If it's set to 4800 and I select 2400, I expect the data stream to be garbage, but it's not. It's valid data. It appears that the baud rate didn't change. It appears to work if the change is faster but not slower.
Compared to most people here I am sub-beginner. Not looking for the most elegant or compact code. Maybe I'll get there someday but at 77 I'm not holding my breath.
This was added to setup(). I probably could have put the baud rate directly into 2 bytes of EEPROM, but I didn't.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // when communicating with serial monitor
Baud_ndx = EEPROM.read(ep_start_P + 46);
switch (Baud_ndx)
{
case 1:
Baud = 1200;
break;
case 2:
Baud = 2400;
break;
case 3:
Baud = 4800;
break;
case 4:
Baud = 9600;
break;
}
Serial1.begin(Baud);
This is what I added to the last instruction page.
tmyGLCD.setFont(Retro8x16);
myGLCD.setColor(0xffffaa);
myGLCD.print("Baud Rate", CENTER, 220); // display baud choices
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (9, 240, 49, 265);
myGLCD.drawRoundRect (69, 240, 109, 265);
myGLCD.drawRoundRect (129, 240, 169, 265);
myGLCD.drawRoundRect (189, 240, 229, 265);
myGLCD.print("1200", 13, 246);
myGLCD.print("2400", 73, 246);
myGLCD.print("4800", 133, 246);
myGLCD.print("9600", 193, 246);
Baud_ndx = EEPROM.read(ep_start_P + 46);
myGLCD.setColor(255, 0, 0); // change current baud box to red
if(Baud_ndx == 1)
myGLCD.drawRoundRect (9, 240, 49, 265);
if(Baud_ndx == 2)
myGLCD.drawRoundRect (69, 240, 109, 265);
if(Baud_ndx == 3)
myGLCD.drawRoundRect (129, 240, 169, 265);
if(Baud_ndx == 4)
myGLCD.drawRoundRect (189, 240, 229, 265);
while(true)
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if (y >= 240 && y <= 265){
myGLCD.setColor(255, 0, 0);
if(x >= 9 && x <= 49){
Baud_ndx = 1;
Baud = 1200;
myGLCD.drawRoundRect (9, 240, 49, 265);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (69, 240, 109, 265);
myGLCD.drawRoundRect (129, 240, 169, 265);
myGLCD.drawRoundRect (189, 240, 229, 265);
}
if(x >= 69 && x <= 109){
Baud_ndx = 2;
Baud = 2400;
myGLCD.drawRoundRect (69, 240, 109, 265);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (9, 240, 49, 265);
myGLCD.drawRoundRect (129, 240, 169, 265);
myGLCD.drawRoundRect (189, 240, 229, 265);
}
if(x >= 129 && x <= 169){
Baud_ndx = 3;
Baud = 4800;
myGLCD.drawRoundRect (129, 240, 169, 265);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (9, 240, 49, 265);
myGLCD.drawRoundRect (69, 240, 109, 265);
myGLCD.drawRoundRect (189, 240, 229, 265);
}
if(x >= 189 && x <= 229){
Baud_ndx = 4;
Baud = 9600;
myGLCD.drawRoundRect (189, 240, 229, 265);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (9, 240, 49, 265);
myGLCD.drawRoundRect (69, 240, 109, 265);
myGLCD.drawRoundRect (129, 240, 169, 265);
}
}
if (((x >= 86) && (x <= 152)) && ((y >= 298) && (y <= 317))) { // EXIT
EEPROM.update((ep_start_P + 46), Baud_ndx);
Serial1.end();
Serial1.begin(Baud);