Arduino project - connection problem

Hi guys, I am working on a project for school assignments. I am making a small game like temple run, but a physical one. I was connecting a LCD monitor and a 4digit 7 segment display. They work fine, but then i added a switch, the switch response very slowly, Pls help me. I am quite new to this and not sure which part is wrong.
Here is my code.

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

#define LED_PIN     34
#define NUM_LEDS    29
CRGB leds[NUM_LEDS];


LiquidCrystal_I2C lcd(0x27,20,4); 

int leftPin = 52;
int rightPin = 24;

int PIN_A = 2;
int PIN_B = 3;
int PIN_C = 4;
int PIN_D = 5;
int PIN_E = 6;  
int PIN_F = 7;
int PIN_G = 8;
int DL4 = 9;
int DL3 = 10;
int DL2 = 11;
int DL1 = 12;
int i = 0;
int j = 0;
int n = 0;
int k = 0;
int h = 0;



int Arduino_Pins[7] = {PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G};   // an array of pin numbers to which LEDs
int Segment_Pins[10][7] = {{0,0,0,0,0,0,1}, // 0
                           {1,0,0,1,1,1,1}, // 1
                           {0,0,1,0,0,1,0}, // 2
                           {0,0,0,0,1,1,0}, // 3
                           {1,0,0,1,1,0,0}, // 4
                           {0,1,0,0,1,0,0}, // 5
                           {0,1,0,0,0,0,0}, // 6
                           {0,0,0,1,1,1,1}, // 7
                           {0,0,0,0,0,0,0}, // 8
                           {0,0,0,1,1,0,0}, // 9
                           };
int DELAY = 1;
int TIME = 0;
void digit4()
{
    digitalWrite(DL1,1);
    digitalWrite(DL2,0);
    digitalWrite(DL3,0);
    digitalWrite(DL4,0);
    for(j=0;j<7;j++)  {      
      digitalWrite(Arduino_Pins[j],Segment_Pins[i][j]);      
    }
    delay(DELAY);    
}
//=============================
void digit3()
{
    digitalWrite(DL1,0); // change 1 to 0 and 0 to 1 (for 7segment anoth type)
    digitalWrite(DL2,1);
    digitalWrite(DL3,0);
    digitalWrite(DL4,0);
    for(j=0;j<7;j++)  
    {      
      digitalWrite(Arduino_Pins[j],Segment_Pins[n][j]);      
    } // end loop j   
    delay(DELAY);
}
//==============================
void digit2()
{
    digitalWrite(DL1,0);
    digitalWrite(DL2,0);
    digitalWrite(DL3,1);
    digitalWrite(DL4,0);
        for(j=0;j<7;j++)
        {          
          digitalWrite(Arduino_Pins[j],Segment_Pins[k][j]);          
        } // end loop k
        delay(DELAY);
}
//==============================
void digit1()
{
    digitalWrite(DL1,0);
    digitalWrite(DL2,0);
    digitalWrite(DL3,0);
    digitalWrite(DL4,1);
        for(j=0;j<7;j++)
        {          
          digitalWrite(Arduino_Pins[j],Segment_Pins[h][j]);          
        } // end loop j
        delay(DELAY);
}
void setup(){

  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

  pinMode(leftPin, INPUT);
  pinMode(rightPin, INPUT);
  Serial.begin(9600);

  //LCD Monitor
  lcd.init();                     
  lcd.init();
  lcd.backlight();
  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"); 

  //4digits 7 segemnt display
  pinMode(PIN_A, OUTPUT);
  pinMode(PIN_B, OUTPUT);
  pinMode(PIN_C, OUTPUT);
  pinMode(PIN_D, OUTPUT);
  pinMode(PIN_E, OUTPUT);
  pinMode(PIN_F, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(DL1, OUTPUT);
  pinMode(DL2, OUTPUT);
  pinMode(DL3, OUTPUT);
  pinMode(DL4, OUTPUT);

}
void loop(){

//RGB light
  for (int i = 0; i <= 29; i++) {
    leds[i] = CRGB ( 255, 0, 5 );
    FastLED.show();
    delay(40);
  }
  for (int i = 29; i >= 0; i--) {
    leds[i] = CRGB ( 0, 0, 0);
    FastLED.show();
    delay(40);
  }

//Switch
  int L = digitalRead(leftPin);
  int R = digitalRead(rightPin);
  
  if(L == HIGH){
    Serial.print("LEFT");
    delay(250); 
    } 
  if(R == HIGH){
    Serial.print("RIGHT");
    delay(250);
    }
  

  //4 digit 7 segment display
  //Count up
  for(i=0;i<10;i++) 
  {
    for(TIME=0;TIME<=250;TIME++)// Simulation (TIME=0;TIME<=250;TIME++)
    {
    digit4();   
    digit3();
    digit2();
    digit1();
    //delay(1);
    }    
    // Condition Count Up
        
            if(i==9) 
            {
              n++;
             
               if(n>9)
               {
                 k++;
                 
                  if(k>9)
                   {
                     h++;
                     
                      if(h>9)
                       {
                         h = 0;
                       } 
                   } 
                    
                     if(k>9)
                      {
                       k = 0;
                      }
               } 
               
              if(n>9) 
              {
                n = 0;
              } 
            }          
         
    } 
    
 } 

with delays , then what would you expect? :wink:

30 x 40ms x 2 = ?

This is not an installation issue - please ask for this topic to be moved.

1 Like

I took your advise and remove the delay. But the message do not pop up as i press the switch.

I have deleted your other cross-post @aaronho020409.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please take some time to pick the forum category that best suits the subject of your question and then only post once to that forum category. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

@aaronho020409, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project.

How are the buttons wired? Post a little schematics. Pen and paper would be fine.

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