CPX IR Range-Sensing Robot
This code powers a range-sensing IR-remote-driven robot with two motors...except that the code is incomplete in these ways!
- the IR remote doesn't actually control anything yet;
- it only runs one of the motors;
- the range sensor control code is pretty basic.
#include <Adafruit_CircuitPlayground.h> #define NO_CODE_DETECTED 0 #define REPEAT -1 #define UP 0xFF629D #define DOWN 0xFFA857 #define LEFT 0xFF22DD #define RIGHT 0xFFC23D #define OK 0xFF02FD #define PIN_TRIG A6 #define PIN_ECHO A7 void setup() { // Initialize the circuit playground CircuitPlayground.begin(); pinMode(PIN_TRIG, OUTPUT); pinMode(PIN_ECHO, INPUT); pinMode(A2, OUTPUT); pinMode(A3, OUTPUT); //pinMode(A4, OUTPUT); //pinMode(A5, OUTPUT); Serial.begin(9600); while (!Serial); // Wait until serial console is opened CircuitPlayground.irReceiver.enableIRIn(); // Start the receiver Serial.println("Ready to receive IR signals"); } void loop() { //digitalWrite(A4, HIGH); //digitalWrite(A5, LOW); digitalWrite(PIN_TRIG, LOW); delayMicroseconds(2); digitalWrite(PIN_TRIG, HIGH); delayMicroseconds(10); digitalWrite(PIN_TRIG, LOW); digitalWrite(PIN_ECHO, LOW); int duration = pulseIn(PIN_ECHO, HIGH, 60000); float distance = duration / 2 * 0.0344; Serial.println(distance); if (distance > 0 && distance < 25) { digitalWrite(A2, LOW); digitalWrite(A3, HIGH); } else { digitalWrite(A2, HIGH); digitalWrite(A3, LOW); } if (CircuitPlayground.irReceiver.getResults()) { CircuitPlayground.irDecoder.decode(); //Decode it int value = CircuitPlayground.irDecoder.value; if (value == UP) { Serial.println("UP"); } else if (value == DOWN) { Serial.println("DOWN"); } else if (value == LEFT) { Serial.println("LEFT"); } else if (value == RIGHT) { Serial.println("RIGHT"); } else if (value == OK) { Serial.println("OK"); } CircuitPlayground.irReceiver.enableIRIn(); // Restart receiver } delay(25); }