Go Down

### Topic: How to start a loop with a manual switch (Read 3808 times)previous topic - next topic

#### namenerb

##### Jul 17, 2013, 04:31 am
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() {

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

#1
##### Jul 17, 2013, 04:46 am

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:
break;
case 38:......

#### Delta_G

#2
##### Jul 17, 2013, 05:49 am
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?
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

#### namenerb

#3
##### Jul 17, 2013, 05:27 pm
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

#4
##### Jul 17, 2013, 05:31 pm

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

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

#### UKHeliBob

#5
##### Jul 17, 2013, 06:19 pm
Code: [Select]
set running false

start of loop
while running is true  //infinite loop doing nothing
end of while
if buttonPin is HIGH
set running true
code to do whatever you want with the LEDs
end of if
end of loop
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### GoForSmoke

#6
##### Jul 17, 2013, 06:33 pm
Bob, you like to type?
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### UKHeliBob

#7
##### Jul 17, 2013, 07:14 pm
No, but I like to give clear advice.  Some/all of my suggestions may not be the best/fastest/cleverest way to do what is wanted but I aim to push the reader in the right direction.  Often it would be faster to post the actual code but IMHO that is not the way to learn.

Much better to take general ideas, such as 'set running false' and turn it into code to initialise a boolean variable called running with the value of false by entering the code yourself.  The most important thing is to end up with working code that you understand.  It can then be modified/improved to use other techniques if you want to.  It seems to me that some people are here to show off rather than give constructive advice.  Just think of what the response would be if someone suggested using Strings in response to a question....

I will continue to provide advice in the form of pseudo code where I think it appropriate.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### GoForSmoke

#8
##### Jul 17, 2013, 07:40 pm
Strings are bad practice on small environments. Using them instead of char array strings develops reliance on String.

Teaching people to over-code is a good thing now?

I guess you have a different idea of showing off too.

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### UKHeliBob

#9
##### Jul 17, 2013, 08:54 pm

Strings are bad practice on small environments. Using them instead of char array strings develops reliance on String.

I think you made my point for me and for what it is worth, I agree with you, but initial use of Strings fits well with the model of get it working, then improve it.

Quote

Teaching people to over-code is a good thing now?

Over-coding is not a good thing, but coding something that works is.  Get it working, improve it, then use the improved techniques to deal with similar situations in the future.

Quote

I guess you have a different idea of showing off too.

I probably didn't explain what I meant too well, so let me expand on it.  An experienced Arduino programmer will approach a problem in a different way to someone new to it.  They will tend to suggest a solution that they themselves would use when a more stepwise (over-coded) solution would be more appropriate starting point, albeit not one that they would use themselves.

You asked if I like to type ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### GoForSmoke

#10
##### Jul 17, 2013, 11:04 pm
With a switch through a pullup (which might have to be explained, but great to know),

Without the pullup

And I do assume there would be questions.

I graduated HS in 75 but I haven't forgotten the better courses and teachers and how those went.
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### Henry_Best

#11
##### Jul 17, 2013, 11:55 pm

Code: [Select]
set running false

start of loop
while running is true  //infinite loop doing nothing
end of while
[/quote]
How is he going to escape that loop?

#### namenerb

#12
##### Jul 18, 2013, 12:34 am
Hey guys, I appreciate all the discussion.  It gives me things to study and discover.  Bob, I really appreciate the advice and welcome the longer, but more conducive to learning, code approach.
Side note:

I expect this process to be similar and I love the learning process.   The frustrating times are when I can't find what I am after in my C programming book and a couple hours online fails to yield any information.  This thread gives me a few directions to go and I appreciate the time y'all spend giving pointers.  I also enjoy the different views on how to approach the solutions.

This first project is for a partner in the film business with a Special FX company.  He needs a sequencer for pyro with 48 channels that have a variable gap between firing as well as a variable duration that each is on.  (Air mortars take a few seconds to unload, while squibs/bullet hits are instant) I plan to end up with a touch screen that sets up his variable library, as well as a sound library so that he can demonstrate the firing sequences to the director to make sure it is what they want.

Next project is an automated parts fixture for my cnc machine.  I use 12 ft. flat bar to produce parts about 6" long.  I have started a design that will feed in the full bar and cut one part at a time.  Ultimate  goal is a 'lights out' system that might run 16 hours per day with us there for 8 hours.
Thanks all, I appreciate your help!

#### GoForSmoke

#13
##### Jul 18, 2013, 05:46 am
I was writing NC and CNC code back in 79-83 before I went on to programming full time. I wrote shop programs to produce tapes for an NC Wiedemann turret press, a Spindle Wizard CNC mill and a CNC FANUC controlled lathe. Like you I had no degree at it, I learned by doing.

Yes Bob, I got the sense of those two cases backwards. We watch pullup switches for LOW and non-pullup switches for HIGH. But my loops when done right exit on switch change.

namenerb; we can connect a switch two different ways.

One way you connect the switch pin to the switch and a resistor then to 5V. If the switch is ON then the switch pin goes HIGH. The code to set the pin up in setup() is pinMode(switchPin, INPUT) and code to read the pin is X = digitalRead(switchPin).

The other way you connect the pin to the switch and the switch to ground. Or, for testing I just stick a jumper in the hole for that pin and tap the other end of the jumper to the grounded USB receptacle. If the "switch" is ON, the pin goes LOW. The code in setup() is pinMode(switchPin, INPUT_PULLUP) in IDE 1.0 and since (let me know if you run an older IDE) and the code to read the pin is the same.
Using the pullup to ground through switch saves you a resistor and guarantees low current use, the pullup resistor built into the chip is 20k or more.. it's safe.

Here's a working test. You can use it to test your switch for bounce.
Code: [Select]

// note that this code does no switch debouncing

byte  switchPin = 7; // I'm using pin 7 to connect my jumper wire "switch"
byte lastSwitchState;

void setup()
{
Serial.begin( 9600 );
Serial.println( "press the switch" );

pinMode( switchPin, INPUT_PULLUP );
while ( digitalRead( switchPin ) == HIGH ); // will loop until the switch grounds the pin
lastSwitchState = 1; // technical lie to trigger the inital print in loop()
}

// switch is pressed, the switch pin is LOW, go ahead and run
// IF your switch does bounce then you will see many lines printed for one ON or OFF
// my "ground the jumper wire switch" bounces no matter how careful I am.

void loop()
{
if ( digitalRead( switchPin ) != lastSwitchState )
{
Serial.print( "Switch is " );
if ( lastSwitchState == 0 ) // in shorthand we write if ( !lastSwitchState )
{
Serial.println( "ON" );
}
else
{
Serial.println( "OFF" );
}
lastSwitchState = !lastSwitchState; // ! means NOT, changes 0 to 1, 1 to 0
}
}

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### UKHeliBob

#14
##### Jul 18, 2013, 08:31 am

Code: [Select]
set running false

start of loop
while running is true  //infinite loop doing nothing
end of while
[/quote]
How is he going to escape that loop?

The way I read what he wants
Quote
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).
he doesn't want to escape the loop.

However, having read the original posting again what may be required is that the LED activity should repeat during the infinite loop.  If so, then my suggested method needs to be changed to put the LED code in the infinite loop or to use the loop() function itself but still with the running flag set by the switch.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up

Please enter a valid email to subscribe