Created
August 16, 2013 02:10
-
-
Save ebot/6246656 to your computer and use it in GitHub Desktop.
Our Coffeebot Arduino Brain
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
/* | |
* CoffeeBot Command: Control a coffeebot with a remote or via sensors | |
* Version 0.1 - August, 2013 | |
* By Ed Botzum | |
* http://edbotz.us | |
* This is an arduino scrip that allows users to switch their coffeebot | |
* between senor mode and Remote Mode by clicking the func/stop button | |
* on the remote. | |
* | |
* I use the DF Robot IR Kit - http://bit.ly/17QL0VX | |
* and the IRremote library - http://bit.ly/vXOcwm | |
* | |
* Sensor Mode - Checks the amount of available light and turns on the | |
* motor of which hand your blocking the light in front of. | |
* Code courtesy of Judy Aime' Castro at make - http://bit.ly/12BJAzC | |
* Remote Mode - Checks the key that the user pressed and turns on the | |
* correct motor(s): | |
* 0 - All motors off | |
* 1 - Left motor on | |
* 2 - Both motors on | |
* 3 - Right motor on | |
*/ | |
#include <IRremote.h> | |
int RECV_PIN = 13; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
boolean sensorMode = false; // switch between remote and sensors | |
void setup() | |
{ | |
// Set the mode of the digital pins to outputs to drive the motors | |
// (the analog inputs are automatically inputs and so don’t need to be set) | |
pinMode( 3, OUTPUT ); // Left Motor | |
pinMode( 9, OUTPUT ); // Left LED | |
pinMode( 5, OUTPUT ); // Right Motor | |
pinMode( 10, OUTPUT ); // Right LED | |
// Start the IR Receiver | |
Serial.begin(9600); | |
irrecv.enableIRIn(); | |
} | |
void loop() { | |
if (sensorMode) { | |
if ( analogRead( 0 ) > analogRead( 2 ) ) { | |
Serial.println("Sensor - Left"); | |
runLeftMotor(); | |
} | |
else if ( analogRead( 0 ) < analogRead( 2 ) ) { | |
Serial.println("Sensor - Right"); | |
runRightMotor(); | |
} | |
else { | |
Serial.println("Sensor - Equal"); | |
runBothMotors(); | |
} | |
// Stop all motors and wait 1 sec to re-check sensors | |
delay(500); | |
stopBothMotors(); | |
delay(1000); | |
if (irrecv.decode(&results)) { | |
String value = (String) results.value; | |
checkForRemoteMode(value); | |
irrecv.resume(); // Receive the next value | |
} | |
} | |
else { | |
if (irrecv.decode(&results)) { | |
String value = (String) results.value; | |
determineRemoteDirection(value); | |
irrecv.resume(); // Receive the next value | |
} | |
} | |
} | |
void checkForRemoteMode(String value) { | |
if (value.startsWith("1")) { | |
Serial.println("---------- Incoming Signal ----------"); | |
Serial.println(value); | |
if (value == "16597183") { | |
Serial.println("Func Pressed - Stop Both Motors and Enable Remote"); | |
stopBothMotors(); | |
sensorMode = false; | |
} | |
} | |
} | |
void determineRemoteDirection(String value) { | |
if (value.startsWith("1")) { | |
Serial.println("---------- Incoming Signal ----------"); | |
Serial.println(value); | |
switch (value.toInt()) { | |
case 16593103: { | |
Serial.println(" 0 pressed - Stop all motors"); | |
stopBothMotors(); | |
break; | |
} | |
case 16582903: { | |
Serial.println(" 1 pressed - Run left motor"); | |
runLeftMotor(); | |
break; | |
} | |
case 16615543: { | |
Serial.println(" 2 pressed - Run both motors"); | |
runBothMotors(); | |
break; | |
} | |
case 16599223: { | |
Serial.println(" 3 pressed - Run right motor"); | |
runRightMotor(); | |
break; | |
} | |
case 16597183: { | |
Serial.println(" Function pressed - Go to sensor mode"); | |
sensorMode = true; | |
break; | |
} | |
default: { | |
Serial.println(" " + value + " not programmed."); | |
break; | |
} | |
} | |
Serial.println("-------------------------------------\n"); | |
} | |
} | |
void stopBothMotors() { | |
// turn off left xand right LEDs | |
digitalWrite( 9, LOW ); | |
digitalWrite( 10, LOW ); | |
// turn off left and right motors | |
digitalWrite( 3, LOW ); | |
digitalWrite( 5, LOW ); | |
} | |
void runLeftMotor() { | |
// turn on left LED | |
digitalWrite( 9, HIGH ); | |
digitalWrite( 10, LOW ); | |
// turn on left motor | |
digitalWrite( 3, HIGH ); | |
digitalWrite( 5, LOW ); | |
} | |
void runRightMotor() { | |
digitalWrite( 9, LOW ); | |
digitalWrite( 10, HIGH ); // turn on right LED | |
// turn on right motor | |
digitalWrite( 3, LOW ); | |
digitalWrite( 5, HIGH ); | |
} | |
void runBothMotors() { | |
// turn on left and right LEDs and motors | |
digitalWrite( 9, HIGH ); | |
digitalWrite( 10, HIGH ); | |
digitalWrite( 3, HIGH ); | |
digitalWrite( 5, HIGH ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment