This is my first post on here. I have hit some problems with my first serious project on Arduino and would appreciate some advice.
The device I am working on is to be mounted in a competition vehicle used for sprints and hillclimbs. These consist of a timed run over a course lasting up to 2 minutes. The objective is to get some feedback on performance during the run by dividing the course into 4 and comparing the time for each section to the previous best time. If it is better a green LED will light. The time difference to the best run will also be shown on an LCD.
The hardware consists of strip board Arduino with a green LED connected to pin 7 and a red LED on pin 13. There is also 16X2 LCD. I am using an A1120EUA-T Hall Effect Switch which senses a magnet glued to the wheel rim. This has a 10K pull up resistor connected across the positive and signal pins. It is connected to pin 2 and I am using an interrupt. There is also a button to change the course length on pin 8.
Everything is working fine when I spin the wheel on the car with it jacked up in the garage or when I drive very slowly, so the logic of my sketch would appear to work. When I drive at faster speeds the sector times will often miss being recorded resulting in nonsense results.
The maximum speed of the wheel is about 1800 RPM. It stops working at speeds well below this. Is the problem likely to be in that I have too much code in the sketch for the Arduino to process between interrupts or should I be using a different sensor? Perhaps a reed switch? I have run a test with the code to light the LED's commented out but this seems to make no difference.
I hope that I have explained this adequately. Any advice would be much appreciated.
(2nd part of the sketch is in the next post)
#include <LiquidCrystal.h>
#include <EEPROM.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 6);
const int inputPin1 = 2; //Interrupt pin with sensor attacehd
const int inputPin2 = 8; //Running button
const int ledPin = 13; //Running LEDS
const int fastPin = 7; //faster light
unsigned long int starttime; //time when the run starts
unsigned int wheelRevs = 0; //Number of wheel revolutions
boolean Running = false; //boolean to control if wheel revs are being recorded
int courselength = 100; //length of sourse in meters. Change this for each venue (Gurston)
const float wheelcirc =1.81; //wheel circumference
int courserevs; //number of time the wheel turns for the whole run
int sector1; //number of times the wheel turns in sector 1
int sector2; //number of times the wheel turns in sector 2
int sector3; //number of times the wheel turns in sector 3
int Button2Press = 0; //variable to hold the state of the running button
float s1time; //Recorded time for sector 1
float s2time; //Recorded time for sector 2
float s3time; //Recorded time for sector 3
float s4time; //Recorded time total
float s1fastest = 0; //fastest sector times, used to calculate target time
float s2fastest = 0;
float s3fastest = 0;
float s4fastest = 0;
int val = 0;
int bounceCheck = 0;
float delta; //running time difference
float s1fastestrun = 0; //sector times in fastest run - usd for calculating deltas to compare fastest run to current run
float s2fastestrun = 0;
float s3fastestrun = 0;
float s4fastestrun = 0;
float fastesttotal = 0;
//variables to write to EEPROM
word s1fastestw;
word s2fastestw;
word s3fastestw;
word s4fastestw;
word s1fastestrunw;
word s2fastestrunw;
word s3fastestrunw;
word s4fastestrunw;
word fastesttotalw;
int course = 1;
void setup(){
pinMode(9, OUTPUT);
analogWrite(9, 50);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(ledPin, OUTPUT); //declare LED as output
pinMode(fastPin, OUTPUT);
pinMode(inputPin1, INPUT); //declare button as input
pinMode(inputPin2, INPUT); //declare button 2 as input
Serial.begin(9600);
digitalWrite(inputPin1,HIGH);
course = EEPROM.read(1);
if(digitalRead(inputPin2) == LOW){
if(course == 3){
course = 1;
}
else{
course++;
}
EEPROM.write(1,course);
for (int i = 2; i < 512; i++)
EEPROM.write(i, 0);
}
if( course == 1){
courselength = 100;
courserevs = int(courselength/wheelcirc); //Calculate number of wheel revolutions for the course.
sector1 = .25*courserevs;
sector2 = .5*courserevs;
sector3 = .75*courserevs;
lcd.print("Lavendon");
}
if (course == 2){
courselength = 1435;
courserevs = int(courselength/wheelcirc); //Calculate number of wheel revolutions for the course.
sector1 = 350/wheelcirc;
sector2 = (350 + 268)/wheelcirc;
sector3 = (350 + 268 + 347)/wheelcirc;
lcd.print("Curborough");
}
if( course == 3){
courselength = 3815;
courserevs = int(courselength/wheelcirc); //Calculate number of wheel revolutions for the course.
sector1 = 1262/wheelcirc;
sector2 = (1262 + 953)/wheelcirc;
sector3 = (1262 + 953 + 815)/wheelcirc;
lcd.print("Goodwood");
}
lcd.setCursor(0,1);
lcd.print(courselength);
lcd.print(" metres");
delay(3000);
lcd.clear();
s1fastestrun = (word(EEPROM.read(2),EEPROM.read(3)));
s2fastestrun = (word(EEPROM.read(4),EEPROM.read(5)));
s3fastestrun = (word(EEPROM.read(6),EEPROM.read(7)));
s4fastestrun = (word(EEPROM.read(8),EEPROM.read(9)));
s1fastest = (word(EEPROM.read(10),EEPROM.read(11)));
s2fastest = (word(EEPROM.read(12),EEPROM.read(13)));
s3fastest = (word(EEPROM.read(14),EEPROM.read(15)));
s4fastest = (word(EEPROM.read(16),EEPROM.read(17)));
fastesttotal = (s1fastestrun+s2fastestrun+s3fastestrun+s4fastestrun);
attachInterrupt(0,buttonPush,RISING); //Interrupt to detect when the sensor is tripped
lcd.print("setup complete");
delay(3000);
}
void buttonPush(){ //Interrupt loop called by interrupt
++wheelRevs;
}/code]