hello guys,
i am working with a switch hall effect, in my script i put a counter to count everytime the switch has changed status, so i need please to know how to get the time that it took between a changed state and another?
here is my script :
const int hallPin = 2; // the number of the hall effect sensor pin
const int ledPin = 13; // the number of the LED pin
boolean lastHallState = HIGH; // pulled up with a resistor
boolean currentHallState = HIGH;
boolean ledOn = false;
// variables will change:
int hallState = 0; // number of rounds
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the hall effect sensor pin as an input:
pinMode(hallPin, INPUT);
}
//Halldebounce function
boolean debounceHall(boolean last)
{
boolean current = digitalRead(hallPin);
if(last != current)
{
delay(5);
current = digitalRead(hallPin);
}
return current;
}
void loop(){
currentHallState = debounceHall(lastHallState);
if(lastHallState == HIGH && currentHallState == LOW)
{
hallState++; // rounds++
}
lastHallState = currentHallState;
Serial.println(hallState);
}