/*
Forum: https://forum.arduino.cc/t/im-having-uploading-my-code-to-my-board-this-error-message-show-up/1184392
Wokwi: https://wokwi.com/projects/380195072459624449
*/
#include <LiquidCrystal.h>
const int rs = 8, en = 2, DB4 = 4, DB5 = 5, DB6 = 6, DB7 = 7;
LiquidCrystal lcd(rs, en, DB4, DB5, DB6, DB7);
byte happyface[] = {
B00000,
B00000,
B00000,
B01010,
B00000,
B10001,
B01110,
B00000,
};
byte sadface[] = {
B00000,
B00000,
B00000,
B01010,
B00000,
B01110,
B10001,
B00000,
};
byte ordinary[] = {
B00000,
B00000,
B01010,
B00000,
B11111,
B00000,
B00000,
B00000,
};
byte die[] = {
B00000,
B10101,
B01010,
B10101,
B00000,
B01110,
B01010,
B01110,
};
constexpr byte trig = 9;
constexpr byte echo = 10;
long duration, distance;
char customChar = 0;
char previousCustomChar = 127;
void setup()
{
lcd.begin(16, 2);
lcd.createChar(1, happyface);
lcd.createChar(3, sadface);
lcd.createChar(2, ordinary);
lcd.createChar(4, die);
lcd.clear();
lcd.print("Custom Character");
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
digitalWrite(trig, LOW);
delayMicroseconds(10);
digitalWrite(trig, HIGH);
duration = pulseIn (echo, HIGH);
distance = duration / 57.8;
Serial.println(distance);
switch (distance) {
case 0 ... 25:
customChar = 1;
break;
case 26 ... 100:
customChar = 2;
break;
case 101 ... 150:
customChar = 3;
break;
case 151 ... 250:
customChar = 4;
break;
default:
customChar = '?'; // Display "?"" if distance > 250
break;
}
if (customChar != previousCustomChar) {
lcd.setCursor(0, 1);
lcd.write(customChar);
previousCustomChar = customChar;
}
}