Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

// Needed Libraries:

// LCD Shield: https://github.com/adafruit/Adafruit-RGB-LCD-Shield-Library


// DHT Sensor: https://github.com/adafruit/DHT-sensor-library

// Temp/Humidity Sensor
//#include "Debounce.h"
/*#include "DHT.h"

// LCD
#include <Wire.h>

// MENU ARRAYS
String menuOption[] = {{"Setting 1"}, {"Setting 2"}, {"Setting 3"}}; //
Text on the top line
String measurementType[] = {{"[DEG. CEL]"}, {"HUMIDITY"}, {"DAYS"}}; // Text on
the bottom line

//Delays values since sensor needs approximately 2 seconds to read values


unsigned long previousMillis = 0UL;
unsigned long interval = 2000UL;

// Temp/Humidity Sensor
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// PINS ON THE ARDUINO.
// MAKE THESE "CONST INT" VARIABLES AS THESE DO NOT CHANGE IN THE PROGRAM.
const int leftButtonPin = 5; // Pin 5 for "Left" command
const int rightButtonPin = 6; // Pin 6 for "Right" command
const int upButtonPin = 7; // Pin 7 for "Up" command
const int downButtonPin = 8; // Pin 8 for "Down" command
const int enterButtonPin = 9; // Pin 9 for "Enter" command
const int clearButtonPin = 10; // Pin 10 for "Clear" command

// NUMBER COUNT OF BUTTON PRESSES AND COUNTER UNITS.


// MAKE THESE A "INT" VARIABLES TO ALLOW FOR NEGATIVE INTEGERS.
int setting1Counter = 0; // Counters for settings 1 - 3
int setting2Counter = 0;
int setting3Counter = 0;
int directionPush = 0; // This counter changes the menu option with each
"left" or "right" button push.
int upPressCount = 0; // This counter measures the amount of times the user
pushes the "up" button.
int downPressCount = 0; // This counter measures the amount of times the user
pushes the "down" button.

// BUTTON PRESS STATES FOR EACH FUNCTION, ALL SET TO "LOW".


// MAKE THESE "BOOLEAN" VARIABLES AS THESE ONLY WILL BE "HIGH" OR "LOW".
bool buttonStateLeft = LOW; // Button states for the "Left" command
bool lastButtonStateLeft = LOW;
bool currentButtonStateLeft = LOW;
bool buttonStateRight = LOW; // Button states for the "Right" command
bool lastButtonStateRight = LOW;
bool currentButtonStateRight = LOW;
bool buttonStateUp = LOW; // Button states for the "Up" command
bool lastButtonStateUp = LOW;
bool currentButtonStateUp = LOW;
bool buttonStateDown = LOW; // Button states for the "Down" command
bool lastButtonStateDown = LOW;
bool currentButtonStateDown = LOW;
bool buttonStateEnter = LOW; // Button states for the "Enter" command
bool lastButtonStateEnter = LOW;
bool currentButtonStateEnter = LOW;
bool buttonStateClear = LOW; // Button states for the "Clear" command
bool lastButtonStateClear = LOW;
bool currentButtonStateClear = LOW;

// OBJECT DECLARATION
//LiquidCrystal_I2C lcd(0x27, 16, 2); // Activates the LCD "object"

// LCD
//Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// Relay
#define RELAYPIN 3

//Motor Control Definitions


#define CW 11 //CW is defined as pin #7//

#define CCW 12 //CCW is defined as pin #8//

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

lcd.backlight(); // THESE COMMANDS TURN ON AND CLEAR THE LCD


SCREEN declared in line 69****************
lcd.init();
lcd.clear();

//Motor Control Pin definitions


pinMode(CW, OUTPUT); //Set Clockwise Motion Relay Controlling Pin as an output//
pinMode(CCW, OUTPUT); //Set CounterClockwise Relay Controling Pin as an output//
pinMode(leftButtonPin, INPUT_PULLUP); // SETS THE leftButtonPin AS AN INPUT
pinMode(rightButtonPin, INPUT_PULLUP); // SETS THE rightButtonPin AS AN INPUT
pinMode(upButtonPin, INPUT_PULLUP); // SETS THE upButtonPin AS AN INPUT
pinMode(downButtonPin, INPUT_PULLUP); // SETS THE downButtonPin AS AN INPUT
pinMode(enterButtonPin, INPUT_PULLUP); // SETS THE enterButtonPin AS AN INPUT
pinMode(clearButtonPin, INPUT_PULLUP); // SETS THE clearButtonPin AS AN INPUT

delay(1000);

// setup the Tempt/Humidity Sensor


dht.begin();

// Setup relay
pinMode(RELAYPIN, OUTPUT);
}

void loop() {
int unitSetting[] = {setting1Counter, setting2Counter, setting3Counter}; //
This variable holds the individual counters in one array

lcd.setCursor(0,0); // Menu displayed on the LCD.


lcd.print(menuOption[directionPush]); // The menuOption that is
displayed is determined by the left or right push.
lcd.setCursor(0,1);
lcd.print(unitSetting[directionPush]); // The setting counter that is
displayed is determined by the left or right push.
lcd.setCursor(5,1); // This cursor setting fixes the
measurementType in one place, preventing offset
lcd.print(measurementType[directionPush]); // by the unitSetting.

// The program at this point is waiting for a button press.


currentButtonStateLeft = digitalRead(leftButtonPin);
currentButtonStateRight = digitalRead(rightButtonPin);
currentButtonStateUp = digitalRead(upButtonPin);
currentButtonStateDown = digitalRead(downButtonPin);
currentButtonStateEnter = digitalRead(enterButtonPin);
currentButtonStateClear = digitalRead(clearButtonPin);

if (currentButtonStateLeft != lastButtonStateLeft || currentButtonStateRight !=


lastButtonStateRight ||
currentButtonStateUp != lastButtonStateUp || currentButtonStateDown !=
lastButtonStateDown || currentButtonStateEnter != lastButtonStateEnter)
// If there is a button push on any of the buttons, the following routine runs
to check if it was a valid press:
{
lastDebounceTime = millis(); // lastDebounceTime is set equal to the
running millis() function.
}

if ((millis() - lastDebounceTime) > debounceDelay)


// If the lastDebounceTime (aka. the "snapshot" time) minus the running
millis() function is higher than the set debounce delay, the following routine
// below runs and checks which button was pushed:
{

// The current state for each button is set not equal to the pressed state
and when it changes, the pressed state becomes equal to the current state.

// LEFT BUTTON PRESS


if (currentButtonStateLeft != buttonStateLeft) // Left button scrolls
the menu options to the left.
{
buttonStateLeft = currentButtonStateLeft;

if (buttonStateLeft == LOW) // Once the button is


released, the push is registered and the code below runs.
{
directionPush--; // Both the up and down
press counts will be reset to zero when the left button is pushed.
upPressCount = 0;
downPressCount = 0;
}

if (directionPush < 0) // If the user tries to


scroll below the first menu option,
{ // the program will loop
back to the last menu option.
directionPush = 2;
}
lcd.clear();
}

// RIGHT BUTTON PRESS


if (currentButtonStateRight != buttonStateRight) // Right button
scrolls the menu options to the right.
{
buttonStateRight = currentButtonStateRight;

if (buttonStateRight == LOW)
{
directionPush++; // Both the up and down
press counts will be reset to zero when the right button is pushed.
upPressCount = 0;
downPressCount = 0;
}

if (directionPush > 2 ) // If the user


tries to scroll above the last menu option,
{ // the program will loop
back to the first menu option.
directionPush = 0;
}
lcd.clear();
}

// UP BUTTON PRESS
if (currentButtonStateUp != buttonStateUp) // Up button scrolls the
setting upward.
{
buttonStateUp = currentButtonStateUp;

if (buttonStateUp == LOW && directionPush == 0) // The first 5 times in


which the "up" button is pushed, each push will add 1 increment to the setting.

{
upPressCount++;
downPressCount = 0; // The downPressCount is
reset to zero.
setting1Counter++;

if (upPressCount > 5) // If the "up" button is


pushed more than 5 times consecutively, the setting increment increases by 5
{ // with every "up"
button push and resets back when the down, left or right button is pushed.
setting1Counter = setting1Counter + 4;
}

if (setting1Counter > 40) // Sets the setting


counter limit to 999. The user cannot increase the counter beyond 999.
{
setting1Counter = 40;
}
}

if (buttonStateUp == LOW && directionPush == 1)


{
upPressCount++;
downPressCount = 0;
setting2Counter++;

if (upPressCount > 5)
{
setting2Counter = setting2Counter + 4;
}

if (setting2Counter > 100) // Sets the setting


counter limit to 100. The user cannot increase the counter beyond 999.
{
setting2Counter = 100;
}
}

if (buttonStateUp == LOW && directionPush == 2)


{
upPressCount++;
downPressCount = 0;
setting3Counter++;

if (upPressCount > 5)
{
setting3Counter = setting3Counter + 4;
}

if (setting3Counter > 999) // Sets the setting


counter limit to 999. The user cannot increase the counter beyond 999.
{
setting3Counter = 999;
}
}

// DOWN BUTTON PRESS


if (currentButtonStateDown != buttonStateDown) // Down button scrolls
the setting downward.
{
buttonStateDown = currentButtonStateDown;

if (buttonStateDown == LOW && directionPush == 0) // The first 5 times in


which the "down" button is pushed, each push will subtract 1 increment to the
setting.
{
downPressCount++;
upPressCount = 0; // The upPressCount is
reset to zero.
setting1Counter--;

if (downPressCount > 5) // If the "down" button


is pushed more than 5 times consecutively, the setting increment decreases by 5
{ // with every "down"
button push and resets back when the up, left or right button is pushed.
setting1Counter = setting1Counter - 4;
}

if (setting1Counter < -1) // Sets the setting


counter limit to -999. The user cannot increase the counter beyond -999.
{
setting1Counter = -1;
}
}

if (buttonStateDown == LOW && directionPush == 1)


{
downPressCount++;
upPressCount = 0;
setting2Counter--;

if (downPressCount > 5)
{
setting2Counter = setting2Counter - 4;
}

if (setting2Counter < 0) // Sets the setting


counter limit to 0 cannot decrease the counter beyond 0.
{
setting2Counter = 0;
}
}

if (buttonStateDown == LOW && directionPush == 2)


{
downPressCount++;
upPressCount = 0;
setting3Counter--;

if (downPressCount > 5)
{
setting3Counter = setting3Counter - 4;
}

if (setting3Counter < 0) // This code prevents the user from entering


{ // a number below "0".
setting3Counter = 0; // Remove this code if you want to allow in
} // negative numbers on a setting.
}

lcd.clear();
}

// ENTER BUTTON PRESS


if (currentButtonStateEnter != buttonStateEnter)
{
buttonStateEnter = currentButtonStateEnter;

if (buttonStateEnter == LOW && directionPush == 0) // The Enter button


simply enters the setting and flashes a brief message.
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DEGREES CEL.");
lcd.setCursor(0,1);
lcd.print("IS SET");
delay(2000);

float ControlTemp = setting1Counter; //Sets the selected temperature as


the control temperature to be compared to
}

if (buttonStateEnter == LOW && directionPush == 1)


{

lcd.clear();
lcd.setCursor(0,0);
lcd.print("HUMIDITY");
lcd.setCursor(0,1);
lcd.print("IS SET");
delay(2000);

float ControlHumidity = setting2Counter; ////Sets the selected humidity as the


control humidity to be compared to
}

if (buttonStateEnter == LOW && directionPush == 2)


{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("NO. OF DAYS");
lcd.setCursor(0,1);
lcd.print("IS SET");
delay(2000);

float ControlDays = setting3Counter;// Sets the days to incubate


}
lcd.clear();
}

// CLEAR BUTTON PRESS


if (currentButtonStateClear != buttonStateClear)
{
buttonStateClear = currentButtonStateClear;

if (buttonStateClear == LOW && directionPush == 0) // The Clear button


clears all setting data depending on what menu option you are viewing.

{ // It flahses a brief
message stating that the data has been cleared.
lcd.clear(); // The press counts
for both the up and down variables are also reset to zero.
lcd.setCursor(0,0);
lcd.print("TEMPERATURE");
lcd.setCursor(0,1);
lcd.print("IS CLEARED");
setting1Counter = 0;
downPressCount = 0;
upPressCount = 0;
delay(2000);
}
if (buttonStateClear == LOW && directionPush == 1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("HUMIDITY");
lcd.setCursor(0,1);
lcd.print("IS CLEARED");
setting2Counter = 0;
downPressCount = 0;
upPressCount = 0;
delay(2000);
}

if (buttonStateClear == LOW && directionPush == 2)


{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("No. OF DAYS");
lcd.setCursor(0,1);
lcd.print("IS CLEARED");
setting3Counter = 0;
downPressCount = 0;
upPressCount = 0;
delay(2000);
}
// After a button is pushed and the count recorded, all the states reset back
to LOW for the data to be processed correctly.
lastButtonStateLeft = currentButtonStateLeft; // resets the left button
state to LOW
lastButtonStateRight = currentButtonStateRight; // resets the right button
state to LOW
lastButtonStateUp = currentButtonStateUp; // resets the up button
state to LOW
lastButtonStateDown = currentButtonStateDown; // resets the down button
state to LOW
lastButtonStateEnter = currentButtonStateEnter; // resets the enter button
state to LOW
lastButtonStateClear = currentButtonStateClear; // resets the clear button
state to LOW

// Automated limit switching

// Wait for 2 seconds between measurements.


unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval)


{

// Reading temperature or humidity takes about 250 milliseconds!


// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
// Read temperature as Fahrenheit
float temp = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

if (temp < ControlTemp) {


digitalWrite(RELAYPIN, HIGH);
} else if (temp > ControlTemp) {
digitalWrite(RELAYPIN, LOW);
}

lcd.setCursor(0,0);
lcd.print("H: ");
lcd.print(humidity);
lcd.print(" % ");
lcd.setCursor(0,1);
lcd.print("T: ");
lcd.print(temp);
lcd.print(" F ");
}
//Updates the delay variable
previousMillis = currentMillis;
}*/

You might also like