Created
February 3, 2016 06:36
-
-
Save helloCaller/161bde373b2eaa0d0b77 to your computer and use it in GitHub Desktop.
project 1 box
This file contains 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
/* | |
* BMO | |
* | |
* A BMO inspired robot asks for hugs! Through the use of a ping sensor, our robot raises arms (connected to servos), and flashes its LED eyes | |
* in accordance to the distance of the user. | |
* | |
* Created by Matt Crans and Luke Garwood with code adapted from | |
https://www.arduino.cc/en/Tutorial/Ping | |
https://www.arduino.cc/en/Tutorial/Sweep | |
https://learn.adafruit.com/adafruit-neopixel-uberguide/overview | |
*/ | |
//---Libraries | |
#include <Servo.h> // servo library | |
#include <Adafruit_NeoPixel.h> //RGB LED library | |
//----Pin Setup | |
int pingPin = 7; //setting up pin 7 for Ping Sensor (orange) | |
int servoPin = 9; // setting up pins 9 and 11 for servos (blue) | |
int servoTwoPin = 11; //(yellow) | |
int neoPin = 5; //setting up pin 5 for our RGB LED (grey) | |
//--Output Setup | |
Servo myservo; //setting up two servos as myservo and myservoTwo | |
Servo myservoTwo; | |
int neoTotal = 2; // number of LEDs | |
Adafruit_NeoPixel neoPixels = Adafruit_NeoPixel(neoTotal, neoPin);//creating a new neoPixel object | |
//--Boolean Setup | |
boolean HugHasHappened = false; // boolean to tell us whether a hug has occurred | |
// when "true" and hugger has put down BMO, a little dance occurs | |
void setup() { | |
neoPixels.begin(); // set rate of communication to default for RGB LED | |
Serial.begin(9600); // set Baud rate to 9600 for serial communication | |
neoPixels.setBrightness(100); //Set Brightness of LED | |
myservo.attach(servoPin); // set myservo on pin ServoPin | |
myservoTwo.attach(servoTwoPin); //set up myservoTwo on pin servoTwoPin | |
} | |
void loop() { | |
long duration, inches; // long sets up extended size variables for numbers and in this case duration and inches for our ping sensor | |
pinMode(pingPin, OUTPUT); //setting up the ping sensor as an Output (done in the loop() because this is changed further down) | |
digitalWrite(pingPin, LOW);//ping sensor sends out signal (LOW first to ensure a "clean" HIGH signal) | |
delayMicroseconds(2); //slight delay | |
digitalWrite(pingPin, HIGH);// send out ping | |
delayMicroseconds(5); | |
digitalWrite(pingPin, LOW); | |
pinMode(pingPin, INPUT); //switch pingPin to Input to read returning ping | |
duration = pulseIn(pingPin, HIGH);// amount of time that passed between ping being sent and recieved | |
inches = microsecondsToInches(duration); //turning the amount of time into distance in inches | |
Serial.print(inches); // print out distance | |
Serial.println("in, "); | |
// Serial.println(HugHasHappened); //print out state of HugHasHappened | |
// delay(100); | |
if (HugHasHappened == false) { //if the hug hasn't happened yet | |
// move servo positions according to the distance picked up by the ping sensor | |
if ((inches >= 30) && (inches <= 50)) { //if Hugger is between 30 and 50 inches, have arms at sides and change eye colour | |
myservo.write(0); //myservo.write(angle of position) | |
myservoTwo.write(180);// arms to a neutral position if not already | |
neoPixels.setPixelColor(0, 200, 0, 255); //change LED eye to purple | |
neoPixels.setPixelColor(1, 200, 0, 255); //change second LED eye to purple | |
neoPixels.show();//update LEDs | |
delay(100); //delay between checking distance changes so that switches don't happen too quickly | |
} else if ((inches < 30) && (inches >= 15)) { //or if Hugger is between 15 and 30 inches, raise arms and change eye colour | |
myservo.write(90); //since both servos are positioned differently they need different angles | |
myservoTwo.write(90); | |
neoPixels.setPixelColor(0, 0, 255, 0); //change LED eye to green | |
neoPixels.setPixelColor(1, 0, 255, 0); //change second LED eye to green | |
neoPixels.show(); //update LEDs | |
delay(100); | |
} else if (inches < 15) {//or if Hugger is less than 15 inches | |
myservo.write(0);//lower one arm | |
if (inches <= 8) { //and if Hugger is less than 8 inches (keep the first arm lowered) | |
myservoTwo.write(180);//and lower the second arm | |
} | |
if (inches <= 3) { //if Hugger is very close to the sensor, it's safe to say a hug has occured | |
neoPixels.setPixelColor(0, 0, 10, 255); //change LED eye colour to blue | |
neoPixels.setPixelColor(1, 0, 10, 255);//change second LED eye colour to blue | |
neoPixels.show();//update LEDs | |
HugHasHappened = true; // a hug has occured, set boolean to true | |
} | |
delay(100); | |
} else { // at any other distance | |
neoPixels.setPixelColor(0, 255, 10, 10);// set LED eyes to red | |
neoPixels.setPixelColor(1, 255, 10, 10); | |
neoPixels.show();//update LEDs | |
delay(100); | |
} | |
}; | |
if (HugHasHappened == true) { //if a hug has happened | |
int lastInches = inches; //capture value of inches for debugging condition below | |
delay(1000); | |
boolean dance = false; //set up dance boolean | |
if ((inches >= 45) && ((lastInches - inches) == 0 )) { // if hugger has now moved away, and the values of distance read the same as last read (to avoid spikes) | |
dance = true; // dance happens | |
//the reason for using the boolean is so that the dance only occurs once and then resets | |
} | |
if (dance == true) { // dance happens by rotating servos and flashing eyes in quick succession | |
for (int i = 0; i <= 150 ; i += 15) { //for loop to create a gradual change of eye color | |
neoPixels.setPixelColor(0, 0 + i, 10, 255 - i); // set LED eyes to gradually turn red | |
neoPixels.setPixelColor(1, 0 + i, 10, 255 - i); // set second LED eyes to gradually turn red | |
neoPixels.show(); | |
delay(100); | |
if (i < 75) { // this allows the lights to flash up until i is 74 | |
neoPixels.setPixelColor(0, 0, 0, 0);// switching LEDs to "black" though its not really black to create a flash between colours | |
neoPixels.setPixelColor(1, 0, 0, 0); | |
neoPixels.show(); | |
delay(50); | |
} | |
} | |
//----servos are rotated for a rhythmical "dance" while the eyes flash in various colours | |
myservo.write(180); | |
neoPixels.setPixelColor(0, 50, 0, 200); | |
neoPixels.setPixelColor(1, 50, 0, 200); | |
neoPixels.show(); | |
delay(500); | |
neoPixels.setPixelColor(0, 0, 0, 0); | |
neoPixels.setPixelColor(1, 0, 0, 0); | |
neoPixels.show(); | |
delay(50); | |
myservo.write(0); | |
neoPixels.setPixelColor(0, 100, 0, 150); | |
neoPixels.setPixelColor(1, 100, 0, 150); | |
neoPixels.show(); | |
delay(500); | |
neoPixels.setPixelColor(0, 0, 0, 0); | |
neoPixels.setPixelColor(1, 0, 0, 0); | |
neoPixels.show(); | |
delay(50); | |
myservoTwo.write(0); | |
neoPixels.setPixelColor(0, 200, 0, 100); | |
neoPixels.setPixelColor(1, 200, 0, 100); | |
neoPixels.show(); | |
delay(200); | |
//-----still dancing | |
neoPixels.setPixelColor(0, 0, 0, 0); | |
neoPixels.setPixelColor(1, 0, 0, 0); | |
neoPixels.show(); | |
delay(50); | |
myservo.write(90); | |
myservoTwo.write(180); | |
neoPixels.setPixelColor(0, 210, 0, 50); | |
neoPixels.setPixelColor(1, 210, 0, 50); | |
neoPixels.show(); | |
delay(500); | |
neoPixels.setPixelColor(0, 0, 0, 0); | |
neoPixels.setPixelColor(1, 0, 0, 0); | |
neoPixels.show(); | |
delay(50); | |
myservoTwo.write(0); | |
neoPixels.setPixelColor(0, 240, 0, 10); | |
neoPixels.setPixelColor(1, 240, 0, 10); | |
neoPixels.show(); | |
delay(600); | |
//---almost done | |
neoPixels.setPixelColor(0, 0, 0, 0); | |
neoPixels.setPixelColor(1, 0, 0, 0); | |
neoPixels.show(); | |
delay(50); | |
myservo.write(180); | |
neoPixels.setPixelColor(0, 255, 0, 0); | |
neoPixels.setPixelColor(1, 255, 0, 0); | |
neoPixels.show(); | |
delay(600); | |
//---almost.. | |
myservoTwo.write(180); | |
delay(200); | |
myservo.write(0); | |
//--Dance done | |
HugHasHappened = false; // reset booleans back to their original state at the end of dance | |
dance = false; | |
} | |
} | |
}//---end of loop function | |
long microsecondsToInches(long microseconds) { //conversion of microseconds to inches | |
return microseconds / 74 / 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment