Help With DO/While Statement

Hi All.

I am trying to learn how to use the do statement.

But I am having no luck. To tell you the truth I am not even sure I should be using that statement to do what I want to do.

What I am looking to is this. I have a Basic little sketch with an RGB LED and a servo. That part works.

I want to use a push button to start and stop the sketch. Can some one please take a look at the attached code and tell me how far off I am. And remember I am new to this so be nice lol.

Thanks

#include <Servo.h>
Servo servoMain; // Define our Servo
int ledRed = 2;
int ledGren =3;
int ledBlu = 4;
int ledTest = 5;
int switchPin = 6;

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state


void setup()
{
  servoMain.attach(8); 
  pinMode(ledRed, OUTPUT);
  pinMode(ledGren, OUTPUT);
  pinMode(ledBlu, OUTPUT);
  pinMode(ledTest, OUTPUT);
  pinMode(switchPin, INPUT);    
  digitalWrite(switchPin, HIGH);
  
}


void loop(){

  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                               // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin); 

  do
  {
    delay(50);          
  
  } 
  while (val2 == HIGH);

 
  // First Movment

  digitalWrite(ledRed,HIGH);   // Turn On Eyes White
  digitalWrite(ledGren,HIGH);   
  digitalWrite(ledBlu,HIGH); 

  servoMain.write(90);  
  delay(3000);
  digitalWrite(ledGren,LOW);   // Trun Eyes Red
  digitalWrite(ledBlu,LOW); 

  servoMain.write(0);   
  delay(3000);          
  digitalWrite(ledGren,HIGH);  // Turn On Eyes White 
  digitalWrite(ledBlu,HIGH);

  servoMain.write(90);  
  delay(3000);          


  digitalWrite(ledGren,LOW);   // Trun Eyes Red
  digitalWrite(ledBlu,LOW); 
  servoMain.write(180); 
  delay(2000);          
  digitalWrite(ledGren,LOW);   // Trun Eyes Red
  digitalWrite(ledBlu,LOW); 

  servoMain.write(0);   
  delay(3000);    

}

Doe this look like what you want

    if ( HIGH == digitalRead(switchPin) )
    {
        delay(50);
        while ( HIGH == digitalRead(switchPin) )
        {
            delay(50);
        }
    }

lloyddean:
Doe this look like what you want

    if ( HIGH == digitalRead(switchPin) )

{
        delay(50);
        while ( HIGH == digitalRead(switchPin) )
        {
            delay(50);
        }
    }

It is stating to do what I want.

It will start when I push the button, but its not looping and how do I get it to stop when push the button a second time.

And sorry, Thank you very much for your help. :slight_smile:

Describe your button.

Check out http://arduino.cc/en/Tutorial/ButtonStateChange

You don't need a do - while loop for this, you can if you want but it is not necessary, just use an IF/ELSE statement. Also look into the Blink without delay example sketch, it will help you a lot. For your button, try the debounce sketch example.

It looks to me that you are using the while loop to see if the button is pressed or not, that is fine. But the setup is improper.

Edit: look into button latching. It is very simple.

Structure it like this.

boolean latch = false; //global variable
void setup {.....}

void loop() {
/* debounce code here*/
Edit: Took out faulty latch code
}

void Run_code() {
/* your code here*/
}

lloyddean:
Describe your button.

Its Black and small , lol

No it is momentary push button normally open. with one pin going to Gnd and one going to pin 6.

It starts when I push it. runs until the last line of the void loop than stops. If I push it again after its stops
it reset and goes back to the start.

HazardsMind:
You don't need a do - while loop for this, you can if you want but it is not necessary, just use an IF/ELSE statement.

Thanks for the input.

I got it to work before with the IF/Else, I was just tying to learn something new.
I should probably take a step back tho.

I think your right and I should use the IF/Else and make sure I get that mastered before moving on.

isn't learning fun!?!? :slight_smile:

Gnd and one going to pin 6.

Is pin 6 pulled high?

Edit:

do
{
delay(50);

}
while (val2 == HIGH);

Do-while loops only loop when the condition is true, so if your button goes to ground when pressed, the loop stops because it is looking for val2 to be high.

HazardsMind:

Gnd and one going to pin 6.

Is pin 6 pulled high?

yes.

HazardsMind:
Structure it like this.

boolean latch = false; //global variable
void setup {.....}

void loop() {
/* debounce code here*/
if(button == HIGH)
{
if(!latch){
Run_code();
latch = true; //this resets the latch
}
}
else latch = false;
}

void Run_code() {
/* your code here*/
}

BTW, this part was very helpful. This is what I need to learn. What I mean is that I need to learn how the code it being read by the processor. ie what order.

let me know if I have this right, please.

The way I have the code now, after set up it runs the Void loop. once it gets to the end of the Void loop it goes back to the star of the void loosp and is waiting for the button to be pushed again. Am I close?

Thank

The way I have the code now, after set up it runs the Void loop. once it gets to the end of the Void loop it goes back to the star of the void loosp and is waiting for the button to be pushed again. Am I close?

Exactly what it is doing. Your code will run, every time your button is pressed.

HazardsMind:
Exactly what it is doing. Your code will run, every time your button is pressed.

Well at lest I feel like I am starting to get a grasp on it, I know I have a log way to go. This is not like the Cisco stuff I am used to seeing.

I did a search on the latching but I could not find any thing. Is there any way I could persuade you into filling in the blank on what you sent me. If I see it once it can usually understand it better.

Please do get me wrong, you have help me a lot and I would totally under stand if you told me to pound sand. I am not tying to get anyone to do it for me as much as I would like to see how its done the right way.

Thanks

ok, I didn't realize that I gave you the wrong latch code before, that one doesn't work, this new one does.
NOTE: It may not work correctly if you dont debounce the button.

boolean latch = false; // GLOBAL VARIABLE sets latch as false to initialize it.

if(button == HIGH) //looks at button state
{
latch = !latch; //changes latch state each time button is pressed.
if(latch){ // if latch is true
digitalWrite(13,HIGH); // LED on if latch is true
}
else { digitalWrite(13,LOW);} // LED off if latch is false

Well even with all the help I was, not able to get it to work.

I decided to go back and do it with the IF/ELSE statement. I think once I mater that I will move on.

I got it work with the Code posted, It starts when I push the button, but I have to hold it down for it to stop.

can someone point out my Mistake please and Thank you.

#include <Servo.h>

/*
 *  
 */
Servo servoMain;
int ledRed = 2;
int ledGren =3;
int ledBlu = 4;
int ledTest = 5;
int switchPin = 6;

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int Mode = 0;                   // What mode is it in?

void setup() {
  servoMain.attach(8);
  pinMode(switchPin, INPUT);       // Set the switch pin as input
  digitalWrite(switchPin, HIGH);  // set internal pull-up resistor
  pinMode(ledRed, OUTPUT);
  pinMode(ledGren, OUTPUT);
  pinMode(ledBlu, OUTPUT);
  Serial.begin(9600);

  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){

  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         
  val2 = digitalRead(switchPin);     // read the input again to check for bounces

  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (Mode == 0) {          // if its off
          Mode = 1;               // turn it on!
        } 
        else {
          if (Mode == 1) {        // if its on
            Mode = 0;             // Trun it off.
          } 


        }
      }
    }
    buttonState = val;                 // save the new state in our variable
    Serial.println(val);


    if (Mode == 0) { // all-off
      digitalWrite(ledRed, LOW);
      digitalWrite(ledGren, LOW);
      digitalWrite(ledBlu, LOW);

    }

    //  Movment
    if (Mode == 1){
      digitalWrite(ledRed,HIGH);   // Turn On Eyes White
      digitalWrite(ledGren,HIGH);   
      digitalWrite(ledBlu,HIGH); 

      servoMain.write(90);  
      delay(3000);
      digitalWrite(ledGren,LOW);   // Trun Eyes Red
      digitalWrite(ledBlu,LOW); 

      servoMain.write(0);   
      delay(3000);          
      digitalWrite(ledGren,HIGH);  // Turn On Eyes White 
      digitalWrite(ledBlu,HIGH);

      servoMain.write(90);  
      delay(3000);          


      digitalWrite(ledGren,LOW);   // Trun Eyes Red
      digitalWrite(ledBlu,LOW); 
      servoMain.write(180); 
      delay(2000);          
      digitalWrite(ledGren,LOW);   // Trun Eyes Red
      digitalWrite(ledBlu,LOW); 

      servoMain.write(0);   
      delay(3000);    
    }
  }
}

I got it work with the Code posted, It starts when I push the button, but I have to hold it down for it to stop.

You will continue having to hold the switch pressed until all the delay() calls are done, unless you get rid of them. Look at, understand, and embrace the blink without delay example, and banish the delay() function from your repertoire.

PaulS:
You will continue having to hold the switch pressed until all the delay() calls are done, unless you get rid of them. Look at, understand, and embrace the blink without delay example, and banish the delay() function from your repertoire.

I am working on it. So all the delay's down in the void looP() need to go bye, bye?

So all the delay's down in the void looP() need to go bye, bye?

The delay(10) for debouncing can stay. The rest have got to hit the road.

PaulS:

So all the delay's down in the void looP() need to go bye, bye?

The delay(10) for debouncing can stay. The rest have got to hit the road.

I am having a real hard time understanding how to incorporate this in to my code.

Do you know of a better example/tutorial I could look at. also can anyone recommend a good book the explains the code better. The books i have seen so far show you how to do things but not why you do them.

I am having a real hard time understanding how to incorporate this in to my code.

You have a watch, a pad of paper and a pencil. Explain how YOU would turn the lights on and off, and move the servo, every two hours.

The millis() function replaces the watch. Variables replace the paper and pencil. The methodology stays the same.

PaulS:
The millis() function replaces the watch. Variables replace the paper and pencil. The methodology stays the same.

Ok, I found a few good examples of the blink with out delay sketch and I have a good understanding of the millis() function.

What I can't wrap my mind around is how do use it in my code. (I am not this dumb I promise).

I think I am over thinking it. but it's driving me crazy.