#include "TimerOne.h" //Include Timer1 library for using Timer1 functions
#include <LiquidCrystal.h> //Include LCD library for using LCD display functions
const int rs = 12, en = 13, d4 = 8, d5 = 9, d6 = 10, d7 = 11; //Define the LCD pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
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
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;
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
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(0,0); //The following code displays welcome messages
lcd.print("CIRCUIT DIGEST");
lcd.setCursor(0,1);
lcd.print("WELCOME TO TAXI");
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.
Comments