reset to zero back

hii all…
I have been working on lcd counter. So i have a problem to reset back the counter to zero “0”. The input for the reset button is “limitswitchpin5”. Please help me!!! :’( :’(

LCD_COUNTER.ino (4.46 KB)

Hi! First of all, I see you didn't initialize the leds (you left the comment for it but didn't write the code for it). Secondly, you are not saving the last button state when you push the reset button, ergo, the last button state doesn't change and no reset command is sent. Make this tweaks to the code and let me know. Take care

hi 58Dude thanks for your feedback. i forget to delete the led comment code sorry for that. could you give some example on how to saving the last button state??
thank you

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// this constant won’t change:
const int limitswitchPin1 = 2;
const int limitswitchPin2 = 3;
const int limitswitchPin3 = 4;
const int limitswitchPin4 = 5;
const int limitswitchPin5 = 6;

// Variables will change:
int buttonPushCounter1 = 1;
int buttonPushCounter2 = 1;
int buttonPushCounter3 = 1;
int buttonPushCounter4 = 1;
int buttonPushCounter5 = 1;
int totalcounter1 = 0;
int totalcounter2 = 0;
int totalcounter3 = 0;
int totalcounter4 = 0;
int totalcounter5 = 0;
int totalallcounter = 0;// counter for the number of button presses
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;// current state of the button
int buttonState5 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;
int lastButtonState3 = 0;
int lastButtonState4 = 0;
int lastButtonState5 = 0;

void setup() {
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(" HOYA ");
delay(1000);

// initialize the button pin as a input:
pinMode(limitswitchPin1, INPUT);
pinMode(limitswitchPin2, INPUT);
pinMode(limitswitchPin3, INPUT);
pinMode(limitswitchPin4, INPUT);
pinMode(limitswitchPin5, INPUT);

// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the limit switch input pin 1:
buttonState1 = digitalRead(limitswitchPin1);
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
totalcounter1= buttonPushCounter1++;
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;

// read the limit switch input pin 2:
buttonState2 = digitalRead(limitswitchPin2);
// compare the buttonState to its previous state
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
totalcounter2= buttonPushCounter2++;

}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState2 = buttonState2;

// read the limit switch input pin 3:
buttonState3 = digitalRead(limitswitchPin3);
// compare the buttonState to its previous state
if (buttonState3 != lastButtonState3) {
// if the state has changed, increment the counter
if (buttonState3 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
totalcounter3= buttonPushCounter3++;
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState3 = buttonState3;

// read the limit switch input pin 4:
buttonState4 = digitalRead(limitswitchPin4);
// compare the buttonState to its previous state
if (buttonState4 != lastButtonState4) {
// if the state has changed, increment the counter
if (buttonState4 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
totalcounter4 = buttonPushCounter4++;
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState4 = buttonState4;

// read the push button input pin 5 for reset:
buttonState5 = digitalRead(limitswitchPin5);
// compare the buttonState to its previous state
if (buttonState5 != lastButtonState5) {
// if the state has changed, increment the counter
if (buttonState5 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
totalallcounter=0;
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState5 = buttonState5;

totalallcounter = totalcounter1+totalcounter2+totalcounter3+totalcounter4;

//Serial.println(“on”);
Serial.print(“PRODUCTION OUTPUT:”);
lcd.setCursor(0,0);
lcd.print(“TOTAL OUTPUT SSC”);
Serial.println( totalallcounter);
lcd.setCursor(7,1);
lcd.print(totalallcounter);

delay(50);

}

No problem I would move all the "lastButtonState" lines inside the if sentences because you save the last state whenever you push it. If you don't go into the if sentences, then the value initialized will remain unchanged.

if i move the "lastbuttonstate",when i push the button it will continuously count. i have no idea about this please help me....thanks

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.... :) Also do a CTRL-T or goto TOOLS and select Auto-Format.

You do not really describe the problem properly ;) One thing is that button 5 resets the total counter but does not reset the individual counters.

And next you happily sum all the individual counters. Maybe that's your problem.

And a line like below might not do what you expect it to do.

      totalcounter4 = buttonPushCounter4++;

It will copy the buttonPushCounter to totalcounter and next increment the buttonPushCounter.