Hi everyone; i need very urgent help. I have a car control system with arduino uno. Arduino Mega is measuring speed from car engine using LM 393. I also have one dc motor and a l293d circuit. I tied them to the arduino mega. the code I need to create is to simulate the opening and closing of the vehicle window in a specific scheme after measuring the speed from the arduino mega. Depending on the speed of the engine you connect to the arduino mega or up to 3 seconds. I've got three seconds to move. the codes I get from my speed sensor and the other codes I want to move the motor do not match.
int encoder_pin = 2; // pulse output from the module
unsigned int rpm; // rpm reading
volatile byte pulses; // number of pulses
unsigned long timeold;
// number of pulses per revolution
// based on your encoder disc
unsigned int pulsesperturn = 12;
int motorpin1 = 3;
int motorpin2 = 4;
int kat=0;
void counter()
{
//Update count
pulses++;
}
void setup()
{
Serial.begin(9600);
pinMode(encoder_pin, INPUT);
//Interrupt 0 is digital pin 2
//Triggers on Falling Edge (change from HIGH to LOW)
attachInterrupt(0, counter, FALLING);
// Initialize
pulses = 0;
rpm = 0;
timeold = 0;
pinMode(motorpin1,OUTPUT); //3.pin çıkış olarak kurduk
pinMode(motorpin2,OUTPUT); // 4.pinimizde çıkış olarak kurduk
}
void loop()
{
if (millis() - timeold >= 1000) {
//Don't process interrupts during calculations
detachInterrupt(0);
rpm = (60 * 1000 / pulsesperturn )/ (millis() - timeold)* pulses;
timeold = millis();
pulses = 0;
Serial.print("RPM = ");
Serial.println(rpm,DEC);
if(rpm==0){
if(kat==0){
digitalWrite(motorpin1,LOW);
digitalWrite(motorpin2,LOW);
}
if(kat==1){
digitalWrite(motorpin1,HIGH); //salar
delay(500);
digitalWrite(motorpin1,LOW);
digitalWrite(motorpin2,LOW); //sarar
}
if(kat==2){
digitalWrite(motorpin1,HIGH);
delay(1000);
digitalWrite(motorpin1,LOW);
digitalWrite(motorpin2,LOW);
}
}
//Restart the interrupt processing
attachInterrupt(0, counter, FALLING);
}
}