Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu. [code]Paste your sketch here[/code]
EDIT
Have a ‘flag’ variable for each LED in the game.
When a LED is on and the switch is pressed, set the associated ‘flag’ to ‘true’ and increment a ‘counter’.
When a ‘flag’ is ‘true’, the LED it is attached to is left turned ON.
When your ‘counter’ reaches a maximum value flash all the LEDs.
Without seeing your code I cannot comment on what you've done but can say you are probably in need of a basic data structure to hold the value of each LED, which is either steady on or not steady on (toggling).
Check out the example that comes with the IDE at File=>Examples=>Control=>Arrays.
If you keep each LED's status in an array then you will know whether an individual LED is to be kept always on or toggled during each pass of the cycle. You can also count how may lLEDs have been set to steady on by examining the array.
I wrote a demo program showing how an array might be used for your project. If you like, upload it to your board and open the serial monitor. It uses keystrokes at the serial monitor to simulate button presses. Each time through loop() if a character is ready at the serial monitor it sets the current LED in the cycle to 1 - steady on, shows the current LED pattern and counts how many lights are steady on.
int leds[] = {0, 0, 0, 0};
int ledIdx = 0;
void setup() {
Serial.begin(9600);
Serial.println("Enter a character");
}
void loop() {
if ( Serial.available() ) /* simulate button press */
{
while ( Serial.available() ) Serial.read(); /* simulate button press */
leds[ledIdx] = 1;
Serial.print("Pattern: ");
for(int i=0; i < 4; i++)
{
if( leds[i] == 1 )
{
Serial.print(" ON ");
} else {
Serial.print(" TOGGLE ");
}
}
Serial.println();
int totalOn = countOn();
Serial.print("Leds on: ");
Serial.println(totalOn);
Serial.println("Enter a character");
}
ledIdx = ledIdx + 1;
if ( ledIdx > 3 ) ledIdx = 0;
}
int countOn()
{
int retVal = 0;
for (int i = 0; i < 4; i++ )
{
retVal = retVal + leds[i];
}
return (retVal);
}
Sorry for taking so long to reply. this is what i got. Ive solved my issue but now there's another problem: how can I replace the delay() function and still get a delay? I nees to count the total amount of clicks and the delay() does not help.
int play_button = 11, reset_button = 12;
int wait_time = 500; // em milissegundos
int end_animation_time = 5; // em segundos
int count = 0; // contador do nº total de clicks
unsigned long start_millis;
unsigned long current_millis;
void setup() {
Serial.begin(9600);
// conversao de s para ms
end_animation_time = end_animation_time * 1000;
// Defenir os pins para os LEDs como Output
for (int i = 2; i <= 10; i++) {
pinMode(i, OUTPUT);
}
// Defenir os pins para os butões como Input
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
long timestamp=millis();
}
void loop() {
unsigned long current_time = 0, previous_time = 0;
int count_victory = 0;
for (int i = 2; i <= 10; i++) {
if (!digitalRead(reset_button) == LOW) {
if (digitalRead(i) == LOW) {
digitalWrite(i, HIGH);
delay(wait_time);
//my_delay(wait_time);
if (!digitalRead(play_button) == HIGH) {
digitalWrite(i, HIGH);
count_clicks();
}
else digitalWrite(i, LOW);
}
else {
delay(wait_time);
//my_delay(wait_time);
}
if (digitalRead(i) == HIGH)
count_victory ++;
} else { // else do if do reset(1º if)
reset();
i = 2;
}
} // fim do ciclo for
if (count_victory == 9) {
end_animation(end_animation_time, count_clicks());
reset();
}
}
void end_animation(int duration, int count) {
Serial.println(count);
unsigned long current_time = millis();
unsigned long next_time = millis();
while ((next_time - current_time) <= duration) {
next_time = millis();
for (int i = 2; i <= 10; i++) {
digitalWrite(i, LOW);
}
delay(300);
for (int i = 2; i <= 10; i++) {
digitalWrite(i, HIGH);
}
delay(300);
}
}
void reset() {
// para dar reset ao numero total de clicks quando se dá reset
count = 0;
//Para quando der reset acender o led no pin 2
digitalWrite(2, HIGH);
delay(wait_time);
digitalWrite(2, LOW);
// para apagar os leds todos
for (int i = 2; i <= 10; i++) {
digitalWrite(i, LOW);
}
}
int count_clicks() {
if (!play_button == LOW) {
count++;
delay(100);
}
return count;
}
you should create an array that holds the on/off state of each light. this way it's easy to check if they are all on. Then you simply write to the pins according to the array.
to check if they are all on you check the array with a loop.
I put all the pin numbers in an array too so that you can turn them all on or off with a loop to create a flash effect at the end.
// put pin numbers here
int pins [] = {2,3,4,5,6,7,8,9,10,11};
bool lights [10];
int c;
int i;
unsigned long last;
byte sw;
bool winner;
void setup() {
Serial.begin(9600);
i = 10;
while(i>0){i--;pinMode(i,OUTPUT);}
}
void loop() {
// simulate button press
if(Serial.available()){ Serial.read();
pressed();
}
// create timer
if(millis()-last>200){last = millis();
c++;if(c>9){c=0;} Serial.println("");
if(!winner){
// loop through all ten lights
i = 10;while(i>0){i--;
if(lights[i]||i==c){
analogWrite(pins[i],255);
Serial.print("+");
}
else{
analogWrite(pins[i],0);
Serial.print("-");
} }}
else{flash();} }
}
void flash(){
if(sw==0){sw=255;}else{sw=0;}
i = 10;while(i>0){i--;
analogWrite(pins[i],sw);
if(sw){Serial.print("+");}else{Serial.print("-");}
}
}
void pressed(){
lights[c] = true;
//check for a light still off
i = 10; while(i>0){i--;
if(!lights[i]){i=-5; } }
if(i>-5){winner = true;
}
}