Hello!
I am looking for help with this code/circuit. in TinkerCad, this code/setup works fine. In real life, I can change the direction of the motor with the remote but cannot get the motor to stop on command.
The IRL wiring is exact to the TinkerCad, save the 9V actually being a 4-AA battery pack, so I know my voltage for powering the motor is in the 4V range. I figured Tinkercad is easier to see my circuit on.
I will also paste my code. Note that IRL the commands are changed to match what my specific IR remote receives with each button.
While I am here, it is worth noting that a while ago, I also had tried controlling two smaller motors and a servo with the IR remote, but the IR receiver would start blinking rapidly and lose all functionality once the motors were turned on. I fear this is still happening with this newer motor, although I can still control direction, just not powering off. Eventually I need to power 4 motors and have remote control over them.
I know it is best to power the motors with external power, which I am doing. Otherwise, I have heard of MOSFETs and external control chips. Right now all I have is an L293D H-bridge Motor Driver. I know DC motors can induce noise that messes with IR receivers.
Any tips or tricks to get clean IR commands while using DC motors?
Thanks,
Blake
//Implement libraries and define pins
#include <IRremote.h>
#include <IRremote.hpp>
#define RECEIVE 6
#define Mod 3
#define M2 2
#define M4 4
//int that stores the last command pressed on the IR remote
int command = 0;
void setup(){
//Begin scanning for remote inputs and start Serial Monitor
Serial.begin(115200);
Serial.println("Enabling IR");
IrReceiver.begin(RECEIVE, ENABLE_LED_FEEDBACK);
Serial.print("Ready to receieve.");
//Set pins for motor
pinMode(Mod, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(M4, OUTPUT);
}
void loop(){
if (IrReceiver.decode()) {
//* Print a short summary of received data
IrReceiver.printIRResultShort(&Serial);
IrReceiver.printIRSendUsage(&Serial);
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));
// We have an unknown protocol here, print more info
IrReceiver.printIRResultRawFormatted(&Serial, true);
}
//Storing last command in "command" variable
command = IrReceiver.decodedIRData.command;
//Print the current command for debug and assignment
Serial.println();
//Start receiving next value after last
IrReceiver.resume(); // Enable receiving of the next value
}
//Simple if statements. 6th button makes motor go forward
if(command == 6){
digitalWrite(M2, HIGH);
digitalWrite(M4, LOW);
analogWrite(Mod, 255);
}
//4th button makes motor reverse
else if(command == 4){
digitalWrite(M2, LOW);
digitalWrite(M4, HIGH);
analogWrite(Mod, 255);
}
//power button makes motor stop.
else if(command == 0){
digitalWrite(M2, LOW);
digitalWrite(M4, LOW);
analogWrite(Mod, 0);
}
}

