Week 10 - Troubleshooting
As we have trouble with setting the Arduino such that it will count the number of pulses every second ONLY, we spent the week reading up on timers, attachinterrupt, rising & falling edges, but to no avail, as we could not understand the timer features.
We used a simple push switch to generate the pulses whereby each press will be recorded as one pulse. The code we have used is as shown below:
*Note: The code is inaccurate as the previous reading is added to the current and will keep accumulating, as we did not add in the timer feature to make the Arduino count the number of pulses of every second ONLY.
Sensor Values Actual
1st reading: A 1st reading: A
2nd reading: B 2nd reading: A+B
3rd reading: C 3rd reading: A+B+C
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
double pi = 3.141592654; // pi
double cir = 1.727875; // pulse per revolution = 36 for 1kW motor
// 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
double motor_speed = 0;
int Rev = 0;
unsigned long current_time;
unsigned long previous_time;
long interval = 1000;
void setup()
{
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state to prevent multiple consecutive values from loop
if (buttonState != lastButtonState)
{
// When the motor is on...
if (buttonState == HIGH)
{
buttonPushCounter++;
current_time = millis(); // assign time elapsed to " time"
while(current_time - previous_time <= interval) // every second do...
{
previous_time = current_time;
Serial.print("Number of pulses: ");
Serial.print(buttonPushCounter);
Serial.print("\t\t\t\t");
//No.revolutions
Serial.print("Number of Revolution: "); Serial.println (Rev);
//Speed
Serial.print("Time: "); Serial.print((current_time-previous_time)/1000); Serial.print(" sec");
Serial.print("\t\t\t\t\t");
motor_speed = (((Rev*cir*3600*pi)/1000));
Serial.print("Speed: "); Serial.print(motor_speed); Serial.println(" km/h");
// motorspeed = ((result/48)*cir*3600)/1000;
if (buttonPushCounter == 4) // set to 36 pulses per second for 1kW motor
{
Rev++;
buttonPushCounter = 0;
}
buttonPushCounter = 0;
Rev = 0;
}
}
else
{
Serial.print("off");
}
delay (50);
// Reset when off (does not work with push button, remove this when testing with push button!!)
/*
else
{
buttonPushCounter = 0;
Rev = 0;
time = 0;
Serial.print("Off!");
}
*/
// Delay a little bit to avoid bouncing
delay(50); //add for push switch only
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
We then sought help from Mr.Steven to learn more on timers and were given some useful links:
We used a simple push switch to generate the pulses whereby each press will be recorded as one pulse. The code we have used is as shown below:
*Note: The code is inaccurate as the previous reading is added to the current and will keep accumulating, as we did not add in the timer feature to make the Arduino count the number of pulses of every second ONLY.
Sensor Values Actual
1st reading: A 1st reading: A
2nd reading: B 2nd reading: A+B
3rd reading: C 3rd reading: A+B+C
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
double pi = 3.141592654; // pi
double cir = 1.727875; // pulse per revolution = 36 for 1kW motor
// 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
double motor_speed = 0;
int Rev = 0;
unsigned long current_time;
unsigned long previous_time;
long interval = 1000;
void setup()
{
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state to prevent multiple consecutive values from loop
if (buttonState != lastButtonState)
{
// When the motor is on...
if (buttonState == HIGH)
{
buttonPushCounter++;
current_time = millis(); // assign time elapsed to " time"
while(current_time - previous_time <= interval) // every second do...
{
previous_time = current_time;
Serial.print("Number of pulses: ");
Serial.print(buttonPushCounter);
Serial.print("\t\t\t\t");
//No.revolutions
Serial.print("Number of Revolution: "); Serial.println (Rev);
//Speed
Serial.print("Time: "); Serial.print((current_time-previous_time)/1000); Serial.print(" sec");
Serial.print("\t\t\t\t\t");
motor_speed = (((Rev*cir*3600*pi)/1000));
Serial.print("Speed: "); Serial.print(motor_speed); Serial.println(" km/h");
// motorspeed = ((result/48)*cir*3600)/1000;
if (buttonPushCounter == 4) // set to 36 pulses per second for 1kW motor
{
Rev++;
buttonPushCounter = 0;
}
buttonPushCounter = 0;
Rev = 0;
}
}
else
{
Serial.print("off");
}
delay (50);
// Reset when off (does not work with push button, remove this when testing with push button!!)
/*
else
{
buttonPushCounter = 0;
Rev = 0;
time = 0;
Serial.print("Off!");
}
*/
// Delay a little bit to avoid bouncing
delay(50); //add for push switch only
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
We then sought help from Mr.Steven to learn more on timers and were given some useful links:
Can look at these references:
- ATmega328 datasheet (timers & interrupts section)
- http://www.instructables.com/id/Arduino-Timer-Interrupts/
- http://letsmakerobots.com/content/arduino-101-timers-and-interrupts
- http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/
Tachometer examples:
- http://arduinoprojects101.com/arduino-rpm-counter-tachometer/
- http://www.instructables.com/id/Measure-RPM-DIY-Portable-Digital-Tachometer/
- http://www.instructables.com/id/Arduino-Bike-Speedometer/
- http://www.instructables.com/id/DIY-SPEEDOMETER-AND-ODOMETER/step2/MATERIALS-REQUIRED/
Books:
- Make: AVR Programming
- Programming Arduino next steps: going further with sketches
Comments
Post a Comment