Sei sulla pagina 1di 6

// This is an example for the BMP085 Barometric

Pressure and Temp Sensor


// Connect VCC of the BMP085 sensor to 3.3V
// Connect GND to Ground
// Connect SCL to I2C clock (on Arduino
Nano/Uno/Duemilanove/etc thats Analog 5
// Connect SDA to I2C data (on Arduino
Nano/Uno/Duemilanove/etc thats Analog 4

#include <Wire.h>
#include <BMP085.h>
BMP085 bmp;

#include <Servo.h>
//SERVO CODE
const int buttonPin = 2;
//SERVO CODE
//const int ledPin = 13;
//SERVO CODE
Servo servo1;
//SERVO CODE

int buttonPushCounter = 0;
//SERVO CODE
int buttonState = 0;
//SERVO CODE
int lastButtonState = 0;
//SERVO CODE
int pos = 0;
//SERVO CODE
// defines pins numbers
//SENSOR CODE A
const int trigPin = 7;
const int echoPin = 6;
const int buzzer = 3;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;
//SENSOR CODE Z

void setup() {
Serial.begin(9600);
bmp.begin();

pinMode(buttonPin, INPUT);
//pinMode(ledPin, OUTPUT);
servo1.attach(9);

pinMode(trigPin, OUTPUT); // Sets the trigPin as an


Output
pinMode(echoPin, INPUT); // Sets the echoPin as an
Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
Serial.print(" Temperature = ");
Serial.print(bmp.readTemperature());
Serial.print(" C, ");
Serial.print(" Pressure = ");
Serial.print(bmp.readPressure());
Serial.print(" Pa, ");
Serial.print(" Altitude = ");
Serial.print(bmp.readAltitude());
Serial.print(" meters, ");
Serial.print(" Pressure at sealevel (calculated) =
");
Serial.print(bmp.readSealevelPressure());
Serial.print(" Pa, ");
Serial.print(" Real altitude = ");
Serial.print(bmp.readAltitude(101500));
Serial.print(" meters" );
Serial.println();
delay(400);

buttonState =
digitalRead(buttonPin); //SERVO
CODE BEGIN

if (buttonState != lastButtonState)

{
if (buttonState == HIGH) {

buttonPushCounter++;
}

else {

}
}

//Servo sweep 1:
//delay(6000);
if (buttonPushCounter % 1 == 0)
{
//digitalWrite(ledPin, HIGH);

for(pos = 0; pos <= 10; pos += 10)


{
servo1.write(pos);
delay(15);
}
for(pos = 10; pos>=0; pos-=10)
{
servo1.write(pos);
delay(15);
}

else
{
//digitalWrite(ledPin, LOW);
}
//Servo sweep 2:
if (buttonPushCounter % 2 == 0)
{
//digitalWrite(ledPin, HIGH);

for(pos = 0; pos <= 90; pos += 10)


{
servo1.write(pos);
delay(15);
}
for(pos = 90; pos>=0; pos-=10)
{
servo1.write(pos);
delay(15);
}

else
{
//digitalWrite(ledPin, LOW);
}

lastButtonState = buttonState;
//SERVO CODE FINISH

// Clears the trigPin


digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel


time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance


distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 5){
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor


Serial.print("Distance: ");
Serial.println(distance);
}

Potrebbero piacerti anche