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
}
}
}
}}