Radio Controlled Ultimate Car Robot

Code for Arduino IDE.

#define EN1 12
#define EN2 26
#define M1O1 13
#define M1O2 14
#define M2O1 27
#define M2O2 25

#define rx1 2
#define rx2 15

long val1 = 0;
long val2 = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(EN1, OUTPUT);
  pinMode(EN2, OUTPUT);
  pinMode(M1O1, OUTPUT);
  pinMode(M1O2, OUTPUT);
  pinMode(M2O1, OUTPUT);
  pinMode(M2O2, OUTPUT);

  pinMode(rx1, INPUT);
  pinMode(rx2, INPUT);
  Serial.begin(9600);
}

void loop() {
  val1 = pulseIn(rx1, HIGH) - 1500;
  val2 = pulseIn(rx2, HIGH) - 1500;

  int a = map(val1 + val2, -1000, 1000, -255, 255);
  int b = map(val1 - val2, -1000, 1000, -255, 255);

  if (a < 0)
    analogWrite(EN1, -1 * a);
  else
    analogWrite(EN1, a);

  if (b < 0)
    analogWrite(EN2, -1 * b);
  else
    analogWrite(EN2, b);

  if (a > 0 && b > 0) {
    digitalWrite(M1O1, HIGH);
    digitalWrite(M1O2, LOW);
    digitalWrite(M2O1, LOW);
    digitalWrite(M2O2, HIGH);
  }

  if (a < 0 && b < 0) {
    digitalWrite(M1O1, LOW);
    digitalWrite(M1O2, HIGH);
    digitalWrite(M2O1, HIGH);
    digitalWrite(M2O2, LOW);
  }
  /*
  // put your main code here, to run repeatedly:
  digitalWrite(EN1, HIGH);    //forward
  digitalWrite(M1O1, LOW);
  digitalWrite(M1O2, HIGH);
delay(1000);

 digitalWrite(EN1, HIGH); //backward
  digitalWrite(M1O1, HIGH);
  digitalWrite(M1O2, LOW);
delay(1000);
digitalWrite(EN2, HIGH);   //backward
  digitalWrite(M2O1, LOW);
  digitalWrite(M2O2, HIGH);
delay(1000);

digitalWrite(EN1, HIGH); //forward
  digitalWrite(M2O1, HIGH);
  digitalWrite(M2O2, LOW);
delay(1000); */
}

Leave a Comment