- 1x RGB led.
- 1x Ultrasonic Sensor.
- 1x Arduino Board.
- Few jumper cables.
- And the sketch below.
The Sketch
int redPin = 4; int greenPin = 5; int bluePin = 6; int trigPin2 = 8; int echoPin2 = 9; #define COMMON_ANODE void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin (9600); pinMode(trigPin2, OUTPUT); pinMode(echoPin2, INPUT); } void setColor(int red, int green, int blue) { #ifdef COMMON_ANODE red = 255 - red; green = 255 - green; blue = 255 - blue; #endif analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); } void loop() { Serial.println("\n"); int duration2, distance2; digitalWrite (trigPin2, HIGH); delayMicroseconds (10); digitalWrite (trigPin2, LOW); duration2 = pulseIn (echoPin2, HIGH); distance2 = (duration2/2) / 29.1; Serial.print("2nd Sensor: "); Serial.print(distance2); Serial.print("cm "); if (distance2 >=9) { // Change the number for long or short distances. setColor(255, 0, 0); delay(1000); setColor(0, 0, 0); } else if (distance2 >=7 && distance2 <=9) { // Change the number for long or short distances. setColor(255, 208, 0); delay(1000); setColor(0, 0, 0); } else if (distance2 <=7) { // Change the number for long or short distances. setColor(101, 255, 0); delay(1000); setColor(0, 0, 0); } else { } delay(3000); // 3 seconds delay } |
Now let me explain what it does. Because it works by calculating the distance between the sensor and the tissue roll, so you might have change it according to your need. In this case it turns on the red led when the distance exceeds 9cm. And if the distance is 7cm to 9cm it turns on the amber led, and the if it's less than 7cm it turns on the green led. After take a rest for 3 seconds and then starts the sensor again to see how much tissue has been used. You can make it take the measurement after each 5 or 10 minutes if you want by changing the number to what ever amount you want.
No comments:
Post a Comment