Hey guys,
I'm having trouble with the c language programing for my arduino board. I am trying to make it so that when I push button1, a servo motor will move one way. When I push button 2, a servo motor will move in the opposite direction. Finally if you push both buttons simultaneously it will reset the servo motor back to a center position.
I'm not sure if my coding is right at all but have a look. I tried if/else statements at first and that seemed to work when I pushed button1 before button2 "simultaneously" but I want the part to work both ways.
If someone can get back to me on this, that would be great. Thanks
#include <Servo.h>
const int buttonPin = 2;
const int motorPin = 3;
const int buttonPin2 = 4;
int buttonState = 0;
int buttonState2 = 0;
byte hh = 0;
byte hl = 0;
byte lh = 0;
byte ll = 0;
byte check = 0;
Servo myservo;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(buttonPin2, INPUT);
Serial.begin(9600);
myservo.attach(3);
myservo.write(90);
}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
hh = digitalRead(buttonPin)==HIGH && digitalRead(buttonPin2)==HIGH;
hl = digitalRead(buttonPin)==HIGH && digitalRead(buttonPin2)==LOW;
lh = digitalRead(buttonPin)==LOW && digitalRead(buttonPin2)==HIGH;
ll = digitalRead(buttonPin)==LOW & digitalRead(buttonPin2)==LOW;
check = digitalRead(buttonPin) && digitalRead(buttonPin2);
switch (check){
case hh:
myservo.write(90);
break;
case hl:
for (int i= myservo.read(); i>= 0; i-=1) {
myservo.write(i);
delay(45);
Serial.println(digitalRead(buttonPin));
if (i == 0) i = 180;
if (digitalRead(buttonPin)== LOW) break;
}
case lh:
for (int i= myservo.read(); i<= 180; i+=1){
myservo.write(i);
delay(45);
Serial.println(digitalRead(buttonPin2));
if (i == 180) i = 0;
if (digitalRead(buttonPin2)== LOW) break;
}
case ll:
break;
}
}