Hi, what my project does is, when sensor1 is activated it starts a timer and when sensor 2 is activated it stops the timer. Lcd displays this calculation 10/..(time between sensors)....=.........
So I added a push button. Every time someone pushes it "10" in the calculation increments by 10. My project works fine until here. Here is my 2 problem:
After I press the button and the value 10 becomes 20, I want it to find the result of the new operation like I push the button 2 times and then activated the sensors for 10 seconds the output on the lcd should be 30/10=3. My code at the moment can add 10 to 10 but it doesn't calculate the operation it looks like 20/10s= 10 did you get it? It still calculates the operation as if 10/10=1 like I didnt push the button.
Second problem I have is; when I press the button more than 5 times and the lcd displays 60, what I want is if i press the key again i want it to go back to 10 and not to 70. I write it like this;
if (buttonPushcounter>5)
ten=10;
lcd.Setcursor(0,1);
lcd.print(ten);
But it didn't work so I deleted it from my code. Here is the rest of my code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
int startPin = 3;
int stopPin = 4;
const int Up_buttonPin = 2 ;
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long startTime;
unsigned long stopTime;
unsigned long screenTime;
float elapsedTimes = 0.0;
float time = 0.0;
bool start = false;
bool lastStart = false;
bool stop = false;
bool lastStop = false;
bool stateRunning = false;
int ten = 10;
int buttonPushCounter = 0; // counter for the number of button presses
int up_buttonState = 0; // current state of the up button
int up_lastButtonState = 0; // previous state of the up button
void setup() {
pinMode(startPin, INPUT_PULLUP);
pinMode(stopPin, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(Up_buttonPin, INPUT_PULLUP);
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(1,1);
lcd.print(buttonPushCounter);
}
void loop() {
checkUp();
checkDown();
lastStart = start;
lastStop = stop;
start = digitalRead(startPin);
stop = digitalRead(stopPin);
// Start Timer
if (!start && lastStart) {
startTime = micros();
stateRunning = true;
digitalWrite(LED_BUILTIN, HIGH);
lcd.setCursor(0,1);
lcd.print((micros()-startTime)/1000000.0);
}
// Stop Timer
if (!stop && lastStop) {
stateRunning = false;
digitalWrite(LED_BUILTIN, LOW);
lcd.setCursor(0,1);
lcd.print("10/ =");
lcd.setCursor(5, 1);
lcd.print((stopTime - startTime)/1000000.0);
lcd.setCursor(9, 1);
lcd.print("s");
}
if (stateRunning) {
stopTime = micros();
}
// LCD Update
if ( micros() - screenTime >= 100000) {
screenTime = micros();
elapsedTimes = (stopTime - startTime) / 1000000.0;
lcd.setCursor(11,1);
lcd.print(ten/elapsedTimes);}
}
void checkUp(){
up_buttonState = digitalRead(Up_buttonPin);
// compare the buttonState to its previous state
if (up_buttonState != up_lastButtonState) {
// if the state has changed, increment the counter
if (up_buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter=buttonPushCounter+10;
lcd.setCursor(0,1);
lcd.print(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
//lcd.setCursor(0,1);
//lcd.print("10");
}
delay(50);
}
// saving the current state as the last state, for next time through the loop
up_lastButtonState = up_buttonState;
}