Hello all, I've created a random message generator that should be displaying a random order of preselected phrases after a button press. It is not doing that, the LCD screen i'm using often glitches out and is filled with special characters or cut off phrases. Let me know if there's any other information I can provide.
#include <LCD-I2C.h>
LCD_I2C lcd(0x27, 16, 2);
const char *messages[] = {
"msg 1",
"msdg 2",
"msg 3",
};
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME_SCK 7
#define BME_MISO 6
#define BME_MOSI 5
#define BME_CS 4
#define SEALEVELPRESSURE_HPA (1015)
Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
unsigned long delayTime;
const int numMessages = sizeof(messages);
const int buttonPin = 2;
const int bu2onPin = 3;
int hits = 0;
int buttonState = 0;
int bu2onState = 0;
int lastButtonState = 0;
int lastBu2onState = 0;
int currentMessageIndex = 0;
void setup() {
Serial.begin(9600);
lcd.begin();
lcd.display();
lcd.backlight();
pinMode(buttonPin, INPUT);
pinMode(bu2onPin, INPUT);
bool status;
status = bme.begin();
if (!status) {
lcd.setCursor(0, 0);
lcd.print("sensor error :[");
delay(2000);
lcd.clear();
while (1)
;
}
lcd.print("sensor online :]");
delay(1000);
lcd.clear();
}
void loop() {
delay(delayTime);
buttonState = digitalRead(buttonPin);
bu2onState = digitalRead(bu2onPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
currentMessageIndex = random(numMessages);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(messages[currentMessageIndex]);
}
delay(500);
}
lastButtonState = buttonState;
if (bu2onState != lastBu2onState) {
if (bu2onState == LOW) {
hits++;
if (hits >= 5) {
lcd.clear();
hits = 0;
}
}
lastBu2onState = bu2onState;
switch (hits) {
case 1:
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(bme.readTemperature());
lcd.write(223);
lcd.print("C");
break;
case 2:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pressure:");
lcd.print(bme.readPressure() / 100.0F);
lcd.print("hPa");
break;
case 3:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Altitude: ");
lcd.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
lcd.print("m");
break;
case 4:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(bme.readHumidity());
lcd.print("%");
break;
case 5:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sophie is: ");
lcd.print(" sexy");
break;
delay(500);
}
}
}