Hello
I'm writing a code for arduino Uno which increases and decreases a motor's speed and need to assign buttons to make it go clockwise and anti-clockwise. I'm not sure why mine doesn't work. How can i fix it?
Thanks in advance.
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int spd=0;
int Mopin1=11;
int Mopin2=12;
int MoSpeedpin=3;
int count=0;
void setup() {
pinMode(Mopin1,OUTPUT);
pinMode(Mopin2,OUTPUT);
pinMode(MoSpeedpin,OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0,1);
lcd.print("Rotation:");
}
int read_LCD_buttons()
{
int adc_key_in = analogRead(0);
if (adc_key_in < 60) return btnRIGHT;
if (adc_key_in < 200) return btnUP;
if (adc_key_in < 420) return btnDOWN;
if (adc_key_in < 600) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE;
}
void loop() {
int lcd_key;
lcd_key = read_LCD_buttons();
lcd.setCursor(8, 1);
if (lcd_key == btnRIGHT){ lcd.print(abs(count)); lcd.print(" ") ;}
if (lcd_key == btnLEFT) { lcd.print(-1*abs(count)) ;lcd.print(" ");}
if (lcd_key == btnUP){ lcd.print(++count); lcd.print(" "); }
if (lcd_key == btnDOWN) lcd.print(--count); lcd.print(" "); }
if (lcd_key == btnSELECT) lcd.print("SELECT");
if (count>0) {
digitalWrite(Mopin1,HIGH);
digitalWrite(Mopin2,LOW);
analogWrite(MoSpeedpin,count);
}
else{
digitalWrite(Mopin1,LOW);
digitalWrite(Mopin2,HIGH);
analogWrite(MoSpeedpin,-count);
}
delay(100);
}