I need to blink a led X times without delay by a pushbutton signal?
const int ledPin = 13;
int ledState = LOW;
int pushBut = 4;
unsigned long previousMillis = 0;
const long interval = 100;
int pushButState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(pushBut, INPUT);
}
void loop()
{
pushButState = digitalRead(pushBut);
{
unsigned long currentMillis = millis();
if(pushButState == HIGH && currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}
if(pushButState =LOW){
digitalWrite(ledPin,LOW);
}
}
Thanks for helping and explaining.
if(pushButState =LOW)
use two == in an if statement.
You are not counting how many times the LED is turned on therefore you are not controlling the number of times you blink.
What is with the unnecessary { and subsequent } in the loop function. ( second use of { )
if(pushButState == HIGH
So you only want to flash the LEDs while the button is pressed irrespective of if the correct number of flashes have been made?
No, I only want to flash the led X (1, 2 or 3....) times immediately when the button is giving a short HIGH pulse.
Then you want a counter and a boolean latch.
If (button is HIGH OR Latch is TRUE) AND Millis value is met
Then make Latch true.
Count up.
Do some stuff.
If Count is greater than a number
Then make latch false.
CaptainPicar3d:
No, I only want to flash the led X (1, 2 or 3....) times immediately when the button is giving a short HIGH pulse.
Yes I know but that is not what you have written. The code will only do what you tell it to do.
By explaining what you have done I was hoping you could see that and learn.
Thanks, for explaining, I know, it was a base to show what i already got.
I was already very happy that i can control the blinking with the pushbutton.
Maybe the base is totally wrong to build further on, thats why.
So, its something whit a counter, counting up++ and down--, but how?
Every time you turn the LED on increment the counter. Then include the counter's value in the if statement along with the millis timer value to see if you need to turn it on again.
Need help,
a lot is wrong,
-the counter keeps counting when the button is pressed,
-"if (buttonState != lastButtonState)" is not functioning for some reason
...
unsigned long interval=200; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
const int buttonPin = 4; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
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
int ledState = LOW;
void setup() {
digitalWrite(13, LOW);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState){
if(buttonState == HIGH){
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}else {
Serial.println("off");
}
}
if (buttonPushCounter >= 1){
if ((unsigned long)(millis() - previousMillis) >= interval) {
previousMillis = millis();
digitalWrite(ledPin, !digitalRead(ledPin));
}else
{
digitalWrite(ledPin, LOW);
}
}
if (ledState == HIGH){
buttonPushCounter--;
}
}
Thanks
Where do you update the lastButtinState variable? It should be the last thing you do in the loop function.
Nice, of course,
thanks, Grumpy_Mike, very useful.
I still don't understand 100% of what i am doing but it works, without delay, like i want to.
After a lot of trail and error i got this:
unsigned long interval=100; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
const int buttonPin = 4; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
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
int ledState = LOW;
void setup() {
digitalWrite(13, LOW);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
ledState = digitalRead(ledPin);
if (buttonState != lastButtonState){
if(buttonState == HIGH){
buttonPushCounter = buttonPushCounter +1;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}else {
Serial.println("off");
}
}
lastButtonState = buttonState;
if (buttonPushCounter >= 1){
if ((unsigned long)(millis() - previousMillis) >= interval) {
previousMillis = millis();
digitalWrite(ledPin, !digitalRead(ledPin));
if (ledState == HIGH){
buttonPushCounter = buttonPushCounter -1;
}
}
}
}
If there are any strange, stupid things in this code,
please let me know.