Go Down

Topic: Looping only once when a button is pressed. (Read 610 times) previous topic - next topic

Sblue004

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.

lloyddean

#16
Feb 11, 2013, 04:10 am Last Edit: Feb 11, 2013, 05:28 am by lloyddean Reason: 1
Lets stop guessing and actually track what's going on, run this

Code: [Select]

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);
}

HazardsMind

Code: [Select]
#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}
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

UKHeliBob


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