- /*
- 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;
- }