Pages: [1] 2   Go Down
Author Topic: How to start a loop with a manual switch  (Read 1546 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have tried the following code with no luck. 
I'm not surprised.
Code:
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:......
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
while (switch is not pressed) { }
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4687
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bob, you like to type?
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4687
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
What is your solution to do what the OP asked ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4687
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With a switch through a pullup (which might have to be explained, but great to know),

while ( !digitalRead( switchPin ));

Without the pullup

while ( digitalRead( switchPin ));

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.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
This programming is for side projects, but my business is cnc machining of oilfield parts.  I just started about 7 years ago at the age of 53 as a complete rookie, but it was something I'd always wanted to do, but had too many other careers going.  I started part time at a casting plant with 5 cnc machines and they would flop you from one machine to another day to day, so I never had time to learn any of the machines in great detail.  They start you out as a 'button pusher'. Load the part, hit the 'go' button, unload the part.  Soon, I could set diameter offsets, etc and gradually learned the G code by watching it go by.  After 8 months, I quit, built a small shop and bought a 15 yr old machine.  I had a steep learning curve, but quickly got to where I could handle daily programming and get the job done.  I hadn't learned about subroutines, and most of my 15 minute part cycles were around 10,000 lines of code.  Improvements in production came from improved fixtures, higher cutting speeds, etc.  Then, just about 6 months ago I finally got time to study subroutines.  Now my programs are maybe 20% as many lines; are easier to modify, etc.  The longhand approach got my machine paid for, then when I had time, the extra learning simplifies the process. 

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!
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4687
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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
  }
}
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1] 2   Go Up
Jump to: