Hello everybody,
I would need your help to figure it out how to make things work.
I would like to simply drive 4 dc motors with an IR remote. I'm using the Adafruit motor shield but this is what happen:
When the command irrecv.enableIRIn() starts M1 and M2 output dont work anymore. I still can get voltage on M3 and M4 but nothing on the others.
I would like to know if there's a way to fix this or you suggest to change to another shield. In this case which would you suggest?
Thank you a lot for your time!
I expect it's a problem in your code or the components you're using or the way you have things connected or powered. But you have provided no information about any of those things which makes it difficult to help.
Steve
Hi, thank you for your reply.
Here's the code I'm using.
I've set the motor*.run(FORWARD); in the setup to test.
If I delete the irrecv.enableIRIn(); command everthing works ok (no matter the pin of the receiver).
Thank you all.
/*
Programma per comandare Lux
Scritto da Francesco Trasimeni
*/
#include <AFMotor.h>
#include "IRremote.h"
AF_DCMotor motor1 (1);
AF_DCMotor motor2 (2);
AF_DCMotor motor3 (3);
AF_DCMotor motor4 (4);
IRrecv irrecv(9); //create instance of 'IRrecv'
decode_results results; //create instance of 'decode_results'
long previous;
unsigned long LastReceive = 0;
unsigned long TimerUp; //UP arrow on the remote
boolean TimerUpFlag = false;
unsigned long TimerDown; //DOWN arrow on the remote
boolean TimerDownFlag = false;
unsigned long TimerIntensity;
unsigned long TimerIncDec;
boolean intesityUpFlag = false;
boolean intesityDownFlag = false;
int brightness;
unsigned long dummy;
unsigned long * TimerPtr = &dummy; //pointer to the current timer
// Imposta i pin di controllo del L298N Dual H-Bridge Motor Controller
//______________________________________________________________________________________________
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); //start receive
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(100);
motor4.setSpeed(100);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
//______________________________________________________________________________________________
//______________________________________________________________________________________________
void loop()
{
if (irrecv.decode(&results)) //is there IR remote button code
{
Serial.println(results.value, HEX);
processButton(); //process button press
irrecv.resume(); //restart for next button press
}
//********************** //Turn off upLED
//if timing is enabled, is it time to stop
if (TimerUpFlag && millis() - TimerUp >= 250ul)
{
TimerUpFlag = false; //disable timing
TimerPtr = &dummy; //pointer to dummy timer
fermo();
}
//********************** //Turn off downLED
//if timing is enabled, is it time to stop
if (TimerDownFlag && millis() - TimerDown >= 250ul)
{
TimerDownFlag = false; //disable timing
TimerPtr = &dummy; //pointer to dummy timer
fermo();
}
}
//______________________________________________________________________________________________
//______________________________________________________________________________________________
void zpositivo() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void znegativo() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void xpositivo() {
}
void xnegativo() {
}
void ypositivo() {
}
void ynegativo() {
}
void fermo() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
//______________________________________________________________________________________________
//______________________________________________________________________________________________
void processButton()
{
switch (results.value)
{
//**********************
case 0xFFA857: //UP Arrow
{
Serial.println("Z+");
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
zpositivo();
TimerUp = millis();
}
break;
//**********************
case 0xFFE01F: //UP Arrow
{
Serial.println("Z-");
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
znegativo();
TimerUp = millis();
}
break;
case 0xFF5AA5: //UP Arrow
{
Serial.println("X-");
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
xpositivo();
TimerUp = millis();
}
break;
case 0xFF10EF: //UP Arrow
{
Serial.println("X+");
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
xnegativo();
TimerUp = millis();
}
break;
case 0xFF18E7: //UP Arrow
{
Serial.println("Y+");
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
ypositivo();
TimerUp = millis();
}
break;
case 0xFF4AB5: //UP Arrow
{
Serial.println("Y-");
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
ynegativo();
TimerUp = millis();
}
break;
case 0xFFFFFFFF: //Repeat code
{
Serial.println("REPEAT");
*TimerPtr = millis(); //reset the current timer
}
break;
default:
{
Serial.println("REPEAT");
*TimerPtr = millis(); //reset the current timer
}
break;
}
}
Hi,
can anyone suggest me what motor shield use?
I would need to drive 4 motors with encoders and use an IR remote.
Thank you very much!
Maybe there's a way to solve with the code.
Is this possible?
I got the same problem, and managed to solve it. There is a conflict with the libraries you are using that causes them to try to use the same timer pin. The Adafruit motor shield library uses Timer0 and Timer2, while the IrMaster library also uses Timer2. To fix this, you need to go into the boarddefs.h file, and change the default Timer from Timer2 to Timer1.
#else
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, Uno etc
// ATmega48, ATmega88, ATmega168, ATmega328
#define IR_USE_TIMER1 // tx = pin 9
//#define IR_USE_TIMER2 // tx = pin 3
2 Likes