I have to write a stopwatch program using push button as a menu and pressing another two buttons


#1

#define sw1 digitalRead(3)
#define sw2 digitalRead(13)
#define sw9 digitalRead(2)

#include<LiquidCrystal.h>

LiquidCrystal lcd(5,6,7,8,9,10);

double i=0, a=millis(),c;
//long starts, elapse,finish;
void setup() {
// put your setup code here, to run once:
pinMode(13,INPUT);
pinMode(12,INPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(14,INPUT);
pinMode(15,INPUT);
pinMode(16,INPUT);
pinMode(17,INPUT);
pinMode(18,INPUT);
pinMode(19,INPUT);

lcd.begin(16,2);
Serial.begin(9600);
lcd.clear();

}

void loop()
{
// put your main code here, to run repeatedly:
lcd.clear();

lcd.print(“press start”);
delay(100);
while(sw9==1)
{
if(sw1 == 0)
{

lcd.clear();

a = millis();
while(sw2 == 1)
{

 c = millis();

i = (c - a) / 1000;
lcd.print(i);
lcd.setCursor(11,0);
lcd.print(“Sec’s”);
lcd.setCursor(0,0);
Serial.println©;
Serial.println(a);
Serial.println(i);
Serial.println("…");
delay(100);
}

if(sw2 == 0)
{
while(sw1 == 1)
{
lcd.setCursor(0,0);
lcd.print(i);
lcd.setCursor(11,0);
lcd.print(“Sec’s”);
lcd.setCursor(0,0);
delay(100);
}
}

}
}
}

i am unable to out of this loop after starting a stopwatch program while pressing a push button, i have to wriiten to menu after stopwatch starts or stops. can anyone guide me how i can do that ?


#2

Why do you want to exit the loop? Presumably you want to create a state machine that reacts to the state of the push buttons inside the loop to do whatever you need to do. If you exit the loop it will stop reacting to the pushbuttons which seems unlikely to be what you want to do. What you need to do isn’t clear from your description and with no comments in your code, it is way too much work to figure out what it currently does. You may also want to explore the preformatted text option (5th icon from the left in the reply tool bar) which allows you (with great difficulty :slight_smile: ) to post properly indented code to make it somewhat more readable.
You may also be better off in the arduino forums as there are mostly hardware type folks in here.

Peter


#3

i want to return to menu using sw9 push button…sw1 is to start a button and sw2 is to stop the button…i have to write a program for entering inside a menu using sw9 and after i enter in sw9 then i have to press sw1 to start a stopwatch and when i press sw2 the stopwatch gets stop and when i press sw9 again then it should ask me to start stopwatch again


#4

OK, so inside your loop which is reading the push buttons (which you don’t want to exit so it stops reading the push buttons), you need to know the current state and therefore what the new state should be based on what button is pressed. If you do a google search on state machines I expect there are examples of how this works available. First you need to create the state diagram which indicates what new state the machine should enterr on the press of any push button and go from there.

Peter