Home > Blockchain >  Code executing only one function regardless of if else statements
Code executing only one function regardless of if else statements

Time:11-08

I'm making an Arduino robot, which is a simple line-following robot, but if I press a button on an IR remote, it continues to line follow, but at a different speed. Here's the code that I've written:

#include <IRremote.h>
const int RECV_PIN = A5;
IRrecv irrecv(RECV_PIN);
decode_results results;
int cs;         // CENTER SENSOR
int lmt1 = 5;   // LEFT MOTOR 1
int lmt2 = 3;   // LEFT MOTOR 2
int rmt1 = 6;   // RIGHT MOTOR 1
int rmt2 = 11;  // RIGHT MOTOR 2

void setup() {
  pinMode(8, INPUT);
  pinMode(lmt1, OUTPUT);
  pinMode(lmt2, OUTPUT);
  pinMode(rmt1, OUTPUT);
  pinMode(rmt2, OUTPUT);

  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop() {
  {
    if (irrecv.decode(&results)) {
      Serial.println(results.value, HEX);
      irrecv.resume();
    }
  }

  cs = digitalRead(8);

  if (cs == LOW) {
    analogWrite(lmt1, 0);
    analogWrite(lmt2, 175);
    analogWrite(rmt1, 175);
    analogWrite(rmt2, 0);
  }
  if (cs == HIGH) {
    analogWrite(lmt1, 175);
    analogWrite(lmt2, 0);
    analogWrite(rmt1, 0);
    analogWrite(rmt2, 175);
  }
  if ((results.value == 0xFE58A7) && (cs == HIGH)) {
    analogWrite(lmt1, 0);
    analogWrite(lmt2, 0);
    analogWrite(rmt1, 100);
    analogWrite(rmt2, 0);
  }
  if ((results.value == 0xFE58A7) && (cs == LOW)) {
    analogWrite(lmt1, 0);
    analogWrite(lmt2, 0);
    analogWrite(rmt1, 100);
    analogWrite(rmt2, 0);
  }
}

The robot follows the line, but already at a lower speed and the IR remote has no effect on the speed of the robot. I appreciate any help. (I've used an Arduino board and an L298N motor driver)

CodePudding user response:

I know nothing about Arduino, but I'm going to take a wild guess. This code

if (digitalRead(results.value == 0xFE58A7) && (cs == HIGH)) {

should be

if (digitalRead(results.value) == 0xFE58A7 && (cs == HIGH)) {

In other words you got your brackets in the wrong place.

Or perhaps you didn't mean to call digitalRead at all. Maybe this was the code you meant to write

if ((results.value == 0xFE58A7) && (cs == HIGH)) {

As I said I know nothing about Arduino, but your code looks odd.

You have the same error a few lines later.

CodePudding user response:

In addition to the changes from john you should use else ifs:

  if (results.value == 0xFE58A7 && (cs == LOW)) {
    analogWrite(lmt1, 0);
    analogWrite(lmt2, 0);
    analogWrite(rmt1, 100);
    analogWrite(rmt2, 0);
  } else if (results.value == 0xFE58A7 && (cs == HIGH)) {
    analogWrite(lmt1, 0);
    analogWrite(lmt2, 0);
    analogWrite(rmt1, 100);
    analogWrite(rmt2, 0);
  } else if (cs == LOW) {
    analogWrite(lmt1, 0);
    analogWrite(lmt2, 175);
    analogWrite(rmt1, 175);
    analogWrite(rmt2, 0);
  } else if (cs == HIGH) {
    analogWrite(lmt1, 175);
    analogWrite(lmt2, 0);
    analogWrite(rmt1, 0);
    analogWrite(rmt2, 175);
  }

The loop() function will repeat infinite times. If you only use if, each one that resolves to true will run. So in your case at least one will run, but if results.value == 0xFE58A7 is true, two of your statements will be true and switch your pins at a very high speed.

The motor has a much slower reaction time and may behave weird or not at all.

To debug your code I recommend writing Serial.println in each if and a delay(100) at the end of the loop. With that you can actually see what your code is currently doing.

Here is a small simulation similar to your example. You have a switch for your cs variable and can use the remote with the 1 key to jump into the first two statements. With any other key you get back.

  • Related