Pages: [1]   Go Down
Author Topic: Starting a loop using a switch  (Read 442 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm looking for a loop to only begin once the user has activated a switch telling it to do so, a start button if you will.
How would one go about writing this in to the code?
Logged

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

I'm currently looking to do the same thing, I'm assuming you don't want the loop running for power reasons otherwise you could just sit in a while loop waiting for a certain input.  If this is the case I would start by looking here, this is the approach I'm probably going to take.

http://forum.arduino.cc/index.php?topic=132947.0
Logged

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

I'm not too concerned about power consumption, I just want the program to run only while the switch is turned on. So a while loop is the way to go about this, how would I write the switch into my code?
Logged

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

Well one thing to consider is simply making the switch a power switch for the Arduino so that your loop function will start to run on power on.  This won't work if you need to have the Arduino on all the time to save variables, light LEDS, or if you're not checking for the switch at the start of your code, just thought I'd mention it.  Using a while loop would look something like this, I'm no expert but I think this would work fine.

Code:
const int switchPin=2;
int switchState = 0;

void setup()
{
  pinMode(switchPin, INPUT);
}

void loop()
{
  switchState = digitalRead(switchPin);
 
  while (switchState == 0){
    switchState = digitalRead(switchPin);
    delay (10);
  }
 
  //Code You wish to run after switch is thrown
}

This would use a SPDT switch with the center pin hooked to the IO and with each side pin attached to VCC and GND respectively. Your main loop should stay inside the while loop checking every 10 msec for the input to change to positive at which point it will move forward with the rest of your main loop, should you switch back to ground the code will be caught by the while loop on its next go around.  I'm sure there may be a better way to handle this but I think it should do the trick.  You can also put the while anywhere throughout your code where you want it to wait for the switch.  Hope this helps.
Logged

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

That helps alot, thank you!
Logged

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1480
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well one thing to consider is simply making the switch a power switch for the Arduino so that your loop function will start to run on power on.  This won't work if you need to have the Arduino on all the time to save variables, light LEDS, or if you're not checking for the switch at the start of your code, just thought I'd mention it.  Using a while loop would look something like this, I'm no expert but I think this would work fine.

Code:
const int switchPin=2;
int switchState = 0;

void setup()
{
  pinMode(switchPin, INPUT);
}

void loop()
{
  switchState = digitalRead(switchPin);
 
  while (switchState == 0){
    switchState = digitalRead(switchPin);
    delay (10);
  }
 
  //Code You wish to run after switch is thrown
}

Condenses to:
Code:
const int switchPin=2;
void setup()
{
  pinMode(switchPin, INPUT);
}
void loop()

  while (digitalRead(switchPin) == 0);
  //Code You wish to run after switch is thrown
}
Quote
This would use a SPDT switch with the center pin hooked to the IO and with each side pin attached to VCC and GND respectively.
Or a normally open press button wired between Vcc and the input pin with a 10k resistor pulling the input to ground.
Logged

Pages: [1]   Go Up
Jump to: