I am starting to learn how to code and have a few sample sketches completed and wanted to start working on a real project. I want to control DC motors with the IR Remote.
Hardware I am using:
Egeloo Mega 2560
L293D
IR remote and receiver
Solderless breadboard
Egeloo 9v Power Supply Module for breadboard
I have this code (below) that should:
Start the motor (clockwise) when "1" is pressed on the remote
Start the motor (counter-clockwise) when "2" is pressed on the remote
Stop the motor when "4" is pressed on the remote
It seems that "2" and "4" on the remote work as they should and I can start / stop the motor without issues repeatedly. BUT any time I press "1" on the remote the motor will start, but no other commands will work from the IR. I have to reset the board to stop the motor.
// Include IR Remote Library by Ken Shirriff
#include <IRremote.h>
#define One_button 0xFF30CF // code received from one button
#define Two_button 0xFF18E7 // code received from two button
#define Four_button 0xFF10EF // code received from four button
// Define sensor pin
int RECV_PIN = 4; //output pin of IR receiver to pin 4 of arduino
//initializing the pins for motors
int right_motor1 = 6; //pin 6 of arduino to pin 2 of l293d
int right_motor2 = 5; //pin 5 of arduino to pin 7 of l293d
// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN); //Arduino will take output of IR receiver from pin 4
decode_results output;
void setup(){
// Serial Monitor @ 9600 baud
Serial.begin(9600);
// Enable the IR Receiver
irrecv.enableIRIn();
//initializing all the pins where we have connected the motors as output pins
pinMode(right_motor1, OUTPUT);
pinMode(right_motor2, OUTPUT);
}
void loop(){
{
if (irrecv.decode(&output)){
unsigned int value = output.value;
switch(value) {
case One_button:
digitalWrite(right_motor1,HIGH);
digitalWrite(right_motor2,LOW);
break;
case Two_button:
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,HIGH);
delay(2000);
break;
case Four_button:
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,LOW);
break;
}
irrecv.resume();
}
}}
My next project is to get some software to draw out the circuit. Attached is a photo of the circuits.
How does the problem change if you swap the wires connected to Pin 5 and Pin 6? That will tell you if the problem is in the sketch or in the motor driver hardware.
johnwasser:
How does the problem change if you swap the wires connected to Pin 5 and Pin 6? That will tell you if the problem is in the sketch or in the motor driver hardware.
It seems that if I reverse Pin 5/6 that the problem reverses as well...
#define One_button 0xFF30CF // code received from one button
#define Two_button 0xFF18E7 // code received from two button
#define Four_button 0xFF10EF // code received from four button
unsigned int value = output.value;
Neither the defined values nor the IR remote key codes will fit in an int data type. Don't you think that the value variable should be a unsigned long?
And I do not see a wire from pin 16 (Vcc) to 5V on the breadboard.
I wired up an Uno, a IR remote decoder and a 754410 (a direct replacement for the L293) on a bread board according to your code. Made the above mentioned changes to your code and tested. I had to make changes to the IR remote codes to work with my remote, you can change them back for your setup. The code works as expected. 1, the motor turns CCW, 2, the motor turns CW, 4, the motor stops.
// Include IR Remote Library by Ken Shirriff
#include <IRremote.h>
#define One_button 0x4eb3c837 // code received from one button
#define Two_button 0x4eb308f7 // code received from two button
#define Four_button 0x4eb3f00f // code received from four button
// Define sensor pin
int RECV_PIN = 4; //output pin of IR receiver to pin 4 of arduino
//initializing the pins for motors
int right_motor1 = 6; //pin 6 of arduino to pin 2 of l293d
int right_motor2 = 5; //pin 5 of arduino to pin 7 of l293d
// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN); //Arduino will take output of IR receiver from pin 4
decode_results output;
void setup()
{
// Serial Monitor @ 9600 baud
Serial.begin(9600);
// Enable the IR Receiver
irrecv.enableIRIn();
//initializing all the pins where we have connected the motors as output pins
pinMode(right_motor1, OUTPUT);
pinMode(right_motor2, OUTPUT);
}
void loop()
{
{
if (irrecv.decode(&output))
{
unsigned long value = output.value; // **** change to unsigned long
//Serial.println(value, HEX);
switch (value)
{
case One_button:
digitalWrite(right_motor1, HIGH);
digitalWrite(right_motor2, LOW);
break;
case Two_button:
digitalWrite(right_motor1, LOW);
digitalWrite(right_motor2, HIGH);
//delay(2000); // ********************** delete delay
break;
case Four_button:
digitalWrite(right_motor1, LOW);
digitalWrite(right_motor2, LOW);
break;
}
irrecv.resume();
}
}
}