Garduino Problems, Help for a first time user

Hi All,
I am trying to build a garduino system from online instructions in order for my plants to be able to water themselves and for me to learn about arduinos. I have all the kit and its ready to go. The only problem is the code. Its taken from a code that involves light water and heat control but i have slightly modified it to omit light and temperture element. the code i am using is;

//define analog inputs to which we have connected our sensors
int moistureSensor = 0;

//define digital outputs to which we have connecte our relays (water)
int waterPump = 7;

//define variables to store moisture
int moisture_val;

//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

void setup() {
//open serial port
Serial.begin(9600);
//set the water, light, and temperature pins as outputs that are turned off
pinMode (waterPump, OUTPUT);
digitalWrite (waterPump, LOW);

}

void loop() {
// read the value from the moisture-sensing probes, print it to screen, and wait a second
moisture_val = analogRead(moistureSensor);
Serial.print("moisture sensor reads ");
Serial.println( moisture_val );
delay(1000);

}

//turn water on when soil is dry, and delay until soil is wet (line 41)
if (moisture_val < 850)

{
digitalWrite(waterPump, HIGH);
}

while (moisture_val < 850)
{
delay(10000);
}

digitalWrite(waterPump, LOW);

However when i run it these error messages are returned;

Guardinho_ReMix:42: error: expected unqualified-id before ‘if’
Guardinho_ReMix:48: error: expected unqualified-id before ‘while’
Guardinho_ReMix:53: error: expected constructor, destructor, or type conversion before ‘(’ token

I would apreciate any help that can be given. I have tried to research the problem found that these are compiler errors. Is this correct and what can i do to fix this and save my flowers from certain death!!!

Many Thanks

If you post code please use the # button to wrap [ code ] and [ /code ] tags around it.

The closing ‘}’ of loop() is moved to the end instead of somewhere in the middle, now it compiles. - don’t know if the semantics are OK.

//define analog inputs to which we have connected our sensors
int moistureSensor = 0;

//define digital outputs to which we have connecte our relays (water)
int waterPump = 7;

//define variables to store moisture
int moisture_val;

//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

void setup() {
  //open serial port
  Serial.begin(9600);
  //set the water, light, and temperature pins as outputs that are turned off
  pinMode (waterPump, OUTPUT);
  digitalWrite (waterPump, LOW);
}

void loop() {
  // read the value from the moisture-sensing probes, print it to screen, and wait a second
  moisture_val = analogRead(moistureSensor);
  Serial.print("moisture sensor reads ");
  Serial.println( moisture_val );
  delay(1000);

  //turn water on when soil is dry, and delay until soil is wet (line 41)
  if (moisture_val < 850)
  {
    digitalWrite(waterPump, HIGH);
  }

  while (moisture_val < 850)
  {
    delay(10000);
  }

  digitalWrite(waterPump, LOW);
}

thanks for the help, all is well now and i appologise for my poor etiquette. I'll have daffodils by spring after all

You're welcome,

I'll have daffodils by spring after all

Post us a picture in this thread by then :)

Hi there, can somebody help me to combine different coding. My project is about to mesure soil moisture sensor. I used Vegetronix VG400 soil moisture probes to detect the moisture and connect it to Arduino Uno board. The coding for this are as below:

void setup() { // Setup serial Serial.begin(9600); }void loop() { Serial.println(analogRead(0));

delay(200); // wait 200 milliseconds }

The output for this sensor is in digital. I want to make this output as digital input to blinking the LED. Red LED for dry, Green LED for wet, and Yellow LED in between. I've found the suitable coding but it has to be change a bit.

int ledPin = 13; // output pin for the LED int inPin = 2; // input pin (for a switch) void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inPin, INPUT); // declare switch as input } void loop() { if (digitalRead(inPin) == HIGH) // check if input is HIGH { digitalWrite(ledPin, HIGH); // turns the LED on delay(1000); // pause for 1 second digitalWrite(ledPin, LOW); // turns the LED off delay(1000); // pause for 1 second } }

As addition, I want to make the water pump so that when the soil is dry, the water pump can water the plant automatically.

I hope somebody can help me because I'm very weak with the coding.

Thank You Raihan

If you post code please use the # button to wrap [ code ] and [ /code ] tags around it. makes code more readable.

from http://vege.securesites.net/Products/VG400/

The VG400 which has an output range of 0 to 3V … related proportionally to water content. Nice sensor.


try this code (not compiled or tested). I have merged serial port and some leds and a switch into it. shaken not stirred. Homework for you is to add the appropiate comments, and extend the application.

#define LEDPIN 13  // output pin for the LED
#define GREEN  4
#define YELLOW 5
#define RED    6
#define SWITCH 2   // input pin (for a switch)
#define VG400  0

void setup()
{
  Serial.begin(9600);
  Serial.println("Start Soil Monitoring");
  
  pinMode(LEDPIN, OUTPUT); 
  pinMode(GREEN, OUTPUT); 
  pinMode(YELLOW, OUTPUT); 
  pinMode(RED, OUTPUT); 

  pinMode(SWITCH, INPUT); 
}

void loop()
{
  int raw = analogRead(VG400);
  float voltage = raw * 5.0 / 1024.0;      
  
  if (digitalRead(SWITCH) == HIGH) 
  {
    Serial.print("val: "); 
    Serial.println(raw);
    if (voltage < 1.2) digitalWrite(RED, HIGH);
    if (voltage > 0.6 && voltage < 2.4) digitalWrite(YELLOW, HIGH);
    if (voltage > 1.8) digitalWrite(GREEN, HIGH);
  
    sleep(2000);
      
    digitalWrite(GREEN, LOW);
    digitalWrite(YELLOW, LOW);
    digitalWrite(RED, LOW);
  }

  digitalWrite(LEDPIN, HIGH);  
  delay(raw);  
  digitalWrite(LEDPIN, LOW); 
  delay(raw);  
}

On the website mentioned above there are some diagrams in which the voltage is related to the moisture% something - that could be added to the app. Furthermore a warning system could be added with a buzzer when ground is too dry. Or add a real time clock RTC and a temp sensor and a light sensor to see how fast the ground is drying under what conditions.

// I like gardening “robots” ever since I saw the movie http://en.wikipedia.org/wiki/Silent_Running :slight_smile:

Hi robtillaart,

Thank You so much for your help. I will try to run the code that you had given to me and hope it will run well. Actually I already got the value for the Vegetronix VG400. The value is between 0-614 which is represent 0-3V. Here is my result:

wet soil: 400-600 in between: 200-400 dry soil: 0-200

If I wanna make the water pump run automatically if the sensor detect the soil is dry, can I use the previous code you had given to the j_me44?? Can I just add the code into my code?

Thank you so much for your help. I really appreciate it and nice to know you ;)

Thank You

wet soil: 400-600 in between: 200-400 dry soil: 0-200

Instead of calculating the voltage you could use the raw values as you propose.

You can add / merge both codes, you only need to read the moisture once. with this value you do the signalling and then you check if you need to start the waterpump. You can do that, just give is a try.

Note: take care when debugging water and electronics don't mix well :)

I’m sorry if I ask so many question. I didn’t get to construct the circuit. I don’t know where to connect this term:

#define LEDPIN 13
#define SWITCH
#define VG400.

I try to change the code you had given to me. But I didn’t try it because of the previous problem. Can you check for me whether the code is correct or not.

#define LEDPIN 13 // output pin for the LED
#define GREEN 4
#define YELLOW 5
#define RED 6
#define SWITCH 2 // input pin (for a switch)
#define VG400 0

void setup()
{
Serial.begin(9600);
Serial.println(“Start Soil Monitoring”);

pinMode(LEDPIN, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);

pinMode(SWITCH, INPUT);
}

void loop()
{
int raw = analogRead(VG400);

if (digitalRead(SWITCH) == HIGH)
{
Serial.println(raw);

if (raw < 200) digitalWrite(RED, HIGH);
if (raw > 200 && raw < 400) digitalWrite(YELLOW, HIGH);
if (raw > 400) digitalWrite(GREEN, HIGH);

sleep(2000);

digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}

digitalWrite(LEDPIN, HIGH);
delay(raw);
digitalWrite(LEDPIN, LOW);
delay(raw);
}

I hope you can help me to solve my problem.

Thank You

robtillaart,

I’d already get the right coding to blinking my LED and it works perfectly. I modified from what you had given to me. But the problem now is to make a water pump. I’d already buy 12V water pump together with 12V relay. I don’t know how to connect it. Can u give me a guidance in order to make it working perfectly. I hope you can help me.

Thank You

Probably not your relay but this is essential how it is done: - http://www.dfrobot.com/wiki/index.php?title=Relay_Module_%28Arduino_Compatible%29_%28SKU:_DFR0017%29

INstead of pinnumber 3 you could use another free one.

Do I need 12V battery to supply voltage to the water pump because my LED run on 5V?

Definitely, you have a 5V output of the Arduino drive the relay, and the relay switches on the pump.

I don't have any idea on how to do it. Can somebody guide my on how to do this step by step :(

I don’t have any idea on how to do it.

I gave you information for the next step, May I ask you how old you are and what is your technical knowledge? Just to understand at what level I should explain it.

Can somebody guide my on how to do this step by step

What is this problem, the hardware or the software? Please be more precise in what you ask

Have you tried the connection of the relay as shown before ? Including running the program at the bottom of the page.

What are the results?
Did it work?
Try changing the condition when the relay activates,

What helps is to write your algorithm down in plain english sentences:

  1. initialize things

  2. determine the moister value

  3. fire the right leds

  4. if too dry start watering for X seconds

  5. wait a minute or so

  6. start over again at 2

I'm 22 years old and currently doing my final year project. I'm studying in electrical and electronic engineering. Honestly, I never do any project alone before this and I've no experience to make water pump. Because of that I really need some guidance.

I already do until the 3rd part which is fire the LEDs and want to proceed with the next step which is: 4) if the soil is too dry start watering for 5 seconds 5) start over again (determine the moisture value)

I hope you can give me the guidance from the 1st step to connect the relay and the water pump.

Thank You

final year project. I’m studying in electrical and electronic engineering.

If you are in your final year you should know how to connect a relay. Maybe you haven’t done this before but that’s no excuse, your an adult so go for it. Fail often to learn fast!

I hope you can give me the guidance from the 1st step to connect the relay and the water pump.

How to connect a relay (I don’t know your relay-type, you do) is in the URL mentioned earlier. Run the little sketch on that page and read the code out loud to understand it. Write down in english statements what it does and compare it with your need.

The second step is to get the relay incorporated in your sketch and that it toggles when the ground is dry. (aka dry simulation) Only if that works it is time to get the pump connected.

So start to rebuild the relay example mentioned earlier or check this one if that makes more sense for you -

When you are familiar with the relay we go to the next step.

Found a nice reader about how to drive a relay - http://www.jaycar.com.au/images_uploaded/relaydrv.pdf

I already done the relay part. But the I don't know why my relay didn't turn on when I connect it to arduino board.

http://www.facebook.com/photo.php?fbid=185927691417320&set=a.167913279885428.43623.100000002707656

here is the link for my connection for relay. I hope if there some error, feel free to inform me.

Thank You

I cannot see the image as I don't facebook ...

What voltage does the relay need? Does it work when connected directly to power supply?