Hi
I'm pretty new to all of this, please bear with me as I fumble my way though. I am attempting to create a computer to give me a visual feed back via LCD screen on a rowing machine I have recently constructed. The machine uses an old bike wheel acting as a fan for resistance. So far I've had success creating a timer, recording speed output and counting revolutions of the wheel which gives me distance. I've based my code on several examples of bike computers as the process is pretty much the same. To do this I've implemented an Uno board using a Hall effect sensor. While this is all well and good I've gotten stuck trying to use the wheel acceleration to keep track of strokes per minute to help with pace. I know that a stroke starts every time the revolution wheel is faster than the past few revolutions of the weel, but I'm stuck getting this to read out. As my code stands below all I can get out is the revolutions/ minute... not the strokes/min. Any help would be greatly appreciated.
#include <Average.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 1);
volatile byte revs;
unsigned int rps;
unsigned int turns = 0;
unsigned int stroke = 0;
unsigned long strokeold;
float spm =0.0;
unsigned long timeold;
float circ = .743; // update this value so aproximate max speed is 7meters per second decrease to slow down speed
//float velocity =0.0;
float spd = 0.0;
unsigned int avg[4]; //array for averaging speed
float distance;
unsigned long elapsed;
int h;
int m;
float s = 0.0;
void setup()
{
digitalWrite(2, HIGH);
attachInterrupt(0, rpm_fun, FALLING);
lcd.begin(20, 4); // set up the LCD's number of columns and rows:
delay(500);
lcd.blink();
lcd.print("READY TO ROW?");
delay(2000);
lcd.clear();
revs = 0;
rps = 0;
timeold = 0;
}
void loop()
{
if (revs >= 2) //Update rps every 2 counts, increase this for better RPM resolution, decrease for faster update
{
rps = 1000/(millis() - timeold)*revs; // this gives us the revolutions per second
timeold = millis();
revs = 0;
timer();
calc();
stroketimer();
lcdupdate();
}
}
void calc()
{
distance = turns * circ;
//velocity = rps * circ; // speed is expressed in meters per second as averaged by the # pass interval specified in the loop
avg[0] = avg[1]; //shift storage array
avg[1] = avg[2]; // "
avg[2] = avg[3]; // "
avg[3] = int(rps); //insert current calculated revolutions per second
spd = ((avg[0] + avg[1] + avg[2] + avg[3]) / 4) * circ; //average the speed over the last four readings (mean)
}
void stroketimer()
{
if (rps > maximum(avg, 4))// if the latest turn of the wheel is faster than the time of the last four turns, assumes acceleration or new stroke
stroker();
spm = 60000/(millis() - strokeold); // this gives us the strokes per minute
strokeold = millis();
stroke = 0;
}
void timer()
{
unsigned long over;
elapsed=millis();
h=int(elapsed/3600000);
over=elapsed%3600000;
m=int(over/60000);
over=over%60000;
s=int(over/1000);
}
void lcdupdate()
{
lcd.setCursor(0,0);
lcd.print("Time = ");
lcd.print(h);
lcd.print( ":");
lcd.print(m);
lcd.print( ":");
lcd.print(s);
lcd.setCursor(0,1);
lcd.print(" Speed:");
lcd.print(spd);
lcd.print(" m/s");
lcd.setCursor(0,2);
lcd.print("Distance");
lcd.print(distance);
lcd.setCursor(0,3);
lcd.print("SPM= ");
lcd.print(spm);
}
void rpm_fun()
{
revs++;
turns++;
//Each rotation, this interrupt function is run once
}
void stroker()
{
stroke ++;
}
Thank you in advance,
-Nathan