my first project with arduino uno automatic watering

Hello, this is my very first project, this Prototype of Automatic Sprinkler and Water Level Controller with Arduino UNO, here is my code

int led0 = 17;
int led1 = 18;
int led2 = 2;
const int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int led8 = 8;
int moistureSensor_1 = 0;
int moistureSensor_2 = 1;
#define RELAY_ON 0
#define RELAY_OFF 1
#define buzzer 9
#define echoPin 10
#define trigPin 11
#define Relay 16
#include<Servo.h>
Servo myservo_1;
Servo myservo_2;





void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(Relay, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  digitalWrite(Relay, RELAY_OFF);
  myservo_1.attach(12);
  myservo_2.attach(13);
}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorValue_1 = analogRead(moistureSensor_1);
  Serial.print("sensor 1 = ");
  Serial.println(sensorValue_1);

  int sensorValue_2 = analogRead(moistureSensor_2);
  Serial.print("sensor 2 = ");
  Serial.println(sensorValue_1);
  long duration, distance, tinggi;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);

  distance = (duration / 2) / 29.1;
  tinggi = 24 - distance;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if (ledState == LOW) {
      ledState = HIGH;
    }
    else {
      ledState = LOW;
    }

}

if (distance >= 1 && distance < 7) {
  digitalWrite(led8, HIGH);
  digitalWrite(led7, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led4, HIGH);
  noTone(buzzer);

}

if (distance >= 7 && distance < 9) {
  digitalWrite(led8, LOW);
  digitalWrite(led7, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led4, HIGH);
  noTone(buzzer);
}

if (distance >= 9 && distance < 11) {
  digitalWrite(led8, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led6, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led4, HIGH);
  noTone(buzzer);
}

if (distance >= 11 && distance < 13) {
  digitalWrite(led8, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led5, HIGH);
  digitalWrite(led4, HIGH);
  noTone(buzzer);
}

if (distance >= 13 && distance < 15) {
  digitalWrite(led8, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led4, HIGH);
  noTone(buzzer);
}

if (distance >= 15 && distance <= 25) {
  digitalWrite(led8, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led4, LOW);
  tone(buzzer, 3000, 150);
  digitalWrite(led3, ledState);
}


if (distance > 25 || distance < 1) {
  Serial.println("Jarak Diluar Jangkauan Bosku! xD");
  digitalWrite(led8, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led3, LOW);
  noTone(buzzer);
}


else{
  Serial.print("ketinggian air = ");
  Serial.print(tinggi);
  Serial.println("cm");
}

  digitalWrite(Relay, RELAY_OFF);
}

if (sensorValue_1 < 400 && sensorValue_2 <= 950) {
  digitalWrite(Relay, RELAY_ON);
}

if (sensorValue_1 < 400 || sensorValue_2 > 600) {
  digitalWrite(led0, HIGH);
}

if (sensorValue_1 < 400 && sensorValue_2 >= 600) {
  digitalWrite(led0, LOW);
}
else {
  digitalWrite(led0, HIGH);
}
delay(0);
}
}
}

But a lot of what I think is an error, I tried that code from this source (Prototype Alat Penyiram Tanaman Otomatis dan Pengontrol Level Air den…)
Here it is my error messages

Arduino: 1.8.3 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\cupes\Documents\Arduino\baru\baru.ino: In function 'void loop()':

baru:64: error: 'previousMillis' was not declared in this scope

   if (currentMillis - previousMillis >= interval) {

                       ^

baru:64: error: 'interval' was not declared in this scope

   if (currentMillis - previousMillis >= interval) {

                                         ^

baru:67: error: 'ledState' was not declared in this scope

     if (ledState == LOW) {

         ^

baru:129: error: 'ledState' was not declared in this scope

   digitalWrite(led3, ledState);

                      ^

C:\Users\cupes\Documents\Arduino\baru\baru.ino: At global scope:

baru:154: error: expected unqualified-id before 'if'

 if (sensorValue_1 < 400 && sensorValue_2 <= 950) {

 ^

baru:158: error: expected unqualified-id before 'if'

 if (sensorValue_1 < 400 || sensorValue_2 > 600) {

 ^

baru:162: error: expected unqualified-id before 'if'

 if (sensorValue_1 < 400 && sensorValue_2 >= 600) {

 ^

baru:165: error: expected unqualified-id before 'else'

 else {

 ^

baru:168: error: expected constructor, destructor, or type conversion before '(' token

 delay(0);

      ^

baru:169: error: expected declaration before '}' token

 }

 ^

exit status 1
'previousMillis' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Any help I appreciate, thank you :slight_smile:

You should post your code in code tags rather than quotes.

It looks like there is a mismatch between your opening and closing curly braces. Also, the variable previousMillis doesn't look like it is ever declared. Are you sure you copied the entire code from wherever you got it? You didn't miss out on any lines?

saximus:
You should post your code in code tags rather than quotes.

It looks like there is a mismatch between your opening and closing curly braces. Also, the variable previousMillis doesn’t look like it is ever declared. Are you sure you copied the entire code from wherever you got it? You didn’t miss out on any lines?

yes sir, im pretty sure copied entire code from the source, or the source give wrong code?

I spent a couple of minutes trying to fix it and it turns out there are multiple undeclared variables so yes, there is definitely some missing either from your copying or the source providing you incomplete code.

saximus:
I spent a couple of minutes trying to fix it and it turns out there are multiple undeclared variables so yes, there is definitely some missing either from your copying or the source providing you incomplete code.

whoa? you fix it sir? would you share with me?

Haha no I didn’t “fix” it. I added the lines you forgot to copy from the screenshots and got rid of unnecessary curly braces so it compiles but there are still things I would call errors and no guarantees it will work as expected.

Maybe if this is your very first Arduino project you should start with something a little simpler and from a more reputable source.

int led0 = 17;
int led1 = 18;
int led2 = 2;
const int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int led8 = 8;
int moistureSensor_1 = 0;
int moistureSensor_2 = 1;
#define RELAY_ON 0
#define RELAY_OFF 1
#define buzzer 9
#define echoPin 10
#define trigPin 11
#define Relay 16
#include<Servo.h>
Servo myservo_1;
Servo myservo_2;
int sound;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 20;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(Relay, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  digitalWrite(Relay, RELAY_OFF);
  myservo_1.attach(12);
  myservo_2.attach(13);
}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorValue_1 = analogRead(moistureSensor_1);
  Serial.print("sensor 1 = ");
  Serial.println(sensorValue_1);

  int sensorValue_2 = analogRead(moistureSensor_2);
  Serial.print("sensor 2 = ");
  Serial.println(sensorValue_1);
  long duration, distance, tinggi;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);

  distance = (duration / 2) / 29.1;
  tinggi = 24 - distance;
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if (ledState == LOW) {
      ledState = HIGH;
    }
    else {
      ledState = LOW;
    }

  }

  if (distance >= 1 && distance < 7) {
    digitalWrite(led8, HIGH);
    digitalWrite(led7, HIGH);
    digitalWrite(led6, HIGH);
    digitalWrite(led5, HIGH);
    digitalWrite(led4, HIGH);
    noTone(buzzer);

  }

  if (distance >= 7 && distance < 9) {
    digitalWrite(led8, LOW);
    digitalWrite(led7, HIGH);
    digitalWrite(led6, HIGH);
    digitalWrite(led5, HIGH);
    digitalWrite(led4, HIGH);
    noTone(buzzer);
  }

  if (distance >= 9 && distance < 11) {
    digitalWrite(led8, LOW);
    digitalWrite(led7, LOW);
    digitalWrite(led6, HIGH);
    digitalWrite(led5, HIGH);
    digitalWrite(led4, HIGH);
    noTone(buzzer);
  }

  if (distance >= 11 && distance < 13) {
    digitalWrite(led8, LOW);
    digitalWrite(led7, LOW);
    digitalWrite(led6, LOW);
    digitalWrite(led5, HIGH);
    digitalWrite(led4, HIGH);
    noTone(buzzer);
  }

  if (distance >= 13 && distance < 15) {
    digitalWrite(led8, LOW);
    digitalWrite(led7, LOW);
    digitalWrite(led6, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led4, HIGH);
    noTone(buzzer);
  }

  if (distance >= 15 && distance <= 25) {
    digitalWrite(led8, LOW);
    digitalWrite(led7, LOW);
    digitalWrite(led6, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led4, LOW);
    tone(buzzer, 3000, 150);
    digitalWrite(led3, ledState);
  }


  if (distance > 25 || distance < 1) {
    Serial.println("Jarak Diluar Jangkauan Bosku! xD");
    digitalWrite(led8, LOW);
    digitalWrite(led7, LOW);
    digitalWrite(led6, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led3, LOW);
    noTone(buzzer);
  }


  else {
    Serial.print("ketinggian air = ");
    Serial.print(tinggi);
    Serial.println("cm");

    digitalWrite(Relay, RELAY_OFF);
  }

  if (sensorValue_1 < 400 && sensorValue_2 <= 950) {
    digitalWrite(Relay, RELAY_ON);
  }

  if (sensorValue_1 < 400 || sensorValue_2 > 600) {
    digitalWrite(led0, HIGH);
  }

  if (sensorValue_1 < 400 && sensorValue_2 >= 600) {
    digitalWrite(led0, LOW);
  }
  else {
    digitalWrite(led0, HIGH);
  }
  delay(0);
}

PS I’m not a “sir”. I’m just a guy trying to offer help to strangers on the Internet.

sorry but that is my habbit, can you tell me where is the line i missed it and thank you so much.

Wow, you typed all that out from those slides? What a pain, and then to find out the code sucks after doing all that work!

Well, there's a lot of code there but can I ask you two things, what are you using for a sensor and what are you trying to irrigate?

Adrifran39:
Well, there's a lot of code there but can I ask you two things, what are you using for a sensor and what are you trying to irrigate?

im trying to irrigating water, this is combined autowaterring and water level sensor

firebof:
im trying to irrigating water, this is combined autowaterring and water level sensor

This must be one of the most useful bits of extra information by an OP that I’ve seen here.

Care to tell what sensor you’re using? Preferably provide a link to the data sheet as well.

wvmarle:
This must be one of the most useful bits of extra information by an OP that I've seen here.

Care to tell what sensor you're using? Preferably provide a link to the data sheet as well.

iam newbie here, sir. so i dont know what to do :frowning: iam using soil sensor and ultrasonic sensor

It looks like you missed the pulsein line. You have nothing that sets duration. Take a look at the ping example or perhaps revisit the slides you copied from.

Well, answer the questions, for starters. Tell what you have.
"Ultrasound sensor" and "soil moisture sensor" is useless without giving at the very least type numbers. I hope you realise that there are more sensors out there than the two you happen to have in your collection.
What you're irrigating was another question; not with what. The "water" part is obvious from the "irrigation" part.

wvmarle:
Well, answer the questions, for starters. Tell what you have.
"Ultrasound sensor" and "soil moisture sensor" is useless without giving at the very least type numbers. I hope you realise that there are more sensors out there than the two you happen to have in your collection.
What you're irrigating was another question; not with what. The "water" part is obvious from the "irrigation" part.

wvmarle:
Well, answer the questions, for starters. Tell what you have.
"Ultrasound sensor" and "soil moisture sensor" is useless without giving at the very least type numbers. I hope you realise that there are more sensors out there than the two you happen to have in your collection.
What you're irrigating was another question; not with what. The "water" part is obvious from the "irrigation" part.

sorry sir, i dont know, im too stupid for this project :frowning: