LCD Screen and RGB timing help

Hello, Im trying to make a gift for my girlfriend I want a LCD screen that prints little cute messages while having LEDs flashing in the background, when I first start the arduino its able to change the messages with the buttons but as soon as I activate the light button the text on the screen starts to get combined with the other messages. I know it has something to do with the delay function I found online but I can't tell how to fix it. Any help is appreciated. Also Im new to arduino so my code is very scrambled and not that great apologize in advance, thank you.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

int red_light_pin= 9;
int green_light_pin = 10;
int blue_light_pin = 8;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  lcd.begin();
  
  //Buttons
    //Light Button
    pinMode(2, INPUT);
    //Message Button
    pinMode(3, INPUT);

  //Lights
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  lcd.print("   Welcome to");
  lcd.setCursor(0,1);
  lcd.print("Korilakkuma Box");
}

int lightMode = 0;
int lightButton = 3;
int MessButton = 2;

void loop() {
  //Phrases
  String phrases[20];
  phrases[0] = " 111111111111";
  phrases[1] = "2222222222222";
  phrases[2] = "33333333333333";
  phrases[3] = "4444444444444";
  phrases[4] = "555555555555";
  phrases[5] = "66666666666";
  phrases[6] = "77777777777777";
  phrases[7] = "8888888888888";
  phrases[8] = "9999999999999";
  phrases[9] = "101010101010";
  phrases[10] = " 11 11 11 11 11 11";
  phrases[11] = "121212121212";
  phrases[12] = "13131313131313";
  phrases[13] = "14141414141414";
  phrases[14] = "1515151515151515";
  phrases[15] = "161616161616";
  phrases[16] = " 17171717";
  phrases[17] = "18181818181818";
  phrases[18] = "19191919191919";
  phrases[19] = " 20202020202020";
  //newMessage(phrases);

  Serial.println(digitalRead(MessButton));
  Serial.println(digitalRead(lightButton));

   if(digitalRead(MessButton) == 1){
    lcd.clear();
    newMessage(phrases);
    delay(1000);
  }

  if(digitalRead(lightButton) == 1){
    lightMode++;
    delay(1000);
  }
  if(lightMode == 1){
    RGBMode(phrases);
  }
  if(lightMode == 2){
    RCMMode(phrases);
  }
  if(lightMode == 3){
    offMode(phrases);
  }
  LMCheck();
  
}

void myDelay(unsigned long duration, int pin, String list[]){  
  unsigned long start = millis();

  while (millis() - start <= duration) {
    checkButtons(pin, list);
  }
}


void checkButtons(int pin, String list[]){
  int  buttonstate = digitalRead(pin);
  if(digitalRead(MessButton) == HIGH){
    newMessage(list);
    delay(1000);
  }

  if(digitalRead(lightButton) == HIGH){
    lightMode++;
    delay(1000);
  }
   
}

void newMessage(String list[]){
  int current = rand() % 20;
  if(list[current].length() > 16){
    String half =list[current].substring(16, list[current].length());
    lcd.print(list[current].substring(0,16));
    lcd.setCursor(0,1);
    lcd.print(half);
  }else{
    lcd.print(list[current]);
  }
}

void RGBMode(String phrases[]){
  RGB_color(255, 0, 0); // Red
    myDelay(1000, lightButton, phrases);
    RGB_color(0, 255, 0); // Green
    myDelay(1000, lightButton, phrases);
    RGB_color(0, 0, 255); // Blue
    myDelay(1000, lightButton, phrases); 
}

void RCMMode(String phrases[]){
  RGB_color(255, 255, 125); // Raspberry
    myDelay(1000, lightButton, phrases);
    RGB_color(0, 255, 255); // Cyan
    myDelay(1000, lightButton, phrases);
    RGB_color(255, 0, 255); // Magenta
    myDelay(1000, lightButton, phrases);
}

void LMCheck(){
  if(lightMode >= 4){
    lightMode = 1;
   }
}

void offMode(String phrases[]){
  RGB_color(255, 255, 255);
  myDelay(1000, lightButton, phrases);
}

void RGB_color(int red_light_value, int green_light_value, int blue_light_value){
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);
}

Hi
actually its delay is somehow impacting performance, but to solve the issue of chained messages I put a "lcd.clear();" between lines 100 and 101 of your code, thus getting the void checkButtons(int pin, String list[ ]) function {

void checkButtons(int pin, String list[]) {
  int  buttonstate = digitalRead(pin);
  if (digitalRead(MessButton) == HIGH) {
    lcd.clear();                                     \\  <<<<<----------------------- 
    newMessage(list);
    delay(1000);
  }
  if (digitalRead(lightButton) == HIGH) {
    lightMode++;
    delay(1000);
  }
}

RV mineirin

1 Like

You’re so right that fixed the message issue, thank you so much!! Idk how I didn’t see that

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.