Hi,
I have been working with the Arduino for about a week now, the idea of this project is to log information from my race bike.
I want to log speed (via GPS later on), suspension travel, brake inputs, and throttle position.
I have managed to get all of the above except the Throttle sensor written to the SD, Using ADAfruits assembled logger BTW.
I can't see why at the moment. It prints 0.00 as a value regardless of input. I have basically copied the suspension travel code, which works great, but the TPS doesn't register...
I'll use a hall sensor for the wheel speed later, just trying it out with a reed for now.
Would anyone be kind enough to give some ideas? I know it's a bit ropey, but I've only been playing with it for 4 days. So pretty new to this stuff!
Thank you.
Sketch:
#include <SD.h>
const int chipSelect = 10;
int frontbrake = 2;
//calculations
//tire radius ~ 11.87 inches
//circumference = pi2r
#define reed A2//pin connected to read switch
//storage variables
int reedVal;
long timer;// time between one full rotation (in ms)
float mph;
float radius = 11.87;// tire radius (in inches)
float circumference;
int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing)
int reedCounter;
void setup() {
pinMode(frontbrake, INPUT);
reedCounter = maxReedCounter;
circumference = 23.14radius;
pinMode(reed, INPUT);
Serial.begin(9600);
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// TIMER SETUP- the timer interrupt allows precise timed measurements of the reed switch
//for more info about configuration of arduino timers see Arduino Playground - Timer1
cli();//stop interrupts
//set timer1 interrupt at 1kHz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;
// set timer count for 1khz increments
OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))8) - 1
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
//END TIMER SETUP
}
ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure reed switch
reedVal = digitalRead(reed);//get val of A2
if (reedVal){//if reed switch is closed
if (reedCounter == 0){//min time between pulses has passed
mph = (56.8float(circumference))/float(timer);//calculate miles per hour
timer = 0;//reset timer
reedCounter = maxReedCounter;//reset reedCounter
}
else{
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
}
else{//if reed switch is open
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
if (timer > 1000){
mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
}
else{
timer += 1;//increment timer
}
}
void loop() {
// read the inputs on analog pins:
int frontsensor = analogRead(A0);
int rearsensor = analogRead(A1);
int leverstate = digitalRead(frontbrake);
int throttle = analogRead(A4);
// print out the values read:
float frontval = frontsensor * (125.0 / 1023); // NOTE: The 100.0 figure is the stroke length of the forks.
float rearval = rearsensor * (55.0 / 1023); // NOTE: The 50.0 figure is the stroke length of shock. If directly mounted.
float frlever = leverstate * (10 * 1); // Write the digital signal as a value 10. To make it readable on graph.
float gas = throttle * (100 / 1023); //Note: the 100 value here is the represent TPS as a %
// open the file. note that only one file can be open at a time,
File dataFile = SD.open("datalog.CSV", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(frontval);
dataFile.print(" , ");
dataFile.print(rearval);
dataFile.print(" , ");
dataFile.print(frlever);
dataFile.print(" , ");
dataFile.print(mph);
dataFile.print(" , ");
dataFile.print(gas);
dataFile.print(" , ");
dataFile.println();
dataFile.close();
// print to the serial port too:
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.CSV");
return;} //If not available, do no more.
Serial.print(frontval);
Serial.print (" , ");
Serial.print(rearval);
Serial.println(" , ");
Serial.print(frlever);
Serial.print(" , ");
Serial.print(mph);
Serial.print(" , ");
Serial.print(gas);
Serial.print(" , ");
delay(250);
} // Delay time is recording accuracy, or resolution.
// NOTES:
// Write prog to be able to edit sample rate externally.
// Look at how to incorporate throttle position
// Rear wheel speed
// Write prog to record to SD Card.
//Connections:
//A0 = Front suspension POT
//A1 = Rear suspension POT
//A2 = Front Wheel speed Input (Hall Sensor) with 10kohm between A2 and 0v
//DI2 = Front brake switch