Skip to content

Instantly share code, notes, and snippets.

@laisan86
Created April 29, 2017 12:44
Show Gist options
  • Save laisan86/c3b8ea734bfc21944b715a4396b02aae to your computer and use it in GitHub Desktop.
Save laisan86/c3b8ea734bfc21944b715a4396b02aae to your computer and use it in GitHub Desktop.
Sensors controlling
#include "Barometer.h"
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial SoftSerial(2, 3);
unsigned char buffer[64]; // buffer array for data receive over serial port
int count=0;
float temperature;
float pressure;
float atm;
float altitude;
Barometer myBarometer;
int lightPin = A2;
int lightValue = 0;
int moisturePin = A3;
int moistureValue = 0;
int incomingByte = 0;
Servo door;
Servo branch;
boolean isOpen = true;
void setup() {
Serial.begin(9600);
SoftSerial.begin(9600);
myBarometer.init();
branch.attach(9);
door.attach(10);
door.write(0);
branch.write(90);
delay(3000);
}
void clearBufferArray() // function to clear buffer array
{
for (int i=0; i<count;i++)
{
buffer[i]=NULL;
} // clear all index of array with command NULL
}
void loop()
{
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (SoftSerial.available()) // if date is coming from software serial port ==> data is coming from SoftSerial shield
{
while(SoftSerial.available()) // reading data into char array
{
buffer[count++]=SoftSerial.read(); // writing data into array
if(count == 64)break;
}
}
if (incomingByte == 'B') {
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;
lightValue = analogRead(lightPin);
moistureValue = analogRead(moisturePin);
Serial.print(temperature, 2); //display 2 decimal places
Serial.print(" ");
/*
Serial.print(pressure, 0); //whole number only.
Serial.print(" ");
*/
Serial.print(atm, 4); //display 4 decimal places
Serial.print(" ");
Serial.print(altitude, 2); //display 2 decimal places
Serial.print(" ");
Serial.print(lightValue);
Serial.print(" ");
Serial.print(moistureValue);
Serial.print(" ");
/*
Serial.write(buffer,count); // if no data transmission ends, write buffer to hardware serial port
clearBufferArray(); // call clearBufferArray function to clear the stored data from the array
count = 0; // set counter of while loop to zero
*/
Serial.println();
if(isOpen == true)
{
door.write(90);
branch.write(0);
delay(3000);
isOpen = false;
}
}
else if(incomingByte == 'R')
{
if(isOpen == false)
{
door.write(0);
branch.write(90);
delay(3000);
isOpen = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment