It keeps doing it!!!!!!

I’m trying to make a program that will open a small door in the morning (using a photocell and a servo) and turn on/off a LED strip. At night the LED strip turns ON then, in the morning I want the servo to turn about 30 degrees one way, wait, then turn 30 degrees in the opposite direction along with the LED strip turning OFF. To my knowledge everything is programed except the servo, it keeps moving in a loop? What do I do?

My code-

Chicken_Coop_INTRO.ino (1.81 KB)

/*

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * Servo attached to pin 9
 * Servo attached to ground
 * Servo attached to +5V
 
 created 2005
 modified 23 Mar 2015
 by Nolan Wethington
 
 */

///////////////////////////////////////////////////////////////////////////////////////

// constants won't change. They're used here to
// set pin numbers:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 
int pos = 0;    // variable to store the servo position 

///////////////////////////////////////////////////////////////////////////////////////

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  
}

///////////////////////////////////////////////////////////////////////////////////////

void loop () {
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH); 
  }
  
///////////////////////////////////////////////////////////////////////////////////////  
  
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

    myservo.write(100);
    delay(2000);
    myservo.write(200);  
    delay(2000);
    myservo.write(100);
    
 }
}

That's exactly what you're telling it to do.

Every iteration of loop(), if the buttonState is not low, it will turn off the LED pin and move the servos.

You need to record that you've already turned off the LED and done the servo thing, and check that before doing that (and reset it when you turn the strip back on)

Yea if you can't tell I'm new to this...
How would I do that?

I guess you know when the conditions happen when you want to turn the servo? And have you heart of variables? Just save it when you did it and check it the next time.

Let me help :wink:

/*

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * Servo attached to pin 9
 * Servo attached to ground
 * Servo attached to +5V
 
 created 2005
 modified 23 Mar 2015
 by Nolan Wethington
 
 */

///////////////////////////////////////////////////////////////////////////////////////

// constants won't change. They're used here to
// set pin numbers:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
bool didStuff = 0; //save if you did stuf

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 
int pos = 0;    // variable to store the servo position 

///////////////////////////////////////////////////////////////////////////////////////

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  
}

///////////////////////////////////////////////////////////////////////////////////////

void loop () {
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH); 
    didStuff = false;
  }
  
///////////////////////////////////////////////////////////////////////////////////////  
  
  else if(didStuff){
    // turn LED off:
    digitalWrite(ledPin, LOW);

    myservo.write(100);
    delay(2000);
    myservo.write(200);  
    delay(2000);
    myservo.write(100);
    didStuff = true;
    
 }
}

The code makes since, but it still doesn't work. A soon as day comes ( the "else" part of the code) the light goes off and the servo does what its supposed to do, but then it keeps doing it. I only what it to happen once.

The loop( ) function will run continuously, hundreds of times per second, until the Arduino is powered off, or reset.

And every one of those millions of times that loop( ) runs, if the button is not currently pressed, then it will attempt to do what you have specified in the else clause of that if statement.

Yea but how could i do what I need to do?

  • pushbutton attached to pin 2 from +5V
  • 10K resistor attached to pin 2 from ground

This is not a great idea. If you ever program that pin as an output, you'll probably destroy the output driver. MUCH better to put the resistor between the pin and +5V, and the switch between the pin and GND. At least the pin driver won't be vulnerable if the programming is wrong.

Regards,
Ray L.

nolanwa:
Yea but how could i do what I need to do?

I'm sure there are better ways, but I would do it like this:

boolean notYetRun = true

void loop 
  if (notYetRun == true) {

  // run your routines here

  notYetRun = false;
 }
}

Loop will keep running, but the program will skip over all the stuff it does the first time.

Chicken_Coop_INTRO.ino: In function 'void loop()':
Chicken_Coop_INTRO.ino:71:6: error: 'notYetRun' was not declared in this scope
Error compiling.

????????????????

Missing a ; here

boolean notYetRun = true; //<<<<<<<<<<<<<<<<<<<<

A W E S O M E !
That part works! But now I have another problem. How would I reset that so it will be run the next morning. Now it will indeed work the first morning but then the next morning that part of the code is ignored…

Use the photocell you mentioned. When it goes dark (night), reset 'notYetRun to true. You will most likely need another parameter to ignore other light sources besides natural daylight. You could inhibit any action for the next 23 hours or so since ‘notYetRun’ was set to false. Or you could ignore any spurious light by setting ‘notYetRun’ to true only after the photocell goes light for for a length of time that would only happen when it becomes daylight. Or perhaps you could place the photocell in a darkened (black) tube oriented to look at the sky. - Scotty

nolanwa:
I'm trying to make a program that will open a small door in the morning (using a photocell and a servo) and turn on/off a LED strip. At night the LED strip turns ON then, in the morning I want the servo to turn about 30 degrees one way, wait, then turn 30 degrees in the opposite direction along with the LED strip turning OFF. To my knowledge everything is programed except the servo, it keeps moving in a loop? What do I do?

My code-

Read your PM

I would like to thank all of you for your help. It works perfectly and i understand most of it.

Final code-

Chicken_Coop_INTRO.ino (2.05 KB)

/*

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * Servo attached to pin 9
 * Servo attached to ground
 * Servo attached to +5V
 
 created 2005
 modified 23 Mar 2015
 by Nolan Wethington
 
 */

///////////////////////////////////////////////////////////////////////////////////////

// constants won't change. They're used here to
// set pin numbers:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 

 
int pos = 0;    // variable to store the servo position 

boolean notYetRun = true;    // create a boolean algebra state

///////////////////////////////////////////////////////////////////////////////////////

void setup() {

  pinMode(ledPin, OUTPUT);    // initialize the LED pin as an output
  pinMode(buttonPin, INPUT);    // initialize the pushbutton pin as an input
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  
}

///////////////////////////////////////////////////////////////////////////////////////

void loop () {
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed...
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH); 

    notYetRun = true;    // says that this code has not run

  }
  
///////////////////////////////////////////////////////////////////////////////////////  
  
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

 if (notYetRun == true) {    // if it hasent run once already, run
    myservo.write(100);    // rotates the servo 100 degres, then -100
    delay(2000);
    myservo.write(200);  
    delay(2000);
    myservo.write(100);
    
    notYetRun = false;    // says the code has run once
    
    } 
  }
}