Learning by doing - in need of guidence

Hi!

I took the arrogant way of trying to learn code but there's only so far "learning by doing" and asking ChatGPT will get you. I'm a real novice when it comes to coding so see me as a "day one coder".

My buddy is all about assembeling stuff and get all the electronics, mechanics, and so on, to work, he hates code though so he asked me if i could learn some. We're trying to build a fairly primitve (both in code and hardware) water distrubution for chiliplants. Now; My question is, as ChatGPT base their answer on "plausibility" (<- Sorry, english is my 2nd language) i'm turning to you guys who actually know facts. Do you think that this code will run flawlessly or am i missing something?

Board; Arduino Uno
Microcontroller: ATmega328P

#include <avr/sleep.h>
#include <avr/power.h>
#include <EEPROM.h>

#define NUM_SENSORS 6                         // Antal sensorer
#define RELAY_PIN 7                           // Reläpinne
#define DRY_THRESHOLD_DEFAULT_3V3 500         // Medelvärde torrtröskel vid 3.3v drift
#define WET_THRESHOLD_DEFAULT_3V3 200         // Medelvärde våttröskel vid 3.3v drift
#define DRY_THRESHOLD_DEFAULT_5V 800          // Medelvärde torrtröskel vid 5v drift
#define WET_THRESHOLD_DEFAULT_5V 400          // Medelvärde våttröskel vid 5v drift
#define MAX_WATER_DURATION 10000              // Maxtid för bevattning
#define CHECK_INTERVAL 60                     // Systemet kontrolleras var 60:e sekund..fundera på om denna ska vara kvar..

const int soilSensorPins [NUM_SENSORS] = {A0, A1, A2, A3, A4, A5};
float supplyVoltage;
bool isWatering = false;
unsigned long wateringStartTime = 0; // Tidpunkt när bevattning startade

int dryThreshold; // Lagras i EEPROM
int wetThreshold; // Lagras i EEPROM

// Deklarationer
void startWatering();
void stopWatering();
void selfCalibrate();
void saveCalibration(int dryValue, int wetValue);
void loadCalibration();
float readSupplyVoltage();

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  Serial.begin(9600);
  Serial.println("Systemet startar...");

  // Identifiera driftspänning och ställ in tröskelvärden
  supplyVoltage = readSupplyVoltage();
  Serial.print("Driftspänning: ");
  Serial.println(supplyVoltage);

  loadCalibration(); // Ladda tidigare kalibreringsvärden från EEPROM
}

void loop() {
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= (CHECK_INTERVAL * 1000)) {
    previousMillis = currentMillis; // Uppdaterar tid
    bool shouldWater = false;

    // Kontrollera varje sensor
    for (int i = 0; i < NUM_SENSORS; i++){
      int soilMoistureValue = analogRead(soilSensorPins[i]);
      Serial.print("Sensor ");
      Serial.print(i + 1);
      Serial.print(" Jordfuktighet: ");
      Serial.println(soilMoistureValue);

      if (soilMoistureValue < dryThreshold) {
        shouldWater = true;
      }
    }

    // Starta eller stoppa bevattning
    if(shouldWater && !isWatering) {
      startWatering();
    } else if (!shouldWater && isWatering){
      stopWatering();
    }
  }
}

// Funktion för att starta bevattning
void startWatering(){
  digitalWrite(RELAY_PIN, HIGH);
  isWatering = true;
  wateringStartTime = millis();
  Serial.println("Bevattning startad.");
}

// Funktion för att stoppa bevattning
void stopWatering(){
  digitalWrite(RELAY_PIN, LOW);
  isWatering = false;
  Serial.println("Bevattning stoppad.");
}

// Funktion för självkalibrering
void selfCalibrate() {
  Serial.println("Självkalibrering startar...");
  int totalDry = 0, totalWet = 0;

  for (int i = 0; i < NUM_SENSORS; i++) {
    int dryValue = analogRead(soilSensorPins[i]);
    delay(5000);
    int wetValue = analogRead(soilSensorPins[i]);

    Serial.print("Sensor ");
    Serial.print(i + 1);
    Serial.print (" Torrvärde: ");
    Serial.println(dryValue);

    Serial.print("Sensor ");
    Serial.print(i + 1);
    Serial.print(" Våtvärde: ");
    Serial.println(wetValue);

    totalDry += dryValue;
    totalWet += wetValue;
  }

  dryThreshold = totalDry / NUM_SENSORS;
  wetThreshold = totalWet / NUM_SENSORS;
  saveCalibration(dryThreshold, wetThreshold);
}

// Funktion för att spara kalibreringsvärden till EEPROM
void saveCalibration(int dryValue, int wetValue) {
  EEPROM.put(0, dryValue);
  EEPROM.put(sizeof(int), wetValue);
  Serial.println("Kalibreringsvärden sparade till EEPROM.");
}

// Funktion för att ladda kalibreringsvärden från EEPROM
void loadCalibration() {
  EEPROM.get(0, dryThreshold);
  EEPROM.get(sizeof(int), wetThreshold);
  Serial.print("Laddade torrtröskel: ");
  Serial.println(dryThreshold);
  Serial.print("Laddade våttröskel: ");
  Serial.println(wetThreshold);
}

// Funktion för att läsa driftspänning (dummyvärde här)
float readSupplyVoltage(){
  return 5.0;
}

The easiest thing to do is run it and find out

Thanks bud! I'll of course run it and see how it goes. We're going to do this tonight. YOu got any advice on how to troubleshoot what string may be at fault if task fails successfully?

All I know so far is that you are trying to water chilipepers.
I need to see a schematic and datasheets for all the components you are using and a description of what the program is actually supposed to do.

There are many reasons why it may fail, some may be hardware related

2 Likes

Awesome, i'll return with schematics, datasheets and a description of what the program is supposed to do asap.

Check it by yourself using the hardware as specified in the sketch.

Did ChatGPT has delivered a connection and wiring diagram too?

1 Like

I haven't asked this question to ChatGPT, so i'll defenitly do this. Thanks bud!
We've just assumed that it'll be wired as the component manufacturers schematic instructs.

Is this what you need to help me with my question?

Specifications:
Capacitive Soil Moisture Sensor:
Operating voltage: 3.3 ~ 5.5 VDC
Output voltage: 0 ~ 3.0 VDC
Interface: PH2.54-3P
Size: 98 x 2m (LxW)
Pin: Analog signal output, GND, VCC
1 Channel 5V Relay Module:
VCC: Connect 5V Positive Pole Power Supply
GND: Connect 5V Negative Pole Power Supply
Mini Water Pump:
DC Voltage: 3-5 V

What the program is supposed to do (in theory):

  • Be able to work with either on 3.3v or 5v supply voltage
  • Identify what voltage is supplied
  • Use thresholds for water supply (individually per soil moisture sensor) based on voltage supplied
  • Selfcalibrate (first read is: dry = read air, wet = read a glass of water)
  • Store historic threshold readings in EEPROM
  • Selfcalibrate sensors based on readings and historic threshold readings
  • Start or stop watering based on thresholds
  • Supply water at a maximum of x seconds (in this code it's 10000 millis)
  • System checks every 60 seconds.

The Uno has no power.
Image is too blury I cant see which pin are connected on the Uno

Output voltage: 0 ~ 3.0 VDC

What is this refering to?

Pin: Analog signal output, GND, VCC

What does this mean? The Uno cannot output an analog voltage.

These lines are not needed, you can delete them. Then move setup() and loop() to the bottom of the code.

1 Like

Have you already purchased the kit? If not I would obtain one sensor, one relay and experiment with ChatGPT’s code, appropriately edited.

Your code will not attempt to water unless the moisture value is less than dry air? That may never be true. Certainty not until long after your chillis are dead!

Yes it's already been bought. i've made som changes to the code now, i realized i haven't shown ChatGPT the connection and wiring diagram so now some errors may have been spared :slight_smile:

Uno will not run at 3.3V

Thanks bud! What i'm reading here is that i should not do the first reading in dry air, i should put the sensor in dry soil to get the right dry reading?

Maybe you should get the hardware correct before you go any further.
Burning out your Uno is "learning by doing" but the hard way.

Why is that important? What decision will be made which depends on the voltage supplied?

The code is not identifying the supply voltage. But I'm not sure that is even necessary.

You should put it in soil that is just at the level of dryness where you would want to water it. Not completely dry soil, because the moisture level would never be lower than that.

The code was written by an AI. They, like yourself, are also "day 1 coders", but unlike human day 1 coders, the AI will give the impression that it is 100% confident and will never say it is unsure or doesn't know.

However, I can be 100% confident that this code will not work flawlessly!

What you are missing is scepticism. It's important not to assume the AI is an infallible expert, even if it appears to act like one. Treat the AI like a random guy off the street.

2 Likes

Yes, this is why i turned to the forums.

1 Like