Variable value disappears?

I read a value from bluetooth(works) and put it from char c into int mins1/mins2/hours3/hours4 (and converting it OF COURSE) and when I print it after putting it in inside a if statement it seems to be there. but when I try to display it on the display nothing shows up(the value does not get updated) so I checked and the value seems to be erased by the time I ask for it in the display section most below, why is that? how do I fix that???

here is my code:

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

SoftwareSerial mySerial(2,3);
String readString = "";
char c;
int mins1 = 0;
int mins2 = 0;
int hours3 = 0;
int hours4 = 0;
bool pastmins1 = false;
bool pasthours3 = false;
bool pastmins2 = false;
bool pasthours4 = false;
String displayoled = "";
void setup()
{
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
mySerial.begin(9600);
Serial.begin(9600);
  display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,10);             // Start at top-left corner
  display.println(F("Power on"));
  display.display();
}
void loop()
{ 
  while (mySerial.available()) {
    delay(10);  //small delay to allow input buffer to fill
    c = mySerial.read();  //gets one byte from serial buffer
    if(pastmins1){
      mins1 = c-'0';
      pastmins1 = false;
      Serial.println(mins1);
      Serial.println(mins2);
      Serial.println(hours3);
      Serial.println(hours4);
    }
    else if(pastmins2){
      mins2 = c-'0';
      pastmins2 = false;
      Serial.println(mins1);
      Serial.println(mins2);
      Serial.println(hours3);
      Serial.println(hours4);
    }
    else if(pasthours3){
      hours3 = c-'0';
      pasthours3 = false;
      Serial.println(mins1);
      Serial.println(mins2);
      Serial.println(hours3);
      Serial.println(hours4);
    }
    else if(pasthours4){
      hours4 = c-'0';
      pasthours4 = false;
      Serial.println(mins1);
      Serial.println(mins2);
      Serial.println(hours3);
      Serial.println(hours4);
    }
    else{
      readString += c; 
    }
    if(readString == 'reset'){
       mins1 = 0;
       mins2 = 0;
       hours3 = 0;
       hours4 = 0;
    }
    if(readString == "setmins1"){
      pastmins1 = true;
      readString="";
    }
    if(readString == "setmins2"){
      pastmins2 = true;
      readString="";
    }
    if(readString == "sethours3"){
      pasthours3 = true;
      readString="";
    }
    if(readString == "sethours4"){
      pasthours4 = true;
      readString="";
    }
  } //makes the string readString
  if (readString.length() > 0) {
      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setTextSize(1);
      display.setCursor(0, 0);
      // Display static text
      display.println(readString);
      display.setCursor(0, 40);
      displayoled += mins1;
      displayoled += mins2;
      char d = ':';
      displayoled += d;
      displayoled += hours3;
      displayoled += hours4;
      display.println(displayoled);
      display.display();  
      displayoled = "";
      readString=""; //clears variable for new input

  }
}

the code has several opportunities to make the readString a "" before the code gets to the display part.

strings use double quotes, ""

What are your lineendings on Serial Monitor when you are sending information to this program?

yeah it does, its so when I enter commands they wont display on screen but will do their part without showing up

Oh you don't want the readString to be displayed on the screen. I got it now. Sorry.

readString is a "" when it gets to display.println(readString);.

okay I fixed the reset button but that is none of my problem. the conversion using strings works fine too.
i dont think there are any. this is not part of the problem, my problem is that the value of the variable gets somehow erased?

What variable?

a7

I'll bet the problem goes away, if you rewrite the program to use C-strings, and the string.h library (strcmp(), etc.), instead of String objects.

What, exactly, does your input look like? Is it all one line? Multiple lines? separated by whitespace, newline, carriage return + newline, ????

Your input capture code is not robust to errant characters.

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