this basic code should get you started.
works with Ardduino UNO,
LCD shield
http://www.dfrobot.com/index.php?route=product/product&product_id=51and motor shield
http://www.dfrobot.com/index.php?route=product/product&product_id=69/*
The circuit:
* LCD RS pin to digital pin 8
* LCD Enable pin to digital pin 9
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 25 July 2009
by David A. Mellis
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//Key message
char msgs[5][17] = {
"RIGHT pressed ",
"UP pressed ",
"DOWN pressed ",
"LEFT pressed ",
"STOP pressed"};
int adc_key_val[5] ={
30, 150, 360, 535, 760 }; // for voltage divider
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
int M1 = 3;
int E1 = 11;
int rpm;
void setup() {
Serial.begin(9600);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("any text here 1");
delay (2000);
lcd.setCursor(0, 1);
lcd.print("any text here 2");
delay (2000);
// pinMode(M1, OUTPUT);
rpm = 70;
}
void loop()
{
adc_key_in = analogRead(0); // read the analog value from the keybaord keys
key = get_key(adc_key_in); // call subroutine to convert analog value into key number
if (key != oldkey) // if keypress is detected (key differs from oldkey)
{
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // call subroutine to convert analog value into key number
if (key != oldkey)
{
oldkey = key;
if (key >=0){
lcd.setCursor(0, 0);
lcd.print(msgs[key]);
lcd.setCursor(0, 1); //line=1, x=0
lcd.print(" "); // clear line
lcd.setCursor(0, 1); //line=1, x=0lcd.print(key);
lcd.print(key);
Serial.println(key);
delay(500);
if (key ==1){
rpm = rpm +5;
if (rpm > 255) rpm = 255;
analogWrite(E1, rpm); //PWM Speed Control
Serial.println(rpm);
}
if (key ==2){
rpm = rpm -5;
analogWrite(E1, rpm); //PWM Speed Control
}
if (key ==0){
digitalWrite(M1,LOW); // set rotation direction
analogWrite(E1, rpm); // PWM Speed Control
}
if (key ==3){
digitalWrite(M1,HIGH); // set rotation direction
analogWrite(E1, rpm); // PWM Speed Control
}
if (key ==4){
digitalWrite(M1,HIGH); // set rotation direction
analogWrite(E1, 0); //PWM Speed Control
}
}
}
}
else
{
lcd.setCursor(0, 0); // no button pressed, display aanpassen
lcd.print("WAIT FOR INFO ");
lcd.setCursor(0, 1);
lcd.print("PRESS A BUTTON ");
}
}
// SUBROUTINE Convert ADC value to key number
int get_key(unsigned int input)
{
int k; //
for (k = 0; k < NUM_KEYS; k++) // increase k as long as it remains smaller than NUM_KEYS
{
if (input < adc_key_val[k]) // compare input with incresing values
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed ,
return k;
}