Hi everyone, im actually getting my first project to work and it's time to start on the next phase
So the image is what I have right now, it's a simple setup, a light strip and an arduino nano every that I just load on a program that animates some lights when plugged in
The next step I'd like to take is to set it up so that when a button is pressed the lights animate for a 10-20 seconds. I know it seems a little silly, but it being a button is important
Most of the tutorials I've seen so far have it so that the button completes the circuit so the code only runs when the button is compressed, is there a way that I can press it once and have it play for a bit? Im really new to the hardware side of things so im not totally sure what even to Google, so any tutorials would be a massive help!
The first rule when asking advice for code, is to post your existing attempt in CODE tags) so we can see where changes may be added,
For any requests about hardware design, it’s also advisable to put together an annotated schematic diagram, so we can see what you’ve done - and you’ll be able to fix it in the future !
Yes, when the switch is closed, you enable and start a TIMER.
As long as the TIMER is enabled, the LED animation runs.
When the TIMER expires, you stop the animation.
Very much a newbie, so im gonna ask a newbie question. Is it because of the length of the light strip or is it because of the uninsulated wires? The video I was watching had a similar length of light strip so I didn't think it was an issue, and the raw wires were because I got ahead of myself and forgot to do the last step
Fully intend to, just got ahead of myself because I didn't think i was going to get it to work, thanks for the advice. I have shrink tubing, but the wires that I have are getting removed for a different type of input once I'm done prototyping, but by the reactions to this I'm assuming what I did was a big no-no
Definitely gonna insulate them! I'll actually be changing them out for different wires which is why my overly hasty self never put the shrink tubing on it
Got it, thank you! The dude on the tutorial definitely had some kind of different setup than I did, so I'm guessing he had something that would actually make it work. I'll keep it to 10 LEDs for testing. Thankfully the code I have set up randomly sets off roughly a third of of the LEDs for a second so there was never all the LEDs running at once, so hopefully no harm done, thanks for saving a USB port haha
File > Examples > 02. Digital > StateChangeDetection
should get you on the right path. Figure out what states you want (maybe just two in general: LEDs off and timed LED routines) then create a variable such as a byte, where byte controlByte = 0; is the do nothing at all state, then detect the state change of the pushbutton to increment the state to say, 1, which calls the LED routine, then detect the next state change of the pushbutton that sets controlByte back to zero, or whatever you like, such as setting the state of controlByte to one but only for 10 seconds, then back to zero after the timer runs out, as others have mentioned.
Consider the following sketch, no hardware required. Just set Serial Monitor to 115200 baud.
Thanks again to forum member @cattledog who posted the countdown thing I couldn't wrap my head around for ages and now refer to often. I think it's similar to what you're after. OG assistance repackaged to help some other forum member, IIRC, and I saved it so here it is. Hope it helps.
/* FINALLY! a dude that gets it. Not the same toggle the light forever thang.
credit whare it's due, cattledog
https://forum.arduino.cc/t/countdown-timer-without-delay/680769
*/
const int ledPin = LED_BUILTIN; // the number of the LED pin
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
int timer = 10; // Set the timer to 10 seconds
boolean bombArmed;
boolean gotTheMessage;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
digitalWrite(ledPin, LOW);
bombArmed = false;
gotTheMessage = false;
Serial.println("\nArm bomb by typing a. Disarm by typing d.");
Serial.println("Typing r resets clock to 10 seconds.\n");
}
void loop() {
if (Serial.available() > 0) {
char setBomb = Serial.read();
switch (setBomb) {
case 'a':
if (gotTheMessage == false) {
Serial.println("\nBomb is armed - do something!\n");
delay(1000);
gotTheMessage = true;
}
bombArmed = true;
break;
case 'd':
bombArmed = false;
Serial.println("\ndisarmed");
break;
case 'r':
Serial.println("\nreset clock");
timer = 10;
break;
}
}
if (bombArmed == true) {
theFinalCountdown();
}
digitalWrite(ledPin, LOW);
}
void theFinalCountdown() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
digitalWrite(ledPin, HIGH);
previousMillis = currentMillis;
Serial.println(timer);
timer--; //decrease timer count
if (timer < 0) {
Serial.println("\nBOOM!");
digitalWrite(ledPin, HIGH);
//while (true); // uncomment for one shot action
while (millis() < previousMillis + (interval * 5)) {
// do a whole lotta nothin'
}
timer = 10;
bombArmed = false;
gotTheMessage = false;
}
}
}