//Keypad Shield_Servo.ino
//Program using Keypad Shield to control a servo and display settings
//Uses libraries Servo.h and LiquidCrystal available in the Arduino IDE #include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
#define keyNONE 0
#define keyRIGHT 1
#define keyLEFT 2
#define keyDOWN 3
#define STEP 10Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boardsint pos = 0; // variable to store the servo position
long width;
long read_keys( ){
int adc_read = analogRead(0);
if (adc_read < 50) return keyRIGHT;
if (adc_read <450) return keyDOWN;
if (adc_read <650) return keyLEFT;
return keyNONE;
}
void setup() {
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Program to test");
lcd.setCursor(0,1);
lcd.print("servos.");
myservo.attach(2); // attaches the servo on pin 2 to the servo object
delay(2000);
width = 325;
}void loop() {
int key = read_keys();
if (key==keyNONE) return;
lcd.clear( );
lcd.setCursor(0,0);
switch(key){
case keyRIGHT : lcd.print("Increase "); if (width<500)width +=STEP; break;
case keyLEFT : lcd.print("Reduce "); if (width>150) width -=STEP; break;
case keyDOWN : lcd.print("Centre "); width = 325; break;
}
lcd.print(width);
pos = (((width -150)*90)/175); //(width * 90 )/325;
myservo.write(pos); // tell servo to go to position in variable 'pos'
lcd.print(" ");
lcd.print(pos);
//lcd.print(")");
delay(1000); //contact bounce
}