1955Views7Replies

# how to count 2 consecutive counting loops with arduino while limiting each count? Answered

this is part of a larger project but i'm stuck on the code for the loop. to make the situation simpler lat's say:
i'm trying to count the number of button pushes and light 2 different LEDs.
let's say green LED for x times, after that the red LED y times and go on repetitively. in the void loop i would like to set the amount of pushes because they are variables set within the loop function and reading different inputs.

Tags:

The forums are retiring in 2021 and are now closed for new topics and comments.

would a simple counter work?:

if (button is pushed) {counter++;}

if (counter > theAmountYouWant){digitalWrite(greenLed, HIGH);}

something like that.

"button is pushed" should be changed for code :)

Ever used a DO WHILE loop.

solved it :)

thank you all for your help :)

thanks for your answers. i have gone through all examples in the arduino IDE. there is a simple counting example that works very well and also has the option to turn the LED on at each 4th count. but in my case i want to use 1 button and 2 LEDs. i managed to light 1 LED using 1 counter. however, the things get complicated when i want to use 1 button and have 2 countings. this is because i have other switches that i use to tell arduino how many counts to light the GREEN and RED respectively. at the void loop im reading the pin inputs and set these variables and here i get lost

Set the variables in the setup not the loop. Start by showing use your setup. What is where?

As i understand it you have multiple switches set up. Tell me if this sounds about right.

Lets say you have 3 switches. Switch 1 is 5 turns, switch 2 is 10 turns and switch 3 is 15 turns. So in the loop you check the state of each switch. If it detects switch 1 it stores 5 to a variable. Lets call this variable count. If it detects switch2 than it stores 10 to count and so on.

In settup you will of course int count = 0 to start it off. You'll also have a variable established to track the button presses. Lets call this variable track.

so back to the loop. after we have checked the switches we want to check the button. If the button gets pressed we store 1 to track. Then we want to compare track and count and decide what will happen. If track is less than count we turn on the green LED. Else we turn the green LED off and the red LED on.