Code for autonomous wheelchair does not work

I am using this code for a project for school. But the ir sensors do not allow the motors to stop. I can't seem to find the problem.

#define enA 7
#define in1 10
#define in2 11
#define enB 6
#define in3 8
#define in4 9

int motorSpeedA = 0;
int motorSpeedB = 0;
int xAxis = 0; // Lees X-as van de joysticklloop))
int yAxis = 0; // Lees Y-as van de joystick
int IRSensor1 = 2; // Verbind IR-sensor module met Arduino pin 2
int IRSensor2 = 3; // Verbind IR-sensor module met Arduino pin 3

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  Serial.begin(115200); // Initialiseer Serial met baudrate 115200
  Serial.println("Serial Working");

  pinMode(IRSensor1, INPUT); // IR Sensor pin als INPUT
  pinMode(IRSensor2, INPUT);
}

void loop() {
  xAxis = analogRead(A0); // Lees X-as van de joystick
  yAxis = analogRead(A1); // Lees Y-as van de joystick

  // Lees waarden van IR-sensoren
  int irSensor1Value = analogRead(IRSensor1);
  int irSensor2Value = analogRead(IRSensor2);

  // Controleer of er een obstakel is gedetecteerd door IR-sensor 1
  if (irSensor1Value < 200) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
  }

  // Controleer of er een obstakel is gedetecteerd door IR-sensor 2
   if (irSensor2Value < 200) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
  }
  else {

  // Joystickbesturing voor motoren
 if (yAxis < 470) {
    // Achteruit
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  } else if (yAxis > 550) {
    // Vooruit
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  } else {
    // Stop
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  // Joystickbesturing voor draaien
  if (xAxis < 470) {
    int xMapped = map(xAxis, 470, 0, 0, 255);
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
  } else if (xAxis > 550) {
    int xMapped = map(xAxis, 550, 1023, 0, 255);
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
  }

  // Beperk motorsnelheden tot 0-255
  if (motorSpeedA < 0) {
    motorSpeedA = 0;
  }
  if (motorSpeedA > 255) {
    motorSpeedA = 255;
  }
  if (motorSpeedB < 0) {
    motorSpeedB = 0;
  }
  if (motorSpeedB > 255) {
    motorSpeedB = 255;
  }

  // Stuur PWM-signalen naar de motoren
  analogWrite(enA, motorSpeedA);
  analogWrite(enB, motorSpeedB);
}
}

Welcome to the forum

As your post does not relate to the operation of the IDE itself it has been moved to the Programming category of the forum

suggest you add some Serial.println() statement at critical points to display values of variables, sensors, etc and show the flow of control
this may give you some idea of where the problem is

what microcontroller are you using?
upload a schematic of the circuit and how you power it?

1 Like

One problem that I see.

If irSensor1Value is less than 200 but irSensor2Value is greater than or equal to 200, the joystick will still do it's job.

I would combine the test for the sensor values in one if condition

if (irSensor1Value < 200 || irSensor2Value < 200)
{
  ...
  ...
}
else
{
  ...
  ...
}

AVR chips have one Analog Converter with many pins.
When you switch from one pin to the next, there must be a time for the pin-to-ADC circuit to settle or the read will be inaccurate!

What the datasheets reccomend is to read twice but ignore the first.
The first read will clear the circuit, allowing a good second read.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.