Hey I am using L298P Motor shield to control 2 DC motors via Arduino Mega 2560 with IR.
I managed already a lot of things and I have still one error. I test both motors before defining the IR receiver. They run in both directions like expected. But if I want to move both in both directions, button 1 and 3 in my example, one of them only runs in one direction, while the other one changes the direction like expected.
I already changes the timer since motor and IR had both timer 2 set. How can I solve this?
#include <IRremote.h>
#define receiverIR 5 //Eingang IR Empfänger
//definiere Motor Outputs
int MOTOR1_DIRECTION_PIN = 13;
int MOTOR2_DIRECTION_PIN = 12;
int MOTOR1_SPEED_PIN = 11;
int MOTOR2_SPEED_PIN = 10;
int motorValue = 0;
void setup() {
//definiere pins als output
pinMode(MOTOR1_DIRECTION_PIN, OUTPUT);
pinMode(MOTOR2_DIRECTION_PIN, OUTPUT);
pinMode(MOTOR1_SPEED_PIN, OUTPUT);
pinMode(MOTOR2_SPEED_PIN, OUTPUT);
Serial.begin(115200);
IrReceiver.begin(receiverIR, ENABLE_LED_FEEDBACK);
}
void loop(){
//decodes the infrared input
if (IrReceiver.decode()){
int command = IrReceiver.decodedIRData.command;
Serial.println(command);
//switch case to use the selected remote control button
switch (command){
case 0x16: //when you press the 1 button
Serial.println("Button 1 gedrückt");
motorValue = 100;
digitalWrite(MOTOR1_DIRECTION_PIN,HIGH);
Serial.println("Richtung 1 gesetzt");
analogWrite(MOTOR1_SPEED_PIN,motorValue);
Serial.println("1 GO");
digitalWrite(MOTOR2_DIRECTION_PIN,LOW);
Serial.println("Richtung 2 gesetzt");
analogWrite(MOTOR2_SPEED_PIN,motorValue);
Serial.println("2 GO");
break;
case 0x19: //when you press the 2 button
motorValue = 0;
analogWrite(MOTOR1_SPEED_PIN,motorValue);
analogWrite(MOTOR2_SPEED_PIN,motorValue);
break;
case 0xD: //when you press the 3 button
motorValue = 100;
digitalWrite(MOTOR1_DIRECTION_PIN,LOW);
digitalWrite(MOTOR2_DIRECTION_PIN,HIGH);
analogWrite(MOTOR1_SPEED_PIN,motorValue);
analogWrite(MOTOR2_SPEED_PIN,motorValue);
break;
}
Serial.println();
IrReceiver.resume();
}
delay(10);
}