Turning on/off continuous sweep servo

Hello. I have been working on a project to create an animatronic dragon, but I have been having trouble with some of the coding.

My goal is to make a button act as a way to wake up the dragon and then, when the button is pressed again, make the dragon go to sleep. The button is to control two servos; one for the tail and the other for the eyes. The tail servo I want to have a continuous sweep when the dragon is turned on and then return to a neutral position when turned off. I have gotten the sweep working but I can’t figure out how to make it work with an if statement.

Here is what I currently have and I attached a picture of my sketch that I designed using the website circuits.io:

#include <Servo.h>

Servo eyeservo, tailservo;  // create servo object to control a servo 
 // a maximum of eight servo objects can be created 

int pos = 90;    // variable to store the tail servo position 
const int maxDeg = 160;
const int minDeg = 45;

const int actPin = 3; //Activation Button

const int output1Pin = 9; // Eye Servo
const int output2Pin = 6; // Tail Servo
int outPin = 13;

int state = HIGH;
int reading;
int previous = LOW;

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup() 
{ 

eyeservo.attach(output1Pin);
tailservo.attach(output2Pin);

pinMode(actPin, INPUT);
pinMode(output1Pin, OUTPUT);
pinMode(output2Pin, OUTPUT);
pinMode(outPin, OUTPUT);
  
} 

void loop() 
{ 
  
//Active Button

  reading = digitalRead(actPin);

 if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(output1Pin, state);
  digitalWrite(output2Pin, state);
  digitalWrite(outPin, state);
  
  previous = reading;
  
  
//Tail Servo
  
  if(state = HIGH){
    for(pos = 0; pos < 160; pos += 1)
  {
    tailservo.write(pos);
    delay(15);
  }
    for(pos = 160; pos>=1; pos-=1)
  {
    tailservo.write(pos);
    delay(15);
   }}
    
   else
   {
   delay(15);
  }


//Eye Servo

//Code not written yet


}

Variables associated with millis() such as your "time" should be unsigned long

I would only read the actPin when the debounce interval has elapsed.

Have you an external pull-down resistor on actPin? If not you will probably get spurious values. It is easier to use pinMode(actPin, INPUT_PULLUP); and wire the switch to pull the pin to GND when pressed. Then a LOW would signify that the the switch has been pressed.

I don't know what you mean by "but I can't figure out how to make it work with an if statement." Please describe how the existing program behaves.

...R

Robin2: I don't know what you mean by "but I can't figure out how to make it work with an if statement." Please describe how the existing program behaves.

The servo does not react at all to the button being pressed. It just runs a continuous sweep.

Here is the link to the project that I am using via the web: https://circuits.io/circuits/2452438-dragon-testing-active-modes

The first thing that I would do is to dump the for loop that sweeps the servo because it does 320 servo writes with a delay() of 15 milliseconds between each of them before it gets round to reading the button input again. If the button is not pressed at that exact moment then the input is not recognised. Instead, use millis() for timing. Move the servo, save the time of the move. Next time through loop() test the elapsed time and if it is time to move the servo then do it. If not, read the input and act on it.

By the way, you need to to fix this problem or it will never work anyway.

  if (state = HIGH) {

You may find some useful ideas in Planning and Implementing a Program. It includes a servo function.

...R

Thanks. I'll look into it. I think I will need to reorder my coding a bit to so I can see what goes with what.

Is there another way to create a continuous sweep besides using "for" statements?

DracoAnimatronica: Is there another way to create a continuous sweep besides using "for" statements?

See the code in the link I gave you.

...R

Robin2: See the code in the link I gave you.

...R

The way you have the programming set up with functions does not seem to work with the system that I am using.

When I put in this:

void loop()
{
//Set up Functions
  checkButtons();
  setTServoPosition();
  moveTServo();
  setEServoPosition();
  moveEServo();
}

It comes back saying that they are not declared in this scope. Can you please help me with that?

You will need to post your complete program.

UKHeliBob: You will need to post your complete program.

The most that there is currently that can run without failing is in the first post.

DracoAnimatronica: It comes back saying that they are not declared in this scope. Can you please help me with that?

As @UKHelibob has said, you need to post the complete program using my suggested approach even if (or especially because) it does not work.

Also post the exact error message.

Perhaps you have not properly created the functions you are trying to call?

...R

Robin2,

I put the values in the same section that you did in your forum. You showed them in the first part of the void loop() section but nothing listed before that section. Am I missing something that you didn't show in your forum?

Here is the code that I was looking at on your forum:

void loop() {
   checkButtons();
   setFlashPeriod();
   flashLedA();
   flashLedB();
   
   askForUserInput();
   getUserResponse();
   
   readPotentiometer();
   setServoPosition();
   moveServo();
}

I can't help until you post a complete program so that I can try it on my PC.

...R

Here. This is all you really need to get what I am encountering. The program doesn’t even get that far into the actual code hence the reason that I wasn’t posting more than that section.

Note: This code looks the same as the original because it is, failures happened to only the first section of the void loop().

#include <Servo.h>

Servo eyeservo, tailservo;  // create servo object to control a servo 
 // a maximum of eight servo objects can be created 

int pos = 90;    // variable to store the tail servo position 
const int maxDeg = 160;
const int minDeg = 45;

const int actPin = 3; //Activation Button

const int output1Pin = 9; // Eye Servo
const int output2Pin = 6; // Tail Servo
int outPin = 13;

int state = HIGH;
int reading;
int previous = LOW;

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup() 
{ 

eyeservo.attach(output1Pin);
tailservo.attach(output2Pin);

pinMode(actPin, INPUT);
pinMode(output1Pin, OUTPUT);
pinMode(output2Pin, OUTPUT);
pinMode(outPin, OUTPUT);
  
} 

void loop() 
{ 
  //Set up Functions
  checkButtons();
  setTServoPosition();
  moveTServo();
  setEServoPosition();
  moveEServo();
}
//Rest of code not important to answer problem.

I suspect the OP missed the point and didn't read the whole thing. It looks like he just copied and pasted your loop code without realizing that he has to now write each of those functions.

OP: when you know what the problem is and how to fix it then you can tell us what sections are important. Of course then there would be no point in this whole exercise. If you want our help then show us what we ask for.

Delta_G:
I suspect the OP missed the point and didn’t read the whole thing. It looks like he just copied and pasted your loop code without realizing that he has to now write each of those functions.

OP: when you know what the problem is and how to fix it then you can tell us what sections are important. Of course then there would be no point in this whole exercise. If you want our help then show us what we ask for.

I did make a void “function” for each of them but it was still failing.

Here’s basically what I did (note I don’t have the revised code using that method since it wasn’t working):

#include <Servo.h>

Servo eyeservo, tailservo;

//all the other stuff

void setup() 
{ 

eyeservo.attach(output1Pin);
tailservo.attach(output2Pin);

pinMode(actPin, INPUT);
pinMode(output1Pin, OUTPUT);
pinMode(output2Pin, OUTPUT);
pinMode(outPin, OUTPUT);
  
} 

void loop() 
{ 
  //Set up Functions
  checkButtons();
  setTServoPosition();
  moveTServo();
  setEServoPosition();
  moveEServo();
}

void checkButtons();
{}

void setTServoPositions();
{}

//etc.

DracoAnimatronica: Here's basically what I did (note I don't have the revised code using that method since it wasn't working):

I don't understand. Do you want to know why that code wasn't working? Then post it. Are you embarrassed that it didn't work or something? Don't be. Post it and we can help you figure out what you did wrong.

If you want help then don't show us "basically" what you did or part of what you did. POST THE WHOLE CODE and we can help you. If you don't want to do that then please don't waste any more of our time posting to this thread.

//Rest of code not important to answer problem.[u]Very important[/u] if the code does not include the functions that you are trying to call.

DracoAnimatronica: Here. This is all you really need to get what I am encountering.

It is not at all sufficient. For the 3rd and last time of asking post the COMPLETE program.

If you are not prepared to provide what you are asked for why should I waste time trying to help? There are dozens of other people on the Forum that I can help without wasting time.

...R

I was rewriting my code for clarity per Robin2’s forum that he gave me the link to.

This is as far as I got and then I ran the program to check it. It failed at the very start the void loop() section.

//Add Servo library
#include <Servo.h>

//List Servo names
  Servo eyeservo, tailservo;

//Set up Constant Pins
  const int actPin = 3; //Activation Button
  const int output1Pin = 9; // Eye Servo
  const int output2Pin = 6; // Tail Servo
  const int ledPin = 13; //Test LED

//Positions for Tail Servo
  const byte maxTDeg = 160;
  const byte minTDeg = 45;
  byte tailPos = minTDeg;

//Positions for Eye Servo
  const int maxEDeg = 179;
  const int minEDeg = 1;
  byte eyePos = maxEDeg;

//Set up values for Button control (no code yet)

void setup()
{
//Attach Servos to pins
  eyeservo.attach(output1Pin);
  tailservo.attach(output2Pin);
  
//Move Servos to starting positions
  tailservo.write(tailPos);
  eyeservo.write(eyePos);
  delay(5000);

//Set up pinModes
  pinMode(actPin, INPUT_PULLUP);
  pinMode(output1Pin, OUTPUT);
  pinMode(output2Pin, OUTPUT);
  pinMode(ledPin, OUTPUT);


}
void loop()
{

//Set up Functions
  checkButtons();  //Failure started here
  setTServoPosition();
  moveTServo();
  setEServoPosition();
  moveEServo();
}

void checkButtons();
//check state of button
{
delay(15);
}

void moveTServo();

{
delay(15);
}

void setTServoPosition();

{
delay(15);
}

void moveEServo();

{
delay(15);
}

//void setEServoPosition();

{
delay(15);
}