I am trying to write code which prints me time between 2 pulses (push button). I do not know how to write correct the code in a loop who prints only one value (not all the same values).
const int buttonPin = 2;
int button_statuss;
unsigned long start_time;
unsigned long final_time;
unsigned long elapsed_time;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
button_statuss = digitalRead(buttonPin);
if(button_statuss == LOW){
final_time = millis();
elapsed_time = final_time - start_time;
}else{
start_time = millis();
Serial.println(elapsed_time/100);
}
delay(button_statuss == HIGH);
}
I want to make a basic bicycle speedometer (I want to write all code by myself). So I need get time between reed switch is ON. I am new in Arduino, but this things I really like! I want to learn something new.
EDIT:
I think I solved it! delay(100);
My finger isn't so fast to push button in 50 millisecounds!
My reed switch sepc:
Action time 1.0ms
Bounce time 0.6ms
Release Time 0.4ms
Response frequency 4000Hz
Maximum operating frequency 500Hz
You want to look at the state change detection example. The time that the pin IS high is irrelevant. The time that the pin BECOMES high is what matters. Similarly, the time that the pin BECOMES low is of interest. The time the pin IS low matters not a whit.