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;
}
}
}
}