Hello.
I am using a motor shield (I think is called L293D shield) with IR sensor and want to control the motors on/off with the remote.
I think there is a conflict between the motor library and the IR library.
These are the two libraries:
If I enable this line, the motors do not start anymore, the IR and servo plug on motor controller still work ok:
irrecv.enableIRIn();
Any tips on how to get around this? Are there any other libraries available for the above purpose?
I noticed that the files of the infrared library refers to Pin 13 and other pins that I think are in use by the motor shield, so that might be the problem, but I am new at this.
If it helps, this is my full code:
// IR SETUP, leave it above motor INCLUDE to avoid an ERROR
#include <IRremote.h>
int RECV_PIN = 14;
IRrecv irrecv(RECV_PIN);
decode_results results;
// MOTOR SETUP
#include <AFMotor.h>
AF_DCMotor left_motor(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor right_motor(2, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
// SERVO SETUP
#include <Servo.h>
Servo servo1;
// some USER VARIABLES
String ir_code = "none";
String ir_last_code = "none";
// motors min and max speed, so we can easy accelerate with percent betwen these two
int motor_min_speed = 200;
int motor_max_sped = 200;
String motor_status = "off";
int servo_position = 0;
int servo_min_position = 50;
int servo_max_position = 125;
// ir string alues
String ir_code_stop = "3782930447";
String ir_code_up = "3782871287";
String ir_code_down = "3782891687";
String ir_code_left = "3782897807";
String ir_code_right = "3782914127";
// 1,2,3 buttons on remote
String ir_code_1 = "3782901887";
String ir_code_2 = "3782885567";
String ir_code_3 = "3782918207";
// the repeat code, this value means that last button is kept pressed
String ir_code_repeat = "4294967295";
// IR
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.println("Could not decode message");
}
else {
if (results->decode_type == NEC) {
//Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
// Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
// Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
// Serial.print("Decoded RC6: ");
}
Serial.print(results->value, HEX);
}
}
void setup() {
// set up Serial library at 9600 bps
Serial.begin(9600);
// set the speed to 200/255
left_motor.setSpeed(50);
right_motor.setSpeed(50);
// Start the IR receiver
// this line seems to jam the motor start ... ?!
irrecv.enableIRIn();
// set the servo
servo1.attach(10); // the 2 servos are on pin 9 and 10
//
servo_position = servo1.read();
}
void loop() {
// start with motor on, just for testing
left_motor.run(FORWARD);
if (irrecv.decode(&results)) {
// Serial.println(results.value, HEX);
ir_code = results.value;
if(ir_code == ir_code_repeat){
ir_code = ir_last_code;
}
// go LEFT
if(ir_code == ir_code_left){
if(servo_position>servo_min_position){
servo_position-=5;
}
}
// go RIGHT
if(ir_code == ir_code_right){
if(servo_position<servo_max_position){
servo_position+=5;
}
}
// go UP
if(ir_code == ir_code_up && motor_status != "forward"){
Serial.println("going up");
left_motor.run(FORWARD);
motor_status = "forward";
}
// go DOWN
if(ir_code == ir_code_down && motor_status != "backward"){
Serial.println("going down");
left_motor.run(RELEASE);
right_motor.run(RELEASE);
left_motor.run(BACKWARD);
right_motor.run(BACKWARD);
motor_status = "backward";
}
// STOP, maybe is best NOT to test if current status is "off" or not, just stop it whenever this button is pressed
if(ir_code == ir_code_stop){
Serial.println("stopping");
left_motor.run(RELEASE);
right_motor.run(RELEASE);
motor_status = "off";
}
// 1 (button)
if(ir_code == ir_code_1){
Serial.println("1 pressed");
}
// 2 (button)
if(ir_code == ir_code_2){
Serial.println("2 pressed");
}
// 3 (button)
if(ir_code == ir_code_3){
Serial.println("3 pressed");
}
servo1.write(servo_position);
Serial.println(servo_position);
// save the last command, we will use this one if received command is "repeat"
ir_last_code = ir_code;
irrecv.resume(); // Receive the next value
}
}
Moderator edit: [code] … [/code] tags added. (Nick Gammon)