Created
October 7, 2017 07:17
-
-
Save sourceperl/a8eb76e0c7f271305d31f70becd3cf6a to your computer and use it in GitHub Desktop.
IR robot : Pololu Zumo + DRV8835 + Arduino IRremote lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from https://github.com/pololu/drv8835-motor-shield | |
#include <DRV8835MotorShield.h> | |
// from https://github.com/z3t0/Arduino-IRremote/ | |
#include <IRremote.h> | |
// some consts | |
#define RECV_PIN 5 | |
#define PWR_IR_VCC 4 | |
#define PWR_IR_GND 3 | |
#define LED_PIN 13 | |
#define STOP_CODE 0xFF02FD | |
#define UP_CODE 0xFFA05F | |
#define DOWN_CODE 0xFF40BF | |
#define LEFT_CODE 0xFF50AF | |
#define RIGHT_CODE 0xFF7887 | |
// some vars | |
DRV8835MotorShield motors; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
int m1_speed = 0; | |
int m2_speed = 0; | |
void setup() | |
{ | |
// IO setup | |
pinMode(PWR_IR_VCC, OUTPUT); | |
pinMode(PWR_IR_GND, OUTPUT); | |
digitalWrite(PWR_IR_VCC, true); | |
digitalWrite(PWR_IR_GND, false); | |
pinMode(LED_PIN, OUTPUT); | |
digitalWrite(LED_PIN, false); | |
// serial init | |
Serial.begin(9600); | |
// IR init | |
irrecv.enableIRIn(); | |
// reverse motor 2 polarity | |
motors.flipM2(true); | |
} | |
void loop() | |
{ | |
// decode IR commands | |
if (irrecv.decode(&results)) { | |
Serial.println(results.value, HEX); | |
switch (results.value) { | |
case UP_CODE: | |
if ((m1_speed < 0) or (m2_speed < 0)) { | |
m1_speed = 50; | |
m2_speed = 50; | |
} else { | |
m1_speed += 50; | |
m2_speed += 50; | |
} | |
break; | |
case DOWN_CODE: | |
if ((m1_speed > 0) or (m2_speed > 0)) { | |
m1_speed = -50; | |
m2_speed = -50; | |
} else { | |
m1_speed -= 50; | |
m2_speed -= 50; | |
} | |
break; | |
case LEFT_CODE: | |
m2_speed -= 50; | |
break; | |
case RIGHT_CODE: | |
m1_speed -= 50; | |
break; | |
case STOP_CODE: | |
m1_speed = 0; | |
m2_speed = 0; | |
break; | |
} | |
irrecv.resume(); | |
} | |
// limit motors speed | |
if (m1_speed > 400) | |
m1_speed = 400; | |
if (m2_speed > 400) | |
m2_speed = 400; | |
if (m1_speed < -400) | |
m1_speed = -400; | |
if (m2_speed < -400) | |
m2_speed = -400; | |
// set motors speed | |
motors.setM1Speed(m1_speed); | |
motors.setM2Speed(m2_speed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment