Home Arduino arduino motor defend – Drawback including IR receiver to sensible car

arduino motor defend – Drawback including IR receiver to sensible car

0
arduino motor defend – Drawback including IR receiver to sensible car

[ad_1]

I’m making a sensible car utilizing an arduino uno, sensor defend v5, motor management with a L298N and a servo motor + HC-SR04 ultrasound sensor. I wish to add a IR receiver on the board so I also can management the car with a distant.

The issue is once I add within the code for the IR Receiver and make the three extra connections, the fitting motor doesn’t reply anymore, it solely strikes initially, although slower than the left one in the course of the preliminary setup, then solely the left motor capabilities correctly.

Right here is the entire code with feedback subsequent to the IR-Receiver particular code:

#embody <Servo.h>
#embody <IRremote.h> //IR Receiver
Servo servo ;
const int trigPin = 13;
const int echoPin = 12;
const int servoPin = 11;
const int enAPin = 6;
const int in1Pin = 7;
const int in2Pin = 5;
const int in3Pin = 4;
const int in4Pin = 2;
const int enBPin = 3;
const int receiverPin = 9; //IR Receiver

IRrecv irrecv(receiverPin);  //IR Receiver
decode_results alerts; //IR Receiver


enum Motor { LEFT, RIGHT };
void go(enum Motor m, int velocity)
{
  digitalWrite (m == LEFT ? in1Pin : in3Pin , velocity > 0 ? HIGH : LOW );
  digitalWrite (m == LEFT ? in2Pin : in4Pin , velocity <= 0 ? HIGH : LOW );
  analogWrite(m == LEFT ? enAPin : enBPin, velocity < 0 ? -speed : velocity );
}



void testMotors ()
{
  static int velocity[8] = { 128, 255, 128, 0 , -128, -255, -128, 0};
  go(RIGHT, 0);
  for (unsigned char i = 0 ; i < 8 ; i++)
    go(LEFT, -speed[i]), delay(200);
  
  for (unsigned char i = 0 ; i < 8 ; i++)
    go(RIGHT, velocity[i]), delay(200);
}

unsigned int readDistance()
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  unsigned lengthy interval = pulseIn(echoPin, HIGH);
  return interval * 343 / 2000;
}



#outline NUM_ANGLES 7
unsigned char sensorAngle[NUM_ANGLES] = {60, 70, 80, 90, 100, 110, 120};
unsigned int distance [NUM_ANGLES];

void readNextDistance()
{
  static unsigned char angleIndex = 0;
  static signed char step = 1;
  distance[angleIndex] = readDistance();
  angleIndex += step ;
  if (angleIndex == NUM_ANGLES - 1) step = -1;
  else if (angleIndex == 0) step = 1;
  servo.write(sensorAngle[angleIndex]);
}


void setup () {
  Serial.start(9600);  //IR Receiver
  irrecv.enableIRIn();  //IR Receiver

  pinMode(trigPin , OUTPUT);
  pinMode(echoPin, INPUT);
  digitalWrite(trigPin, LOW);
  pinMode(enAPin, OUTPUT);
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
  pinMode(enBPin, OUTPUT);
  servo.connect(servoPin);
  servo.write(90);
  go(LEFT, 0);
  go(RIGHT, 0);
  testMotors();
  servo.write(sensorAngle[0]);
  delay(200);
  for (unsigned char i = 0 ; i < NUM_ANGLES ; i ++)
    readNextDistance(), delay (200);
}



void loop () {

 //IR Receiver:
  if (irrecv.decode(&alerts)) {
        Serial.println(alerts.worth);
        irrecv.resume();
        if (alerts.worth == 0xFFA25D) {
          Serial.println("yeees");
        }
  }


  readNextDistance ();
  
  unsigned char tooClose = 0;
  for (unsigned char i = 0 ; i < NUM_ANGLES ; i++)
    if (distance[i] < 200)
      tooClose = 1;
  
  if (tooClose) {
    go(LEFT, 180);
    go(RIGHT, -80);
  } else {
    go(LEFT, -180);
    go(RIGHT, 180);
  }
  delay (50);
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here