Pages: 1 [2]   Go Down
Author Topic: Looping only once when a button is pressed.  (Read 567 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First, allow me to apologize for my ignorance. There is obviously a very simple thing that I'm just not seeing. To me, it looks as if the loop checks the variable and if false, runs a sequence that changes the variable to true at the end. So, one change. But it's clear I'm mistaken.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lets stop guessing and actually track what's going on, run this

Code:
bool flag = false;

void loop()
{
    if ( flag == false )
    {
        for ( int pos = 180; pos >= 1; pos -= 5 )
        {                                
            flag = !flag;
            Serial.print("pos = ");
            Serial.print(pos);
            Serial.print(", flag = ");
            Serial.println(flag);
        }
    }
}

void setup()
{
    Serial.begin(9600);
}
« Last Edit: February 10, 2013, 11:28:57 pm by lloyddean » Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3413
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
int led = 12;
int button =2;
int val = 0;
int pos = 0;    // variable to store the servo position
boolean counter = false;

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
   pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}
 
 
void loop() {
 
 if(counter == false) {
     
  for(pos = 0; pos < 180; pos += 5)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=5)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);     
  }
  counter = !counter; // after the first loop, this will lock it out until arduino is manually reset or if reset by soon to be implemented button.
 }
// **Add button here ** if button is pressed, counter = false, code will loop again.
}

OR...

if(button == HIGH), {
//rotate servo 90 degrees then back to 0
}
else {// do nothing}
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

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

First, allow me to apologize for my ignorance. There is obviously a very simple thing that I'm just not seeing. To me, it looks as if the loop checks the variable and if false, runs a sequence that changes the variable to true at the end. So, one change. But it's clear I'm mistaken.
Believe me, I know how you feel (BTDTGTTS)
Quote
runs a sequence that changes the variable to true at the end.
The problem is that the change to true, or more exactly flipping the value, does not happen at the end of the sequence, but within the for loop that moves the servo, so it happens many times instead of once.  Ironically, if you had set it to true explicitly it would have worked.
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: