#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
#define VERT_PIN A1
#define HORZ_PIN A2
#define SEL_PIN 12
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const int analogPin = A0;
const int ledCount = 10;
int ledCountNow;
int checkLevel = 0;
int sensorReading;
int procLevel;
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
void resist(){
sensorReading = analogRead(analogPin);
procLevel = map(sensorReading, 0, 1023, 0, 100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Potentiometer");
lcd.setCursor(1, 1);
lcd.print(procLevel);
lcd.setCursor(4, 1);
lcd.print("%");
while(1){
sensorReading = analogRead(analogPin);
procLevel = map(sensorReading, 0, 1023, 0, 100);
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
if(checkLevel != ledLevel){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Potentiometer");
lcd.setCursor(1, 1);
lcd.print(procLevel);
lcd.setCursor(4, 1);
lcd.print("%");
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
checkLevel = ledLevel;
if (digitalRead(SEL_PIN) == LOW) {
break;
}
delay(100);
}
}
void joystick(){
ledCountNow = checkLevel;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Joystick");
lcd.setCursor(1, 1);
lcd.print(ledCountNow);
while(1){
int horz = analogRead(HORZ_PIN);
int vert = analogRead(VERT_PIN);
if (vert > 300) {
if(ledCountNow < 11) ledCountNow++;
}
if (vert < 700) {
if(ledCountNow > 0) ledCountNow--;
}
if (horz < 700) {
if(ledCountNow < 11) ledCountNow++;
}
if (horz > 300) {
if(ledCountNow > 0) ledCountNow--;
}
if(checkLevel != ledCountNow){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Joystick");
lcd.setCursor(1, 1);
lcd.print(ledCountNow);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
if (thisLed < ledCountNow) {
digitalWrite(ledPins[thisLed], HIGH);
}
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
checkLevel = ledCountNow;
if (digitalRead(SEL_PIN) == LOW) {
break;
}
delay(100);
}
}
void setup() {
lcd.init();
lcd.backlight();
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
}
void loop() {
resist();
delay(300);
joystick();
delay(300);
}