Wednesday, July 20, 2011

Stupid Pet Trick

So, I finished my midterm, the "stupid pet trick", and well before 2 am to boot! With the exception of the LED face not working 100% like I wanted it to, it all came together relatively easily.

The "sleepy robot" is now the "peaceful robot", it just seems more fitting. The robot basically just displays a peaceful, happy face when left undisturbed, but when a light is shined in its face or the surface it is on is disturbed, its face changes to a slightly alarmed look and it shakes its head repeatedly. Then after a few seconds of inactivity it shuts off.

Note: the adjectives peaceful, happy and alarmed are used loosely. Its just a few LEDs for eyes and a mouth so Im not exactly on the cutting edge of artificial emotion.

The code for the robot is below. The alarmed look and head shake routine was really simple so I didnt even bother using any for loops to create the motion. I am, however, kind of proud of the initial calibration feature you can see it the void setup area. After you turn it on, the program waits for 3 seconds for you to close the back of the robot and step away so it can take a baseline reading of the ambient light without casting your own shadow over it, then it uses a specified value as a threshold to determine what constitutes "light in my face". I didnt put in a calibration step for the piezo because it wasnt as sensitive as the photoresistor so it wouldnt have been worth it. One cool feature to add later: an analog input to increase or decrease sensitivity of the robot on the fly.

#include //loads servo library

Servo neck; //creates servo object to control a servo

int onLedPin = 2;
int happyLedPin = 3;
int sadLedPin = 4;
int ldrPin = 0;
int ldrBaseline = 0; //sets ambient ldr value
int ldrThreshold = 100; //sets threshold for activation
int piezoPin = 1;
int piezo = 0; //stores piezo value
int inactivity = 5000; //time inactive before going to sleep
int neckDelay = 300; //delay between neck movements
int neckNeutral = 90; //sets neutral position for neck servo
int neckLeft = 45;
int neckRight = 135;

void setup() {
neck.attach(9); //attaches servo to pin 9
pinMode(onLedPin, OUTPUT);
pinMode(happyLedPin, OUTPUT);
pinMode(sadLedPin, OUTPUT);
ldrBaseline = analogRead(ldrPin); //sets ambient ldr value
neck.write(neckNeutral); //sets head to neutral position


void loop() {
digitalWrite(sadLedPin, LOW); //makes sure sad face is off
digitalWrite(onLedPin, HIGH); //Sets sleepy state
digitalWrite(happyLedPin, HIGH);
piezo = analogRead(piezoPin); //reads piezo and assigns value to variable


if((piezo > 100) || (analogRead(ldrPin) > ldrBaseline + ldrThreshold)){ //if ldr or piezo are activated, activate wake up sequence
digitalWrite(happyLedPin, LOW); //turns off happy face, sets neutral face
digitalWrite(sadLedPin, HIGH); //turns on sad face
neck.write(neckRight); //begins shaking of head to one direction
neck.write(neckLeft); //moves head to other direction
neck.write(neckNeutral); //returns head to neutral position
delay(inactivity); //waits predetermined amount of time
} //returns to begining to fall asleep

No comments:

Post a Comment