Hello everybody,
I tried some basic stuff with arduino, and I wonder how long the led2 flashes after the button is pushed, it seems like extremely short time, but I don't know exactly how long it is?
Here is the code:
const int buttonPin = 2;
const int led2 = 13;
const int led1 = 10;
Led2 will stay on while the button is pressed by the look of it - not a specific time. You'll probably be getting some contact bounce too, but it's not likely to be human visible.
yes, the 1. output is led1 , when I tried with only led 2, then the led 2 glows exactly the same time the button is pushed, but with led 1 and 2 it glows for extremely short time independently on the time I hold the button. Is there any way I can measure that time?
Thx for the timer, I put the timer after the led 2 and the problem is that it's time constantly increases. When I push the button, then it shows the time in serial monitor. What can I change to get only the led 2 flashing time ?
unsigned long time;
const int buttonPin = 2;
const int led2 = 13;
const int led1 = 10;
int buttonState = 0;
void setup() {
Serial.begin(9600);
pinMode(led2, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
time = millis();
Serial.println(time);
}
else {
digitalWrite(led2, LOW);
}
}
millis() returns the number of milliseconds since the program started. If you store it once, you can compare to that stored value to calculate time elapsed.