Go Down

Topic: Using IR Receiver to control robot direction (Read 836 times) previous topic - next topic

Relic

Right now I'm just testing that the arduino and IR receiver are outputting the correct info before testing it completely out with the motor controller and motors.

Currently, pressing up, down, and enter on the remote have the desired effect of increasing, decreasing, and resetting the speed of the motor. However, when I press left or right it has the same effect as enter in that s and i remain the same and the speeds of the left and right motor are 0.

I didn't bother including a circuit as I am receiving the IR code for the left and right buttons in the serial monitor, it just doesn't increment/decrement i. Any ideas as to why this would be the case?

Thanks.

I am open to any suggestions/corrections/etc.


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

//Right motor
const int enablepinA = 3;
const int drivepin1 = 2;
const int drivepin2 = 4;

//Left motor
const int enablepinB = 5;
const int drivepin3 = 6;
const int drivepin4 = 7;

const int RECV_PIN = 8;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;

int speed[] = {240, 180, 120, 0, 120, 180, 240}; //PMW values up to 255
int s = 3; //index for speed vector
byte d = 00000101; // index of high and low for driver pins

int turn[] = {240, 180, 0, 180, 240}; // PMW values up to 255
int i = 2;  //index for turning direction
int right = 0;
int left = 0;

void setup() {
    Serial.begin(9600);
    irrecv.enableIRIn();
    irrecv.blink13(true);
   
   /* pinMode(enablepinA, OUTPUT);
    pinMode(drivepin1, OUTPUT);
    pinMode(drivepin2, OUTPUT);
    pinMode(enablepinB, OUTPUT);
    pinMode(drivepin3, OUTPUT);
    pinMode(drivepin4, OUTPUT);*/
}

void loop() {
  //direction 1

   
  if (irrecv.decode(&results)){
 
    if (results.value == 0xFFFFFFFF)
      results.value = key_value;
 
    switch(results.value) {
     
      case 0x9EB92:
      Serial.println("up");
      s++;
      i=2;
      break;
       
      case 0x5EB92:
      Serial.println("down");
      s--;
      i=2;
      break;
           
      case 0x3EB92:
      Serial.println("right");
      s=3;
      i = i + 1;
      break;
     
      case 0xDEB92:
      Serial.println("left");
      s=3;
      i = i - 1;
      break;
         
      case 0xD0B92:
      Serial.println("enter");
      left=0;
      right=0;
      s=3;
      i=2;
      break;
    }
         
    key_value = results.value;
    irrecv.resume();
  }
  if (s>6)
    s=6;
  if (s<0)
    s=0;
  if (i>4)
    i=4;
  if (i<0)
    i=0;
         
  if (i=2) {
   
    if (s>=3) {
      d = 00000101;
    }
   
    else {
      d = 00001010;
    }
   
    left = speed[s];
    right = speed[s];
  }
 
  if (i != 2) {
   
    if (i>2) {
      left = turn[i];
      right = 120;
    }
   
    else {
      right = turn[i];
      left = 120;
    }
  }
  Serial.print(results.value);
  Serial.print(",");
  Serial.print(left);
  Serial.print(",");
  Serial.print(right);
  Serial.print(",");
  Serial.print(s);
  Serial.print(",");
  Serial.print(i);
  Serial.print(",");
  Serial.println(d);

delay(1000);

  /*digitalWrite(drivepin1, bitRead(d,0));
  digitalWrite(drivepin2, bitRead(d,1));
  analogWrite(enablepinA, right);
 
  digitalWrite(drivepin3, bitRead(d,2));
  digitalWrite(drivepin4, bitRead(d,3));
  analogWrite(enablepinB, left);    */

}

jremington

Quote
it just doesn't increment/decrement i
How do you know? Put a print(i) statement after the increment and post the result.

Code: [Select]
      case 0x3EB92:
      Serial.println("right");
      s=3;
      i = i + 1;
      break;

Relic

For one, I did a bunch of Serial prints at the end of my and thats where it showed i not incrementing or decrementing. I tried what you asked and it did show i going up and down but not past 1 or 3 or to through the end of the switch, which is what I need.

Any ideas why that might be?

I put the serial results under 'code' just so its easier to read.
Again, first value is the IR code, then left pmw, right pmw, s value, i value, and direction motors

Code: [Select]
0,0,0,3,2,65
0,0,0,3,2,65
0,0,0,3,2,65
up
650130,120,120,4,2,65
650130,120,120,4,2,65
650130,120,120,4,2,65
left
1
912274,0,0,3,2,65
912274,0,0,3,2,65
left
1
912274,0,0,3,2,65
left
1
912274,0,0,3,2,65
912274,0,0,3,2,65
right
3
256914,0,0,3,2,65
right
3
256914,0,0,3,2,65
256914,0,0,3,2,65
256914,0,0,3,2,65
256914,0,0,3,2,65
256914,0,0,3,2,65

jremington

#3
Feb 21, 2018, 05:03 pm Last Edit: Feb 21, 2018, 05:04 pm by jremington
The posted results don't mean anything to me.

Add a print(i) statement after i is incremented, and post the results for several presses of the "right" button. Post the modified code, too, using code tags.

Relic

Here it is. It's no different other than removing the serial prints I had at the end to give the results I need.


left
1
left
1
left
1
right
3
right
3

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

//Right motor
const int enablepinA = 3;
const int drivepin1 = 2;
const int drivepin2 = 4;

//Left motor
const int enablepinB = 5;
const int drivepin3 = 6;
const int drivepin4 = 7;

const int RECV_PIN = 8;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;

int speed[] = {240, 180, 120, 0, 120, 180, 240}; //PMW values up to 255
int s = 3; //index for speed vector
byte d = 00000101; // index of high and low for driver pins

int turn[] = {240, 180, 0, 180, 240}; // PMW values up to 255
int i = 2;//index for turning direction
int right = 0;
int left = 0;

void setup() {
    Serial.begin(9600);
    irrecv.enableIRIn();
    irrecv.blink13(true);
   
   /* pinMode(enablepinA, OUTPUT);
    pinMode(drivepin1, OUTPUT);
    pinMode(drivepin2, OUTPUT);
    pinMode(enablepinB, OUTPUT);
    pinMode(drivepin3, OUTPUT);
    pinMode(drivepin4, OUTPUT);*/
}

void loop() {
  //direction 1

   
  if (irrecv.decode(&results)){
 
    if (results.value == 0xFFFFFFFF)
      results.value = key_value;
 
    switch(results.value) {
     
      case 0x9EB92:
      Serial.println("up");
      s++;
      i=2;
      break;
       
      case 0x5EB92:
      Serial.println("down");
      s--;
      i=2;
      break;
           
      case 0x3EB92:
      Serial.println("right");
      s=3;
      i = i + 1;
      Serial.println(i);
      break;
     
      case 0xDEB92:
      Serial.println("left");
      s=3;
      i = i - 1;
      Serial.println(i);
      break;
         
      case 0xD0B92:
      Serial.println("enter");
      left=0;
      right=0;
      s=3;
      i=2;
      break;
    }
         
    key_value = results.value;
    irrecv.resume();
  }
  if (s>6)
    s=6;
  if (s<0)
    s=0;
  if (i>4)
    i=4;
  if (i<0)
    i=0;
         
  if (i=2) {
   
    if (s>=3) {
      d = 00000101;
    }
   
    else {
      d = 00001010;
    }
   
    left = speed[s];
    right = speed[s];
  }
 
  if (i != 2) {
   
    if (i>2) {
      left = turn[i];
      right = 120;
    }
   
    else {
      right = turn[i];
      left = 120;
    }
  }
}

slipstick

It doesn't actually have quite "the same effect as Enter", which sets s=3 and i=2. It has the effect that you would expect to see IF you got an Enter code immediately preceding each Left or Right. So i is set to 2 and then either incremented or decremented as it should be.

Presumably you have checked that you are only receiving a single code when you press Left or Right?

Steve

Relic

When testing it, I had the serial monitor pick up the code from each button press and only showed it receiving the left or right. I added the delay because I'm using a Sony protocol remote and uses a signal repeated 3 times to be considered 1 press and also found it difficult to only have one press and not a holding press.

Regardless, why would i increment and then s and i go to their starting points? The only place I could figure is the conditionals after the switch cases but doesn't seem likely.

slipstick

I can't see what's going on.

I'd put a print(i) immediately before the switch statement. That might give you an idea of when the value is getting messed up.

If that doesn't do it I think my next step would be to narrow things down by temporarily commenting out everything after the irrecv.resume(); to see if it is actually failing to increment/decrement or it's getting reset when you don't expect it. At least then you'll know which section of code is doing something odd.

Steve

Go Up