I am making a IR remote controlled motor but it is not working

I am new to coding but it is not working. The problem is described in the code comments. For my circuit i just combined these too screenshots and attempted to combine the code. One screenshot is the circuit for the motor the other is for the IR receiver. I have pretty much never coded c++ before so i mainly just got Arduino to work with the physical circuits. Please help.

Also, my computer is about to die so i will respond on my other account on my phone.


//2024.04.01
/*
My code is supposed to run a motor forward and backward, using the signals from an elegoo IR Remote and reciver module. 
I am using a arduino mega. I also have a power module which is connected up to the bread board.
I am controling the motor with a L293D motor controller.
The ports things are hooked up to are as follow,
  Motor controler
    Enable- pin 5
    DIRB- pin 4
    DIRA- pin 3

  IR reciver
    G- ground (of power module)
    R- vcc (of power module)    
    Y- pin 11

The code is supposed to make it so that when you push:
  volume-up it spins forward;
  pause it stops
  volume-down it spins reverse

The problem  
  Whenever i press any button on the remote it imediatly starts moving the motor forward.
  I only coded C++ a few times before, so most of this code is pulled together from 2 tutorials and the internet.
  I have tried coding solutions but with my limited knowlage, it breaks the code and will not compile.

*/



#include "IRremote.h"

#define ENABLE 5
#define DIRA 3
#define DIRB 4

int MOVEMENT = 3;
int ONE = 1;
int ZERO = 0;
int TWO = 2;
int i;
int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
//vairable uses to store the last decodedRawData
uint32_t last_decodedRawData = 0;
/*-----( Function )-----*/
void translateIR() // takes action based on IR code received
{
  // Check if it is a repeat IR code 
  if (irrecv.decodedIRData.flags)
  {
    //set the current decodedRawData to the last decodedRawData 
    irrecv.decodedIRData.decodedRawData = last_decodedRawData;
    Serial.println("REPEAT!");
  } else
  {
    //output the IR code on the serial monitor
    Serial.print("IR code:0x");
    Serial.println(irrecv.decodedIRData.decodedRawData, HEX);
  }
  //map the IR code to the remote key
  switch (irrecv.decodedIRData.decodedRawData)
  {
    case 0xBA45FF00: Serial.println("POWER"); break;
    case 0xB847FF00: Serial.println("FUNC/STOP"); break;

    case 0xB946FF00: MOVEMENT = 2; Serial.println("forward top code"); break;//this is to be volume up,   this should spin the motor forward
    case 0xBB44FF00: Serial.println("FAST BACK");    break;
    case 0xBF40FF00: MOVEMENT = 0; Serial.println("stop top code");    break;//this is to be pause, this   should stop the motor
    case 0xBC43FF00: Serial.println("FAST FORWARD");   break;
    case 0xF807FF00: Serial.println("DOWN");    break;
    case 0xEA15FF00: MOVEMENT = 2; Serial.println("reverse top code");    break;//this is to be volume down,   this should spin the motor in reverse
    case 0xF609FF00: Serial.println("UP");    break;
    case 0xE619FF00: Serial.println("EQ");    break;
    case 0xF20DFF00: Serial.println("ST/REPT");    break;
    case 0xE916FF00: Serial.println("0");    break;
    case 0xF30CFF00: Serial.println("1");    break;
    case 0xE718FF00: Serial.println("2");    break;
    case 0xA15EFF00: Serial.println("3");    break;
    case 0xF708FF00: Serial.println("4");    break;
    case 0xE31CFF00: Serial.println("5");    break;
    case 0xA55AFF00: Serial.println("6");    break;
    case 0xBD42FF00: Serial.println("7");    break;
    case 0xAD52FF00: Serial.println("8");    break;
    case 0xB54AFF00: Serial.println("9");    break;
    default:
      Serial.println(" other button   ");
  }// End Case
  //store the last decodedRawData
  last_decodedRawData = irrecv.decodedIRData.decodedRawData;
  delay(500); // Do not get immediate repeat
} //END translateIR

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn(); // Start the receiver

  //---set pin direction
  pinMode(ENABLE,OUTPUT);
  pinMode(DIRA,OUTPUT);
  pinMode(DIRB,OUTPUT);

  digitalWrite(ENABLE,HIGH); // enable on
}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
    if (irrecv.decode()) // have we received an IR signal?
  {
    translateIR();
    if(MOVEMENT = ONE){
      digitalWrite(DIRA,HIGH); //forward
      digitalWrite(DIRB,LOW);
      Serial.println("FORWARD");
      irrecv.resume(); // receive the next value
    }
    else{
      if(MOVEMENT = TWO){
        digitalWrite(DIRA,LOW);  //reverse
        digitalWrite(DIRB,HIGH);
        Serial.println("REVERSE");
        irrecv.resume(); // receive the next value
      }
      else{
      if(MOVEMENT = ZERO){
        digitalWrite(DIRA,LOW);
        digitalWrite(DIRB,LOW);
        Serial.println("OFF");
        irrecv.resume(); // receive the next value
      }
    }    
    
  }
  }}
  

Which type of IR controller you are using?
if the below, use this IR Controller library and refer this Arduino IR car tutorial

1 Like

Verify the IRremote and IRreceiver work (you can see what you sent) without a motor.

1 Like

Hello this is my other account
Sorry for taking so long

I don’t know what type it is but here is a picture

It works, it won’t let me attach a video to this of the serial monitor but here is a photo

IMG_0577

You should not have two accounts.

@jake2024dowh @jake-m2024

These lines are written wrong. "=" is wrong, "==" is right

    if (MOVEMENT = ONE) {

      if (MOVEMENT = TWO) {

        if (MOVEMENT = ZERO) {
1 Like

Sorry

It’s because I have one on my computer and one on my phone

You should not have two accounts.

Ohh ok thank you

Do you know how I can link the accounts.

Sorry I am new

Click the "flag" on any of your posts and ask the moderator to link them. Thank you.
You can use one account on multiple devices.

1 Like

Thank you

Your sketch (without motors) "works"... but nearly every button wants to send the motors forward.

Why does it try to send the motors forward

Also here is a picture of the circuit in case that helps

Because the line immediately after translateIR() says to do so... when "MOVEMENT = ONE"... that is an assignment ( = ), not a comparison ( == ).

    translateIR();
    if (MOVEMENT = ONE) {
      digitalWrite(DIRA, HIGH); //forward
      digitalWrite(DIRB, LOW);
      Serial.println("FORWARD");
      irrecv.resume(); // receive the next value
    }

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