Hello, i recently made one project which worked well for the rest of the time but at some point faulted. The issue is that in the 2 row in which this text is print on it
lcd.print(String(" P (%): ") + ((float) programVariables.timesHit / programVariables.timesRan * 100.0) + String(" %"));
the String(" %") sometimes may get print for the second time therefore resulting in 2 percentage characters together instead of one.
Code:
#include <LiquidCrystal.h>
//Counters
struct counters {
unsigned int timesHit = 0;
unsigned int numberToHit = 2; /* Number to check for */
unsigned int numberLimit = 3; /* Range 0 to ? */
unsigned int timesRan = 0;
bool canRun = true;
const int relay = 13;
};
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
counters programVariables;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(programVariables.relay, 0x0);
randomSeed(analogRead(0));
lcd.begin(20, 4);
lcd.setCursor(0, 0);
lcd.print(String("R: ") + int(programVariables.numberLimit) + String(" T: ") + int(programVariables.numberToHit));
setDefault();
}
void loop() {
changeTime();
int randomNumber = random(programVariables.numberLimit);
programVariables.timesRan = programVariables.timesRan + 1;
if (randomNumber == programVariables.numberToHit && programVariables.canRun == true) {
blinkLED(400);
programVariables.canRun = false;
programVariables.timesHit = programVariables.timesHit + 1;
lcd.setCursor(0, 2);
lcd.print(String("Hit ") + int(programVariables.timesHit) + String(" Times"));
lcd.setCursor(0, 1);
lcd.print(String(" P (%): ") + ((float) programVariables.timesHit / programVariables.timesRan * 100.0) + String(" %"));
programVariables.canRun = true;
};
delay(300);
}
void blinkLED(int delayTime) {
digitalWrite(programVariables.relay, 0x1); //HIGH
Serial.println("Pin 13 High");
delay(delayTime);
digitalWrite(programVariables.relay, 0x0);
Serial.println("Pin 13 Low");
}
void changeTime(){
unsigned long currentMillis = millis();
unsigned long seconds = currentMillis / 1000;
unsigned long minutes = seconds / 60;
unsigned long hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
lcd.setCursor(0, 3);
lcd.print("Time: ");
lcd.print(hours);
lcd.print(":");
if (minutes < 10) {
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
}
void setDefault(){
lcd.setCursor(0, 2);
lcd.print(String("Hit ? Times"));
lcd.setCursor(0, 1);
lcd.print(String("P (%) ?%"));
}
This does not always happen after uploading the code nevertheless sometimes it happens. Moreover, i also noticed this happens after very long hours of uptime.
