Edit: Okay, maybe it's because I made my "period" integer WAY to high, but I can't go back to test my code at the moment. Regardless, thank you to those who responded
Hello, I'm a student. I'm supposed to do an arduino project for a final class assignment. Mine's supposed to be one of those automated soil moisture projects.
However, Despite not getting error messages anymore, it still doesn't seem to work how I want it to. The values I would get from a sensor would seem too low (you'll see it in the code), and the text doesn't appear in the serial monitor. Maybe its how I built it, I don't know. First is the code I used to get the values to put in my actual test code
/*
*/
int sensorValue;
int readPin = A0;
void setup() {
pinMode(readPin,INPUT);
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(readPin);
delay(2000); //wait for voltage to settle
Serial.println(sensorValue);
}
Then here's the test code
/*
*/
const int Pin_D1 = 8;
const int Hydro_D1 = 7; //hydro pump connect to plant of sensor 1
//Measure Soil Moisture Levels
int Value_D1;
int readPin = A0;
//Measure time
int delayTime = 100;
int period = 3600000;
unsigned long time_now = 0;
//Measure Water Level
int waterSensorPin = A1;
int waterValue;
//Water parameter
const int waterThreshold = 320; //value for low water
//Soil parameters
const int dryWall = 350; //value for dry soil
const int wetWall = 335; //value for wet soil
void setup() {
Serial.begin(9600); //serial monitor
pinMode(Pin_D1,OUTPUT); //soil moisture sensor 1
pinMode(readPin,INPUT); //analog input pin
pinMode(waterSensorPin,INPUT); //monitor water level
}
void loop(){
time_now = millis ();
nourishment();
while(millis() < time_now + period){
//wait approx. [period] ms
}
}
void nourishment() {
//PLANT ONE
digitalWrite(Pin_D1,HIGH); //soil moisture sensor 1 ON
delay(2000); //wait for voltage to settle
Value_D1 = analogRead(readPin); //Read analog pin as sensor 1
Serial.println("D1 =");
Serial.print(Value_D1);
waterCheck();
if (Value_D1 < dryWall) {
digitalWrite(Hydro_D1,HIGH); //hydro pump 1 ON
Value_D1 = analogRead(readPin);
delay(delayTime);
Serial.println("D1 =");
Serial.print(Value_D1);
waterCheck();
} else if (Value_D1 < wetWall) {
Value_D1 = analogRead(readPin);
delay(delayTime);
Serial.println("D1 =");
Serial.print(Value_D1);
waterCheck();
}
if (Value_D1 > wetWall) {
digitalWrite(Hydro_D1,LOW);
}
waterCheck();
delay(1000);
}
void waterCheck() {
waterValue = analogRead(waterSensorPin);
Serial.println("Water =");
Serial.print(waterValue);
if (waterValue < waterThreshold) {
lowWater();
}
}
void lowWater() {
digitalWrite(Pin_D1,LOW);
digitalWrite(Hydro_D1,LOW);
}
One more example that shows that the default compiler warning level should be ALL.
arduino-cli compile -b arduino:avr:uno --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:12:14: warning: overflow in implicit constant conversion [-Woverflow]
int period = 3600000;
^~~~~~~
Sketch uses 5600 bytes (17%) of program storage space. Maximum is 32256 bytes.
Global variables use 208 bytes (10%) of dynamic memory, leaving 1840 bytes for local variables. Maximum is 2048 bytes.
Compilation finished successfully.
Compiling it in the IDE with the compiler warning level set to ALL would seem to be an obvious solution. Or you could just read the warning message that the compiler generated for me...