#include "stdlib.h"
#include "EEPROM.h"
#include "string.h"
#include "DHT.h"
#include "math.h"
#define DHTPIN 14
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//defining pins for motor 1 (top plate)
const int dirPin1 = 8;
const int stepPin1 = 9;
//defining pins for motor 2 (leadscrew)
const int dirPin2 = 12;
const int stepPin2 = 11;
//defining steps once, as it is same for both
const int stepsPerRevolution = 200;
int deplevel;
tring userinput;
int y = 0;
int x = 0;
//define push button to be used as toggle for manual and autopilot mode
int button1 = 3;
int status = false;
//define led, ,when led = high, autopilot = ON
int led1 = 13;
//define push buttons to be used a increment and decrement buttons
int button2 = 5;
int status2 = false;
int button3 = 7;
int status3 = false;
//define leds that will light up upon pressing push buttons
int led2 = 4;
int led3 = 6;
//define push button to be used as toggle for folding and unfolding
int button4 = 2;
int status1 = false;
//define led, ,when led = high, unfolding and unfolded
int led4 = 15;
int counter = 0;
unsigned long lastInterrupt1;
unsigned long lastInterrupt2;
void writeIntIntoEEPROM(int address, int number)
{
EEPROM.write(address, number >> 8);
EEPROM.write(address + 1, number & 0xFF);
}
int readIntFromEEPROM(int address)
{
return (EEPROM.read(address) << 8) + EEPROM.read(address + 1);
}
int eepromnumber = readIntFromEEPROM(45);
//setup is run only once, at the start
void setup()
{
Serial.begin(9600);
Serial.setTimeout(100);
Serial.println(F("DHT testing"));
dht.begin();
// Declare pins as Outputs
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
//declare pinmode for push button
pinMode(button1, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
//declare pinmode for led
pinMode(led1, OUTPUT);
pinMode(led4, OUTPUT);
Serial.println("Input actual level of deployment: ");
attachInterrupt (digitalPinToInterrupt(3), statuschange, CHANGE);
attachInterrupt (digitalPinToInterrupt(2), foldchange, CHANGE);
while (counter < 100)
{
counter++;
delay(25);
}
if (Serial.available() >= 1)
{
userinput = Serial.parseInt();
eepromnumber = Serial.parseInt();
}
else
{
int eepromnumber = readIntFromEEPROM(45);
Serial.print("Reading Last Value from EEPROM: ");
Serial.println(eepromnumber);
userinput = eepromnumber;
}
//double check of data input
Serial.println("Level in memory is: ");
Serial.println(userinput);
Serial.print("Reading Last Value from EEPROM: ");
Serial.println(eepromnumber);
}
void loop()
{
//small wait time to allow sensor to initialise properly
delay(3000);
//reading values from stemp sensor
float h = dht.readHumidity();
float t = dht.readTemperature();
//simple data validation method
if (isnan(h) or isnan(t) )
{ Serial.println(F("Failed to read from DHT sensor!"));
return;
}
int deplevel = userinput.toInt();
//displaying received values on serial monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.println("%");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println("°C");
//ideal temperature equation of line
float i_t = 0.3 * t + 18;
//display ideal temperature
Serial.print("Ideal Temperature: ");
Serial.println(i_t);
//check for autopilot mode through button logic
if (status == 0)
{
digitalWrite (led1, !status); //toggle led to indicate manual/autopilot
Serial.println("Status is 0, autopilot is ON and blue led1 is ON");
Serial.println("Read digital read buttin 4 white");
Serial.println(digitalRead(button4));
}
/*if ( (digitalRead(button4) == false) && ( y == 1000) )
{
delay(50);
Serial.print("Lead Screw is deactivated");
digitalWrite(dirPin2, LOW);
for (int y = 0; y < 1000; y++)
{ digitalWrite(stepPin2, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin2, LOW);
delayMicroseconds(6000);
}
delay(1000);
}*/
while ( (status == 0) && (digitalRead (button4) == true) )//while autopilot is ON
{
delay(50);
//check for folding/unfolding through button logic
if ( (eepromnumber == 0) && ( y != 100) )
{
status1 = !status1; //toggle status
digitalWrite (led4, status1); //toggle led to indicate if folded or not
Serial.print("Lead Screw is activated");
digitalWrite(dirPin2, HIGH);
while (y < 100)
{ digitalWrite(stepPin2, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin2, LOW);
delayMicroseconds(6000);
y++;
}
delay(1000);
}
if (t - i_t < -1) //condition for colder outside
{ Serial.println(F("It is colder outside"));
deplevel--;
if (deplevel <= 0)
{
eepromnumber = 0;
}
else
{
eepromnumber = deplevel;
}
Serial.print("The deployment level now should be: ");
Serial.println(deplevel);
writeIntIntoEEPROM(45, eepromnumber);//function to write latest deplevel into EEPROM
switch (deplevel)
{ //for case 0, the deplevel was previously at 1, therefore digitalwrite needs to go HIGH for x=41
case 0:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
Serial.print("Reading Last Value from EEPROM: ");
Serial.println(eepromnumber);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{ digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
case 1:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
Serial.print("Reading Last Value from EEPROM: ");
Serial.println(eepromnumber);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{ digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
case 2:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{ digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
case 3:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{ digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
default:
Serial.println("Deployment is already null");
Serial.print("Reading Last Value from EEPROM: ");
Serial.println(eepromnumber);
delay(6000);
break;
}
delay(5000);
}
else if (t - i_t > 1) //condition for hotter outside
{
Serial.println(F("It is hotter outside"));
deplevel++;
if (deplevel >= 4)
{
eepromnumber = 4;
}
else
{
eepromnumber = deplevel;
}
Serial.print("The deployment level now should be: ");
Serial.println(deplevel);
writeIntIntoEEPROM(45, eepromnumber);//function to write latest deplevel into EEPROM
switch (deplevel)
{
//for case 0, the deplevel was previously at 1, therefore digitalwrite needs to go LOW for x=50
case 1:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{
digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
case 2:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{
digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
case 3:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{
digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
case 4:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{
digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
default:
Serial.println("Deployment is already full");
delay(6000);
break;
}
delay(5000);
}
else
{
Serial.println(F("Temperature is within comfortable range"));
delay (5000);
}
}
Serial.println("Autopilot is OFF, Manual is ON");
if (digitalRead(button2) == true)
{
status2 = !status2;
Serial.println(F("You incremented deplevel manually"));
deplevel++;
if (deplevel >= 4)
{
eepromnumber = 4;
}
else
{
eepromnumber = deplevel;
}
Serial.print("The deployment level now should be: ");
Serial.println(deplevel);
writeIntIntoEEPROM(45, eepromnumber);//function to write latest deplevel into EEPROM
switch (deplevel)
{
//for case 0, the deplevel was previously at 1, therefore digitalwrite needs to go LOW for x=50
case 1:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{
digitalWrite (led2, !status);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
case 2:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{
digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
case 3:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{
digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
case 4:
Serial.print("The deployment level now is: ");
Serial.println(deplevel);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < 41; x++)
{
digitalWrite(stepPin1, HIGH);
delayMicroseconds(6000);
digitalWrite(stepPin1, LOW);
delayMicroseconds(6000);
}
delay(1000);
break;
default:
Serial.println("Deployment is already full");
delay(6000);
break;
}
}
}
void statuschange()
{
if(millis() - lastInterrupt1 > 1500) // we set a 10ms no-interrupts window
{
status = !status;
lastInterrupt1 = millis();
Serial.println("interrupt just worked");
Serial.println(status);
Serial.println(digitalRead (button1));
digitalWrite (led1, !status);
//return (status);
}
}
void foldchange()
{
if(millis() - lastInterrupt2 > 1500) // we set a 10ms no-interrupts window
{
status = !status;
lastInterrupt2 = millis();
Serial.println("interuput just worked");
Serial.println(status1);
Serial.println(digitalRead (button4));
digitalWrite (led4, !status1);
//return (status);
}
}