'attaching and detaching interupts

i am trying to make a robot that will be moving around using z-aix and a certain distance. I am using aobstacle avoidance sensor /IR sensor. i have attached it to pin 2 for arduino which is an interrupt pin. the sensor is attached to the fly wheel of one wheel and it will be calculating the number of interrupts. the wheel has 20 slots, this adds up to 40 interrupts.

#include <AFMotor.h>
#include <Wire.h>
#include <MPU6050_light.h>
MPU6050 mpu(Wire);
unsigned long timer = 0;
float radius_of_wheel = 0.033;  //Measure the radius of your wheel and enter it here in cm



//initial motors pin
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR34_8KHZ);
AF_DCMotor motor4(4, MOTOR34_8KHZ);

int val;
int Speeed = 110;
int left_intr = 0;
/*boolean conditions*/
boolean one = true;
boolean two = true;
boolean three = true;
/*Hardware details*/
int x;
int y;
int z;
int Y;
/* values of boolean as 1 and 0 in integer format*/
int w = 0;
int q = 0;
int e = 0;
int r = 0;
int l = 0;
int p = 0;
int u = 0;
//int pp;
/*distance value*/
int d;
volatile byte rotation; // variable for interrupt fun must be volatile
float timetaken, rpm, dtime;
float v;
int distance;
unsigned long pevtime;
float rev;
void setup()
{
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
  rotation = rpm = pevtime = 0; //Initialize all variable to zero
  attachInterrupt(digitalPinToInterrupt(2), Left_ISR, CHANGE); //Left_ISR is called when left wheel sensor is triggered

  // mpu setting
  Wire.begin();

  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while (status != 0) { } // stop everything if could not connect to MPU6050

  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  // mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
  mpu.calcOffsets(); // gyro and accelero
  Serial.println("Done!\n");
}
void Left_ISR()
{
  left_intr++; delay(10);
  rotation++;
  dtime = millis();
  if (rotation >= 40)
  {
    timetaken = millis() - pevtime; //timetaken in millisec
    rpm = (1000 / timetaken) * 60; //formulae to calculate rpm
    pevtime = millis();
    rotation = 0;
  }
}
void loop() {
  // read mpu
  mpu.update();

  if ((millis() - timer) > 100) { // print data every 10ms
    Serial.print("X : ");
    Serial.print(x);
    Serial.print("\tY : ");
    Serial.print(y);
    Serial.print("\tZ : ");
    Serial.println(z);
    x = round(mpu.getAngleX());
    y = round(mpu.getAngleY());
    z = round(mpu.getAngleZ());
    //Serial.print("X : ");
    //  Serial.print(x);
    //  Serial.print("\tY : ");
    //  Serial.print(y);
    //  Serial.print("\tZ : ");
    //  Serial.println(z);
    timer = millis();
  }
  /*positive is anticlockwise ...negative clockwise/ forward one*/
  if (w == 0) {
    if (z == 0) {
      left();
    }
    if ((z > 0) && (z <= 4)) {
      //delay(200);
      left();
    }
    else if ((z >= 4) && (z <= 9)) {

      //Stop();
      //delay(100);
      forward1();
      /*distance condition*/


      //set boolean to false/true


    }

  }

  //  /* move z axis right/ forward 2*/
  if (p == 1) {
    //Stop();
    /*edit values of z*/
    if (z > 0) {
      right();
    } else if (z < 0) {
      //Stop();
      Y = abs(z) /*+ 1000*/;
      Serial.println(Y);
    }

    if ((Y > 57) && (y < 60)) {
      //Serial.print("y is greater than 1030");
      //left_intr=0;
      forward2();

    } else if (y < 55) {
      right();
    } 
  }
  /*move z axis second right forward 3*/
  if (e == 1) {
    /*edit values of z / as z is in negative direction convert to positive*/

    if (z < 0) {
      //Stop();
      Y = abs(z) /*+ 1000*/;
      Serial.println(Y);
    }

    if ((Y > 140) && (y < 144)) {
      //Serial.print("y is greater than 1030");
      forward3();

    } else if (y < 140) {
      right();
    }
  }
  /*move z axis second right forward 4*/
  if (r == 1) {
    /*edit values of z / as z is in negative direction convert to positive*/
    if (z < 0) {
      //Stop();
      Y = abs(z) /*+ 1000*/;
      Serial.println(Y);
    }

    if ((Y > 210) && (y < 214)) {
      //Serial.print("y is greater than 1030");
      forward4();

    } else if (y < 210) {
      right();
    }else if (y > 250) {
      left();
    }

  }
  /*move z axis second right forward 4*/
   /*move z axis second right forward 4*/
  if (u == 1) {
    /*edit values of z / as z is in negative direction convert to positive*/
    if (z < 0) {
      Stop();
      Y = abs(z) /*+ 1000*/;
      Serial.println(Y);
    }
//
//    if ((Y > 200) && (y < 210)) {
//      //Serial.print("y is greater than 1030");
//      forward4();
//
//    } else if (y < 200) {
//      right();
//    }

  }
  
}
void forward1()
{
  //one=false;
  motor1.setSpeed(Speeed); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(Speeed); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(Speeed);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(Speeed);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
  v = radius_of_wheel * rpm * 0.104; //0.033 is the radius of the wheel in meter
  distance = (2 * 3.141 * radius_of_wheel) * (left_intr / 40);


  Serial.print("rotation");  Serial.print(left_intr);
  Serial.print("v"); Serial.print(v);
  Serial.print("d"); Serial.print(distance); Serial.println("");
  //d=4;
  //delay(20000);
  if (distance == 1) {
    //Serial.println("distance is 4:");
    //delay(500);
    //l = 1;

    e = 0;
    w = 1;
    p = 1;
    r = 0;
    u = 0;
    //Stop();
    //noInterrupts();
    //interrupts();
  }
}
void forward2()
{
  //one=false;
  // interrupts(); //Left_ISR is called when left wheel sensor is triggered
  motor1.setSpeed(Speeed); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(Speeed); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(Speeed);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(Speeed);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
 
  delay(1500);
  e = 1;
 
  p = 0;
  
  //Stop();
  //    if (distance == 5) {
  //      Serial.println("distance is 4:");
  //      //l = z;
  //      e = 1;
  //    p = 0;
  //      Stop();
  //
  //    }
}
void forward3()
{
  //one=false;
  motor1.setSpeed(Speeed); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(Speeed); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(Speeed);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(Speeed);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
 delay(1500);
  r = 1;
  e= 0;
}

the problem i am facing is that when the second forward function is called the values of distance are not starting again from zero , it continues counting the interrupts even when the function is not yet called.

i want the code to work when the foward1 function is called the interrupts to be counted and when the function ends the interrupts to start from zero in the next forward 2 function and so on.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source