Ok the first sketch Ive trialed
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
if (rpmcount >= 20) {
//Update RPM every 20 counts, increase this for better RPM resolution,
//decrease for faster update
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm,DEC);
}
}
void rpm_fun()
{
rpmcount++;
//Each rotation, this interrupt function is run twice
}
This doesnt seem to give the correct rpm, (Im using 1 magnet) and not sure why the function rpm_fun() runs twice (giving 30,000)
When inserting this in "wifi sheild code" for posting to cosm it doesnt output zero rpm when wheel is not spinning. Somehow I think the 10sec wifi output only refers to the last known rpm reading.
Anyway the second sketch I trialled here,
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
//const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
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
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastEvent = 0; // the last time the output pin was toggled
long interval = 10; // the debounce time; increase if the output flickers
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
//pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
// if the state has changed, increment the counter
if (buttonState == HIGH && (millis() - lastEvent > interval) ) // <-- Ignore this press if it is too close to the last one
{
lastEvent = millis(); // <-- record when the last good event occurred
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
This is really good for "smoothing out" debouncing problems with the "interval" feature, but Id really like to use it to turn the count (buttonPushCounter) into an rpm reading or a rev count per hr to dsitingusih when the shaft is rotating. ie after 1 hr(some defined time period) output the count of revs. I could later define motion/movement by a threshold of more than a certain number or revs per hr (min) etc.
Hope that makes sense
Cheers