ARDUINO CODE
/*
* Bicycle Reed Switch Interface
*
* Setup:
* LED on digital pin 11 (for debugging)
* Reed switch on digital pin 2 (interrupt 0)
*/
int ledPin = 11;
int counter = 0;
long nextSwitchTime = 0;
int resolution = 330; // ms between sending readings via serial
int incomingByte = 0;
long nextCountTime = 0;
int countResolution = 5;
long nextStopCountTime = 0;
int stopCountResolution = 2;
volatile int state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
attachInterrupt(0, blink, RISING);
nextSwitchTime = millis() + resolution;
nextCountTime = millis() + resolution;
Serial.begin(9600);
Serial.println("ready");
}
void loop () {
digitalWrite(ledPin, state);
if (nextSwitchTime < millis()) {
// Rotation count
nextSwitchTime = millis() + resolution;
Serial.println(counter);
Serial.println(counter, BYTE);
counter = 0;
}
}
void blink() {
if(nextCountTime < millis()) {
nextCountTime = millis() + countResolution;
counter++;
state = !state;
}
}
void lift() {
if(nextStopCountTime < millis()) {
nextStopCountTime = millis() + stopCountResolution;
}
}
void drop() {
if(nextStopCountTime < millis()) {
nextStopCountTime = millis() + stopCountResolution;
}
} |