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;
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++.
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
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.
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.
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?
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.
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);
}
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.
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.