Thanks for helping me out, so close to get it working:)
#include <Stepper.h>
#include <LiquidCrystal.h>
LiquidCrystal LCD(8, 9, 4, 5, 6, 7);
#define step_pin 3 // Define pin 3 as the steps pin
#define dir_pin 2 // Define pin 2 as the direction pin
#define MS1 12 // Define pin 5 as "MS1"
#define MS2 13 // Define pin 4 as "MS2"
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
#define trigPin 0
#define echoPin 1
int button;
int direction; // Variable to determine the sense of the motor
int steps = 500; // Number of steps that you want to execute (for full steps, 200 = 1 turn)
void setup() {
LCD.begin(16,2);
LCD.setCursor(0,0);
LCD.print(" Pall 120");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(MS1, OUTPUT); // Configures "MS1" as output
pinMode(MS2, OUTPUT); // Configures "MS2" as output
pinMode(dir_pin, OUTPUT); // Configures "dir_pin" as output
pinMode(step_pin, OUTPUT); // Configures "step_pin" as output
digitalWrite(MS1, LOW); // Configures the steps division (see above)
digitalWrite(MS2, LOW); // Configures the steps division (see above)
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance =(((duration/2)/29.4)*2) ;
LCD.setCursor(0,1);
LCD.print(" ");
LCD.setCursor(0,1);
LCD.print(distance);
LCD.print(" cm");
delay(250);
int buttonVal = read_buttons();
if(buttonVal == btnLEFT)
{
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps--;
}
if(buttonVal == btnRIGHT)
{
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, LOW);
delay(3);
digitalWrite(step_pin, HIGH);
delay(3);
}
}
int read_buttons() {
int adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
}