Hi
could someone give me a few pointers on my code please, this is my first attempt at writing a sketch
I am using an Arduino UNO R4 Minima with a Arduino 4 relays shield and 2 capacitance moisture sensors, using the digital output to signal high or low for soil moisture, 2 water pumps
I want to use a photo transistor light sensor, to sense day or night, so no watering at night, then switch each relay in turn, to turn on the moisture sensor and then water pump if required, then a delay (set to 6 sec for testing), before the cycle starts again. There are no errors, and the serial print of the light value works, which is less than 300, but the code still runs, also with or without a digital output from the moisture sensors the pumps run, so I quess the layout of the code is wrong
Thanks for your help
int relay1_pin = 4; // power sensor left
int relay2_pin = 7; // power sensor right
int relay3_pin = 8; // power water pump left
int relay4_pin = 12; // power water pump right
int Lsensor_pin = 2; // digital pin from sensor
int Rsensor_pin = 13; // digital pin from sensor
int waittime = 3;
int sleep = 6; // sleep time before cycle starts again
int light_sensor = A0; // read if day or night
int day_night = 3; // feed to light sensor
int wateringtime = 15; // time water pump on
void setup() {
Serial.begin(9600);
pinMode(relay1_pin, OUTPUT);
pinMode(relay2_pin, OUTPUT);
pinMode(relay3_pin, OUTPUT);
pinMode(relay4_pin, OUTPUT);
pinMode(Lsensor_pin, INPUT);
pinMode(Rsensor_pin, INPUT);
pinMode(light_sensor, INPUT);
pinMode(day_night, OUTPUT);
}
void loop() {
digitalWrite(day_night, HIGH); // turn on light sensor
int lightValue = analogRead(A0);
Serial.print("Analog reading (0-1023: ");
Serial.print(lightValue);
if (light_sensor > 300); { // light
digitalWrite(relay1_pin, HIGH); // if light turn on left sensor
delay(waittime * 1000);
digitalRead(Lsensor_pin); // read left sensor
}
if (Lsensor_pin == HIGH); {
digitalWrite(relay3_pin, HIGH); // turn on left water pump
delay(wateringtime * 1000);
digitalWrite(relay3_pin, LOW); // turn off water pump
digitalWrite(relay1_pin, LOW); // turn off left sensor
}
if (Lsensor_pin == LOW); {
digitalWrite(relay1_pin, LOW);
digitalWrite(relay2_pin, HIGH); // turn on right sensor
delay(waittime * 1000);
digitalRead(Rsensor_pin); // read right sensor
}
if (Rsensor_pin == HIGH); {
digitalWrite(relay4_pin, HIGH); // turn on right pump
delay(wateringtime * 1000);
digitalWrite(relay4_pin, LOW); // turn off right pump
digitalWrite(relay2_pin, LOW); // turn off right sensor
}
if (Rsensor_pin == LOW); {
digitalWrite(relay2_pin, LOW);
delay(sleep * 1000);
}
if (light_sensor < 300); {
digitalWrite(day_night, LOW);
delay(sleep * 1000); // delay 1 hour
}
}
If the code works, why not be happy with that. Only thing that is generally avoided is delay(), since it halts the execution. millis is good, since the code can continue to execute.
Your code has several issues that prevent it from functioning as expected. I'll go through the key points and provide a corrected version of the code:
Issues in the Original Code:
Semicolon Misuse
Many if statements end with a semicolon (;), which effectively nullifies the condition check.
Incorrect Sensor Logic digitalRead should be assigned to a variable before checking its value.
Light Sensor Logic
You are comparing light_sensor (the pin) instead of the lightValue read from analogRead.
Code Layout
The logic for reading sensors and controlling relays should be more structured to avoid overlapping and incorrect relay control.
Corrected Code:
int relay1_pin = 4; // Power sensor left
int relay2_pin = 7; // Power sensor right
int relay3_pin = 8; // Power water pump left
int relay4_pin = 12; // Power water pump right
int Lsensor_pin = 2; // Digital pin for left sensor
int Rsensor_pin = 13; // Digital pin for right sensor
int waittime = 3; // Wait time in seconds
int sleep = 6; // Sleep time in seconds
int light_sensor = A0; // Analog pin for light sensor
int day_night = 3; // Pin to control light sensor power
int wateringtime = 15; // Watering time in seconds
void setup() {
Serial.begin(9600);
pinMode(relay1_pin, OUTPUT);
pinMode(relay2_pin, OUTPUT);
pinMode(relay3_pin, OUTPUT);
pinMode(relay4_pin, OUTPUT);
pinMode(Lsensor_pin, INPUT);
pinMode(Rsensor_pin, INPUT);
pinMode(light_sensor, INPUT);
pinMode(day_night, OUTPUT);
}
void loop() {
// Turn on light sensor
digitalWrite(day_night, HIGH);
int lightValue = analogRead(light_sensor);
Serial.print("Analog reading (0-1023): ");
Serial.println(lightValue);
if (lightValue > 300) { // Daytime
// Check left sensor
digitalWrite(relay1_pin, HIGH); // Power left sensor
delay(waittime * 1000);
int leftMoisture = digitalRead(Lsensor_pin);
if (leftMoisture == HIGH) { // Dry soil
digitalWrite(relay3_pin, HIGH); // Turn on left water pump
delay(wateringtime * 1000);
digitalWrite(relay3_pin, LOW); // Turn off left water pump
}
digitalWrite(relay1_pin, LOW); // Turn off left sensor
// Check right sensor
digitalWrite(relay2_pin, HIGH); // Power right sensor
delay(waittime * 1000);
int rightMoisture = digitalRead(Rsensor_pin);
if (rightMoisture == HIGH) { // Dry soil
digitalWrite(relay4_pin, HIGH); // Turn on right water pump
delay(wateringtime * 1000);
digitalWrite(relay4_pin, LOW); // Turn off right water pump
}
digitalWrite(relay2_pin, LOW); // Turn off right sensor
} else {
// Nighttime, no watering
Serial.println("Nighttime - No watering");
}
delay(sleep * 1000); // Sleep before the next cycle
}
that does make sense, think I was trying to do to much to start with, then got lost, I am going to try your examples on a small block of code first, so I can understand how it should work, before testing the full code.
This is by far a too short "explanation" how non-blocking timing based on millis() works.
And this too short hints are the source of the each and every week new arising questions and difficulties of beginners to understand non-blocking timing.
You have to emphasize the fundamental difference how nonblocking timing works before going into the code
I respect your point of view.. but I think that artificial intelligence. We are forced to deal with it in the future. And until now it is still in the data collection stage. For me, I have developed several projects within the framework of my work with Arduino. A year ago, this used to take a lot of time, especially since my bosses at work quickly change their plans. But after starting to use artificial intelligence, it became easier for me. .. This does not mean that in some things it is very stupid and the more I ask it, the more stupid it becomes. .. For people who do not have basic knowledge of programming. I do not think they will benefit from it much. ... Currently, I am working on a project to control a machine via a computer and 2. Arduino mega.. such as plc 48 input 48 output.. The progress is nearing the end. .. If this project is completed, it will save a significant amount compared to plc.. and I would not have completed it in record time without the help of artificial intelligence... .. I only spoke from my experience and I respect everyone's opinion
Watching grown children need mommy to tie their shoes because they have only had loop-and-hook (the AI of "laces") is not a point-of-view. It is contempt. If you fix your own interaction with ChatGPT, good, keep it to yourself. Bringing that trash here, where humans try to help humans wastes human time. If you rely on a crutch, you will, forever, be lame, and never write code. Learn to code until you have no need for ChatGPT, then you can use it as a tool, and not a savior.
What was the record? I bet there was a flaw in the code, even after several attempts. And you are going to push a product to market without verifying...
You were right when you said that humans teach humans. And maybe I was wrong at first. But for my projects, I use AI as a tool because it cannot write a complete code to complete a complex project. And I confirm that artificial intelligence so far can only solve a small percentage of problems. Even Devin AI did not exceed solving a small percentage of problems... But you were also wrong when you accused me of creating projects without checking them. I have completed more than 20 projects in the company I work for since 2020. With or without the help of AI. And my bosses would not have given me the opportunity to develop a new project if a single problem occurred.