Project Help - switch not responding quickly

I am working on a small game. I will connect the some motor, a LCD screen, and two switch. Right now, i have tested all components seperately, but when i put the code of LCD screen and switch together, the switch is not responding fast. Pls help, I dont know what is wrong, and I am quite new to this.

#include <hd44780.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <CapacitiveSensor.h>
#include <AFMotor.h>

AF_DCMotor MotorL(1);
AF_DCMotor MotorR(2);
 

long previousMillis = 0;
long interval = 1; 
long intervalCoin1 = 3000;
long intervalCoin2 = 3000;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);

LiquidCrystal_I2C lcd(0x27,20,4); 

int gameState = 0; //Gameover

int leftPin = 53;
int rightPin = 52;



void setup(){

  Serial.begin(9600);

  pinMode(leftPin, INPUT);
  pinMode(rightPin, INPUT);
  

  //LCD Monitor
  lcd.init();                     
  lcd.init();
  lcd.backlight();
   lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("LeaderBoard:");
    lcd.setCursor(0,1);
    lcd.print("Player 1 : 90pts");
    lcd.setCursor(0,2);
    lcd.print("Player 2 : 85pts");
    lcd.setCursor(0,3);
    lcd.print("Player 3 : 30pts");


}
void loop(){
  
    long start = millis();
    long total1 =  cs_4_2.capacitiveSensor(30);
    unsigned long currentMillis = millis();

 
  
//    delay(3000);
  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Insert Coin to PLAY ! ! !");
//    delay(3000);



    if (total1 > 5000){
      gameState = 1; 
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Game Start ! ! !");
  }

//if (gameState = 1){
    
 //Switch
  if( digitalRead(leftPin)== HIGH){
    Serial.println("LEFT"); 
    delay(250);
    } 
    
  if(digitalRead(rightPin) == HIGH){
    Serial.print("RIGHT");
    delay(250);
    }



}
  
  

What value pulldown resistors do you have on the switch inputs?

Post a schematic, please.

Why are you including the hd44780 library but using the LiquidCrystal_I2C library?

Hello aaronho020409
Avoid the ussage of the delay() function at all. This function will block the execution of the sketch in realtime.
Take a view here to gain the knowledge.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

1 Like

I was trying to figure out what library work with my LCD screen

Why do you have the delay(250) calls?

Go for the library suggested by groundFungus! He tipped me about it and it's working like a charm in multiple projects!

Your circuit would be simpler if you use INPUT_PULLUP and the internal pull-up resistor.

You are updating the LCD every time through loop(). That is not necessary as humans can't read that fast. I set up a millis() timer to update the display every 1/2 second. More than enough. Also included the right files, constructor and begin function to use the better hd44780 library for the LCD. Untested.

#include <Wire.h>

//#include <LiquidCrystal_I2C.h>
#include <CapacitiveSensor.h>
#include <AFMotor.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

AF_DCMotor MotorL(1);
AF_DCMotor MotorR(2);


long previousMillis = 0;
long interval = 1;
long intervalCoin1 = 3000;
long intervalCoin2 = 3000;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4, 2);

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

int gameState = 0; //Gameover

int leftPin = 53;
int rightPin = 52;

void setup()
{
   Serial.begin(9600);

   pinMode(leftPin, INPUT);
   pinMode(rightPin, INPUT);

   //LCD Monitor
   lcd.begin(16, 2);
   lcd.backlight();
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("LeaderBoard:");
   lcd.setCursor(0, 1);
   lcd.print("Player 1 : 90pts");
   lcd.setCursor(0, 2);
   lcd.print("Player 2 : 85pts");
   lcd.setCursor(0, 3);
   lcd.print("Player 3 : 30pts");
}

void loop()
{
   long start = millis();
   long total1 =  cs_4_2.capacitiveSensor(30);

   static unsigned long lcdTimer = 0;
   unsigned long lcdInterval = 500;
   unsigned long currentMillis = millis();
   if (currentMillis - lcdTimer > lcdInterval)
   {
      //    delay(3000);

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Insert Coin to PLAY ! ! !");
      //    delay(3000);
   }

   if (total1 > 5000)
   {
      gameState = 1;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Game Start ! ! !");
   }

   //if (gameState = 1){

   //Switch
   if ( digitalRead(leftPin) == HIGH)
   {
      Serial.println("LEFT");
      delay(250);
   }

   if (digitalRead(rightPin) == HIGH)
   {
      Serial.print("RIGHT");
      delay(250);
   }
}

Avoid the ussage of the delay() function igram at all. This function will block the execution of the sketch in realtime.

And that's only part of the rules

Have the code decide whether anything has changed since the last time. Only if something has changed and it has been at least say, 200 ms since the display was last written, do you update it and you generally do no need to clear it to do so unless everything on it is to be changed.

If you set the cursor to the part that needs to be changed and write as many characters as that part was previously showing, then only that part will be updated and it will take no longer than necessary (as lcd.clear() is quite slow).

If the delay calls are for switch debouncing, they're much longer than needed. The delay in the example sketch DigitalReadSerial uses a
delay(1);
"for stability". There are other methods to debounce a switch, including libraries that are expressly for debouncing switches.
Also, I have always found it easier to use the built in 10k pullup resistors on the digital pins for simple switches. Just declare in setup
pinMode(leftPin, INPUT_PULLUP);
and you're good to go. Just be aware that doing this will reverse your switch logic.

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