im working on a bit of animatronics and i have half the program correct and working right. but the other half for buttons C and D are not holding when the switch is pressed. it moves in the direction that I want but it goes back to neutral position then back up to the programmed position when the button is pressed. im not sure what is up but the code looks right.
here is the program can you please help...im a bit lost as far as what to do now.
#include <Servo.h>
Servo wag; // left and right direction for the tail
Servo mood; // up and down direction for the tail
int pos = 90; // servo start position
int buttonA = 12; // tail wag on Pin 12
int buttonB = 8; // standing tail wag, slow on pin 11
int buttonC = 7; //happy mood no wag, on pin 10
int buttonD = 4; //sad/scared mood no wag on pin 9
int A = 0;
int B = 0;
int C = 0;
int D = 0;
void setup()
{
wag.attach(6); // wag motion pin 6 output
mood.attach(5); //mood motion pin 5 output
pinMode(pos, OUTPUT);
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
pinMode(buttonC, INPUT);
pinMode(buttonD, INPUT);
digitalWrite(12, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
digitalWrite(4, HIGH);
}
void loop()
//this section for button A, fast wag
{
A = digitalRead(buttonA);
if (A == LOW)
{
for(pos = 90; pos < 130; pos += 4) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
wag.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(250);
for(pos = 130; pos>=47; pos-=4) // goes from 180 degrees to 0 degrees
{
wag.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{
delay(250);
}
if (A == 0)
{
for(pos = 47; pos<=90; pos+=4)
{
wag.write(pos);
delay(15);
}
}
//this section bellow is for buttonB, slow wag
}
B = digitalRead(buttonB);
if (B == LOW)
{
for(pos = 90; pos < 130; pos += 2)
{
wag.write(pos); delay(15);
}
delay(100);
for(pos = 130; pos >=47; pos -= 2)
{
wag.write(pos), delay(15);
}
delay(15);
}
if(B == 0)
{
for(pos = 47; pos<=90; pos+=2)
{
wag.write(pos);
delay(15);
}
}
//this section bellow is for button C, happy mood
C = digitalRead(buttonC);
if (C == LOW)
{
for(pos = 90; pos <=150; pos+=4)
{
mood.write(pos); delay(15);
}
}
if (C == 0)
{
for(pos = 150; pos>=90; pos-=4)
{
mood.write(pos); delay(15);
}
}
//this section bellow is for button D, sad/scared mood
D = digitalRead(buttonD);
if (D == LOW)
{
for(pos = 90; pos >=20; pos-=4)
{
mood.write(pos); delay(15);
}
}
if (D == 0)
{
for(pos = 20; pos<=90; pos+=4)
{
mood.write(pos); delay(15);
}
}
}