IntelliBin code 1 - depth sensor & LED
This is the initial code to get the Arduino reading the distance using the ultrasound sensor, and then turn off or flash the LED depending on how full the bin is. The more full it is, the slower the flash. Under 5 inches (the depth of the bin lid) and the LED turns off. Now to get it to work wirelessly and with the IBM broker.
Code written by me with help from Tom Piercey.
unsigned long echo = 0; int ultraSoundSignal = A0; // Ultrasound signal pin unsigned long ultrasoundValue = 0; int counter = 100; int ledPin = 13; // an array of pin numbers to which LEDs are attached void setup (){ pinMode (ledPin, OUTPUT); // different led patterns Serial.begin(9600); pinMode(ultraSoundSignal,OUTPUT); } unsigned long ping(){ pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output 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 pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches return ultrasoundValue; } /* Lite up LED if any value is passed by the echo pulse * ------------------------------------------------------------------- */ void loop () { int x = 0; x = ping(); Serial.println(x); delay(250); //delay 1/4 seconds. if(x < 5){ digitalWrite(ledPin, LOW); } else if(x > 5 && x <= 8){ digitalWrite(ledPin, HIGH); delay(400); digitalWrite(ledPin, LOW); delay(400); } else if(x > 8 && x <= 13){ digitalWrite(ledPin, HIGH); delay(200); digitalWrite(ledPin, LOW); delay(200); } else if(x > 13 && x <= 17){ digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay(100); } else if(x > 17 && x <= 20){ digitalWrite(ledPin, HIGH); delay(50); digitalWrite(ledPin, LOW); delay(50); } else if(x > 20 && x <= 23){ digitalWrite(ledPin, HIGH); delay(5); digitalWrite(ledPin, LOW); delay(5); } }









