Help with annoying bug thats driving me mad

/* test code to store and forward SMS messages recevied over UART*/


//#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

#define OLED_WIDTH 128
#define OLED_HEIGHT 64

#define OLED_ADDR 0x3C

Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);

//SoftwareSerial Serial1(8, 10);  // RX, TX
int RxCnt = 0;
int state = 0;
int RxOver = 1;




int in[35];
char msg1[35];
String Smsg1;
char clipMsg1[31];
char msg2[35];
String Smsg2;
char clipMsg2[31];
char msg3[35];
String Smsg3;
char clipMsg3[31];

int action = 0;
int slot1=0;
int slot2=0;
int slot3=0;

unsigned long previousMillis = 0;
unsigned long currentMillis;
const long interval = 2000;
int ready = 1;

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  //display.clearDisplay();
  Serial.begin(115200);
  while (!Serial) {
    ;
  }
  Serial.println("Message server start");
  Serial1.begin(38400);
  Serial.println("Serial 1 started");


  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  Serial.println("display begin");


  // display.setTextSize(2);
  //display.setTextColor(WHITE);
  //display.setCursor(0, 0);
  //Serial.println("text sent");
  // display.println("Welcome");
  //Serial.println("text printed");
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  // Serial.println("welcome screen");
  display.println("ARDUINO");
  display.println("SMS");
  display.println("SERVER");
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 51);
  display.println("READY");


  display.display();
  // Serial.println("delay");
  delay(1000);
  display.clearDisplay();
  Serial.println("entering loop");
}

void loop() {  // run over and over
if(slot3==1){
  Serial.println("slot 3 full");
  delay(2000);
}
/*if(slot2==1){
  Serial.println("slot 2 full");
  delay(2000);
}*/
if (currentMillis - previousMillis >= interval) {
        // save the last time you blinked the LED
        previousMillis = currentMillis;
        display.clearDisplay();
        display.setTextSize(2);
        display.setTextColor(WHITE);
        display.setCursor(0, 0);
        display.println("ARDUINO");
        display.println("SMS");
        display.println("SERVER");
        display.setTextSize(1);
        display.setTextColor(WHITE);
        display.setCursor(0, 51);
        display.println("READY");
        display.display();
      }
  
  //Serial.println("loopstart");
  //digitalWrite(13,HIGH);
  //delay(1000);
  //digitalWrite(13,LOW);
  //delay(1000);
  if (Serial1.available() > 0) {
    Serial.println("data incoming");
    action=0;


    for (byte i = 0; i < 36; i = i + 1) {
      in[i] = (Serial1.read());
      Serial.print(" in:  ");
      Serial.print(i);
      Serial.print(":   ");
      Serial.println(in[i]);
      delay(10);
    }
    if (in[3] == 60) {
      Serial.println("message was incoming");
      ready = 0;
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("INCOMING");
      display.setCursor(0, 17);
      display.println("MESSAGE");
      display.display();
      Serial.print("Action   :");
    action = in[4] - 48;

    Serial.println(action);
    Serial.println((String)"slot1:"+slot1);
    Serial.println((String)"slot2:"+slot2);
    Serial.println((String)"slot3:"+slot3);

    }
    if (in[3] == 62) {
      Serial.println("message was outgoing");
    }
    
    //delay(1000);

    if (action == 1) {  // If first character is a 1, then the message gets stored in the msg1 array
      for (byte i = 0; i < 36; i = i + 1) {
        msg1[i] = in[i];  // store the received message in the array so it can be recalled later, furutre messages wont delete the stored message unless the first character is a 1
      }
      for (byte i = 0; i < 36; i = i + 1) {
        Serial.print("MSG1 char:");
        Serial.print(i);
        Serial.print("    :");
        Serial.println(msg1[i]);
      }
      
      delay(2000);                       // needed? to settle down the radio
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("MSG1 RCVD");
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 17);
      display.println(msg1);
      display.display();
      currentMillis = millis();
      Serial1.println("SMS:msg1 rcvd");  // confirmation of msg1 receipt
      Serial.println("slot1 full");
      slot1=1;
    }
    if (action == 2) {  // same for message 2
      for (byte i = 0; i < 36; i = i + 1) {
        msg2[i] = in[i];
      }
      for (byte i = 0; i < 36; i = i + 1) {
        Serial.print("MSG2 char:");
        Serial.print(i);
        Serial.print("    :");
        Serial.println(msg2[i]);
      }
      
      delay(2000);
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("MSG2 RCVD");
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 17);
      display.println(msg2);
      display.display();
      currentMillis = millis();
      Serial1.println("SMS:msg2 rcvd");
      Serial.println("slot2 full");
      slot2=1;
    }
    if (action == 3) {  // same for message 3
    Serial.println("action 3 called");
      for (byte i = 0; i < 36; i = i + 1) {
        msg3[i] = in[i];
      }
      for (byte i = 0; i < 36; i = i + 1) {
        Serial.print("MSG3 char:");
        Serial.print(i);
        Serial.print("    :");
        Serial.println(msg3[i]);
      }
      
      delay(2000);
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("MSG3 RCVD");
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 17);
      display.println(msg3);
      display.display();
      currentMillis = millis();
      Serial1.println("SMS:msg3 rcvd");
      Serial.println("slot3 full");
      slot3=1;
    }

    if (action == 7) {  // recall message 1
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("MSG1");
      display.setCursor(0, 17);
      display.println("REQUESTED");
      display.display();
      currentMillis = millis();
      for (byte i = 0; i < 32; i = i + 1) {
        int y = (i+5);
      clipMsg1[i] = msg1[y];  
      }
      Smsg1=clipMsg1;
      if(slot1==1){ 
      Serial.println((String) "MSG1 :" + Smsg1);
      //Serial.println(Smsg1);
      Serial1.println((String) "SMS:" + Smsg1);
      }
      if(slot1==0){ 
      Serial.println((String) "MSG1 : EMPTY");
      //Serial.println(Smsg1);
      Serial1.println((String) "SMS:EMPTY");
      }

    }
    if (action == 8) {  // recall message 2
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("MSG2");
      display.setCursor(0, 17);
      display.println("REQUESTED");
      display.display();
      currentMillis = millis();
      for (byte i = 0; i < 32; i = i + 1) {
        int y = (i+5);
      clipMsg2[i] = msg2[y];  
      }
     
      Smsg2 = clipMsg2;
      if(slot2==1){
      Serial.println((String) "MSG2 :" + Smsg2);

      Serial1.println((String) "SMS:" + Smsg2);
      }
      if(slot2==0){
        Serial.println((String) "MSG2 :" + Smsg2);
      //Serial.println(Smsg1);
      Serial1.println((String) "SMS:EMPTY");
      }
    }
    if (action == 9) {  // recall message 3
      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("MSG3");
      display.setCursor(0, 17);
      display.println("REQUESTED");
      display.display();
      currentMillis = millis();
      for (byte i = 0; i < 32; i = i + 1) {
        int y = (i+5);
      clipMsg3[i] = msg3[y];  
      }
     
      Smsg3 = clipMsg3;
      
      if(slot3==1){
      Serial.println((String) "MSG3 :" + Smsg3);
      Serial1.println((String) "SMS:" + Smsg3);
      }
      if(slot3==0){
        Serial.println((String) "MSG3 : EMPTY");
      //Serial.println(Smsg1);
      Serial1.println((String) "SMS:EMPTY");
      }
    }

    //////////////////CLEAR THE ARRAY
    for (byte i = 0; i < 36; i = i + 1) {  // write 0 to all array locations for the input array, this should avoid confusion about where corrupt data comes from, not possible to be left in the array
      in[i] = 0;
      Serial.print("   ");
      Serial.print(i);
      Serial.print(":   ");
      Serial.println(in[i]);
      

      delay(10);
    }
  }
}

Hi,
My code is above, I am experiencing a glitch which I cannot track down the cause of.

basically.
slot1, slot2 and slot3 are integers being used to keep track of if there is data in arrays msg1, msg2 and msg3.

when data is added to the arrays, the slot is set to 1, this affects how the code works later, if the slot value is a 1 or 0.

directly before setting the slots to 1, there is a serial.println to indicate that this has been done deliberately in the right part of the code.

There is only one place where each slot is set to 1 in the code, and there is a serial prompt generated as the 1 is stored.

when the code sets slot 1 as 1, randomly slots 2 or 3 will get set to one also, only ever one of them though, I have not seen both.

This is realiable, there is no serial prompt to show that they should have been set to 1, just the serial prompt for slot 1.

and what is breaking my mind, is that if i put

if(slot3==1){
  Serial.println("slot 3 full");
  delay(2000);
}
/*if(slot2==1){
  Serial.println("slot 2 full");
  delay(2000);
}*/

at the start of the loop, this unwanted behaviour stops for the slot which is not commented out. If I remove the comment exclusion from both slots, then neither gets set incorrectly.

if i comment both out, then the random setting of one slot by error resumes.

Does anyone have any idea what may cause such behaviour?

perhaps there is a bug in the code, but the fact that just including an if with a serial print, stops it happening, doesnt make sense to me.

Its late where I am , so perhaps ive messed up the code and cant see it. perhaps not.

hopefully someone can help

thanks in advance

When I hear memory changing and you didn't code to write there I start looking for memory corruption. First suspect is an array being written out of bounds. This for loop will write to msg1[35] but msg1 is only 35 characters long so it only has 0 - 34.

That's one. You've got a lot of arrays. I'll let you hunt through to see if there are more.

1 Like

There's another one.

3rd:

4th:

and so on

The whole code is just a mess of similar pieces, copied and changed. The variable names are uninformative, there are no procedures, everything is placed in a loop.
Such programs are very difficult to debug once they no longer fit on one page.

2 Likes

You commented-out SoftwareSerial library and the instance, then called it. Not that it caused or is part of your problem, but it shows your editing is causing problems before the mentioned problem.

this is the first time ive attempted to use arrays as ive always been able to just make a ton of IF statements to suit my needs.

I will fix this and have another try

thanks

copied and changed.........

yes, since i want to do the same thing with several arrays, copying was the quickest way.
what would you suggest ?

i had indeed commented out software serial. I had been using it for the uart output, but when i added an i2c oled to the mix the code started to reset the mcu. reading around software serial uses a lot of time polling the pins?

so I decided to use a mega r3 instead ofg an uno and use its native uart pins instead, that fixed the issue of the mcu resetting.

clearly I missed a call to it . thanks

could you show the line where I call it? I cannot see it.

You could use the find command in your text editor.

TBH I'd be lost without it. I never look for anything. That's what text editors do better.

a7

@spruce_m00se To be honest, the first thing you should do is, in the IDE, go to File\Preferences


and turn on "Show verbose output diring compilation and upload,
then set Compiler warnings to More, or better yet, All.

Then recompile your code, and look through the output. You'll note that the compiler is trying to tell you about several things that, while not explicit errors, are very likely unintended consequences of the code you've written.

Sometimes, the compiler cannot discern whether what you've coded is 'correct by intent', or unknowing violation, so instead of an error, you get a warning. Heed them. You can see some of what I'm talking about in the background of my screen capture. Address each and every one of them, if you can; if not, come back and ask questions, quoting exact error messages, and providing an updated code in tags for continuity.

Those are the overt mistakes in your code. Once those are fixed, re-post, and we can work on the rest. Makes no sense to provide you with a fixup, until those basic problems are understood and repaired, and you can tell us what/if the code is still not doing what you want.

Here are 10 of the 29 occurrences...

  if (Serial1.available() > 0) {
    Serial.println("data incoming");
    action=0;

Was it? Because you ended up here for a day trying to sort it out. I would break that stuff up into some functions that do one simple thing each and give them some meaningful names.

The commented out software serial was called mySerial orginially, when I did a complete replace with, the commented out line also switched to Serial1, so I understand thinking that I am still calling it, but infact it refers to the second serial port on the mega r3.

So in this case its ok,

Thanks for this. I didnt know about this, I will do so and try to improve it......

As a hobbyist, generally if it compiles and works as expected I call it good and move on.....

thanks

1 Like

One updateing the size of the arrays corectly, there are no red lines in the verbose output.

I think I may have once had verbose turned on, but it liekly got reset on an IDE update or when I changed a PC in the past,

thanks for the advice on that

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.