love-o-meter stage 1

[QUICKTIME http://www.cameroncundiff.com/media/proximity.mov 320 257]

Senses proximity and indicates the programmed comfort zone or "personal space" of the sensor through a series of LEDs.


/* Ultrasound Sensor
* Reads values (00014-01199) from an ultrasound sensor (3m sensor)
* and writes the values to the serialport.
* http://www.xlab.se | http://www.0j0.org
* copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
*/

int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin01 = 13; // LED connected to digital pin 13
int ledPin02 = 10;
int ledPin03 = 9;
int ledPin04 = 8;
int ledPin05 = 6;
int ledPin06 = 5;
int ledPin07 = 4;
int ledPin08 = 3;
int ledPin09 = 2;

void setup() {
beginSerial(9600); // Sets the baud rate to 9600
pinMode(ledPin01, OUTPUT); // Sets the digital pin as output
pinMode(ledPin02, OUTPUT);
pinMode(ledPin03, OUTPUT);
pinMode(ledPin04, OUTPUT);
pinMode(ledPin05, OUTPUT);
pinMode(ledPin06, OUTPUT);
pinMode(ledPin07, OUTPUT);
pinMode(ledPin08, OUTPUT);
pinMode(ledPin09, OUTPUT);
}

void loop() {
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor
* -------------------------------------------------------------------
*/
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse
* -------------------------------------------------------------------
*/
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a low value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

/* Writing out values to the serial port
* -------------------------------------------------------------------
*/
ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

serialWrite('A'); // Example identifier for the sensor
ultrasoundValue = map(ultrasoundValue, 100, 600, 0, 20);
printInteger(ultrasoundValue);
serialWrite(10);
serialWrite(13);

// check proximity and indicate comfort level

if(ultrasoundValue < 20 && ultrasoundValue >= 18) {
digitalWrite(ledPin01, LOW);
digitalWrite(ledPin02, HIGH);
digitalWrite(ledPin03, LOW);
digitalWrite(ledPin04, LOW);
digitalWrite(ledPin05, LOW);
digitalWrite(ledPin06, LOW);
digitalWrite(ledPin07, LOW);
digitalWrite(ledPin08, LOW);
digitalWrite(ledPin09, LOW);
}

else if(ultrasoundValue < 18 && ultrasoundValue >= 15) {
digitalWrite(ledPin01, LOW);
digitalWrite(ledPin02, LOW);
digitalWrite(ledPin03, HIGH);
digitalWrite(ledPin04, LOW);
digitalWrite(ledPin05, LOW);
digitalWrite(ledPin06, LOW);
digitalWrite(ledPin07, LOW);
digitalWrite(ledPin08, LOW);
digitalWrite(ledPin09, LOW);
}

else if(ultrasoundValue < 15 && ultrasoundValue >= 13) {
digitalWrite(ledPin01, LOW);
digitalWrite(ledPin02, LOW);
digitalWrite(ledPin03, LOW);
digitalWrite(ledPin04, HIGH);
digitalWrite(ledPin05, LOW);
digitalWrite(ledPin06, LOW);
digitalWrite(ledPin07, LOW);
digitalWrite(ledPin08, LOW);
digitalWrite(ledPin09, LOW);
}

else if(ultrasoundValue < 13 && ultrasoundValue >= 10) {
//if light dim go to white, then delay
//if no pressure (kiss) then return to red
//if pressure before white light, buzzer
digitalWrite(ledPin01, LOW);
digitalWrite(ledPin02, LOW);
digitalWrite(ledPin03, LOW);
digitalWrite(ledPin04, LOW);
digitalWrite(ledPin05, HIGH);
digitalWrite(ledPin06, LOW);
digitalWrite(ledPin07, LOW);
digitalWrite(ledPin08, LOW);
digitalWrite(ledPin09, LOW);
}

else if(ultrasoundValue < 10 && ultrasoundValue >= 8) {
digitalWrite(ledPin01, LOW);
digitalWrite(ledPin02, LOW);
digitalWrite(ledPin03, LOW);
digitalWrite(ledPin04, LOW);
digitalWrite(ledPin05, LOW);
digitalWrite(ledPin06, HIGH);
digitalWrite(ledPin07, LOW);
digitalWrite(ledPin08, LOW);
digitalWrite(ledPin09, LOW);
}

else if(ultrasoundValue < 8 && ultrasoundValue >= 6) {
digitalWrite(ledPin01, LOW);
digitalWrite(ledPin02, LOW);
digitalWrite(ledPin03, LOW);
digitalWrite(ledPin04, LOW);
digitalWrite(ledPin05, LOW);
digitalWrite(ledPin06, LOW);
digitalWrite(ledPin07, HIGH);
digitalWrite(ledPin08, LOW);
digitalWrite(ledPin09, LOW);
}

else if(ultrasoundValue < 6 && ultrasoundValue >= 3) {
digitalWrite(ledPin01, LOW);
digitalWrite(ledPin02, LOW);
digitalWrite(ledPin03, LOW);
digitalWrite(ledPin04, LOW);
digitalWrite(ledPin05, LOW);
digitalWrite(ledPin06, LOW);
digitalWrite(ledPin07, LOW);
digitalWrite(ledPin08, HIGH);
digitalWrite(ledPin09, LOW);
}

else if(ultrasoundValue < 3 && ultrasoundValue >= 0) {
digitalWrite(ledPin01, LOW);
digitalWrite(ledPin02, LOW);
digitalWrite(ledPin03, LOW);
digitalWrite(ledPin04, LOW);
digitalWrite(ledPin05, LOW);
digitalWrite(ledPin06, LOW);
digitalWrite(ledPin07, LOW);
digitalWrite(ledPin08, LOW);
digitalWrite(ledPin09, HIGH);
}

else
{
digitalWrite(ledPin01, HIGH);
digitalWrite(ledPin02, LOW);
digitalWrite(ledPin03, LOW);
digitalWrite(ledPin04, LOW);
digitalWrite(ledPin05, LOW);
digitalWrite(ledPin06, LOW);
digitalWrite(ledPin07, LOW);
digitalWrite(ledPin08, LOW);
digitalWrite(ledPin09, LOW);
}

/* Light up LED if any value is passed by the echo pulse
* -------------------------------------------------------------------
*/
/*if(timecount > 0){
digitalWrite(ledPin01, HIGH);
}*/

/* Delay of program
* -------------------------------------------------------------------
*/
delay(100);
}