Hi All,
It's my first time posting, but I've been reading the forum for a few weeks. It's very clear that the quality of advice is dependent on the quality of the information given, so I will try to be thorough and stick to the guidelines.
The Build:
I'm taking Mark Rober's online creative engineering course, and I am on the electrical engineering build. I have to make something "art" related (a piece of art, a machine that makes art, a machine that buys art on ebay, something like that). I came up with an idea I am calling the HoloGIF. It is a machine that displays a holographic 2D animation, 5 frames long. There are 5 acrylic slides, each one positioned above a row of LED’s that light in sequence. Each slide has 1 frame of an animation etched into it that lights up when the LED’s below are on. Here is a link to a video of the prototype: Scotty (he/him) on Instagram: "Now that my proof of concept is complete, I took the time to try a more complicated image. I’m so happy with how this turned out at the prototype level. Going to make a more permanent container for all the wire bits and the Mk.1 HoloGIF will be complete. #berniesanders #diy #arduino #handy #artstuff"
The Hardware:
I’m running an Elegoo Nano v3, which I think is the equivalent of an Arduino Nano.
Digital Pins 2-6 are each controlling a bank of 8 LED’s through a transistor.
The LED’s are powered from the 5V pin and each has its own resistor.
A momentary switch is connected to digital pin 12 with a pullup resistor (I did my homework!)
The Code:
Since I want the slides to be interchangeable, and some animations look good at different speeds, and some will be reciprocal (like a bouncing ball) and some cyclical (like a wolf running), I want to be able to click a button to cycle through different light programs.
The Problem:
I can’t figure out how to make any blink-without-delay codes to work with variable timing. I’m really over my head. I can make a basic code where two LED’s blink at different intervals without DELAY, but I can’t figure out how to make it work with varying times and with multiple “programs”. Is the solution some type of blink array? I dunno. I’m sleepy. Thanks for your time and advice!
//disclosure - some of this code is mine, most is copied and pasted from tutorials
const int buttonPin = 12; //momentary switch for program selection
const int ledPin1 = 2; //I know there is a way to do ID all the LED's in a
const int ledPin2 = 3; //single line of code, but I got lazy and did'nt look it up
const int ledPin3 = 4;
const int ledPin4 = 5;
const int ledPin5 = 6;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter == 0)
{
digitalWrite(ledPin1, HIGH); //this is a code for my "bouncing ball" animation
digitalWrite(ledPin2, LOW); //it looks best when it speeds up near the bounce
digitalWrite(ledPin3, LOW); //and slows down at the apex. thats why the
digitalWrite(ledPin4, LOW); //changing values
digitalWrite(ledPin5, LOW); //it lights LED's in the order 1,2,3,4,5,4,3,2,
delay(180);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(130);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
delay(110);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, HIGH);
delay(100);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, HIGH);
delay(90);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin4, HIGH);
delay(100);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin3, HIGH);
delay(110);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin2, HIGH);
delay(130);
}
else if (buttonPushCounter == 1)
{
digitalWrite(ledPin1, HIGH); //this is the "cyclical animation" I use is for
digitalWrite(ledPin2, LOW); //the running wolf animation.
digitalWrite(ledPin3, LOW); //it lights the LED's in the order 1,2,3,4,5,
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
delay(150);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(150);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
delay(150);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, HIGH);
delay(150);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, HIGH);
delay(150);
}
else (buttonPushCounter) = 0; //this resets the push button count
}