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

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.

sort by: active | newest | oldest
kenyer3 years ago

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 :)

iceng3 years ago

Ever used a DO WHILE loop.

neofral (author) 3 years ago

solved it :)

thank you all for your help :)

neofral (author) 3 years ago

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.

Sound about right?

neofral (author)  mpilchfamily3 years ago

almost :)

i have 2 switches and using the pull_up method i can see in position they are. if switch 1 position 1 than 1 times LED, position 3 should be 3 times LED.

int green = g1 + 2*g2 + 3*g3;

int red = r1 + 2*r2 + 3*r3;

Use variables to track it all. Designate one variable to track one button and the other to rack the other. So when the arduino detects button1 is press it adds 1 to button1's variable and so on. Then check the variable. If the variable is less than x then light the green LED. If the variable is greater than x and less than y turn the green off and light the red LED.