-
-
Save ajfisher/1061793 to your computer and use it in GitHub Desktop.
/* | |
WARMER COOLER GAME | |
Use a ping sensor to determine distance and then depending on the distance, | |
show warmer or cooler colours. | |
Built using a ping))) Sensor and connected to a RGB display. | |
Uses the ping))) example in the base Arduino install, written by David Mellis & Tom Igoe | |
http://www.arduino.cc/en/Tutorial/Ping | |
*/ | |
#define PING_PIN 7 | |
#define RED 9 | |
#define GREEN 10 | |
#define BLUE 11 | |
#define BLUE_LONG 400 | |
#define BLUE_SHORT 175 | |
#define GREEN_LONG 225 | |
#define GREEN_SHORT 75 | |
#define RED_LONG 125 | |
#define RED_SHORT 0 | |
void setup() { | |
// initialize serial communication: | |
Serial.begin(9600); | |
// set the LED off | |
pinMode(RED, OUTPUT); | |
pinMode(GREEN, OUTPUT); | |
pinMode(BLUE, OUTPUT); | |
// uses a common anode RGB LED (so +5v turns them off) | |
digitalWrite(RED, HIGH); | |
digitalWrite(GREEN, HIGH); | |
digitalWrite(BLUE, HIGH); | |
} | |
void loop() | |
{ | |
long distance; | |
distance = ping(); | |
// now depending on the distances we map the colours on the LED. | |
// BLUE will be bright at about 350 cm fading out to 175cm | |
// GREEN will be bright about 225cm fading out to about 75cm | |
// RED will be faded at about 125cm getting bright to 0cm | |
if (distance > RED_LONG) { | |
digitalWrite(RED, HIGH); | |
} else { | |
analogWrite(RED, map(distance, RED_LONG, RED_SHORT, 255, 0)); | |
} | |
if ((distance > GREEN_LONG) || (distance < GREEN_SHORT)) { | |
digitalWrite(GREEN, HIGH); | |
} else { | |
analogWrite(GREEN, map(distance, GREEN_LONG, GREEN_SHORT, 255, 0)); | |
} | |
if ((distance > BLUE_LONG) || (distance < BLUE_SHORT)) { | |
digitalWrite(BLUE, HIGH); | |
} else { | |
analogWrite(BLUE, map(distance, BLUE_LONG, BLUE_SHORT, 255, 0)); | |
} | |
Serial.print(distance); | |
Serial.print("cm"); | |
Serial.println(); | |
delay(100); | |
} | |
long ping() { | |
// returns the distance to the nearest object. | |
// establish variables for duration of the ping, | |
// and the distance result in inches and centimeters: | |
long duration, cm; | |
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds. | |
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse: | |
pinMode(PING_PIN, OUTPUT); | |
digitalWrite(PING_PIN, LOW); | |
delayMicroseconds(2); | |
digitalWrite(PING_PIN, HIGH); | |
delayMicroseconds(5); | |
digitalWrite(PING_PIN, LOW); | |
// The same pin is used to read the signal from the PING))): a HIGH | |
// pulse whose duration is the time (in microseconds) from the sending | |
// of the ping to the reception of its echo off of an object. | |
pinMode(PING_PIN, INPUT); | |
duration = pulseIn(PING_PIN, HIGH); | |
// convert the time into a distance | |
return (microsecondsToCentimeters(duration)); | |
} | |
long microsecondsToCentimeters(long microseconds) | |
{ | |
// The speed of sound is 340 m/s or 29 microseconds per centimeter. | |
// The ping travels out and back, so to find the distance of the | |
// object we take half of the distance travelled. | |
return microseconds / 29 / 2; | |
} |
Hi there AJ, thanks for the code! I'm wondering how to change the code to work with a common cathode RGB LED. Is it a simple change from "digitalWrite(RED, HIGH);" to "digitalWrite(RED, LOW);" for each color? I have tried this and it doesn't seem to be working correctly so I am asking why and how other parts of the codes may require editing based on drawing from ground as opposed to +5V.
hi AJ! your code is great, we are trying to turn off the other 2 colors of light when the distance is at the specific color! can you help, pleaseeeeeeeeee!
Hi can you tell me how we make Ultrasonic sensor colourful?
means can be add RGB LED Strip inside ultrasonic sensor?
@Bhavdeep13 I'm not certain exactly what you're trying to do however the section of code that changes the colour based on distance is this: https://gist.github.com/ajfisher/1061793#file-gistfile1-c-L52-L68
This circuit assumes a Common Anode RGB LED. Wire up ANODE to +5V and the other connections to your PWM pins which must be driven LOW to turn the LED on. Driving PWM pins HIGH will turn the LED off.
Wire up an ultrasonic sensor to a digital input pin on the Arduino. Code for this is based on ping))) from Parallax and uses the public domain code written by David Mellis, edited by Tom Igoe.