Hi!
I am new here, I would like to seek some advice on how to solve the following problem:
I would like to have cca. 7 LED. Each led has an assigned switch.
The action is:
To have a sequence, that lights up each LED after one another, but when any switch is pressed (one at a time, so the one that was pressed "faster") the sequence should pause, the assigned number of LED should light up as long as the button is pressed, when the button is released, the sequence should continue from where it was paused.
LEDs should go ON and OFF dimmed, so from 0-100% in let's say 0,5 sec, 1 sec ON, then 100-0% in 0,5sec.
LED1 goes on for 2 sec,
LED1 goes off,
delay 0,1sec,
LED2 goes on for 2 sec,
LED2 goes off,
delay 0,1sec,
and so on...
There is no code yet that I could post, only some very basic idea that lacks the main solution for the problem.
I do appreciate help of any kind!
I think you could use any Arduino board, including UNO like @anantmohit already said.
I don't know if the fact you're saying you're "new here" means you have some kinda experience with Arduino and/or programming. If you don't have such experience I think you need to start learning a bit about Arduino before, together with at least a basic knowledge of digital electronics (limited to pushbuttons and LEDs just for now) and the C programming language.
If you already know enough about that, my personal advice is to use a simulator like Wokwi, so you can just test the wirings (you can draw your circuit with an Arduino and all the other elements needed) and the code without even buying the hardware. when the simulation looks good to you, you can start putting your hands on the real hardware to build a prototype.
I have some experience with Arduino, I am 99% sure about wiring this exact circuit.
I have started the code by defining pin layout, and a basic sequence that lights up the LEDs, but
I am stuck figuring out which command/s in which order will give the required result at the end.
Nice. So, post your current code (even if it's "a very basic idea") and we'll take a look at it.
But we also need yo to draw your current wiring an post it here (as I said before, IMHO the best way to do it is using Wokwi, where you can also test your code, and share here the whole project).
In the meantime I have learned that the delay function is unusable for this, as it stops the program as long as it lasts.
I have found the "blink without delay" sketch, which made me a bit more confused.
I'm figuring out the Wokwi app, as soon as I managed to model the wiring I'll post it.
const int LED1 = 13;
const int LED2 = 12;
const int LED3 = 11;
const int buttonPin1 = 10;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// put your setup code here, to run once:
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(buttonPin1, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED1, HIGH); //turn on led 1
delay(2000); //keep led on for 2sec
digitalWrite(LED1, LOW); // turn off led1
delay(100); //wait 0,1 sec
digitalWrite(LED2, HIGH); //turn led2 on
delay(2000); //keep led2 on for 2 sec
digitalWrite(LED2, LOW); //turn led2 off
delay(100); //wait 0,1 sec
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(LedPin1, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}
I understand being confused, I get confused, too with "millis()". There is no "right" way to do this, but some methods are preferred. The more you tinker with it, the more you will understand. Here is my spin on "blink without delay" that you can plug into WOKWI.COM for testing. The LED_BUILTIN blinks all the time UTIL the button is pressed, and the LED pauses in the state it was in UNTIL the button is released. The Serial Monitor also shows the state of the LED.
sketch.ino
unsigned long newMillis = 0, oldMillis = 0; // event timers
unsigned long interval = 500; // milliseconds
bool state; // the state of the LED
byte buttonPin = 2; // button pin
void setup() {
Serial.begin(115200); // configure Serial Monitor baud
pinMode(LED_BUILTIN, OUTPUT); // configure LED_BUILTIN/DIO pin 13 as OUTPUT
pinMode(buttonPin, INPUT_PULLUP); // configure button pin as INPUT with internal pullup resistor
}
void loop() {
blink(); // call the "blink" function
}
void blink() {
newMillis = millis(); // start an event
if (newMillis - oldMillis > interval) { // compare difference in event times to interval
oldMillis = newMillis; // store the new event start time
state = !state; // flip the current state for the LED
digitalWrite(LED_BUILTIN, state); // set the LED to the current state
if (state) Serial.print("1"); // show current state in Serial Monitor
else Serial.print("0"); // show current state in Serial Monitor
}
while (!digitalRead(buttonPin)) {} // hold here while button is pressed
}