I have been trying to monitor robot car speed using Arduino and the design was to control it's movement using RF remote so the problem come when I try to drive it using remote it does not work including speed sensor code but when I remove the speed sensor the car is moving so I cannot monitor the speed and also if I try to monitor speed without moving the car using remote it give me the actual speed reading anyone can help me?
The code is attached below
#include <Wire.h>
#include "TimerOne.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16,2);
#include <IRremote.h>
#define Next_button 765
#define Prev_button 8925
#define left_button 43095
#define right_button 57375
#define Stop_button 49725
int receiver_pin = 8;
//initializing the pins for leds
int left_motor1 = 4;
int left_motor2 = 6;
int right_motor1 =5;
int right_motor2 = 7;
int motorEnA = 9;
int motorEnB = 10;
IRrecv receiver(receiver_pin);
decode_results output;
int encoder = 2;
volatile unsigned int counter;
const byte MOTOR_A = 3;
const byte MOTOR_B = 2;
const float stepcount = 20.00;
const float wheeldiameter = 66.10;
float diskslots = 20;
volatile int counter_A = 0;
volatile int counter_B = 0;
void ISR_countA()
{
counter_A++;
}
void ISR_countB()
{
counter_B++;
}
int CMtoSteps(float cm) {
int result;
float circumference = (wheeldiameter * 3.14) / 10;
float cm_step = circumference / stepcount;
float f_result = cm / cm_step;
result = (int) f_result;
return result;
}
void ISR_timerone()
{
Timer1.detachInterrupt();
float rotation1 = (counter_A / diskslots) * 60.00;
counter_A = 0;
float rotation2 = (counter_B / diskslots) * 60.00;
counter_B = 0;
Timer1.attachInterrupt( ISR_timerone );
}
void setup()
{
Timer1.initialize(1000000);
attachInterrupt(digitalPinToInterrupt (MOTOR_A), ISR_countA, RISING);
attachInterrupt(digitalPinToInterrupt (MOTOR_B), ISR_countB, RISING);
Timer1.attachInterrupt( ISR_timerone );
receiver.enableIRIn();
lcd.begin();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("car speed");
lcd.setCursor(4,1);
lcd.print("limiter");
pinMode(left_motor1, OUTPUT);
pinMode(left_motor2, OUTPUT);
pinMode(right_motor1, OUTPUT);
pinMode(right_motor2, OUTPUT);
pinMode(motorEnA, OUTPUT);
pinMode(motorEnB, OUTPUT);
}
void countpulse()
{
counter++;
}
void loop() {
if (receiver.decode(&output))
{
unsigned int value = output.value;
switch(value) {
case right_button:
digitalWrite(left_motor1,LOW);
digitalWrite(left_motor2,HIGH);
digitalWrite(right_motor1,HIGH);
digitalWrite(right_motor2,LOW);
analogWrite( motorEnA,130);
analogWrite( motorEnB,130);
break;
case Prev_button:
analogWrite( motorEnA,250);
analogWrite( motorEnB,250);
digitalWrite(left_motor1,HIGH);
digitalWrite(left_motor2,LOW);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,HIGH);
break;
case Next_button:
analogWrite( motorEnA,250);
analogWrite( motorEnB,250);
digitalWrite(left_motor1,LOW);
digitalWrite(left_motor2,HIGH);
digitalWrite(right_motor1,HIGH);
digitalWrite(right_motor2,LOW);
break;
case left_button :
digitalWrite(left_motor1,HIGH);
digitalWrite(left_motor2,HIGH);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,HIGH);
break;
case Stop_button:
digitalWrite(left_motor1,LOW);
digitalWrite(left_motor2,LOW);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,LOW);
break;
}
receiver.resume();
}
}