Skip to content

Instantly share code, notes, and snippets.

@tvass

tvass/radio.ino Secret

Created Oct 21, 2020
Embed
What would you like to do?
#include <LiquidCrystal_I2C.h>
#define INPUT_PIN A0
int incomingByte = 0;
String cmd;
String param;
String message1 = "", message2 = "";
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
byte customChar[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
void printlcd(String line1, String line2) {
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
}
void volume(int vol) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Volume");
lcd.setCursor(7, 0);
lcd.print(vol);
if (vol != 0) {
int i = 0;
int position = vol * 16 / 100;
for (i = 0; i <= position; i++) {
lcd.setCursor(i, 1);
lcd.write(0);
}
}
delay(1000);
lcd.clear();
}
void readAnalogButton() {
int button = analogRead(INPUT_PIN);
if (button > 430 && 450 > button) {
Serial.println("mpc prev");
lcd.clear();
printlcd("mpc prev", "");
delay(500);
lcd.clear();
}
if (button > 250 && 270 > button) {
Serial.println("mpc next");
lcd.clear();
printlcd("mpc next", "");
delay(500);
lcd.clear();
}
if (button > 160 && 180 > button) {
Serial.println("mpc vol -10");
lcd.clear();
printlcd("mpc vol", "-10");
delay(500);
lcd.clear();
}
if (button > 110 && 130 > button) {
Serial.println("mpc vol +10");
lcd.clear();
printlcd("mpc vol", "+10");
delay(500);
lcd.clear();
}
if (button > 50 && 60 > button) {
Serial.println("mpc toggle");
lcd.clear();
printlcd("mpc toggle", "");
delay(500);
lcd.clear();
}
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.backlight();
lcd.createChar(0, customChar);
}
void loop() {
while (Serial.available() > 0 ) {
String str = Serial.readString();
cmd = str.substring(0, 3);
param = str.substring(4);
param.trim();
if (cmd == "lcd") {
message1 = param.substring(0, 17);
message2 = param.substring(16, 33);
lcd.clear();
}
if (cmd == "cls") {
message1 = "";
message2 = "";
lcd.clear();
}
if (cmd == "vol") {
volume(param.toInt());
}
}
printlcd(message1, message2);
int button = analogRead(INPUT_PIN);
// Serial.print("debug> ");
// Serial.println(button);
readAnalogButton();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment