//Created by Barbu Vulc!
/*
* In this sketch we will simulate a automatic gearbox & orientation of a car...
* ...with a joystick and 16x2 LCD! We have: drive (D), neutral (N) & reverse (R)!
* The LCD is used as an on-board car screen!
*/
//LCD (library & object):
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Define Joystick variables:
#define joyX A0
#define joyY A1
//LCD Initialization!
void setup() {
lcd.init(); lcd.backlight();
lcd.setCursor(0, 0); lcd.print("Engine started!");
delay(2000); lcd.clear();
}
void loop() {
//Read joystick values (gear - pin A0 & direction - pin A1):
int gear = analogRead(joyX), direction = analogRead(joyY);
//Print gear & orientation depending on joystick values:
if(gear == 1023){
lcd.setCursor(0, 0); lcd.print("Gear: D");
if(direction == 1023){
lcd.setCursor(0, 1); lcd.print("Dir.: D-Left ");
}else if(direction == 512){
lcd.setCursor(0, 1); lcd.print("Dir.: Forward");
}else{
lcd.setCursor(0, 1); lcd.print("Dir.: D-Right");
}
}if(gear == 512){
lcd.setCursor(0, 0); lcd.print("Gear: N");
lcd.setCursor(0, 1); lcd.print(" ");
}if(gear == 0){
lcd.setCursor(0, 0); lcd.print("Gear: R");
if(direction == 1023){
lcd.setCursor(0, 1); lcd.print("Dir.: R-Left ");
}else if(direction == 512){
lcd.setCursor(0, 1); lcd.print("Dir.: Reverse");
}else{
lcd.setCursor(0, 1); lcd.print("Dir.: R-Right");
}
}
}