#include "TimerOne.h" //Include Timer1 library for using Timer1 functions
#include <LiquidCrystal_I2C.h> //Include LCD library for using LCD display functions
LiquidCrystal_I2C lcd (0x27, 16, 2);
volatile unsigned int counter=0;
volatile unsigned int rotation=0;
float rotationinm=0;
unsigned int speed=0;
void count() // ISR for counts from the speed sensor
{
counter++; //increase the counter value by one
rotation++; //Increase the rotation value by one
delay(10);
}
void timerIsr()
{
detachInterrupt(digitalPinToInterrupt(2)); //Stops the interrupt pin 2
Timer1.detachInterrupt(); //Stops the timer1 interrupt[/color]
lcd.clear();
float speed = (counter / 20.0)* 60.0; //Calcukate speed in minute (20-No of slots in Encoder Wheel)
float rotations = 230*( rotation / 20); //Calculate distance in cm (230-Circumference of the wheel assumed 20- No of slots)
rotationinm = rotations/100;[/color]
lcd.setCursor(0,0);
lcd.print("Dist(m):");
lcd.print(rotationinm); //Display rotationinm at LCD
lcd.setCursor(0,1);
lcd.print("Speed(RPM):");
lcd.print(speed); //Dsiplay speed in RPM
counter=0; //Reset counter to 0 [/color]
int analogip = analogRead(A0); // Analog read from pin A0
int motorspeed = map(analogip,0,1023,0,255); //convert digital vales 0-1023 to 0-255
analogWrite(5,motorspeed); //Sets PWM value at pin 5
Timer1.attachInterrupt( timerIsr ); //Starts timer1 again
attachInterrupt(digitalPinToInterrupt(2), count, RISING); //Attaches interrupt at pin2 again
}
void generatefare() //ISR to generate the fareamount
{
detachInterrupt(digitalPinToInterrupt(2)); //Disables the Interrupt pin at 2
Timer1.detachInterrupt(); //Disables the Timer1 interrupt
float rupees = rotationinm*5; //Muliply 5 with distance travelled (Rs 5 per meter )
lcd.clear(); //Clears LCD
lcd.setCursor(0,0);
lcd.print("FARE Rs: ");
lcd.print(rupees); //Display fare amount
lcd.setCursor(0,1);
lcd.print("Rs 5 per metre");
}
void setup()
{
pinMode(A0,INPUT); //Sets pin A0 as INPUT
pinMode(5,OUTPUT); //Sets pin 5 as OUTPUT
lcd.begin(16,2); //Sets LCD as 16x2 type
lcd.setCursor(4,0); //The following code displays welcome messages
lcd.print("Welcome!");
lcd.setCursor(1,1);
lcd.print("-Drive Safely-");
delay(3000);
lcd.clear();
lcd.print("LETS START :)");
delay(1000);
lcd.clear();
Timer1.initialize(1000000); //Initilize timer1 for 1 second
Timer1.attachInterrupt( timerIsr ); //ISR routine to be called for every one second
attachInterrupt(digitalPinToInterrupt(2), count, RISING); // Pin 2 as Interrupt pin with count ISR is called when LOW to RIGH happens.
attachInterrupt(digitalPinToInterrupt(3), generatefare, HIGH); //Pin 3 as Interrupt pin with generatefare ISR is called when HIGH is detected.
}
void loop()
{
}
Hello! I'm just new in arduino and the above code is the code i used in a project called Digital Taxi Fare Meter. I used an Arduino Uno and an IR speed sensor module. When i tried to compile it the lcd only displays the welcome message until "lets start". It does not proceed to display the distance and speed in the Isr function. Moreover, when button is pressed, fare isn't displayed also. I think the mess up with the interrupt or not? What seems to be the problem in the code? Thank you for upcoming responses.
As a beginner, my project is not really as complicated as a real taxi fare meter. It is only a prototype that measures the speed and distance travelled using IR Speed sensor and an encoder wheel attached to motor. Then generate fare when button is pressed. Also, control the speed of the motor using ULN2003 IC and potentiometer.
To measure the speed of rotation I used the number of slots present in encoder wheel. I have an encoder wheel with 20 slots in it. When they rotate one complete rotation we have 20 pulses at the output. So to calculate speed we need number of pulses produced per second.
For example:
If there are 40 pulses in one second, then
Speed = Noo. Of pulses / No. of slots = 40/20 = 2RPS (Revolution per second)
For calculating speed in RPM (Revolutions per Minute) multiply with 60.
Speed in RPM = 2 X 60 = 120 RPM (Revolutions per Minute)
Measuring Distance:
Before calculating distance, the circumference of the wheel should be known.
Circumference of the wheel = π*d
Where d is the diameter of the wheel.
Value of π is 3.14.
I have a wheel (RC car wheel) of diameter 6.60 cm so the circumference is (20.7 cm).
So to calculate the distance travelled, just multiply the no of pulses detected with the circumference.
Distance Travelled = Circumference of Wheel x (No. of Pulses / No. of slots)
So when a wheel of Circumference 20.7cm takes 20 pulses that is one rotation of encoder wheel then the distance travelled by wheel is calculated by
Distance travelled = 20.7 x (20/20) = 20.7cm
In order to calculate the distance in meter divide the distance in cm value by 100.
Calculating the Fare Based on Distance Travelled:
To get the total fare amount, multiply the distance travelled with the fare rate (amount/meter).
I’m assuming that Rs 5 is charged per meter.
So if the wheel is travelled 20m then fare amount will be 20*5=100 rupees.
That's how everything should works. I'm sorry that i did not mention it before. The connections are below:
Connections between Speed sensor module and Arduino
SPEED SENSOR (FC-03) ARDUINO
VCC 5V
GND GND
D0 2
16x2 I2C LCD ARDUINO
GND. GND
VCC. +Ve
SDA. SDA
SCL SCL
Connection between Arduino & ULN2003
ARDUINO ULN2003
5. IN1
GND. GND
Connection between ULN2003, DC Motor and 9v Battery
I have used a 9V battery and ULN2003 IC to externally power the motor.
ULN2003 MOTOR 9V battery
OUT1 -Ve Motor
COM. +Ve Motor +Ve
GND. -Ve
Push button & Potentiometer Connection
A push button with pull down resistor is connected to the pin 3 of Arduino for generating fare amount when pressed.
A potentiometer is used to give analog input voltage to the pin A0 of the Arduino for varying speed of the motor wheel.