So i've been working on this project for most of the day. Quick run down of my project. I've got a bunch of sensors and one 2X16LCD display. I already got it to display a simple message earlier, so no problem getting it to display.
So the problem. In the main loop function i'm continuously getting data from the sensors, but i only have one small lcd to display it on, so i need for the screen to display data as alternating screens or as a scrolling message that pans over the data so that all the data can be read off it. I would also prefer not using millis() function since I've read that it overflows after 50 days and messes everything up. I may also be getting a real time clock (RTC) on thursday so i may use that if i figure out to use it at all. Any suggestions would be great, i'm just going to post my code currently
/*
created by John Chabot
arduino control system for hydroponic design project
*/
// include the library code:
#include <LiquidCrystal.h>
#include <Time.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int lightSensor1 = A0; //analog input port for light sensor 1
const int lightSensor2 = A1; //analog input port for light sensor 2
const int lightSensor3 = A2; //analog input port for light sensor 3
const int lightSensor4 = A3; //analog input port for light sesnor 4
const int phSensor1 = A4; //analog ph sensor input port
const int phSensor2 = A5; //analog ph sensor input port
const int phSensor3 = A6; //analog ph sensor input port
const int phSensor4 = A7; //analog ph sensor input port
const int led1 = 6; // digital pin to activate pump 1
const int led2 = 7; // digital pin to activate pump 2
const int led3 = 8; // digital pin to activate pump 3
const int led4 = 9; // digital pin to activate pump 4
const int maxLight = 800; // Light Value under direct Sunlight
const int minLight = 0; // Light Value when photocell is covered
const int numReading = 10; //number of reading for data smoothing
//variables for turning on section of code in the loop if they are connected
int on1 = 0;
int on2 = 0;
int on3 = 0;
int on4 = 0;
//variables to writing values coming off the light sensors
int lightValue1 = 0;
int lightValue2 = 0;
int lightValue3 = 0;
int lightValue4 = 0;
//variables to writing values coming off the ph sensors
int phValue1 = 0;
int phValue2 = 0;
int phValue3 = 0;
int phValue4 = 0;
//array used for smoothing data from light sensor
int array1[10];
int array2[10];
int array3[10];
int array4[10];
//variables to storing smoothed averages
int avgValue1 = 0;
int avgValue2 = 0;
int avgValue3 = 0;
int avgValue4 = 0;
int currentIndex = 0; //store current loop position
const int lightOnPin = 13; //digital pin location for lightOn switch
int lightOn = 0; //store current loop position
const int autoSwitchPin = 14; //digital pin locatoin for autoSwitch switch
int autoSwitch = 0; //store current loop position
const int manStatePin = 15; //digital pin locatin for manSwitch switch
int manState = 0; //store current loop position
void setup() {
int setupTime = 60000; //allow for 1 minutes to check for light sensors
lcd.begin(16, 2); //activate lcd
// Uses this loop to ensure that modules are attached, actives code in the loop section accordingly
lcd.print("Intializing ");
lcd.setCursor(0,1);
lcd.print("Please Wait ");
// while loop to detect which which modules are connected
while (millis() < setupTime){
//current light reading
int lightValue1 = analogRead(lightSensor1);
int lightValue2 = analogRead(lightSensor2);
int lightValue3 = analogRead(lightSensor3);
int lightValue4 = analogRead(lightSensor4);
if(lightValue1 != minLight){
on1 = 1;
pinMode(led1, OUTPUT); //sets pin as output (activates)
clearScreen(); //calls clearScreen function to erase lcd
//tells users unit detected the resets screen
lcd.print("detected unit 1 ");
delay(1000);
lcd.setCursor(0,0);
lcd.print("Intializing ");
lcd.setCursor(0,1);
lcd.print("Please Wait ");
setupTime = setupTime + 1000;
}
if(lightValue2 != minLight){
on2 = 1;
pinMode(led2, OUTPUT); //sets pin 7 to output for pump2
clearScreen();
lcd.print("detected unit 2 ");
delay(1000);
lcd.setCursor(0,0);
lcd.print("Intializing ");
lcd.setCursor(0,1);
lcd.print("Please Wait ");
setupTime = setupTime + 1000;
}
if(lightValue3 != minLight){
on3 = 1;
pinMode(led3, OUTPUT); //sets pin 8 to output for pump3
clearScreen();
lcd.print("detected unit 3 ");
delay(1000);
lcd.setCursor(0,0);
lcd.print("Intializing ");
lcd.setCursor(0,1);
lcd.print("Please Wait ");
setupTime = setupTime + 1000;
}
if(lightValue4 != minLight){
on4 = 1;
pinMode(led4, OUTPUT); //sets pin 9 to output for pump4
clearScreen();
lcd.print("detected unit 1 ");
delay(1000);
lcd.setCursor(0,0);
lcd.print("Intializing ");
lcd.setCursor(0,1);
lcd.print("Please Wait ");
setupTime = setupTime + 1000;
}
}
clearScreen();
}
void loop() {
//read digital pin state to determin state of control switches
lightOn = digitalRead(lightOnPin);
autoSwitch = digitalRead(autoSwitchPin);
manState = digitalRead(manStatePin);
//increments counter for data smoothing
currentIndex++;
if(on1 == 1){ // code only active if detected in setup function
phValue1 = map(analogRead(phSensor1),0, 1023, 0, 14); //reads current light value from photocell
//maps light from analog input to 0 to 100 scale
int adjustedLightValue1 = map(lightValue1, 0, 800, 0, 100);
//creats updates array for data smoothing
array1[currentIndex] = adjustedLightValue1;
avgValue1 = smoothAvg(array1);
//logic tree for light controls
if(lightOn == 1){
if(autoSwitch == 1){
setLightLevelAuto(avgValue1, led1);
}
else{
setLightLevelMan(led1);
}
}
}
if(on2 == 1){
phValue2 = map(analogRead(phSensor2),0, 1023, 0, 14);
lightValue2 = analogRead(lightSensor2);
int adjustedLightValue2 = map(lightValue2, 0, 800, 0, 100);
array2[currentIndex] = adjustedLightValue2;
avgValue2 = smoothAvg(array2);
if(lightOn == 1){
if(autoSwitch == 1){
setLightLevelAuto(avgValue2, led2);
}
else{
setLightLevelMan(led2);
}
}
}
if(on3 == 1){
phValue3 = map(analogRead(phSensor3),0, 1023, 0, 14);
lightValue3 = analogRead(lightSensor3);
int adjustedLightValue3 = map(lightValue3, 0, 800, 0, 100);
array3[currentIndex] = adjustedLightValue3;
avgValue3 = smoothAvg(array3);
if(lightOn == 1){
if(autoSwitch == 1){
setLightLevelAuto(avgValue3, led3);
}
else{
setLightLevelMan(led3);
}
}
}
if(on4 == 1){
phValue4 = map(analogRead(phSensor4),0, 1023, 0, 14);
lightValue4 = analogRead(lightSensor4);
int adjustedLightValue4 = map(lightValue4, 0, 800, 0, 100);
array4[currentIndex] = adjustedLightValue4;
avgValue4 = smoothAvg(array4);
if(lightOn == 1){
if(autoSwitch == 1){
setLightLevelAuto(avgValue1, led1);
}
else{
setLightLevelMan(led1);
}
}
}
//resets currentIndex if it advances 10 positions
if(currentIndex > numReading -1){
currentIndex = 0;
}
}
//function for clearing lcd display
void clearScreen(){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,0);
}
//averages array
int smoothAvg(int array[]){
int total = 0;
for (int n = 0; n < 10; n++){
total = total + array[n];
}
int average = total/10;
return average;
}
//control logic for auto light mode
void setLightLevelAuto(int lightLevel, int pin){
if(lightLevel > 60)
{
digitalWrite(pin, LOW);
}
else if(lightLevel < 61 && lightLevel > 29)
{
int fade = map(lightLevel, 30, 60, 0, 255);
analogWrite(pin, fade);
}
else(lightLevel < 30);
{
digitalWrite(pin, HIGH);
}
}
//control logic for man light mode
void setLightLevelMan(int pin){
if (manState == 1)
{
digitalWrite(pin, HIGH);
}
else
{
analogWrite(pin, 100);
}
}