Hi, for some reason when I turn on my reedswitch which is the pushbutton, the green light comes on, and no matter how many different time intervals I use, it always stays on, and the other 2 LEDs are off.
Here's my code:
#include <TimerOne.h>
/*
*/
const int buttonPin = 46; // the number of the pushbutton pin
const int ledGreen = 22; // the number of the LED pin
const int ledYellow = 24;
const int ledRed = 26;
const long circumference = 0.5; //0.5 metre bike wheel circumference
int buttonState = 0; // variable for reading the pushbutton status
long elapsedTime = 0;
long currTime = 0;
long prevTime = 0;
long bikeSpeed = 0; //metres per second
int period = 8000000; //8 seconds
bool alreadyChecked = 0;
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(buttonPin, INPUT);
Timer1.initialize(period);
Timer1.start();
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && alreadyChecked == LOW) {
currTime = Timer1.read();
elapsedTime = currTime - prevTime;
displaySpeed();
prevTime = currTime;
alreadyChecked = HIGH;
}
else if (buttonState == LOW) {
alreadyChecked = LOW;
}
}
void displaySpeed() {
if (elapsedTime < 2000000) {
digitalWrite(ledGreen, HIGH);
digitalWrite(ledYellow, LOW);
digitalWrite(ledRed, LOW);
}
else if (elapsedTime > 2000000 && elapsedTime < 4000000) {
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, HIGH);
digitalWrite(ledRed, LOW);
}
else if (elapsedTime > 4000000) {
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledRed, HIGH);
}
}
I read that the period parameter and the time reading were in microseconds which is why i was using such large values, however it seems as though they are in milliseconds.
Here is my new code:
#include <TimerOne.h>
/*
*/
const int buttonPin = 46; // the number of the pushbutton pin
const int ledGreen = 52; // the number of the LED pin
const int ledYellow = 50;
const int ledRed = 48;
const long circumference = 0.5; //0.5 metre bike wheel circumference
int buttonState = 0; // variable for reading the pushbutton status
long elapsedTime = 0;
long currTime = 0;
long prevTime = 0;
long bikeSpeed = 0; //metres per second
int period = 8000; //8 seconds
bool alreadyChecked = 0;
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(buttonPin, INPUT);
Timer1.initialize(period);
Timer1.start();
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && alreadyChecked == LOW) {
currTime = Timer1.read();
elapsedTime = currTime - prevTime;
//calculateSpeed();
displaySpeed();
prevTime = currTime;
alreadyChecked = HIGH;
}
else if (buttonState == LOW) {
alreadyChecked = LOW;
}
}
//void calculateSpeed() {
//bikeSpeed = circumference/elapsedTime;
//}
void displaySpeed() {
if (elapsedTime < 2000) {
digitalWrite(ledGreen, HIGH);
digitalWrite(ledYellow, LOW);
digitalWrite(ledRed, LOW);
}
else if (elapsedTime > 2000 && elapsedTime < 4000) {
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, HIGH);
digitalWrite(ledRed, LOW);
}
else if (elapsedTime > 4000) {
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledRed, HIGH);
}
}
However because there is no debugger in the Arduino IDE I can't work out what is going wrong.
The LEDs are lighting up strangely, the green is mostly on and sometimes depending on the frequency of the reedswitch being activated the yellow led goes on too.
However because there is no debugger in the Arduino IDE I can't work out what is going wrong.
The Arduino IDE has an icon tagged Serial Monitor.
Add Serial.begin(9600); to setup. Add Serial.print() statements to loop(). Compile and upload. Then, press the Serial Monitor icon. The Serial Monitor will open, and you can see your print()-ed output.
const long circumference = 0.5; //0.5 metre bike wheel circumference
Long is 16 bit integer. Do you know the difference between an integer and a float/double?
The real reason for using Timer1 instead of millis() or micros() is so that you can have code called automatically at exact times. If you are not registering a callback (and you aren't), the TimerOne class is not really what you want to be using.
Hey Paul,
thanks for your help,
the millis() method is working great, I found the Timer1 class quite confusing, and there are no simple tutorials on how to use it.
I learnt the difference between int long etc but forgot, I need to have a readup on it.
I learnt the difference between int long etc but forgot, I need to have a readup on it.
Integer types hold whole numbers. Floating point types can hold fractional values, too.
0.5 is NOT a whole number. When stored in an int, the fractional part is lost, leaving you with a circumference of 0. I do hope you don't divide by the circumference somewhere. At least not until you use the correct variable type.
bool variables are byte sized, so they can be set to 0, LOW (alias for 0), true (another alias), false (also an alias), HIGH (another alias), 1, 17, etc.
Normally, they are reserved for true/false, but that is simply a convention.