So I have a circuit for a timer (I don't think it is the problem)
Sorry for it being a little messy.
And I have this code (probably the problem):
// LiquidCrystal - Version: Latest
#include <LiquidCrystal.h>
/*
*/
const int switchUnitButton = 9;
const int selectButton = 10;
const int startButton = 8;
const int stopButton = 7;
const int d4 = 5;
const int d5 = 4;
const int d6 = 3;
const int d7 = 2;
const int rs = 12;
const int en = 11;
String unit = "sec";
int unitCycle = 1;
int secCount = 0;
int minCount = 0;
int hourCount = 0;
int dayCount = 0;
int switchUnitState;
int prevUnitState;
int selectState;
int prevSelectState;
int startState;
int prevStartState;
int stopState;
int prevStopState;
LiquidCrystal lcd = LiquidCrystal(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
pinMode(switchUnitButton, INPUT);
pinMode(selectButton, INPUT);
pinMode(startButton, INPUT);
pinMode(stopButton, INPUT);
lcd.begin(16, 2);
lcd.clear();
}
void loop() {
switchUnitState = digitalRead(switchUnitButton);
selectState = digitalRead(selectButton);
startState = digitalRead(startButton);
stopState = digitalRead(stopButton);
if (switchUnitState == HIGH) {
if (prevUnitState != switchUnitState) {
if (unitCycle < 4) {
unitCycle++;
} else if (unitCycle == 4) {
unitCycle = 1;
}
}
}
Serial.print(unitCycle + "\n");
if (unitCycle == 1) {
unit = "sec";
} else if (unitCycle == 2) {
unit = "min";
} else if (unitCycle == 3) {
unit = "hour";
} else {
unit = "day";
}
Serial.print(unit + "\n");
if (selectState == HIGH) {
if (prevSelectState != selectState) {
if (unit == "day") {
dayCount++;
} else if (unit == "sec") {
secCount++;
} else if (unit == "hour") {
hourCount++;
} else if (unit == "min") {
minCount++;
}
}
}
lcd.setCursor(0, 0);
lcd.print("Sec: " + String(secCount) + " Min: " + String(minCount));
lcd.setCursor(0, 1);
lcd.print("Hr: " + String(hourCount) + " Day: " + String(dayCount));
prevUnitState = switchUnitState;
prevSelectState = selectState;
prevStartState = startState;
prevStopState = stopState;
}
And sometimes without me clicking a button the unit value changes or sometimes when I click the selectButton for the amount to go up it changes which unit it is adding to. And sometimes the string has parts before it from other units like "inhour" what is happening?
-
Your second button is wired wrong - or is that just a "typo"?Majenko– Majenko2019年04月25日 11:05:15 +00:00Commented Apr 25, 2019 at 11:05
-
Oh, and all that String abuse will cause untold problems. You don't need any of it.Majenko– Majenko2019年04月25日 11:05:46 +00:00Commented Apr 25, 2019 at 11:05
-
So I should just use unitCycle for which unit rather than the changing string? And yes that button was a "typo";BeastCoder2– BeastCoder22019年04月25日 11:07:16 +00:00Commented Apr 25, 2019 at 11:07
-
I fixed that "typo"BeastCoder2– BeastCoder22019年04月25日 11:09:51 +00:00Commented Apr 25, 2019 at 11:09
-
1Seems you forgot to debounce the buttons.Edgar Bonet– Edgar Bonet2019年04月25日 11:53:07 +00:00Commented Apr 25, 2019 at 11:53
1 Answer 1
You are seriously abusing the String class. There is absolutely no need to use any Strings at all in your program.
For example:
if (unitCycle == 1) {
unit = "sec";
} else if (unitCycle == 2) {
unit = "min";
} else if (unitCycle == 3) {
unit = "hour";
} else {
unit = "day";
}
Serial.print(unit + "\n");
would be better written as:
if (unitCycle == 1) {
Serial.println(F("sec"));
} else if (unitCycle == 2) {
Serial.println(F("min"));
} else if (unitCycle == 3) {
Serial.println(F("hour"));
} else {
Serial.println(F("day"));
}
And:
if (selectState == HIGH) {
if (prevSelectState != selectState) {
if (unit == "day") {
dayCount++;
} else if (unit == "sec") {
secCount++;
} else if (unit == "hour") {
hourCount++;
} else if (unit == "min") {
minCount++;
}
}
}
as:
if (selectState == HIGH) {
if (prevSelectState != selectState) {
if (unitCycle == 1) {
secCount++;
} else if (unitCycle == 2) {
minCount++;
} else if (unitCycle == 3) {
hourCount++;
} else {
dayCount++;
}
}
}
And then the cardinal sin:
lcd.print("Sec: " + String(secCount) + " Min: " + String(minCount));
All that string creation, concatenation, and destruction is just evil. Instead, use:
lcd.print(F("Sec: "));
lcd.print(secCount);
lcd.print(F(" Min: "));
lcd.print(minCount);
-
@BeastCoder2 You might want to read this: majenko.co.uk/blog/evils-arduino-stringsMajenko– Majenko2019年04月25日 11:25:16 +00:00Commented Apr 25, 2019 at 11:25
-
Alright I will, your website is really cool!BeastCoder2– BeastCoder22019年04月25日 11:26:43 +00:00Commented Apr 25, 2019 at 11:26
-
eyeroll... I had one wire in the wrong pin...BeastCoder2– BeastCoder22019年04月25日 22:20:20 +00:00Commented Apr 25, 2019 at 22:20