Hey guys! I’m new here, and I have a problem with my program.
Basically , I’ve built an RPM counter , using the IR Sensor, LCD to show the value, and one button which has the functionality to save the current RPM when pressed ( using an interrupt function ).
My problem is with the motor speed control. With the code below, the LCD is showing random numbers for RPM, even tho im not holding the sensor towards the motor, and when I do it , i’m pretty sure its not working 100% correct ( my values are not constant, jumping to 10k + which should be imposibble ) .
When I tried to power up the motor using a second arduino ( so im not using analogRead(motor,90) in the main code anymore ) , the code is working as intended .
Im not sure when to disable the interrupts, maybe its from them . Or maybe im using to much power having the motor conected to the arduino aswell ?
#include<LiquidCrystal.h> //librarie LCD
LiquidCrystal lcd(7,8,9,10,11,12);
volatile unsigned long rev=0;
volatile unsigned long OK;
unsigned long rpm;
unsigned long time , oldtime = 0 ;
const int buton=3;
const int motor=5;
void RPM_Count()
{
rev++;
}
void button()
{
OK=rpm;
}
void setup()
{
lcd.begin(16,2);
attachInterrupt(0,RPM_Count,RISING); // RPM count interrupt
pinMode(motor,OUTPUT);
pinMode(buton,INPUT_PULLUP); // button to save RPM
attachInterrupt(1,button,FALLING); // pull-up buton interrupt
}
void loop()
{
delay(1000); // update RPM every second
analogWrite(motor,90);
noInterrupts();
//--------- Also, if someone can explain this next 4 lines for me ,and how RPM is calculated,as I'm a bit confused.
time=millis()-oldtime;
rpm=(rev*60000/time);
oldtime=millis();
rev=0;
//---------
lcd.clear();
lcd.setCursor(0,0);
lcd.print(rpm);
lcd.print(" RPM");
interrupts();
lcd.setCursor(0,1);
lcd.print("Saved:");
lcd.print(OK);
}