Source
AtmosphericPrognosticator.ino
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Stepper.h>
// the toggle switches for temp, pressure, humidity - all three must be on
const int tempToggle = 11; // the pin the temp switch is on
int ttoggleState;
const int pressToggle = 10; // the pin the pressure switch is on
int ptoggleState;
const int humeToggle = 35; // the pin the humidity switch is on
int htoggleState;
//lights
const int tempLight = 12; //the pin the temp light is on
const int pressLight = 13; //the pin the pressure light is on
const int humeLight = 33; //the pin the humidity light is on
// sesnsor
Adafruit_BME280 bme; // I2C
float prevTemp;
float currentTemp;
float tempChange;
float prevPress;
float currentPress;
float pressChange;
float prevHume;
float currentHume;
float humeChange;
//motors
#define STEPS 2048 //steps per revolution
Stepper tStepper(STEPS, 2, 4, 3, 5); //temp motor
Stepper pStepper(STEPS, 6, 8, 7, 9); //pressure motor
Stepper hStepper(STEPS, 36, 38, 37, 39); //humidity motor
int tmotSpeed = 7;
int tSteps;
int tstepGoal;
int tstepsTaken;
int tstepstoZero;
int pSteps;
int pstepGoal;
int pstepsTaken;
int pstepstoZero;
int hSteps;
int hstepGoal;
int hstepsTaken;
int hstepstoZero;
//timing
unsigned long currentMillis = 0;
unsigned long prevmeasureTime = 0;
unsigned long lastTeventTime = 0;
unsigned long lastHeventTime = 0;
unsigned long lastPeventTime = 0;
unsigned long tzeroTime = 0;
unsigned long hzeroTime= 0;
unsigned long pzeroTime = 0;
const unsigned long period = 1000;
const unsigned long tmotorPeriod = 3000;
const unsigned long pmotorPeriod = 3000;
const unsigned long hmotorPeriod = 3000;
//user defined functions
void readSensor() { //function to take values from sensor and convert to steps
currentMillis = millis(); //set current time
if (currentMillis - period > prevmeasureTime) {
prevmeasureTime = currentMillis;
currentTemp = bme.readTemperature(); //read temp value from sensor
currentPress = bme.readPressure(); //read pressure value from sensor
currentHume = bme.readHumidity(); //read humidity value from sensor
currentTemp = (currentTemp * 9 / 5) + 32; //covert C temp to F temp
tempChange = currentTemp - prevTemp; //define change in temp - first loop will set actual current temp - Change from zero
tSteps = int(trunc(tempChange)); //truncate change in temp value to int
tstepGoal = map(tSteps, 0, 105, 0, 1120); //map temp reading to steps
prevTemp = currentTemp; //set the temp to be checked again
currentPress = (currentPress / 100) * 0.02953; // convert millibars to hPA
pressChange = currentPress - prevPress; // define change in pressure - first loop will set actual current pressure change - from zero
pSteps = int(trunc(pressChange)); //truncate change in pressure value to int
pstepGoal = map(pSteps, 20, 60, 0, 1630); //map pressure reading into steps
prevPress = currentPress; //set oressure to be checked again
humeChange = currentHume - prevHume; // define change in humidity - first loop will set actual humidity - change from zero
hSteps = int(trunc(humeChange)); //truncate change in humidity value to int
hstepGoal = map(hSteps, 0, 100, 0, 1120); //map humidity reading into steps
prevHume = currentHume; // set humidity to be checked again
} else {
Serial.println("wait");
}
}
void runtempMotor() { //function to move the temp stepper
currentMillis = millis(); //set current time
if (currentMillis - tmotorPeriod > lastTeventTime) {
lastTeventTime = currentMillis; //reset timer
if (tSteps != 0) {
tstepGoal = tstepGoal * -1; //check if steps were defined in sensor read
tStepper.step(tstepGoal); //step to move to goal
Serial.print("The converted temp change to change to steps is: ");
Serial.println(tSteps);
Serial.print("Taking this many steps on the temp motor ");
Serial.println(tstepGoal);
Serial.print("The previous temp value has been set to the curren temp of: ");
Serial.println(prevTemp);
tstepstoZero = tstepstoZero + tstepGoal;
} else {
Serial.println("no temp steps to take");
lastTeventTime = currentMillis; //reset timer
}
} else {
Serial.println("wait");
}
}
void runpressMotor() { //function to move the pressure stepper
currentMillis = millis(); //set current time
if (currentMillis - pmotorPeriod > lastPeventTime) {
lastPeventTime = currentMillis; //reset timer
if (pSteps != 0) { //check if steps were defined in sensor read
pstepGoal = pstepGoal * -1;
pStepper.step(pstepGoal); //step to move to goal
Serial.print("The converted pressure change to change to steps is: ");
Serial.println(pSteps);
Serial.print("Taking this many steps on the pressure motor ");
Serial.println(pstepGoal);
Serial.print("The previous pressure value has been set to the current pressure of: ");
Serial.println(prevPress);
pstepstoZero = pstepstoZero + pstepGoal;
} else {
Serial.println("no pressure steps to take");
lastPeventTime = currentMillis; //reset timer
}
} else {
Serial.println("wait");
}
}
void runhumeMotor() { //function to move the humidity stepper
currentMillis = millis(); //set current time
if (currentMillis - hmotorPeriod > lastHeventTime) {
lastHeventTime = currentMillis; //rest timer
if (hSteps != 0) {
hstepGoal = hstepGoal * -1; //check if steps were defined in sensor read
hStepper.step(hstepGoal); //step to move to goal
Serial.print("The current humidity variable is set to: "); //print stuff instead of moving motor
Serial.println(currentHume); //test print
Serial.print("Taking this many steps on the humidity motor ");
Serial.println(hstepGoal); //test print
hstepstoZero = hstepstoZero + hstepGoal; //set step position
} else {
Serial.println("no humidity steps to take");
lastHeventTime = currentMillis; //rest timer
}
} else {
Serial.println("wait");
}
}
void zerotempMotor() { //function to return motor to starting point
currentMillis = millis(); //set current time
if (currentMillis - tmotorPeriod > tzeroTime) {
tzeroTime = currentMillis; //reset timer
if (tstepstoZero != 0) { // check to see if steps have happened at some point and step backwards
tstepstoZero = tstepstoZero * -1; // reverse step value
tStepper.step(tstepstoZero); // step backwards
Serial.println("Since the switches are off"); //test print
Serial.print("return the temp motor this many steps:"); //test print
Serial.println(tstepstoZero); //test print original backwards steps
tstepstoZero = 0; //set the number of steps to zero back to zero
Serial.print("the new temp step to zero value is:"); //reset step counter to home point after return
Serial.println(tstepstoZero); //test print 0 backwards steps
lastTeventTime = currentMillis; //reset timer
} else {
Serial.println("the temp motor doesn't need to be zeroed");
tzeroTime = currentMillis; //reset timer
}
} else {
Serial.println("wait");
}
}
void zeropressMotor() { //function to return motor to starting point
currentMillis = millis(); //set current time
if (currentMillis - pmotorPeriod > pzeroTime) {
pzeroTime = currentMillis; //reset timer
if (pstepstoZero != 0) { // check to see if steps have happened at some point and step backwards
pstepstoZero = pstepstoZero * -1; // reverse step value
pStepper.step(pstepstoZero); // step backwards
Serial.println("Since the switches are off"); //test print
Serial.print("return pressure motor this many steps:"); //test print
Serial.println(pstepstoZero); //test print original backwards steps
pstepstoZero = 0; //set the number of steps to zero back to zero
Serial.print("the new pressure step to zero value is:"); //reset step counter to home point after return
Serial.println(pstepstoZero); //test print 0 backwards steps
} else {
Serial.println("the pressure motor doesn't need to be zeroed");
pzeroTime = currentMillis; //reset timer
}
} else {
Serial.println("wait");
}
}
void zerohumeMotor() { //function to return motor to starting point
currentMillis = millis(); //set current time
if (currentMillis - hmotorPeriod > hzeroTime) {
hzeroTime = currentMillis; //else reset timer
if (hstepstoZero != 0) { // check to see if steps have happened at some point and step backwards
hstepstoZero = hstepstoZero * -1; // reverse step value
hStepper.step(hstepstoZero); // step backwards
Serial.print("return the humidity motor this many steps:");
Serial.println(hstepstoZero); //test print original backwards steps
hstepstoZero = 0; //set the number of steps to zero back to zero
Serial.print("the new humidity step to zero value is:"); //reset step counter to home point after return
Serial.println(hstepstoZero); //test print 0 backwards steps
} else {
Serial.println("the humidity motor doesn't need to be zeroed");
hzeroTime = currentMillis; //else reset timer
}
} else {
Serial.println("wait");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //begin serial monitor
lastTeventTime = millis(); //set start time
lastPeventTime = millis(); //set start time
lastHeventTime = millis(); //set start time
prevmeasureTime = millis(); //set start time
tstepstoZero = 0; //set return steps as 0
hstepstoZero = 0; //set return steps as 0
pstepstoZero = 0; //set return steps as 0
pinMode(tempToggle, INPUT_PULLUP); //set toggle switch
pinMode(pressToggle, INPUT_PULLUP); //set toggle switch
pinMode(humeToggle, INPUT_PULLUP); //set toggle switch
pinMode(tempLight, OUTPUT); //set temp light
pinMode(pressLight, OUTPUT); //set the pressure light
pinMode(humeLight, OUTPUT); //set the pressure light
tStepper.setSpeed(tmotSpeed); // set up the gauges default state
pStepper.setSpeed(tmotSpeed); // set up the gauges default state
hStepper.setSpeed(tmotSpeed); // set up the gauges default state
//tStepper.step(20); //use if need to reset motor position
//hStepper.step(-100); //use if need to reset motor position
//pStepper.step(600); //use if need to reset motor position
unsigned status; // start the sensor
status = bme.begin(); // start the sensor
prevTemp = 0; // set the initial temp sensor readings to zero
prevPress = 0; // set the initial temp sensor readings to zero
prevHume = 0; // set the initial temp sensor readings to zero
}
void loop() {
// put your main code here, to run repeatedly:
currentMillis = millis(); //set time when loop starts
ttoggleState = digitalRead(tempToggle); //check temp switch state
ptoggleState = digitalRead(pressToggle); //check pressure switch state
htoggleState = digitalRead(humeToggle); //check pressure switch state
if (ttoggleState == LOW && ptoggleState == LOW && htoggleState == LOW) {
//if swtich is on turn on light, read sensor, and move steps
digitalWrite(tempLight, HIGH); //first turn light on
digitalWrite(pressLight, HIGH); //first turn light on
digitalWrite(humeLight, HIGH); //first turn light on
readSensor(); //next read sensor and convert temp change to steps
runtempMotor(); //if steps exist run temp motor
runpressMotor(); //if steps exist run pressure motor
runhumeMotor(); //if steps exist run humidity motor
} else { //if switch is off
digitalWrite(tempLight, LOW); //turn off light
digitalWrite(pressLight, LOW); //turn off light
digitalWrite(humeLight, LOW); //turn off light
Serial.println("The switches are off"); //print test message
zerotempMotor(); //return temp motor to starting point which should be 0 steps if never turned on
zeropressMotor(); //return pressure motor to starting point which should be 0 if never turned on
zerohumeMotor(); //return pressure motor to starting point which should be 0 if never turned on
} //switch process complete
} //loop complete