Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ============================================================================
- // Name : Hot / Cold Arduino Project
- // Author : Catarina Moreira
- // ============================================================================
- int SensorPinEcho = 8; // Sonar sensor
- int SensorPinTrigg = 7; // Sonar sensor
- int RedLedPin=5; // Red Led pin
- int BlueLedPin=6; // Blue Led pin
- int buzzer = 9; // Buzzer pin
- int distance; // Distance from sonar sensor
- unsigned long pulseDuration = 0;
- void setup()
- {
- noTone( buzzer );
- }
- void loop()
- {
- sendPing( SensorPinTrigg, 10 ); // Send a ping
- pulseDuration = receivePing( SensorPinEcho ); // Wait for response
- distance = convertToDistance( pulseDuration ); // Distance in cm
- lightUpRedLight( RedLedPin, distance ); // Turn on Red Led
- if( distance <= 10 ) // When the distance is smaller than 10...
- {
- buzz(buzzer, 2500, 500); // Play alarm sound
- delayMicroseconds(5); // Wait a little
- }
- lightUpBlueLight( BlueLedPin, distance ); // Turn on Blue Led
- }
- // light up red led according to the distance: the smaller the distance, the brighter the LED is
- void lightUpRedLight(int redLedPin, int distance )
- {
- if ( ( distance > 0 ) && ( distance < 25 ) )
- {
- int RedValue = (25-distance)*10.2;
- analogWrite(redLedPin, RedValue);
- }
- else
- {
- analogWrite(RedLedPin, LOW);
- }
- }
- // light up blue led according to the distance: the smaller the distance, the brighter the LED is
- void lightUpBlueLight( int blueLedPin, int distance )
- {
- if ( ( distance > 10 ) && ( distance < 25) )
- {
- int BlueValue = ( distance - 10 )*10.2;
- analogWrite( blueLedPin, BlueValue );
- }
- else if ( ( distance > 25 ) && ( distance < 50) )
- {
- int BlueValue = (50-distance)*10.2;
- analogWrite(BlueLedPin, BlueValue);
- }
- else
- {
- analogWrite(BlueLedPin, 0);
- }
- }
- void buzz(int targetPin, long frequency, long length)
- {
- long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
- long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
- for (long i=0; i < numCycles; i++) // for the calculated length of time...
- {
- digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphragm
- delayMicroseconds(delayValue); // wait for the calculated delay value
- digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphragm
- delayMicroseconds(delayValue); // wait again for the calculated delay value
- }
- }
- void sendPing( int sensor, int delay )
- {
- pinMode( sensor, OUTPUT );
- digitalWrite( SensorPinTrigg, HIGH );
- delayMicroseconds( delay );
- digitalWrite( sensor, LOW );
- }
- int receivePing( int sensor )
- {
- pinMode ( sensor, INPUT);
- return pulseIn(sensor, HIGH);
- }
- int convertToDistance( int pulseDuration )
- {
- pulseDuration=pulseDuration/2; // divide by two (back/forth for a single trip)
- return int(pulseDuration/29); //divided by speed of sound = distance in cm
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement