Trouble with trying to control motors with IR

#include <IRremote.h>

int reciever = 2;
int m1pos = 10;
int m1neg = 5;
int m2pos = 9;
int m2neg = 6;

int speed = 0;
bool right = false;
bool left = false;

IRrecv irrecv(reciever);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn();
  
  pinMode(m1pos,OUTPUT);
  pinMode(m1neg,OUTPUT);
  pinMode(m2pos,OUTPUT);
  pinMode(m2neg,OUTPUT);
  
}

void loop()
{
  //IF receiver
  if (irrecv.decode(&results)){
        Serial.println(results.value, HEX);
    switch(results.value){
    case 0xFD08F7:
    	digitalWrite(3,HIGH);
      	digitalWrite(4,LOW);
      	digitalWrite(7,LOW);
      	left = false;
      	right = false;
    	break; 
    case 0xFD8877:
      	digitalWrite(4,HIGH);
      	digitalWrite(3,LOW);
      	digitalWrite(7,LOW);
      	left = false;
      	right = false;
      	break; 
    case 0xFD48B7:
      	digitalWrite(7,HIGH);
      	digitalWrite(4,LOW);
      	digitalWrite(3,LOW);
      	left = false;
      	right = false;
  		break;
    case 0xFD807F:
      	speed += 1;
      	Serial.println(speed);
      	left = false;
      	right = false;
      	break;
    case 0xFD906F:
      	speed -=1;
      	Serial.println(speed);
      	left = false;
      	right = false;
      	break;
    case 0xFD609F:
      	right = false;
      	left = true;
      	break;
    case 0xFD20DF:
      	left = false;
      	right = true;
      	break;
    case 0xFD00FF:
      	speed = 0;
      	left = false;
      	right = false;
      	break;
    }
    irrecv.resume();
    
    //Motor Variation
    if(speed == 0){
      analogWrite(m1pos,0);
      digitalWrite(m1neg,LOW);
      analogWrite(m2pos,0);
      digitalWrite(m2neg,LOW);
    }
    if(speed == 1){
      analogWrite(m1pos,127);
      digitalWrite(m1neg,LOW);
      analogWrite(m2pos,127);
      digitalWrite(m2neg,LOW);
    }
    if(speed == 2){
      analogWrite(m1pos,255);
      digitalWrite(m1neg,LOW);
      analogWrite(m2pos,255);
      digitalWrite(m2neg,LOW);
    }
    if(speed == -1){
      analogWrite(m1neg,127);
      digitalWrite(m1pos,LOW);
      analogWrite(m2neg,127);
      digitalWrite(m2pos,LOW);
    }
    if(speed == -2){
      analogWrite(m1neg,255);
      digitalWrite(m1pos,LOW);
      analogWrite(m2neg,255);
      digitalWrite(m2pos,LOW);
    }
    //Directions
    if(right == true){
      analogWrite(m1pos,255);
      digitalWrite(m1neg,LOW);
      analogWrite(m2pos,0);
      digitalWrite(m2neg,LOW);
    }
    if(left == true){
      analogWrite(m2pos,255);
      digitalWrite(m2neg,LOW);
      analogWrite(m1pos,0);
      digitalWrite(m1neg,LOW);
    }
  }
  delay(100);
}

Hi, everybody! I am trying to control two motors with a IR receiver and a IR remote.
I have managed to control the speed of the motors using the volume keys on my IR remote but as soon as the variable I am using goes negative the results the IR receiver return are different. This means when the motors go backwards they are stuck in that state, I don't really understand how this is happening. I have tried changing the button that varies the variable speed but that didn't work. I have also attached a picture of my electronics.
Would greatly appreciate any help.

For starters lose the 9V battery it is worthless in most Arduino applications. Try with mechanical buttons first and when they work add the IR, it is much easier then trying to debug the whole thing at once. It can be like the dog chasing the fuzzy thing that is following him. A schematic, not a frizzy drawing would be a lot easier to read.

The code will not compile. It is incomplete. Post the whole program.

Are there really no current limit resistors on the LEDs?

Is the 9V battery really connected to the Uno 5V as shown in the diagram?

looks like sometimes you use digitalWrite() to control an output pins and sometimes you use analogWrite(). in the speed ==0 case you use analogwrite to set m1pos and m2pos to zero.

it would be better to consistently use analogWrite() and it would be better to only use those functions in a single sub-function and to call that sub-function to set a speed

#include <IRremote.h>

int reciever = 2;
int m1pos = 10;
int m1neg = 5;
int m2pos = 9;
int m2neg = 6;

int speed = 0;
bool right = false;
bool left = false;

IRrecv irrecv(reciever);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn();
  
  pinMode(m1pos,OUTPUT);
  pinMode(m1neg,OUTPUT);
  pinMode(m2pos,OUTPUT);
  pinMode(m2neg,OUTPUT);
  
}

void loop()
{
  //IF receiver
  if (irrecv.decode(&results)){
        Serial.println(results.value, HEX);
    switch(results.value){
    case 0xFD08F7:
    	digitalWrite(3,HIGH);
      	digitalWrite(4,LOW);
      	digitalWrite(7,LOW);
      	left = false;
      	right = false;
    	break; 
    case 0xFD8877:
      	digitalWrite(4,HIGH);
      	digitalWrite(3,LOW);
      	digitalWrite(7,LOW);
      	left = false;
      	right = false;
      	break; 
    case 0xFD48B7:
      	digitalWrite(7,HIGH);
      	digitalWrite(4,LOW);
      	digitalWrite(3,LOW);
      	left = false;
      	right = false;
  		break;
    case 0xFD807F:
      	speed += 1;
      	Serial.println(speed);
      	left = false;
      	right = false;
      	break;
    case 0xFD906F:
      	speed -=1;
      	Serial.println(speed);
      	left = false;
      	right = false;
      	break;
    case 0xFD609F:
      	right = false;
      	left = true;
      	break;
    case 0xFD20DF:
      	left = false;
      	right = true;
      	break;
    case 0xFD00FF:
      	speed = 0;
      	left = false;
      	right = false;
      	break;
    }
    irrecv.resume();
    
    //Motor Variation
    if(speed == 0){
      analogWrite(m1pos,0);
      digitalWrite(m1neg,LOW);
      analogWrite(m2pos,0);
      digitalWrite(m2neg,LOW);
    }
    if(speed == 1){
      analogWrite(m1pos,127);
      digitalWrite(m1neg,LOW);
      analogWrite(m2pos,127);
      digitalWrite(m2neg,LOW);
    }
    if(speed == 2){
      analogWrite(m1pos,255);
      digitalWrite(m1neg,LOW);
      analogWrite(m2pos,255);
      digitalWrite(m2neg,LOW);
    }
    if(speed == -1){
      analogWrite(m1neg,127);
      digitalWrite(m1pos,LOW);
      analogWrite(m2neg,127);
      digitalWrite(m2pos,LOW);
    }
    if(speed == -2){
      analogWrite(m1neg,255);
      digitalWrite(m1pos,LOW);
      analogWrite(m2neg,255);
      digitalWrite(m2pos,LOW);
    }
    //Directions
    if(right == true){
      analogWrite(m1pos,255);
      digitalWrite(m1neg,LOW);
      analogWrite(m2pos,0);
      digitalWrite(m2neg,LOW);
    }
    if(left == true){
      analogWrite(m2pos,255);
      digitalWrite(m2neg,LOW);
      analogWrite(m1pos,0);
      digitalWrite(m1neg,LOW);
    }
  }
  delay(100);
}

Hi everybody, sorry for the inconvenience I didn't post the complete code, it works now I had to switch the pin 11 to pin 5 for some reason I didn't really understand why or how but I fixed it. I also included a new picture of the new electronics.
pd: I don't really know how to do schematics, as I am relatively new to this. sorry :frowning:

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