document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // ============================================================================
  2. // Name        : Hot / Cold Arduino Project
  3. // Author      : Catarina Moreira
  4. // ============================================================================
  5.  
  6. int SensorPinEcho = 8;      // Sonar sensor
  7. int SensorPinTrigg = 7;     // Sonar sensor
  8.  
  9. int RedLedPin=5;        // Red Led pin
  10. int BlueLedPin=6;       // Blue Led pin
  11.  
  12. int buzzer = 9;         // Buzzer pin
  13.  
  14. int distance;           // Distance from sonar sensor
  15. unsigned long pulseDuration = 0;
  16.  
  17. void setup()
  18. {
  19.     noTone( buzzer );
  20. }
  21.  
  22. void loop()
  23. {
  24.     sendPing( SensorPinTrigg, 10 );         // Send a ping
  25.     pulseDuration = receivePing( SensorPinEcho );   // Wait for response
  26.  
  27.     distance = convertToDistance( pulseDuration );  // Distance in cm
  28.  
  29.     lightUpRedLight( RedLedPin, distance );     // Turn on Red Led
  30.    
  31.     if( distance <= 10 )                // When the distance is smaller than 10...
  32.     {
  33.         buzz(buzzer, 2500, 500);            // Play alarm sound
  34.         delayMicroseconds(5);               // Wait a little
  35.     }
  36.    
  37.     lightUpBlueLight( BlueLedPin, distance );       // Turn on Blue Led
  38. }
  39.  
  40. // light up red led according to the distance: the smaller the distance, the brighter the LED is
  41. void lightUpRedLight(int redLedPin, int distance )
  42. {
  43.     if ( ( distance > 0 ) && ( distance < 25 ) )
  44.     {
  45.         int RedValue = (25-distance)*10.2;
  46.         analogWrite(redLedPin, RedValue);
  47.     }
  48.     else
  49.     {
  50.         analogWrite(RedLedPin, LOW);
  51.     }
  52. }
  53.  
  54. // light up blue led according to the distance: the smaller the distance, the brighter the LED is
  55. void lightUpBlueLight( int blueLedPin, int distance )
  56. {
  57.     if ( ( distance > 10 ) && ( distance < 25) )
  58.     {
  59.         int BlueValue = ( distance - 10 )*10.2;
  60.     analogWrite( blueLedPin, BlueValue );
  61.     }
  62.     else if ( ( distance > 25 ) && ( distance < 50) )
  63.     {
  64.         int BlueValue = (50-distance)*10.2;
  65.         analogWrite(BlueLedPin, BlueValue);
  66.     }
  67.     else
  68.     {
  69.        analogWrite(BlueLedPin, 0);
  70.     }
  71. }
  72.  
  73. void buzz(int targetPin, long frequency, long length)
  74. {
  75.     long delayValue = 1000000/frequency/2;         // calculate the delay value between transitions
  76.  
  77.     long numCycles = frequency * length / 1000;    // calculate the number of cycles for proper timing
  78.  
  79.     for (long i=0; i < numCycles; i++)         // for the calculated length of time...
  80.     {
  81.         digitalWrite(targetPin,HIGH);          // write the buzzer pin high to push out the diaphragm
  82.         delayMicroseconds(delayValue);         // wait for the calculated delay value
  83.         digitalWrite(targetPin,LOW);           // write the buzzer pin low to pull back the diaphragm
  84.         delayMicroseconds(delayValue);         // wait again for the calculated delay value
  85.      }
  86. }
  87.  
  88. void sendPing( int sensor, int delay )
  89. {
  90.     pinMode( sensor, OUTPUT );
  91.     digitalWrite( SensorPinTrigg, HIGH );
  92.     delayMicroseconds( delay );
  93.     digitalWrite( sensor, LOW );
  94. }
  95.  
  96. int receivePing( int sensor  )
  97. {
  98.     pinMode ( sensor, INPUT);
  99.     return pulseIn(sensor, HIGH);
  100. }
  101.  
  102. int convertToDistance( int pulseDuration )
  103. {
  104.     pulseDuration=pulseDuration/2;  // divide by two (back/forth for a single trip)
  105.     return int(pulseDuration/29);   //divided by speed of sound = distance in cm   
  106. }
');