Turning on/off continuous sweep servo

You could have reduced your typing and increased clarity if this

const int output1Pin = 9; // Eye Servo Pin

was

const int eyeServoPin = 9;

Are you saying that the tailServo is not moving?

Have you checked to see what values the variable tailPos has?

...R

UPDATE:

DracoAnimatronica:
I have been having some slight difficulty suddenly with my program. For some reason the servos aren't moving; well the one that I have set up with code that is. Can someone help me with figuring out why its not moving?

I figured out how to fix this by moving the connection of the LED to pin 11.

DracoAnimatronica:
Also could use some help with getting the button to tell the servo to go to a preset location if its in the off position?

I still need help with this though.

Thanks.

DracoAnimatronica:
I still need help with this though.

Post your latest code and explain exactly what it does and what you want it to do.

...R

Robin2:
Post your latest code and explain exactly what it does and what you want it to do.

...R

Here's what the program currently does.

  1. The on board LED is set to blink continuously. (I may take this off at a later time since it doesn't apply to my final objective)
  2. The button is set to turn on and off the LED connected to pin 11. (I am using this as a temporary checking device for the button)
  3. The tail servo is set up for continuous sweep.
  4. The eye servo does not have any coding set up for it yet.

Here's what I want the program to do:

  1. The button is to act as a way to activate and reset the two servos (mainly to act as awake and sleep function for my dragon project)
  2. The tail servo is to activate the sweep program when the button is activated and then when the button is deactivated, return to a preset location.
  3. The eye servo is to activate a blink program (similar to the blinking of an eye) when the button is activated and then when the button is deactivated, return to a preset location.

I am having trouble figuring out how to get the button to work with the currently coded servo (I'll use the same code set for connecting the other servo to the button as well).

Here's my current code (please note that most of the code is copied sections of the "Several Things at the Same Time" program):

// Libraries
#include <Servo.h>

//======================

// Constants

 //Pins
  const int actPin = 7; //Activation Button
  const int onBoardLedPin = 13; //On Board LED
  const int TestPin = 11; //Test LED
  const int output1Pin = 9; // Eye Servo Pin
  const int output2Pin = 6; // Tail Servo Pin
  
 //Degrees for Eye servo
  const int maxEDeg = 90;
  const int minEDeg = 1;

 //Degrees for Tail servo
  const byte maxTDeg = 160;
  const byte minTDeg = 0;

 //Item Intervals & Durations
  const int onBoardLedInterval = 500; // number of millisecs between blinks
  const int buttonInterval = 300; // number of millisecs between button readings
  const int blinkDuration = 500; // number of millisecs that Led is on


//======================

// Variables

 //Call out servo names
  Servo eyeservo, tailservo;

 //Servo Starting positions 
  int eyePos = 179;
  int tailPos = 1;

  int servoSlowInterval = 10; // millisecs between servo moves
  int servoFastInterval = 10;
  int servoInterval = servoSlowInterval; // initial millisecs between servo moves
  int servoDegrees = 2;       // amount servo moves at each step 
                              // will be changed to negative value for movement in the other direction

 //Button variable (will be used to tell the servos to run the sleep program)
     //NOTE: NOT IMPLIMENTED YET IN PROGRAM!
  int push = 0;

 //Item States

 byte onBoardLedState = LOW; // used to record whether the LED is on or off
 byte buttonLed_State = LOW; //used to record whether the button is pushed or not

 //Millis()
  unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
  unsigned long previousOnBoardLedMillis = 0;   // will store last time the LED was updated
  unsigned long previousButtonMillis = 0; // time when button press last checked
  unsigned long previousEServoMillis = 0; // the time when the Eye servo was last moved
  unsigned long previousTServoMillis = 0; // the time when the Tail servo was last moved

//======================

void setup()
{
  
 Serial.begin(9600);
 Serial.println("Starting DragonModes.ino");  // so we know what sketch is running
  
 //Set up OUTPUT Pins
  pinMode(onBoardLedPin, OUTPUT);
  pinMode(TestPin, OUTPUT);
  
 //Set up Button INPUT Pin
  pinMode(actPin, INPUT_PULLUP);

 //Set up Servos
  eyeservo.write(eyePos);
  eyeservo.attach(output1Pin);
  tailservo.write(tailPos);
  tailservo.attach(output2Pin);

}

//=======================

void loop()
{
  //Millis()
  currentMillis = millis();
  
 //Call out functions
  checkButton();
  Power();
  setLED();
  servoTailSweep();
//  servoEyeBlink();
  
}

//=======================

void checkButton() 
{ //check the state of the button
 
 if (millis() - previousButtonMillis >= buttonInterval)
 {
  if (digitalRead(actPin) == LOW)
  {
   buttonLed_State = ! buttonLed_State;
   previousButtonMillis += buttonInterval;
  }
 }
  //Add program for push variable?
}

//=======================

void Power() 
{ //Turn on Programs

digitalWrite(onBoardLedPin, onBoardLedState); //On board light
digitalWrite(TestPin, buttonLed_State); //Test LED
//Tail Servo (to be written)
//Eye Servo (to be written)

}

//=======================
void setLED()
{ //Set On Board LED state to blink

  if (onBoardLedState == LOW)
  {
    if (currentMillis - previousOnBoardLedMillis >= onBoardLedInterval) {
       onBoardLedState = HIGH;
       previousOnBoardLedMillis += onBoardLedInterval;
    }
  }
  else
 {
  
    if (currentMillis - previousOnBoardLedMillis >= blinkDuration) {
       onBoardLedState = LOW;
       previousOnBoardLedMillis += blinkDuration;
    } 
  }
}

//=======================

void servoTailSweep()
{  //Start the Tail Sweep servo  
  //Need to figure out how to connect this to the button.
 if (currentMillis - previousTServoMillis >= servoInterval)
 {
    previousTServoMillis += servoInterval;
    tailPos = tailPos + servoDegrees;
    
  if (tailPos <= minTDeg)
  {
   if (servoInterval == servoSlowInterval)
   {
       servoInterval = servoFastInterval;
   }
    else
    {
        servoInterval = servoSlowInterval;
    }
  }

  if ((tailPos >= maxTDeg) || (tailPos <= minTDeg))  
  {  
      servoDegrees = - servoDegrees; 
      tailPos = tailPos + servoDegrees; 
  }
    tailservo.write(tailPos);
 }
}

//===============================

//void servoEyeBlink() 
//{ //Start the Eye Blink servo

  //I haven't written the program for this yet

//}

//=======================

when the button is deactivated,

What do you mean by deactivated ? Do you mean that the button is released or that the button becomes pressed for a second time ? Either way is possible.

It seems to me that you need a variable to keep track of whether the dragon (?) is sleeping or awake and the state of the variable would be changed whenever you press the button. Let's call it dragonState and it will have the value 'S' when sleeping and 'W' when awake.

Then your tail function could then be extended like this

void servoTailSweep()
{  //Start the Tail Sweep servo 
   if (dragonState == 'S') {
      return; // i.e. do nothing
   }
   if (currentMillis - previousTServoMillis >= servoInterval)
      {
       previousTServoMillis += servoInterval;

Does that give you some idea of the direction to go?

...R

UKHeliBob:
What do you mean by deactivated ? Do you mean that the button is released or that the button becomes pressed for a second time ? Either way is possible.

The button is already running the LED the way I would like to run the servos.

Robin2:
It seems to me that you need a variable to keep track of whether the dragon (?) is sleeping or awake and the state of the variable would be changed whenever you press the button. Let's call it dragonState and it will have the value 'S' when sleeping and 'W' when awake.

Then your tail function could then be extended like this

void servoTailSweep()

{  //Start the Tail Sweep servo
  if (dragonState == 'S') {
      return; // i.e. do nothing
  }
  if (currentMillis - previousTServoMillis >= servoInterval)
      {
      previousTServoMillis += servoInterval;




Does that give you some idea of the direction to go?

...R

:smiley: Thanks. That really helped with getting the code to do what I wanted. Now all I have left is to get the eye servo running.

I think I have to play around with what pin the eye servo is plugged into. I copied the code for the tail servo into the eye servo section but its not moving. :stuck_out_tongue:

I'll post more in here if I have any other questions and once I have the final code.

Do I need to have a set of this code for each of the servos or can I run both on the same set?

  int servoSlowInterval = 10; // millisecs between servo moves
  int servoFastInterval = 10;
  int servoInterval = servoSlowInterval; // initial millisecs between servo moves
  int servoDegrees = 2;       // amount servo moves at each step 
                              // will be changed to negative value for movement in the other direction

If the values in the variables are appropriate for both servos and if they won't change then they can be shared. I think it would be better to have separate variables for each servo if the values have to change - unless you deliberately want to have the same values for both servos - for example the servoInterval and servoDegrees.

...R