Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.85 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.   WARMER COOLER GAME
  3.  
  4.   Use a ping sensor to determine distance and then depending on the distance,
  5.   show warmer or cooler colours.
  6.  
  7.   Built using a ping))) Sensor and connected to a RGB display.
  8.  
  9.   Uses the ping))) example in the base Arduino install, written by David Mellis & Tom Igoe
  10.   http://www.arduino.cc/en/Tutorial/Ping
  11.  
  12.  */
  13.  
  14. #define PING_PIN 7
  15. #define RED 9
  16. #define GREEN 10
  17. #define BLUE 11
  18.  
  19. #define BLUE_LONG 400
  20. #define BLUE_SHORT 175
  21. #define GREEN_LONG 225
  22. #define GREEN_SHORT 75
  23. #define RED_LONG 125
  24. #define RED_SHORT 0
  25.  
  26. void setup() {
  27.   // initialize serial communication:
  28.   Serial.begin(9600);
  29.  
  30.   // set the LED off
  31.   pinMode(RED, OUTPUT);
  32.   pinMode(GREEN, OUTPUT);
  33.   pinMode(BLUE, OUTPUT);
  34.  
  35.   // uses a common anode RGB LED (so +5v turns them off)  
  36.   digitalWrite(RED, HIGH);
  37.   digitalWrite(GREEN, HIGH);
  38.   digitalWrite(BLUE, HIGH);
  39. }
  40.  
  41. void loop()
  42. {
  43.  
  44.   long distance;
  45.   distance = ping();
  46.  
  47.   // now depending on the distances we map the colours on the LED.
  48.   // BLUE will be bright at about 350 cm fading out to 175cm
  49.   // GREEN will be bright about 225cm fading out to about 75cm
  50.   // RED will be faded at about 125cm getting bright to 0cm
  51.  
  52.   if (distance > RED_LONG) {
  53.     digitalWrite(RED, HIGH);
  54.   } else {
  55.     analogWrite(RED, map(distance, RED_LONG, RED_SHORT, 255, 0));
  56.   }
  57.  
  58.   if ((distance > GREEN_LONG) || (distance < GREEN_SHORT)) {
  59.       digitalWrite(GREEN, HIGH);
  60.   } else {
  61.       analogWrite(GREEN, map(distance, GREEN_LONG, GREEN_SHORT, 255, 0));
  62.   }
  63.  
  64.   if ((distance > BLUE_LONG) || (distance < BLUE_SHORT)) {
  65.       digitalWrite(BLUE, HIGH);
  66.   } else {
  67.       analogWrite(BLUE, map(distance, BLUE_LONG, BLUE_SHORT, 255, 0));
  68.   }
  69.  
  70.   Serial.print(distance);
  71.   Serial.print("cm");
  72.   Serial.println();
  73.  
  74.   delay(100);
  75. }
  76.  
  77. long ping() {
  78.   // returns the distance to the nearest object.
  79.   // establish variables for duration of the ping,
  80.   // and the distance result in inches and centimeters:
  81.   long duration, cm;
  82.  
  83.   // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  84.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  85.   pinMode(PING_PIN, OUTPUT);
  86.   digitalWrite(PING_PIN, LOW);
  87.   delayMicroseconds(2);
  88.   digitalWrite(PING_PIN, HIGH);
  89.   delayMicroseconds(5);
  90.   digitalWrite(PING_PIN, LOW);
  91.  
  92.   // The same pin is used to read the signal from the PING))): a HIGH
  93.   // pulse whose duration is the time (in microseconds) from the sending
  94.   // of the ping to the reception of its echo off of an object.
  95.   pinMode(PING_PIN, INPUT);
  96.   duration = pulseIn(PING_PIN, HIGH);
  97.  
  98.   // convert the time into a distance
  99.   return (microsecondsToCentimeters(duration));
  100.  
  101. }
  102.  
  103. long microsecondsToCentimeters(long microseconds)
  104. {
  105.   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  106.   // The ping travels out and back, so to find the distance of the
  107.   // object we take half of the distance travelled.
  108.   return microseconds / 29 / 2;
  109. }