///////////////////////////////////////////////////////////////////////////// ////// Code for Personal Spheres Project ////// ////// by Brian && Daniel ////// ////// October, 2006 ////// ////// ////// ////// "Dipping the biscuits while the gravy is still hot." ////// ///////////////////////////////////////////////////////////////////////////// int trigger = 8; // trigger pin int echo = 9; // echo pin int toggle = 0; // for lights to flash on and off int treesandplants; // added to give this code some environmental charm int i, x, y, count, count2, val1, val2, output, outputLED; // give me a sandbox...its playtime!! float ratio; // to toggle output values to serial port int pin1 = 2; //pins correspond to lights int pin2 = 3; int pin3 = 4; int pin4 = 5; int pin5 = 6; int pin6 = 7; int potPin = 0; //pin for potentiometer int potVal; //var to determine pot value (usually $45 for an 1/8oz) int potThreshold; //user controlled threshold void setup() { pinMode(trigger, OUTPUT); //sets trigger pin as output pinMode(echo, INPUT); //sets echo pin as input pinMode(potPin, INPUT); //sets potpin as input Serial.begin(9600); printMode(SERIAL); // sets the print method to print to the serial port count = 0; //sets counter to zero y = 0; val2 = 0; pinMode(pin1, OUTPUT); //sets LED pins as outputs pinMode(pin2, OUTPUT); pinMode(pin3, OUTPUT); pinMode(pin4, OUTPUT); pinMode(pin5, OUTPUT); pinMode(pin6, OUTPUT); } void loop() { //////////ultrasonic sensor code below////////////////////////////////// digitalWrite(trigger, 1); //send trigger pulse delayMicroseconds(10);// delay loop for long-enough pulse digitalWrite(trigger, 0); val1 = digitalRead(echo); while(val1 != 1) //waits for echo pin to go high { val1 = digitalRead(echo); } while(val1 == 1) //times the duration that the echo pin is high (corresponds to distance) { count = count + 1; val1 = digitalRead(echo); } output = count/12; //reduces count so numbers range from 0-100 potVal = analogRead(potPin)/4; //read pot value 0-255 (for testing purposes) outputLED = count/potVal; //for case statement //////////////////////////output to LEDS below///////////////////////////////////// if (output != 0 && outputLED < 25) { printInteger(outputLED); // send it out the serial port serialWrite(10); //line break serialWrite(13); //start at beginnig of line switch (outputLED) //not the most elegant way to code, but it works just fine. { case 0: // uh oh, personal sphere has been violated toggle = 1; digitalWrite(pin1, toggle); // Q. what do you get when you cross digitalWrite(pin2, toggle); // mark foley and a librarian?? digitalWrite(pin3, toggle); // A. i don't know, but he sure knows digitalWrite(pin4, toggle); // how to "turn" pages. digitalWrite(pin5, toggle); digitalWrite(pin6, toggle); delay(100); toggle = 0; digitalWrite(pin1, toggle); digitalWrite(pin2, toggle); digitalWrite(pin3, toggle); digitalWrite(pin4, toggle); digitalWrite(pin5, toggle); digitalWrite(pin6, toggle); break; case 1: // hey, you are pushing it buddy digitalWrite(pin1, 1); digitalWrite(pin2, 1); digitalWrite(pin3, 0); digitalWrite(pin4, 0); digitalWrite(pin5, 0); digitalWrite(pin6, 0); break; case 2: // object is pretty close digitalWrite(pin1, 0); digitalWrite(pin2, 0); digitalWrite(pin3, 1); digitalWrite(pin4, 1); digitalWrite(pin5, 0); digitalWrite(pin6, 0); break; case 3: // an object has been detected at a distance digitalWrite(pin1, 0); digitalWrite(pin2, 0); digitalWrite(pin3, 0); digitalWrite(pin4, 0); digitalWrite(pin5, 1); digitalWrite(pin6, 1); break; default: // i was alone, i was all by myself, no one was looking, i was thinking of you... digitalWrite(pin1, 1); digitalWrite(pin2, 1); digitalWrite(pin3, 1); digitalWrite(pin4, 0); digitalWrite(pin5, 1); digitalWrite(pin6, 1); //whew!! i love the copy and paste functions. } output = count2; } delay(50); //delay 50ms before next ping count = 0; //sets count back to zero }