Go Down

Topic: Arduino robot and IR remote (Read 105 times) previous topic - next topic

LookaTriffan

I have built an Arduino Nano based robot controlled by an Adafruit IR remote. As my code shows below, is is designed to start the functions controlling the motors and to wait for code 28 to be received in order to stop the motors using the appropriate command. However when I press a button on the remote, the motors move but do not stop when I press the button on the remote. They only stop when I reset the Arduino. Is there any way to improve the code?

Thanks

Code: [Select]
#include <Adafruit_NECremote.h>

#define IR 2
#define ENA 11
#define IN1 7
#define IN2 6
#define IN3 5
#define IN4 4
#define ENB 3

Adafruit_NECremote remote(IR);

void forwd(){
  digitalWrite(IN1, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  digitalWrite(IN2, LOW);
}

void reverse(){
  digitalWrite(IN1, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(IN2, HIGH);
}

void left(){
  digitalWrite(IN1, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(IN2, HIGH);
}

void right(){
  digitalWrite(IN1, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(IN2, LOW);
}

void stopall(){
  digitalWrite(IN1, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(IN2, LOW);
}
void setup() {
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENB, OUTPUT);
  analogWrite(ENA, 200);
  analogWrite(ENB, 200);
}
int lastcode = -1;
int checkCode(){
    int c = remote.listen();
  int code = 0;
  if (c >= 0){
    code = c;
    lastcode = c;
  }else if (c == -2) {
    code = lastcode;
  }return code;
}
void loop() {

while (checkCode() == 24){
    if (checkCode() == 28){
      stopall();
      break;
    }forwd();
  }while (checkCode() == 82){
    if (checkCode() == 28){
      stopall();
      break;
    }reverse();
  }while (checkCode() == 8){
    if (checkCode() == 28){
      stopall();
      break;
    }left();
  }while (checkCode() == 90){
    if (checkCode() == 28){
      stopall();
      break;
    }right();
  }
}

Perehama

add some Serial statements to discover the value of checkCode. You only take action "While" on 4 values. What happens if say checkCode == -2 or 0. Just put Serial.begin(9600); in your setup() and at the last line just before return code; put Serial.print(code);
The slender snake hissed as she slithered slowly over the rocks, grass and leaves.

UKHeliBob

Code: [Select]
  while (checkCode() == 24)
  {
    if (checkCode() == 28)

is checkCode() ever going to be 28 while checkCode() is 24 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

LookaTriffan

Code: [Select]
  while (checkCode() == 24)
  {
    if (checkCode() == 28)

is checkCode() ever going to be 28 while checkCode() is 24 ?
Is there any other way to refresh the code while the loop is running?

Perehama

#4
Jan 22, 2019, 09:23 pm Last Edit: Jan 22, 2019, 09:25 pm by Perehama
Is there any other way to refresh the code while the loop is running?
Code: [Select]
#include <Adafruit_NECremote.h>

#define IR 2
#define ENA 11
#define IN1 7
#define IN2 6
#define IN3 5
#define IN4 4
#define ENB 3

Adafruit_NECremote remote(IR);

int lastcode = -1;

void setup() {
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENB, OUTPUT);
  analogWrite(ENA, 200);
  analogWrite(ENB, 200);
}
void loop() {
  int c = remote.listen();
  if (c == 24) forwd();
  else if (c == 82)reverse();
  else if (c == 8)left();
  else if (c == 90)right();
  else if (c == 28)stopall();
}

void forwd() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  digitalWrite(IN2, LOW);
}

void reverse() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(IN2, HIGH);
}

void left() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(IN2, HIGH);
}

void right() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(IN2, LOW);
}

void stopall() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(IN2, LOW);
}



The slender snake hissed as she slithered slowly over the rocks, grass and leaves.

Go Up