Hi! I'm very very new to Arduino so I apologize if my question seems very basic. I am trying to control a micro-servo to go to 4 different positions (10, 60, 120, 170) with 2 buttons attached to separate digital pins (5+6). I understand how to get the micro servo to go to a position when just one button is HIGH/LOW but I don't know how to make it go to a specific position if say button 1=HIGH and button 2=HIGH. When I tried to use if statements the micro servo just kept stuttering as if it was struggling to go between two different positions. I would also like to write this information on an LCD.
In summary...
-
Four Different Positions linked to Four Different Button Mvts
a. 10=Button 1 (HIGH), Button 2(HIGH)
b. 60=Button 1(LOW), BUtton 2(HIGH)
c. 120=Button 1(HIGH), Button 2(LOW)
d. 170=Button1(LOW), Button2(LOW) -
Get LCD to display the "position" of the micro-servo (10,60,120,170)
Thanks for your help!
Code:
#include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,7,4,3,2);
int x;
int prox=A2;
int val;
int button1 = 6; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
int press3=0;
int press4=0;
Servo servo1;
void setup()
{
lcd.begin(16,2);
pinMode(A2, INPUT);
Serial.begin(9600);
pinMode(9, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(9);
digitalWrite(6, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == HIGH && press2==HIGH)
{
servo1.write(170);
}
press2 = digitalRead(button2);
if (press2 == LOW && press2==LOW)
{
servo1.write(10);
}
}
