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.
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.
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.
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:
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.
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):
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.
What do you mean the failure started there?
Is that the line that Sketch said there was an error on? What error?
You may be throwing everyone off when you're pointing us to 'where' the failure starts when in reality the failure is elsewhere because of where it references to.
INTP:
What do you mean the failure started there?
Is that the line that Sketch said there was an error on? What error?
You may be throwing everyone off when you're pointing us to 'where' the failure starts when in reality the failure is elsewhere because of where it references to.
The program that I have been using highlighted the text in the line, along with the other functions I was trying to make, and gave me the error saying "not declared in this scope".