I am using the robojax l293d motor sheild with ir but cannot get it to work.
Everything is printing to the serial monitor when its supposed to, the motors run without the ir code, but the motors are not running when their supposed to.
I am 99% sure that it is the code. Please, give me the edits I need to make.
- Thanks!
#include <AFMotor.h>
#include <IRremote.h>
AF_DCMotor m1(1);//define motor 1 as m1
AF_DCMotor m2(2);//define motor 2 as m2
int IR_RECEIVE_PIN = 19;
void setup() {
m1.setSpeed(0);//motor 1 is turned off to turn on change 0, to 255
m2.setSpeed(0);//motor 2 is turned off
Serial.begin(9600);
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
IrReceiver.begin(IR_RECEIVE_PIN);
Serial.print(F("Ready to receive IR signals at pin "));
Serial.println(IR_RECEIVE_PIN);
}
void loop() {
/*
* Check if received data is available and if yes, try to decode it.
* Decoded result is in the IrReceiver.decodedIRData structure.
*
* E.g. command is in IrReceiver.decodedIRData.command
* address is in command is in IrReceiver.decodedIRData.address
* and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData
*/
if (IrReceiver.decode()) {
// Print a short summary of received data
IrReceiver.printIRResultShort(&Serial);
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
// We have an unknown protocol here, print more info
IrReceiver.printIRResultRawFormatted(&Serial, true);
}
/*
* !!!Important!!! Enable receiving of the next value,
* since receiving has stopped after the end of the current received data packet.
*/
IrReceiver.resume(); // Enable receiving of the next value
Serial.println(IrReceiver.decodedIRData.command);
Serial.println();
/*
* Finally, check the received data and perform actions according to the received command
*/
if (IrReceiver.decodedIRData.command == 12) {
Serial.println("Motors on");
uint8_t i;
Serial.println("Motor 1 FORWARD 100% speed");
m1.run(FORWARD);
m1.setSpeed(speed(100));
Serial.println("m4 FORWARD 100%");
m2.run(FORWARD);
m2.setSpeed(speed(100));
delay(3000);
m1.run(RELEASE);
Serial.println("M1 RELEASE");
m2.run(RELEASE);
Serial.println("m4 RELEASE");
delay(3000);
Serial.println("M1 BACKWARD 80%");
m1.run(BACKWARD );
m1.setSpeed(speed(80));
Serial.println("m4 BACKWARD 60%");
m2.run(BACKWARD );
m2.setSpeed(speed(60));
delay(3000);
delay(3000);
m1.run(RELEASE);
Serial.println("M1 RELEASE");
m2.run(RELEASE);
Serial.println("m4 RELEASE");
delay(3000);
Serial.println("=============");
}
}
}
int speed(int b)
{
return map(b, 0, 100, 0, 255);
}