I am a newbie, beginning to post more.. to learn more.
My first question about Momentary buttons is close to a solution but still far away.
The goal is to cycle through programs with the touch of a momentary button.
I am trying to learn and understand butI know something isn't jibing...
Any help would be greatly appreciated...
int LED = 13;
int Button = 12;
int programState = 0;
void setup()
{
pinMode(LED,OUTPUT);
pinMode(Button,INPUT);
}
void loop(){
if (digitalRead (Button)==0){ // assumes button declared as INPUT with internal pullup enabled
programState = programState +1;}
if (programState ==3){ // reset after 5, or whatever amount you select
programState = 1;
delay (25); // may not need if the programs run long enough for the button to debounce
}
switch(programState);
case 1;
digitalWrite(LED,HIGH);
delay (500);
digitalWrite(LED,LOW);
delay (500);
break;
switch(programState);
case 2;
digitalWrite(LED,HIGH);
delay (100);
digitalWrite(LED,LOW);
delay (100);
break;
switch(programState);
case 3;
switch(programState);
digitalWrite(LED,LOW);
}
Thanks WildBill
I am grateful for all comments and help. I feel I know the "essence" of how to contruct the program in terms of logic, it is the details that get me..
Thanks again, let me digest, process and implement my new helpful comments...
I'll be back..
I feel I am close...
when compiling, it is not happy with the "break" portion..There is something with the "switch" that isn't jibing..
I tried to troubleshoot but now I'm a bit confused..
Can someone take a look? I know this is simple, but I am trying to learn!
int LED = 13;
int Button = 12;
int programState = 0;
void setup()
{
pinMode(LED,OUTPUT);
pinMode(Button,INPUT);
digitalWrite(Button,HIGH); //enable pull-up
}
void loop(){
if (digitalRead (Button)==0){ // assumes button declared as INPUT with internal pullup enabled
programState = programState +1;}
if (programState ==3){ // reset after 5, or whatever amount you select
programState = 1;
delay (25); // may not need if the programs run long enough for the button to debounce
}
switch(programState)
case 1:
digitalWrite(LED,HIGH);// basic blink
delay (500);
digitalWrite(LED,LOW);
delay (500);
break;
case 2:
digitalWrite(LED,HIGH);//quicker blink
delay (100);
digitalWrite(LED,LOW);
delay (100);
break;
case 3:
digitalWrite(LED,LOW); //off
}
I believe that did it! I fixed it and more importantly, I learned!
It compiled fine but I am at work and so I am dying to go home and see if it REALLY works..
ok, I give.
I had excellent input from which I have learned from...but...I can't get this to do what I want. I got the code to compile and circuit seems right, but when I try and cycle through programs, it will respond to a button push, but then revert back to what it was doing.
OK vague.
Goal: push button: start first program, push button: go to 2nd program, push button: got to 3rd program, push button: stop.
NOW: I have tried tinkering with it and lowering the "cases" to understand the system.
I upload and it the LED blinks..then when I push the button it stops for a second...then starts blinking again..
Though I have had excellent input and advice, I still am at odds with this.
Is a momentary button not right for turning on a system-cycling through programs ans turning off??
int LED = 13;
int Button = 12;
int programState = 0;
void setup()
{
pinMode(LED,OUTPUT);
pinMode(Button,INPUT);
digitalWrite(Button,HIGH); //enable pull-up
}
void loop(){
if (digitalRead (Button)==0){ // assumes button declared as INPUT with internal pullup enabled
programState = programState +1;}
if (programState ==2){ // reset after 5, or whatever amount you select
programState = 1;
}
switch(programState)
{
case 1:
digitalWrite(LED,HIGH);
delay (100);
digitalWrite(LED,LOW);
delay(100);
case 2:
digitalWrite(LED,LOW); //off
}
}
You should probably consider debouncing the switch too - a delay(20); in the initial if would do it. It doesn't matter as your code is now, but it may well do when you've made the fixes for the issues PaulS pointed out. Also, somewhere along the way, your break statements went away.