lcd display

i made a program that should display 2 questions and you can answer using the push button and Potentiometer to select which answer you want to submit. when i try to program a third question the program gets laggy. What should i do different. leave a comment

arduinoprojectbroken.ino (10.5 KB)

the program that work for me

arduinoproject2.0.ino (8.14 KB)

The problem is almost certainly caused by the 12 delay()s in your program

Are all, or any of them necessary ?

What should i do different

  1. Learn to use arrays

  2. Learn to use arrays

  3. Learn to use arrays

  4. Learn to use arrays

  5. Learn to use arrays

  6. Learn to use arrays

  7. Learn to use arrays

  8. Stop using Strings

  9. Stop using Strings

Stop using delay() and millis() in the same code.

loop() loops. Accept, and make use of that. Quit trying to force it into a wiat-for-an-answer mode.

Ask a question. Keep track of which question was asked, so that when, eventually, an answer arrives, you know which question it is the answer to. That will be orders of magnitude easier to do if you have all the questions in an array, and all the answers in another array.

Using meaningful names in your code would be a good idea. x, y, z, and u don't mean a thing.

You will get more help if you post your code as described in the how to use this forum-please read stickies. Many member do not want to download code and others won't because their devices can not open ino files. Make it easier to help you to get better help.

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

int redPin = 5;
int greenPin = 10;
int bluePin = 6;

int buttonPin1 = 2;

int potPin = 2;
int val;


unsigned long startMillis1;
unsigned long currentMillis1;
const unsigned long period1 = 250;

unsigned long startMillis2;
unsigned long currentMillis2;
const unsigned long period2 = 250;

unsigned long startMillis3;
unsigned long currentMillis3;
const unsigned long period3 = 250;

unsigned long startMillis4;
unsigned long currentMillis4;
const unsigned long period4 = 250;

unsigned long startMillis5;
unsigned long currentMillis5;
const unsigned long period5 = 250;

unsigned long startMillis6;
unsigned long currentMillis6;
const unsigned long period6 = 250;

unsigned long startMillis7;
unsigned long currentMillis7;
const unsigned long period7 = 250;

unsigned long startMillis8;
unsigned long currentMillis8;
const unsigned long period8 = 250;

unsigned long startMillisTest;
unsigned long currentMillisTest;
const unsigned long periodTest = 12000;

unsigned long startMillisTest1;
unsigned long currentMillisTest1;
const unsigned long periodTest1 = 12000;

unsigned long startMillisTest2;
unsigned long currentMillisTest2;
const unsigned long periodTest2 = 12000;

unsigned long startMillisTest3;
unsigned long currentMillisTest3;
const unsigned long periodTest3 = 12000;

int buttonState1 = 0;

int x = 0;
int y = 0;
int z = 0;
int u = 0;



int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
int counter7 = 0;
int counter8 = 0;

int aantalLetters1;
int aantalLetters2;
int aantalLetters3;
int aantalLetters4;
int aantalLetters5;
int aantalLetters6;
int aantalLetters7;
int aantalLetters8;


String tekst1;
String message1 = "                Hoeveel meter is een olympisch zwembad?                ";

String tekst2;
String message2 = "                Het juiste antwoord was...   C:50m                " ;

String tekst3;
String message3 = "                Van welke boom komt het blad op de Canadese vlag?                " ;

String tekst4;
String message4 = "                Het juiste antwoord was...   A:Esdoorn                " ;
String tekst5;
String message5 = "                Hoeveel ribben heeft een kubus                " ;

String tekst6;
String message6 = "                Het juiste antwoord was...   A:12                " ;

String tekst7;
String message7 = "                Wie is de partij voorzitter van Groen                " ;

String tekst8;
String message8 = "                het juiste antwoord was...     B:Meyrem Almaci                " ;


LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
  aantalLetters1 = message1.length();
  aantalLetters2 = message2.length();
  aantalLetters3 = message3.length();
  aantalLetters4 = message4.length();
  aantalLetters5 = message5.length();
  aantalLetters6 = message6.length();
  aantalLetters7 = message7.length();
  aantalLetters8 = message8.length();

  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.home();

  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);

  pinMode(buttonPin1, INPUT);
  pinMode(potPin, INPUT);
}

void loop() {

  vraag1();
  

  if (val >= 683 && val <= 1024) {
    y++;
  }
  else if (val <= 683 && val >= 342) {
    z++;
  }
  else if (val <= 342 && val >= 0) {
    x++;
  }
  buttonState1 = 0;
  antwoord1();

  y = 0;
  x = 0;
  z = 0;
  lcd.clear();
  

  buttonState1 = 0;

  while (buttonState1 == 0) {
    buttonState1 = digitalRead(buttonPin1);
    lcd.setCursor(1, 0);
    lcd.print("volgende vraag  ");
  }


  buttonState1 = 0;
  
  lcd.clear();

  vraag2();


  if (val >= 683 && val <= 1024) {
    y++;
  }
  else if (val <= 683 && val >= 342) {
    z++;
  }
  else if (val <= 342 && val >= 0) {
    x++;
  }

  buttonState1 = 0;

  
  antwoord2();
  buttonState1 = 0;
  y = 0;
  x = 0;
  z = 0;
  lcd.clear();
  

  while (buttonState1 == 0) {
    buttonState1 = digitalRead(buttonPin1);
    lcd.setCursor(1, 0);
    lcd.print("volgende vraag  ");
  }
  buttonState1 = 0;
  
  lcd.clear();

  vraag3();
  if (val >= 683 && val <= 1024) {
    y++;
  }
  else if (val <= 683 && val >= 342) {
    z++;
  }
  else if (val <= 342 && val >= 0) {
    x++;
  }

  buttonState1 = 0;

  delay(500);
  antwoord3();
  buttonState1 = 0;
  y = 0;
  x = 0;
  z = 0;
  lcd.clear();
  
  while (buttonState1 == 0) {
    buttonState1 = digitalRead(buttonPin1);
    lcd.setCursor(1, 0);
    lcd.print("volgende vraag  ");
  }

}




void vraag1()
{
  startMillis1 = millis();
  while (buttonState1 == 0) {
    buttonState1 = digitalRead(buttonPin1);
    lcd.home();
    tekst1 = message1.substring(counter1, counter1 + 16);

    currentMillis1 = millis();

    if (currentMillis1 - startMillis1 >= period1) {
      lcd.print(tekst1);
      counter1 = counter1 + 1;
      startMillis1 = currentMillis1;
    }

    if (counter1 > (aantalLetters1 - 16)) {
      counter1 = 0;
    }


    lcd.setCursor(5, 1);
    val = analogRead(potPin);
    if (val <= 342 && val >= 0) {
      lcd.print("A:25m ");
    }
    else if (val <= 683 && val >= 342) {
      lcd.print("B:100m");
    }
    else if (val <= 1024 && val >= 683) {
      lcd.print("C:50m ");
    }

  }

}

void antwoord1()
{
 
  startMillis2 = millis();
  startMillisTest = millis();
  lcd.setCursor(0, 1);
  lcd.print("                ");
  while (buttonState1 == 0) {
    lcd.setCursor(0, 0);
    buttonState1 = digitalRead(buttonPin1);
    tekst2 = message2.substring(counter2, counter2 + 16);
    currentMillis2 = millis();
    if (currentMillis2 - startMillis2 >= period2) {
      lcd.print(tekst2);
      counter2 = counter2 + 1;
      startMillis2 = currentMillis2;
    }
    if (counter2 > (aantalLetters2 - 16)) {
      counter2 = 0;
    }

    currentMillisTest = millis();
    if (currentMillisTest - startMillisTest >= periodTest) {
      startMillisTest = currentMillisTest;
      if (y == 1) {
        lcd.setCursor(2, 1);
        lcd.print("C:50m  JUIST");
        digitalWrite(greenPin, HIGH);
        u++;

      }
      else if (z == 1) {
        lcd.setCursor(2, 1);
        lcd.print("B:100m  FOUT");
        digitalWrite(redPin, HIGH);
      }
      else {
        lcd.setCursor(3, 1);
        lcd.print("A:25m  FOUT");
        digitalWrite(redPin, HIGH);

      }



    }

  }

  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);

}

void vraag2()
{
  
  startMillis3 = millis();
  while (buttonState1 == 0) {
    buttonState1 = digitalRead(buttonPin1);
    lcd.home();
    tekst3 = message3.substring(counter3, counter3 + 16);

    currentMillis3 = millis();

    if (currentMillis3 - startMillis3 >= period3) {
      lcd.print(tekst3);
      counter3 = counter3 + 1;
      startMillis3 = currentMillis3;
    }

    if (counter3 > (aantalLetters3 - 16)) {
      counter3 = 0;
    }


    lcd.setCursor(3, 1);
    val = analogRead(potPin);
    if (val <= 342 && val >= 0) {
      lcd.print("A:Esdoorn");
    }
    else if (val <= 683 && val >= 342) {
      lcd.print("  B:Wilg    ");
    }
    else if (val <= 1024 && val >= 683) {
      lcd.print("  C:Iep     ");
    }

  }

}

void antwoord2()
{
 
  startMillis4 = millis();
  startMillisTest1 = millis();
  lcd.setCursor(0, 1);
  lcd.print("                ");
  while (buttonState1 == 0) {
    lcd.setCursor(0, 0);
    buttonState1 = digitalRead(buttonPin1);
    tekst4 = message4.substring(counter4, counter4 + 16);
    currentMillis4 = millis();
    if (currentMillis4 - startMillis4 >= period4) {
      lcd.print(tekst4);
      counter4 = counter4 + 1;
      startMillis4 = currentMillis4;
    }
    if (counter4 > (aantalLetters4 - 16)) {
      counter4 = 0;
    }

    currentMillisTest1 = millis();
    if (currentMillisTest1 - startMillisTest1 >= periodTest1) {
      startMillisTest1 = currentMillisTest1;
      if (y == 1) {
        lcd.setCursor(0, 1);
        lcd.print("  C:Iep  FOUT");
        digitalWrite(redPin, HIGH);

      }
      else if (z == 1) {
        lcd.setCursor(0, 1);
        lcd.print("  B:Wilg  FOUT");
        digitalWrite(redPin, HIGH);
      }
      else {
        lcd.setCursor(1, 1);
        lcd.print("A:Esdoorn JUIST");
        digitalWrite(greenPin, HIGH);
        u++;

      }

    }

  }

}

PaulS:

  1. Learn to use arrays

  2. Learn to use arrays

  3. Learn to use arrays

  4. Learn to use arrays

  5. Learn to use arrays

  6. Learn to use arrays

  7. Learn to use arrays

  8. Stop using Strings

  9. Stop using Strings

Stop using delay() and millis() in the same code.

loop() loops. Accept, and make use of that. Quit trying to force it into a wiat-for-an-answer mode.

Ask a question. Keep track of which question was asked, so that when, eventually, an answer arrives, you know which question it is the answer to. That will be orders of magnitude easier to do if you have all the questions in an array, and all the answers in another array.

Using meaningful names in your code would be a good idea. x, y, z, and u don't mean a thing.

where do i need to use arrays?

any tips on how to scroll a message? is the current methode bad? i'm using an array of sentences now

where do i need to use arrays?

Everywhere where you have a bunch of variables that have numeric suffixes.

Can someone link me code for a good scroller program that i can use in my code