Timer Interrupt not working

#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.

LM-393-Speed-Sensor-Module.jpg

LM-393-Speed-Sensor-Module.jpg

edit the post and get rid of the color tags that make it hard to read...

You should not try to update the LCD from within the ISR. handle that in the loop and really there is no need for an ISR, just use millis to do something every second in the loop directly...

what's the idea for having a delay(10)inside an interrupt? if this is to avoid bouncing, handle it in hardware for example.

Hello! Thank you for pointing my fault. I know it's a lot to ask but can you please edit my code? If you have spare time tho. I'm really new to this. But I want to learn how to use millis so i will read and study about it later. I just need my code to work soon.

Why do you need to code to work soon? if it's professional work, then hire professional help. If it's school work, we won't do your homework :slight_smile:

The goal of this forum is not to write code for others projects , it's to help them develop the skills to become self sufficient.

It looks like you miss some of the basis. So you should set your project aside and learn a few things first... that will make your life easier later on.

There are tons of tutorials explaining how to use millis and other techniques --> I would suggest thus to start by reading this post: Useful links - check here for reference posts / tutorials that is pinned at the top of the forum (for good reasons).

I'm really sorry. But still thank you for pointing my problem not only with my code but with myself as well. I'll try to fix my code with your suggestions. And if there are still some problems i hope you can guide me again.

don't be. just focus on this and you'll solve it. We'll help along the way

@OP

Can you please, provide the following information of your proposed Taxi Fare Meter?

1. How may pulse does the IR sensor produce when the Taxi moves for 1km distance?

2. What is the fare for each km traveled?

3. Is there any flat fare for the first 2 km (or some other value) journey? If yes, how much it is?

4. How much do you want to pay to the Taxi Driver as an advancement payment? It is usually 1/5th of the value of Step-2.

5. What is the waiting time charge (charge for each minute waiting)?

Have a look here for the design principles and implementation techniques of a commercial Taxi Fare Meter.

Hello! I edited my post and included some details regarding my project

You have nicely documented all the details of your project. Now, follow SSS Methodology (Start Small Strategy) and realize the Project.

1. Press RESET Button. The LCD will show:
LCDTax-1.png
Figure-1:

2. Allow the Taxi to move. Check that DDM (Digital Distance Meter) of LCD Panel advances by 0.1 Meter (10 cm) after implementing the following steps.
(1) Connect IR's output to DPin-4 of UNO. Count 10 pulses (simplified calculation) of IR using TC0 Module of UNO and then advance DDM by 0.1. Here are the sample codes:

float DDM = 0.0;   //Digital Distance Meter

void setup()
{
    Serial.begin(9600);
    //---TC0 initialization and START ---------------------
    TCCR0A = 0x00;    //normal up counting Mode of TC0
    TCCR0B = 0x00;             //TC0 is OFF
    TCNT0 = 0xF6;              //pre-set count so that TOV) occurs after counting 10 IR pulses (0x100 - 10)
    TCCR0B = 0x07;            //Start TC0 with external pulse of IR
    //-------------------------------------------------------
}

void loop()
{
    while(bitRead(TIFR0, TOV0) != HIGH)  //check if 10 cm is crossed by Taxicab
    {
          ;//wait and do nothing
    }
    bitSet(TIFR0, TOV0);   //clear the TOV0 flag
    TCNT0 = 0xF6;         //re-load preset value: 0xF0 (0x100 - 10 = 0x100 - 0x0A = 0xF6) = 246
    updateDDM();           //update Distance Meter of LCD
}

void updateDDM()
{
 DDM = DDM + 0.1;
 Serial.println(DDM, 1);   //show distance on Serial Monitor with 1-digit after decimal point
}

(2) Add codes with the sketch of Step-1 to show Rs 5.00 on DFM (digital Fare Meter) when the taxicab has travelled 1 m (100 cm) distance.

LCDTax-1.png

Hi GolamMostafa! Sorry for the late notice. I followed your directions but failed to compile the code.

bitSet(TIFR0, TOV0);

T0V0 was not declared in this scope

P.S. I attached an image of what my speed sensor looks like. It has 4 pins: A0, D0, GND, VCC

luffyturok:

bitSet(TIFR0, TOV0);

T0V0 was not declared in this scope

TOV0, not T0V0. Tango Oscar Victor zero !

luffyturok:
Hi GolamMostafa! Sorry for the late notice. I followed your directions but failed to compile the code.

bitSet(TIFR0, TOV0);

T0V0 was not declared in this scope

Are you using IDE-1.8. or IDE-1.8.13?

Very strange! The codes are not compiled in IDE-1.8.13; the various symbolic names of TC0 are not recognized at all.

I have been using IDE-1.8.9 without any problem and have tested now in my other PC. The sketch is compiled without any error. Please, switch back to IDE-1.8.9 and compile the codes.

Does anybody has any suggestion on how to get the following codes compiled using IDE-1.8.13?

float DDM = 0.0;   //Digital Distance Meter

void setup()
{
    Serial.begin(9600);
    //---TC0 initialization and START ---------------------
    TCCR0A = 0x00;    //normal up counting Mode of TC0
    TCCR0B = 0x00;             //TC0 is OFF
    TCNT0 = 0xF6;              //pre-set count so that TOV) occurs after counting 10 IR pulses (0x100 - 10)
    TCCR0B = 0x07;            //Start TC0 with external pulse of IR
    //-------------------------------------------------------
}

void loop()
{
    while(bitRead(TIFR0, TOV0) != HIGH)  //check if 10 cm is crossed by Taxicab
    {
          ;//wait and do nothing
    }
    bitSet(TIFR0, TOV0);   //clear the TOV0 flag
    TCNT0 = 0xF6;         //re-load preset value: 0xF0 (0x100 - 10 = 0x100 - 0x0A = 0xF6) = 246
    updateDDM();           //update Distance Meter of LCD
}

void updateDDM()
{
 DDM = DDM + 0.1;
 Serial.println(DDM, 1);   //show distance on Serial Monitor with 1-digit after decimal point
}

Does anybody has any suggestion on how get the following codes compiled using IDE-1.8.13?

It compiled for me in 1.8.13 with no errors.

aarg:
It compiled for me in 1.8.13 with no errors.

Thanks @aarg. I made the blunder mistake; where, I was careless and was compiling the codes under ESP8266 Boards (NodeMCU).

There is no problem with IDE-1.8.13; the codes are well compiled under Arduino AVR Boards (UNO).

Erik_Baas:
TOV0, not T0V0. Tango Oscar Victor zero !

Did you even read my reply #10?? There is a typo in "TOV0" !!

TOV0.jpg

TOV0.jpg

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.