motor conversion according to speed measurement

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);
   }
}

From all that, I get this: "the codes I get from my speed sensor and the other codes I want to move the motor do not match.".

What does that mean and what are the codes and why should they match.

Paul

Paul_KD7HB:
From all that, I get this: "the codes I get from my speed sensor and the other codes I want to move the motor do not match.".

What does that mean and what are the codes and why should they match.

Paul

I made a change in the code. but there was no improvement. The speed sensor is being monitored every second and the motor that I want to move is working with this control. I just want to make 1 unit. But the engine is constantly moving 1 speed with the speed sensor. The speed is 0 when the engine does not move, the speed is 2 when the engine moves once. But with the speed timer, the motor is constantly bulging.

I'm not sure I understand your problem but I see a couple of issues with your code:

  1. kat never changes. You set it to zero in the initial declaration but nothing ever changes it.

  2. You have some big delay()'s. So big that if you actually do one of those then the millis() timer will expire immediately and the interrupt never gets a chance to count any RPM's at all.

  3. detaching and re-attaching the interrupt is an "expensive" operation. You do not normally want to do that. Instead, suspend interrupts for a short time (like under a millisecond) and re-enable the interrupts as quickly as possible. Then the interrupt can keep counting while you're doing other things like calculations and delay().

Take this section of code...

    //Don't process interrupts during calculations

detachInterrupt(0);
    rpm = (60 * 1000 / pulsesperturn ) / (millis() - timeold) * pulses;
    timeold = millis();
    pulses = 0;




and change it to...


//Suspend interrupts for a short time
    noInterrupts();
    byte localCopyPulses = pulses;
    pulses = 0;
    unsigned long oldTimeOld = timeold;
    timeold = millis();
    interrupts();  //allow the interrupt to resume counting

rpm = (60 * 1000 / pulsesperturn ) / (timeold - oldTimeOld ) * localCopyPulses ;



Then delete the attachInterrupt() near the bottom.

By the way, are you sure you will never get more than 128 pulses in a second? Maybe pulses should be bigger than a byte.