ok, did take the previous advices in account, spent several hours working on it.
currently I'm having 2 problems,
-
From the values that I get, I would assume that the millis() function is working improperly. How do you guys see that?
-
does it make sense how I print to lcd and serial.monitor? Do I understand the concept right (I tried to put it into place, but probably failed...)
-
Loop
sensor reads, value is stored
value is printed to lcd
-
loop
(sensor is off)
"stored" value is printed to lcd
-
loop
(sensor is off)
"stored" value is printed to lcd
...
- loop
sensor reads, value is updated
updated value is printed to lcd
Otherwise, I'm open for criticism too, it's my first arduino project and I'm trying to make (as good as I can) sense of the advices I get and the information I find.
i've currently only 1 sensor in place, the goal would be to have several and the goal would also be to trigger solenoid valves for bringing the water to the plants.
thanks a lot for help
#include "U8glib.h"
int Values [] = {700, 200, 300};
int plantnumber = 0;
unsigned long previousMillisS=0; // millis() returns an unsigned long. SerialMonitor.
unsigned long previousMillisOn=0; // millis() returns an unsigned long. SensorOn
unsigned long previousMillisR=0; // millis() returns an unsigned long. SensorRead
unsigned long previousMillisOff=0; // millis() returns an unsigned long. SensorOff
const int ValueP = 5;
const int ValueM = 6;
const int Plants = 7;
int buttonPushCounterValue = 0;
int buttonPushCounterPlants = 0;
int buttonState1 = 0; // current state of the button
int buttonState2 = 0;
int buttonState3 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;
int lastButtonState3 = 0;
int moistureSensorValue1; // stores the moisture sensor values
int time_between_reads = 10000;
int SensorPower = 2; //digital 2 for powering mosfet that turns on the soil moisture sensor
int moistureSensor [] = {0, 0, 0}; //analog 0 = Sensor1
int moistureSensorValue [] = { 0, 0, 0};
// int moistureSensor1 = 0; //analog 0 = Sensor1
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
void setup()
{
Serial.begin(9600);
u8g.setFont(u8g_font_unifont);
u8g.setColorIndex(1); // Instructs the display to draw with a pixel on.
}
void loop()
{
buttonPushCounterValue = 0;
buttonPushCounterPlants = 0;
pinMode(SensorPower,OUTPUT);
checkForPress();
SensorOn();
SensorRead();
SensorOff();
SerialPrint();
u8g.firstPage();
do {draw_lcd();}
while( u8g.nextPage() );
water();
}
int checkForPress()
{
buttonState1 = digitalRead(ValueP);
buttonState2 = digitalRead(ValueM);
buttonState3 = digitalRead(Plants);
if (buttonState1 !=lastButtonState1)
{if (buttonState1 == HIGH)
{Values [plantnumber] += 5;}
delay(50);}
lastButtonState1 = buttonState1;
if (buttonState2 != lastButtonState2)
{if (buttonState2 == HIGH)
{Values [plantnumber] -= 5;}
delay(50); }
lastButtonState2 = buttonState2;
if (buttonState3 != lastButtonState3)
{if (buttonState3 == HIGH)
{plantnumber++;
if(plantnumber == 3)
plantnumber = 0;}
delay(50);}
lastButtonState3 = buttonState3;
}
void SensorOn(){
unsigned long currentMillisOn = millis(); // grab current time
if ((unsigned long)(currentMillisOn - previousMillisOn) >= time_between_reads) {
previousMillisOn = millis();
digitalWrite(SensorPower, HIGH);
}}
void SensorRead(){
unsigned long currentMillisR = millis(); // grab current time
if ((unsigned long)(currentMillisR - previousMillisR) >= (time_between_reads + 500) ) {
previousMillisR = millis();
moistureSensorValue [0] = analogRead(moistureSensor [0]);
}}
void SensorOff(){
unsigned long currentMillisOff = millis(); // grab current time
if ((unsigned long)(currentMillisOff - previousMillisOff) >= (time_between_reads + 2000)) {
previousMillisOff = millis();
digitalWrite(SensorPower, LOW);
}}
int SerialPrint(){
unsigned long currentMillisS = millis(); // grab current time
if ((unsigned long)(currentMillisS - previousMillisS) >= 6000) {
previousMillisS = millis();
// Serial.println(plantnumber);
// Serial.println(Values [plantnumber]);
Serial.println(moistureSensorValue [0]);
// Serial.println(moistureSensorValue [1]);
// Serial.println(" ");
}
}
void draw_lcd(){
u8g.drawStr( 0, 10, "1.");
u8g.setPrintPos(20, 10);
u8g.print(moistureSensorValue [0]);
if(plantnumber == 0)
u8g.drawStr( 52, 10, "-");
u8g.setPrintPos(60, 10);
u8g.print(Values[0]);
// u8g.drawStr( 0, 22, "2.");
if(plantnumber == 1)
u8g.drawStr( 52, 22, "-");
u8g.setPrintPos(60, 22);
// u8g.print(Values[1]);
// u8g.drawStr( 0, 34, "3.");
if(plantnumber == 2)
u8g.drawStr( 52, 34, "-");
u8g.setPrintPos(60, 34);
// u8g.print(Values[2]);
// u8g.drawStr( 0, 46, "4.");
// u8g.drawStr( 0, 58, "5.");
}
void water(){
/* int Mosfet1 = 11; //digital 11
pinMode(Mosfet1,OUTPUT);
if(moistureSensorValue1 > Values[0])
{digitalWrite(Mosfet1, HIGH);
delay(3000);
digitalWrite(Mosfet1,LOW);
delay(5000);
}
else
{digitalWrite(Mosfet1,LOW);} // keep closed solenid
*/
}