Hello guys!!
I am new with arduino. My objetive is make a simple tachometer with arduino, but I don't be able to to entry in the interrupt. The interrupt occurs with a frequency from 1 Hz up to 130Hz
Here is my code. Someone can give me a little bit of light about it?
#include <avr/interrupt.h>
//Global constants
const int pin_motor=2; //Pin interrupt 0
//Global Variables//
int int1=0;//variable for first interrupt.
int int2=0;//Variable for second interrupt.
int ini=0;//Start variable.
int t1=0;//Time 1 safe
int t2=0;//Time 2 calculated
int long RPM_motor=0;
void setup()
{
pinMode(pin_motor,INPUT); //pin config as input.
attachInterrupt(0,Sensor_lessen,FALLING); //config interrup (nummer interrupt, function call, mode of interrupt)
Serial.begin(9600); // Serial speed config.
sei();
}
void loop()
{
unsigned long ms_first_int;
unsigned long ms_Aktuell=millis(); //millis(),Returns the number of microseconds since the Arduino board began running the current program.
//save this mircos in a variable. Overflow occurs after 50 days.
if((t1==0)&&(int1==1)&&(int2==0))
{
ms_first_int=ms_Aktuell;
t1=1;
t2=0;
}
if((t1==0)&&(int1==0)&&(int2==1))
{
RPM_motor=(1/(ms_Aktuell-ms_first_int))*60;
t2=1;
t1=0;
}
Serial.println(RPM_motor);
delay(100);
}
void Sensor_lessen()
{
if(((int1==0)&&(t2==1))||(ini==0))
{
int1=1;
int2=0;
ini=1;
}
if((int2==0)&&(t1==1)&&(int1==1))
{
int2=1;
int1=0;
}
}
Moderator edit: CODE TAGS. AWOL.