Looping only once when a button is pressed.

Hello!

I'm fairly new to the idea of micro-controlers and writing programing for anything other than the ti-83. Here's my situation:
The idea is that when a momentary button is pressed, a led will turn on and a servo will sweep 90 degrees forward and back once. I know how to get the led on and the servo moving, but the servo just repeats it's loop. I know I can do this easily using PLC, but this C++ is killing me. By the by, I'm using the sweep example to get the servo moving. Any help would be very much appreciated! Thanks!

What have you tried already, post your code so far.

Have a boolean variable that is false at the start of the program and is set to true when the button is pressed. Before running the code that moves the servo check the variable. If it is true then don't move the servo.

set moved to false;

beginning of loop
  if button is pressed and moved is false
    turn on the LED
    move the servo
    set moved to true
  end of if
end of loop

That's not your code. That's a pseudo description of what you think your code does.

If you want help debugging then you need to post your full code for people to look over.

Some confusion maybe ?

That is my pseudo code of what I understand the OP wants to do. Once he has tried his hand at turning it into real code then we can give more help. I deliberately did not post any real code to encourage him to try for himself and learn along the way.

Thank you for taking a look. It's really frustrating when what you want is simple, and the method of obtaining it is anything but intuitive. Anyway, here's what my un-working sketch looks like at the moment.
It is not registering that the boolean value has changed. I'm almost at the point of giving up and doing it mechanically, but I really want to know how to do this with a controller...

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

for(pos = 0; pos < 180; pos += 5)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree

The code does neither of the things mentioned in the comment, so why bother with the comment?

I'm building the sketch in pieces. In the code I've posted I'm just trying to get the boolean variable to work. So when the arduino is turned on, the servo will sweep once and stop.

Well what you can do is, take everything out of the loop() and make a separate function. Then to make it just run once, call the function in setup().

In the end, though, there will be two buttons. One that turns on a light and sweeps a servo back and forth once(my current problem), and the other kills the led and resets the boolean allowing the sequence to be run again if the first button is pressed. So really, I suppose, I just need to know why my boolean isn't working.
Thanks again!

Look where you change the value of your boolean.

How many times does its value get inverted?
Is that number odd or even?

It looks like once. So, theoretically, the loop should run once, change the value of "counter" and stop, right? But this isn't true and I know not why.

It's in a loop, so "once" seems unlikely.
Look again, carefully.

I've stared at the code until the words lost their meaning. Not that I really understood them in the first place. I'm stumped, I have no clue why this isn't working.

Temporarily getting rid of some of your code may help you see the wood for the trees

void loop() 
{
  if(counter == false) 
  {
    for(pos = 0; pos < 180; pos += 5)  // goes from 0 degrees to 180 degrees 
    {                                  // in steps of 1 degree 
    } 
    for(pos = 180; pos>=1; pos-=5)     // goes from 180 degrees to 0 degrees 
    {                                
      counter = !counter;     
    } 
  }
}

Now tell me how many times counter gets changed each time through loop() ?

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.

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

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

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.

Believe me, I know how you feel (BTDTGTTS)

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.