Go Down

Topic: How to start a loop with a manual switch (Read 1 time) previous topic - next topic

namenerb

Hey all,
I have a loop that works fine.  It starts a sequence of pins turned on and left on for a variable duration on each pin. An LED lights upon completion of each pin's cycle and stays on for verification.

The next step is to have the board powered on, then with a flip of a normally open spring loaded switch, start the loop and let it continue until finished (loop forever). 

I have tried the following code with no luck. 


Code: [Select]
void loop() {

digitalRead(buttonPin);
switch(1)
{
case 1: 
if(buttonPin == HIGH && Count == 0)
{
startTime = millis();
digitalWrite(ch1,HIGH);
Count = 1;                  // Here is where I expect it to start looping

case 2:
if(buttonPin == HIGH && Count == 1)  // Here is where I want it to continue looping until finished
 
 

In setup, I have defined Count = 0, buttonPin,LOW
Do I need another case statement or 'break' in case buttonPin is LOW? (it will be until I flip the switch)
Do I need a different function?  None of the button tutorials looked like they would work.  Once I flip the switch, it stays on for the duration.
I appreciate any input, be nice.  Pointers help more than "you made a mistake and I won't tell you anything" Thanks!

Henry_Best


I have tried the following code with no luck. 
I'm not surprised.
Code: [Select]
switch(1)
Switch the value of a variable, not a number.
e.g. switch (x) where, in your case, x can have the value 1 or 2.
End each case: with a break; statement.
e.g. case 37:
       do your thing
       break;
       case 38:......

Delta_G

So the switch thing happens only once?  Why not just block at the end of the setup function and wait for the switch before ending setup.  Then loop takes over from there?

namenerb

Henry, thanks, I'm feeling my way in the dark....

Delta_G, I like the sound of that, what function could I use to stall it there?   That would be the optimal place.

And, yes, switch happens once to activate the loop, and will stay on.    Testing the loop, I had startTime = millis() in setup, but if I pause at the start of the loop waiting for a button push then the millis() reading won't be correct.  A block of some type in setup would solve that problem and leave my loop intact.  I appreciate the help.

Arrch


what function could I use to stall it there?   That would be the optimal place.


Code: [Select]
while (switch is not pressed) { }

Go Up