/* Banganga-Bit v1.0
Based on a basic BMP085 barometer script which triggers a piezo buzzer
when its about to rain and reads the water level in a tank using an ultrasonic
rangefinder which maps to a series of LEDs
*/
#include “Barometer.h”
#include
//Barometer definitions
float temperature;
float pressure;
float atm;
float altitude;
Barometer myBarometer;
int pinSpeaker = 3; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
const float p0 = 101325; // Pressure at sea level (Pa)
// Add these to the top of your program
const float currentAltitude = 1580.08; // current altitude in METERS
const float ePressure = p0 * pow((1-altitude/44330), 5.255); // expected pressure (in Pa) at altitude
float weatherDiff;
//Ultrasonic Definitions
//Digital pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
const int pwPin = 7;
//variables needed to store values
long pulse, inches, cm;
void setup(){
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
myBarometer.init();
for (int i=8; i<14; i++){
pinMode(i, OUTPUT); //sets the led pins 8 to 13 to output
}}
void loop()
{
//Barometer loop
temperature = myBarometer.bmp085GetTemperature(myBarometer.bmp085ReadUT()); //Get the temperature, bmp085ReadUT MUST be called first
pressure = myBarometer.bmp085GetPressure(myBarometer.bmp085ReadUP());//Get the temperature
altitude = myBarometer.calcAltitude(pressure); //Uncompensated caculation - in Meters
atm = pressure / 101325;
Serial.print("Temperature: ");
Serial.print(temperature, 2); //display 2 decimal places
Serial.println("deg C");
Serial.print("Pressure: ");
Serial.print(pressure, 0); //whole number only.
Serial.println(" Pa");
Serial.print("Ralated Atmosphere: ");
Serial.println(atm, 4); //display 4 decimal places
Serial.print("Altitude: ");
Serial.print(altitude, 2); //display 2 decimal places
Serial.println(" m");
// Add this into loop(), after you've calculated the pressure
weatherDiff = pressure - ePressure;
if(weatherDiff > 250){
Serial.println(“Sunny!”);
}
else if ((weatherDiff <= 250) || (weatherDiff >= -250)){
Serial.println(“Partly Cloudy”);
}
else if (weatherDiff > -250){
playTone(300, 160);
delay(150);
Serial.println(“Rain :-(“);
}
Serial.println();
//Ensure to turn off ALL LEDs before continuing
for (int i=8; i<14; i++){
digitalWrite(i, LOW);
}
pinMode(pwPin, INPUT);
//Used to read in the pulse that is being sent by the MaxSonar device.
//Pulse Width representation with a scale factor of 147 uS per Inch.
pulse = pulseIn(pwPin, HIGH);
//147uS per inch
inches = pulse/147;
//change inches to centimetres
cm = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
/* Read the ultrasonic Level
Adjust the value 12 to 27.5 to span 8 to 13 */
int tanklevel = map(cm, 12, 27.5, 8, 13);
// Make sure the value does not go beyond 4 or 13
int LEDnum = constrain(tanklevel, 8, 13);
/*Call the blink function: this will turn the LED on for 10 milliseconds, and keep it
off for only 1 millisecond. You can change the blink rate by changing these values,
however, I want a quick response time when the flex sensor bends, hence the small
values. LEDnum determines which LED gets turned on.*/
blink(LEDnum, 10,1);
delay(1000); //wait a second and get values again.
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
// The blink function - used to turn the LEDs on and off
void blink(int LEDPin, int onTime, int offTime){
// Turn the LED on
digitalWrite(LEDPin, HIGH);
// Delay so that you can see the LED go On.
delay(100);
// Turn the LED Off
digitalWrite(LEDPin, LOW);
// Increase this Delay if you want to see an actual blinking effect.
delay(0);
}