i'm tying to make a game and the rules are simple : the light goes from one led to another (there are 10 LED, 8 are green and one is red at each end) there are 2 buttons, one for each player. When you press the button when your LED is on you get a point and the first player to get 5 points wins the game (i'm still working on the counter and any help will be appreciated).
My problem is that i can't get the code to work on one side when the light goes from left to right so i can try it one the other side.
int led[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; //to intailaize the led to pin
int ledState; //to get the led state if it is HIGH or LOW
int button1 = 2; //button for player 1
int button2 = 13; //button for player 2
int P1 = 0, P2 = 0; //dealing with buttons
int P1_counter = 0 , P2_counter = 0; //score counter
const int Del = 500;
void setup() {
for (int i = 0; i <= 10; i++) {
pinMode(led[i], OUTPUT);
}
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop() {
P1 = digitalRead(button1);
digitalWrite(led[1], ledState);
for (int i = 0; i <= 10; i++) { //light goes from right to left
digitalWrite(led[i], HIGH);
delay(Del);
digitalWrite(led[1], ledState);
/*if the player press the button while the led is on, the players gets a point
and the leds on his side blink to indicate that of the new point*/
if (P1 == HIGH && ledState == HIGH ) {
P1_counter = P1_counter + 1;
digitalWrite(led[3], HIGH);
digitalWrite(led[4], HIGH);
}
//to return the state of the led to low again
ledState = LOW;
digitalWrite(led[i], LOW);
}
for (int i = 10; i >= 0; i--) { //light return from left to right
digitalWrite(led[i], HIGH);
delay(Del);
digitalWrite(led[i], LOW);
}
}
Welcome to the forum.
++Karma; // For using code tags correctly on your first post.
You have defined your button inputs as 'INPUT', do you have pull up / pull down resistors? You need them otherwise when your button is not pressed the input will not be in any particular state and will read nonsense. Ideally put the buttons between the input and 0V and use INPUT_PULLUP
You are using delay inside your for loops, that means that for almost all the time in the for loop nothing is happening, including the buttons are not being tested to see if they have been pressed. You must learn to stop using delay, it just stops anything else from happening.
Thank you for the replay, as PerryBebbington have told me , I have changed my code and removed the delay() and used milllis() instead of it although it took from me time to understand it but it was wroth the effort.
My problem now is that the LED doesn't go from right to left, the first led only keeps blinking and nothing happens, I used serial monitor to show me what the program is doing and it keeps showing me "end of if" which means that there is no increment in the loop !
int led[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; //to intailaize the led to pin
unsigned long currentTime; //some global variables available anywhere in the program
const unsigned long Interval = 1000; //time for each loop/blink
unsigned long previousTime = 0; //time that changes with every successive loop
bool ledState; //record the state of LED for each player
int button1 = 2; //button for player 1
int button2 = 13; //button for player 2
int P1 = 0, P2 = 0; //dealing with buttons
int P1_counter = 0 , P2_counter = 0; //score counter
void setup() {
Serial.begin(9600);
for (int i = 0; i <= 9; i++) {
pinMode(led[i], OUTPUT);
}
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop() {
P1 = digitalRead(button1);
digitalWrite(led[1], ledState);
currentTime = millis();
//light goes from right to left
for (int i = 0; i <= 9; i++) {
Serial.println("before");
if (currentTime - previousTime >= Interval) { //false until the period elapses
digitalWrite(led[i], !digitalRead(led[i]));
previousTime = currentTime;
}
Serial.println("end of if");
}
/* if (P1 == HIGH && ledState == HIGH ) {
P1_counter = P1_counter + 1;
digitalWrite(led[3], HIGH);
delay(500);
digitalWrite(led[4], HIGH);
P1 == LOW;
}
//light goes from left to right
for (int i = 9; i >= 0; i--) {
digitalWrite(led[i], HIGH);
delay(Del);
digitalWrite(led[i], LOW);
}
*/
}
So how should i make the light go from one led to another ?
I mean the first led turns on then wait for 500 mills then turns off and the second led turns on and so on
//to intailaize the led to pin
const byte led[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
const byte button1 = 2; //button for player 1
const byte button2 = 13; //button for player 2
byte counter;
//Timing stuff
unsigned long previousTime = 0; //time that changes with every successive loop
const unsigned long Interval = 500; //time for each loop/blink
bool ledState; //record the state of LED for each player
int P1 = 0, P2 = 0; //dealing with buttons
int P1_counter = 0;
int P2_counter = 0; //score counter
//********************************************************************************
void setup()
{
Serial.begin(9600);
//*********************
for (byte i = 0; i < 10; i++)
{
pinMode(led[i], OUTPUT);
digitalWrite(led[i], LOW);
}
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
} //END of setup()
//********************************************************************************
void loop()
{
moveLED();
//**************************************
// Other non blocking code goes here
//**************************************
} //END of loop()
//********************************************************************************
void moveLED()
{
//*********************
if (millis() - previousTime >= Interval)
{
previousTime = millis();
digitalWrite(led[counter], !digitalRead(led[counter]));
//*********************
counter++;
if (counter >= 10)
{
counter = 0;
}
}
} //END of moveLED()
//********************************************************************************