Counting number of times a set of LED's flash

I just got an Arduino and some electronic components for the first time the other day. I watched a few Arduino IDE lectures, took notes, and began to assemble some basic projects with LED's. Furthermore, I'm curious how I can count the total number of LED flashes between 3 different LEDs for the entire amount of time I leave the program running. Thanks, I'll leave the code I wrote just for you guys to look at.

const int ledPin = 6;
const int ledPinG= 5;
const int ledPinR= 4;

void setup() {
}

void loop() {
int wait = random(50,1000);
digitalWrite(ledPinG, OUTPUT);
pinMode(ledPinG, HIGH);
delay(wait);
pinMode(ledPinG, LOW);
delay(wait);
digitalWrite(ledPin, OUTPUT);
pinMode(ledPin, HIGH);
delay(wait);
pinMode(ledPin, LOW);
delay(wait);
digitalWrite(ledPinR, OUTPUT);
pinMode(ledPinR, HIGH);
delay(wait);
pinMode(ledPinR, LOW);
delay(wait);
}

Hello nickolea
You should start with the BLINKWITHOUTDELAY example to be found in the IDE to gain the knowledge how to code.
Have a nice day and enjoy coding in C++.

You may want to visit the reference for the digitalWrite() function and the pinMode() function. You seem to have a mix-up.

It functions properly still, I see that I have made a mix-up. Thanks

Where should I look to find information on how to complete my desired action?

You should, except in rare cases, set pinMode in setup() as the mode should not change.

Use mills() for non-blocking timing.
Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

Do you want separate counters for each color or 1 counter that accumulates for the 3?

I want a cumulative counter. I think I understand the commands, I just got them mixed up. Likewise, I think the random blinking intervals are nicer to watch, also more entertaining.

I don't even know how to begin to make the counter

Make a variable called BlinkCount (or whatever you want) and initialized it to zero in setup().

Then every time through the loop,
BlinkCount = BlinkCount +1;

Or in "C++ style" the same thing can be done as,
BlinkCount++;

You can multiply that times 3 if you want the combined counts of all 3 LEDs.

You'll also need to "print" the count to the serial monitor so you can see it. See the Analog Read Serial Example.

Note that BlinkCount = BlinkCount +1; is an expression, NOT a equation. It doesn't make sense as an equation. :wink:

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

So if I use 3 LEDS, I'd just make it do +3 every time? Or could I make it do +1 after each blink of a different LED? I want to eventually put the number on an LCD

I would use a state change detection method to detect when each color is turned on, that is the transition from off to on. Each time that happens, increment your counter.

Okay, the website tried to warn me of it looking messy, but I said fuck it. I will post properly formatted code from now on.

Yes, this like what I want theoretically. If I watch a few lectures on State of Change Detection, should I have the knowledge I need to develop this code?

I think that it would help.

Great, I'm an avid fan of idle games too. I will also be able to count the number of times I press a button which seems like a cool idea too, especially if displayed on an LCD. Watching number go up is fun.

Here is my state change with active low inputs tutorial. May help.

I take it If and Else statements are a prerequisite?

I got the counter working, only it adds up all the values after the loop and sends them in a heap. A nice smooth counter would be a nice upgrade for it. I did it sorta lazily, and I also fixed the issues in some parts that I could see needed fixing.

I also tried to format the code for the website. Someone let me know if I did it right, I highlighted everything and pushed the button.

const int ledPin = 6;
const int ledPinG= 5;
const int ledPinR= 4;

int wait = random(50,1000);

 int LedBlinkC = 0;
  int LedState = 0;
  int LedState2 = 0;
  int LedState3 = 0;
  int LastLedState = 0;

void setup() {
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPinR, OUTPUT);
   Serial.begin(9600);
 
}

void loop() {
LedState = digitalRead(ledPinG);
LedState2 = digitalRead(ledPin);
LedState3 = digitalRead(ledPinR);

digitalWrite(ledPinG, HIGH);  
delay(wait);
digitalWrite(ledPinG, LOW);
delay(wait);
digitalWrite(ledPin, HIGH);
delay(wait);
digitalWrite(ledPin, LOW);
delay(wait);
digitalWrite(ledPinR, HIGH);
delay(wait);
digitalWrite(ledPinR, LOW);
delay(wait);

if (LedState != LastLedState); // Compares the current state of the LED to the previous
if (LedState == HIGH); // Checks If LedState switches to HIGH
LedBlinkC++; // adds +1 to the counter
Serial.println("on");
Serial.print("Blinks:");
Serial.println(LedBlinkC);


if (LedState2 != LastLedState);
if (LedState == HIGH);
LedBlinkC++;
Serial.println("on");
Serial.print("Blinks:");
Serial.println(LedBlinkC);


if (LedState3 != LastLedState);
if (LedState == HIGH);
LedBlinkC++;
Serial.println("on");
Serial.print("Blinks:");
Serial.println(LedBlinkC);
}

No if statement should end with a semicolon (;).

You need to enclose the instructions that will execute conditionally in curly brackets ({ }).

You need a last LED state variable for each LED.
You need to declare the last state variables static or global scope so they retain their values.

You need to record the current state into the last state variable each time.

static  bool LastLedState2  = 0;
if (LedState2 != LastLedState2)
{
    if (LedState2 == HIGH)
   {
        LedBlinkC++;
        Serial.println("on");
        Serial.print("Blinks:");
        Serial.println(LedBlinkC);
    }
    LastLedState2 = LedState2;
{

Here is my demo. I did not know exactly what you wanted to do so I set it up to light random LED(s) for random times and get a cumulative count of the times each goes for off to on. I leave the millis() timing to you.

const int ledPin = 6;
const int ledPinG = 5;
const int ledPinR = 4;

unsigned long count = 0;

void setup()
{
   Serial.begin(115200);
   pinMode(ledPin, OUTPUT);
   pinMode(ledPinG, OUTPUT);
   pinMode(ledPinR, OUTPUT);
}

void loop()
{
   int wait = random(50, 1000);
   int color = random(2);  // 0 or 1
   
   digitalWrite(ledPinG, color);
   checkCount();
   delay(wait);

   digitalWrite(ledPin, color);
   checkCount();
   delay(wait);

   digitalWrite(ledPinR, color);
   checkCount();
   delay(wait);

   Serial.print("count = ");
   Serial.println(count);
}

void checkCount()
{
   static bool ledPinLastState = 0;
   static bool ledPinRLastState = 0;
   static bool ledPinGLastState = 0;

   bool ledPinState = digitalRead(ledPin);
   if ( ledPinState != ledPinLastState)
   {
      if (ledPinState == HIGH)
      {
         count ++;
      }
      ledPinLastState = ledPinState;
   }

   bool ledPinRState = digitalRead(ledPinR);
   if ( ledPinRState != ledPinRLastState)
   {
      if (ledPinRState == HIGH)
      {
         count ++;
      }
      ledPinRLastState = ledPinRState;
   }

   bool ledPinGState = digitalRead(ledPinG);
   if ( ledPinGState != ledPinGLastState)
   {
      if (ledPinGState == HIGH)
      {
         count ++;
      }
      ledPinGLastState = ledPinGState;  
   }
}

I watched a video on if and else statements and I understand them better now. Thank you, currently working on displaying my constantly changing variable on the screen, and I upgraded it to make the count tick up when (LedR != HIGH) right after it turns on, so it counts up 1.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.