I want to make an Led blink for 1 second, and then turns off for another second.
The blink without delay sketch has already explained this one and there is no problem.
But I want the second led to blink for 100ms and turn off for another 100ms 3 times, BUT only upon pressing a button.
I have tried SO many times but kept failing
Any ideas?
Do you mean a button press starts it? How do you stop it?
Or do you mean while a button is pressed? If you impress the button, does the LED go off immediately even if it has been on only a fraction of the time it would stay on to blink?
My real project is actually making an rc car with a turret that is controlled with a motor
But I only need to understand how to make that delay to actually make the turret shoot while the car is moving
int ledState = LOW;
int extraDelay = 0;
char flag;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup(){
Serial.begin(9600);
}
void loop(){
unsigned long currentMillis = millis();
unsigned long shootMillis = millis();
flag = Serial.read();
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
Serial.println("LED_1 ON");
}
else {
ledState = LOW;
Serial.println("LED_1 OFF");
}
}
if (flag == 'A'){
for (shootMillis; shootMillis>=interval; shootMillis - interval){
extraDelay = shootMillis;
}
}
}
That is as far as I could write before my brain stopped mathing after the for function
I don't even know if the for function actually does what I think it do.
Now we know that the blink without delay runs every 1 second.
But I may not always press the button at the exact second the script runs.
So lets say I clicked the button after 2600 milliseconds from the arduino start time.
And that's where my brain stopped working.
There is 400 millisecond that I have no idea what to do with.
Should I add it to the next Blink Without Delay script?
Or should I subtract it?
After blinking 3 times it should automatically stop
The blinking should not start again if I pressed the button again while its blinking.
It should finish blinking 3 times then be ready to blink again when the button is pressed.
Thanks a lot @LarryD@gcjr@KawasakiZx10r for helping me with this.
I connected the hardware using a breadboard and the code @LarryD made work.
I couldn't check the other codes but I am sure that they will be more than helpful.
There is a lot of things I have to learn, thanks for helping <3