Q:

if ((left_counter - right_counter) > 10) { while (left_counter != right_counter) { digitalWrite(motor_vcc_left, LOW); }

int motor_vcc_left = 12; //LEFT TIRE - BLUE
int pull_up_left = 3; //LEFT TIRE - GREEN

int motor_vcc_right = 11; //RIGHT TIRE - ORANGE
int pull_up_right = 2; //RIGHT TIRE - BLUE

int encoder_vcc_pin = 7;

volatile int left_counter = 0;
volatile int right_counter = 0;

void setup() {
  pinMode(motor_vcc_left, OUTPUT);
  pinMode(motor_vcc_right, OUTPUT);

  pinMode(encoder_vcc_pin, OUTPUT);
  digitalWrite(encoder_vcc_pin, HIGH);

  pinMode(3, INPUT_PULLUP); //LEFT TIRE
  attachInterrupt(1, blinkleft, CHANGE); //LEFT TIRE

  pinMode(2, INPUT_PULLUP); //RIGHT TIRE
  attachInterrupt(0, blinkright, CHANGE); //RIGHT TIRE

  digitalWrite(motor_vcc_left, HIGH);
  digitalWrite(motor_vcc_right, HIGH);
}

void loop() {
  if ((left_counter - right_counter) > 10) {
    while (left_counter != right_counter) {
      digitalWrite(motor_vcc_left, LOW);
    }
    digitalWrite(motor_vcc_left, HIGH);
  } else if ((right_counter - left_counter) > 10) {
    while (right_counter != left_counter) {
      digitalWrite(motor_vcc_right, LOW);
    }
    digitalWrite(motor_vcc_right, HIGH);
  }
}

void blinkleft() {
  left_counter++;
}

void blinkright() {
  right_counter++;
}
0

Tags

New to Communities?

Join the community