Hi,
I'm working on a project using an Arduino Uno and need to calculate the rate of deceleration of a wheel. I'm using a Hall effect speed sensor which gives out a digital square wave - 12 pulses per revolution. The sensor works fine with a scope and I have a sketch/code written which calculates the rpm of the wheel and the correect values show on the serial monitor (I've tested with an accelerometer). The problem is that when I try to edit the sketch to calculate the acceleration, I get a few errors which I can't get past. To get the acceleration I tried to use 2 different rpm values / change in time, i.e. - (rpmA - rpmB)/(millis() - timeOld) = acceleration.
I'm sure the code is completly wrong but as I'm a complete beginner using Arduino, C++ and any type of programming in general, would appreciate some help on this!!
Note: For the output I need either a pulsating output or a constant High output, until acceleration = 0.
See sketch and errors below. (could not find much on the error "not included in scope" and "can't use as function").????
Thanks
JH
/*Arduino code for calculating acceleration */
volatile byte rpmcount; //stores rpm count in RAM (ie outside of a function)
//which can be accessed later in program
volatile byte acceleration; //stores acceleration values in RPM
unsigned int rpmA; //counts rpm integers from zero up
unsigned int rpmB; //counts rpm integers from zero up
unsigned long timeoldA; //counts timeoldA decimals from zero up
unsigned long timeoldB; //counts timeoldB decimals from zero up
void setup()
{
Serial.begin(9600); //opens serial port, set data rate to 9600 bps
attachInterrupt(0, rpmA_fun, RISING); //Interrupt 0 is digital input pin 2. Triggers on
//rising. ie when square wave starts to go high
rpmcount = 0; //sets rpm count to 0
acceleration = 0; //sets acceleration to 0
rpmA = 0; //sets rpmA to 0
rpmB = 0; //sets rpmB to 0
timeoldA = 0; //sets timeoldA to 0
timeoldB = 0; //sets timeoldB to 0
}
void loop() //starts loop
{
if (rpmcount >=2) //restarts count after every 2 pulses (increase for less error)
{
rpmA = 5*1000/(millis()-timeoldA)*rpmcount; //calculates value for rpmA.
//5*1000 is the time between each pulse
//for one rev/min (ie 60 sec/12 pulses)
// x 1000 milliseconds
timeoldA = millis();
rpmB = 5*1000(millis()-timeoldB)*rpmcount; //calculates value for rpmB
timeoldB = millis(); //changes variables
acceleration = (rpmB-rpmA/(timeoldA-timeoldB); //calculates acceleration
rpmB = rpmA
rpmAcount = 0;
rpmBcount = 0; //resets the rpm count
Serial.print("rpmA: "); //prints values to serial monitor
Serial.println(rpmA, DEC);
Serial.print(" Acc: ");
Serial.println(acceleration, DEC);
}
{
void rpmA_fun() //each rotation the interrupt is run 30 times (60/2=30)
}
rpmcount++; //rpmcount plus 1
}
These are the errors*
Acc_Code.ino: In function 'void setup()':
Acc_Code:15: error: 'rpmA' cannot be used as a function
Acc_Code.ino: In function 'void loop()':
Acc_Code:35: error: '1000' cannot be used as a function
Acc_Code:38: error: expected `)' before ';' token
Acc_Code:40: error: expected `;' before 'rpmAcount'
Acc_Code:41: error: 'rpmBcount' was not declared in this scope
Acc_Code:49: error: expected initializer before '}' token