Hello, I am starting a new project. I plan to make a K2SO droid head from StarWars. I want the eyes to move and flash to act like it real and broken in a way. Right now I am trying to figure out how to write my code. So far I have it where once when I turn it on the led flashes a bit and then stays on like it starts up/wakes up. from there I want the LED to flash fast, pulse, dim down and back up, and maybe a couple of other variations of these randomly. I'm not sure how I want to go about this. I just need to be able to code these patterns and then have a loop where it chooses between these patterns randomly. This is what I have so far It's not pretty but it works for the wake-up part of this project.
I am pretty new to coding but am determined so leading me to different forums will help a ton or if you could explain your reasoning would be amazing.
-Vlassov
const byte ledPin = 2;
byte flashCount = 0;
byte state = 0;
unsigned long waitStart;
unsigned long shortPeriod = 45;
unsigned long longPeriod = 5000;
unsigned long currentTime;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
waitStart = millis();
}
void loop() {
switch (state) {
case 0:
currentTime = millis();
if (currentTime - waitStart >= shortPeriod) {
digitalWrite(ledPin, !digitalRead(ledPin));
waitStart = currentTime;
flashCount++;
if (flashCount == 6) {
state = 1;
}
}
break;
case 1:
currentTime = millis();
if (currentTime - waitStart >= longPeriod) {
waitStart = currentTime;
flashCount = 0;
state = 1;
}
break;
}
}