Coding for an Autonomous garden navigating robot using

So after coding for Arduino for a while I decided to make a robot for our garden to spray water on them whilst considering the amount they need as well.

Here is the pseudo code / program flow:

wait for a character ' ' // to be sent by the computer wirelessly

case A: 
Run forward() until the collision sensor detects something and the robot will stop
then Arduino will send Serial.println('C') to my computer

//My PC will receive the 'C' sent by Arduino
//and send Another character to my arduino i.e. 'E' 

Arduino will receive the 'E' 
and will then operate the water pump in some number of seconds.
After the water pumping, the robot should run again forwards, detect with collision sensor and stop again for x number of times.(depending on the number of plants in the row)

The problem with this way is that after the collision sensor detects something and stop there, the next time it will run the forward-detect again it will detect the same pot again because it was not able to move forward a bit before it detects again. :frowning:

here is what I have right now:

int serialData = 0;
int proxpin =6;

void setup()
{
  Serial.begin(9600);
  pinMode(2,OUTPUT); //for pump
  pinMode(3,OUTPUT); //for pump
  pinMode(4,OUTPUT); //for pump
  pinMode(5,OUTPUT); //for pump
  pinMode(8,OUTPUT); //Motor 
  pinMode(9,OUTPUT); //Motor
  pinMode(10,OUTPUT); //Motor
  pinMode(11,OUTPUT); //Motor
  pinMode(proxpin,INPUT_PULLUP); //proximity collision sensor
}

void loop()
{
  while (digitalRead(proxpin) == HIGH) {

    if (Serial.available() > 0)
    {
      // Serial.println("listening"); 
      serialData = Serial.read();

      switch(serialData)
      {
      case 'A':
        //Serial.println("move forward"); 
        forward();
        break; 

      case 'B': 
        //Serial.println("stop moving");
        stopna();
        break;

      case 'E':
        pump1sec();
        pause(2);
        forward();
        break;

      }
    }
  }

   Serial.println("C");
  stopna();
}



void forward()
{
  // Serial.println("running forward");
  digitalWrite(8,LOW);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(11,LOW);

}

void stopna()
{
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
}

void pump1()
{
  digitalWrite(2,HIGH);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,HIGH);
  delay(1000);
}

My Code works up to the point where the robot stops when it detects something and Send 'C' to my Computer. And when My PC send 'E' to my computer the pumps would work and then stop there.

the next time it will run the forward-detect again it will detect the same pot again because it was not able to move forward a bit before it detects again.

Why, when the robot has done nothing about the pot, would you scan again? How do YOU avoid obstacles in the dark? Your stumbling along, and kick a chair leg. Do you keep kicking the chair leg? Or are you smart enough to try turning first?

here is what I have right now:

Why did you post that crappy looking code without using Tools + Auto Format first? Pay attention to proper indenting as you go, and your code won't look like that in the first place.

Why, when the robot has done nothing about the pot, would you scan again? How do YOU avoid obstacles in the dark? Your stumbling along, and kick a chair leg. Do you keep kicking the chair leg? Or are you smart enough to try turning first?

What I'm actually thinking is to move forward for like 1 second after the pump action and run forward again and this time trying to detect with the collision sensor. I'm just not sure how to put it into coding as the robot only needs to do this for 14 times only. :slight_smile:

What I'm actually thinking is to move forward for like 1 second after the pump action and run forward again and this time trying to detect with the collision sensor.

It will detect the obstruction immediately because that is why it stopped in the first place. After watering it needs to turn before moving forward again. Depending on the type of sensor used it may need to back off before turning.

How will the robot identify the places to stop and water as opposed to say a fence or a wall ?

Sorry I forgot to mention that the robot is facing sideways.
like this. the 'O' are pots the robot is the '>>' and the proximity sensor is facing the plants.

O O O O O O O O

----> direction of the robot

The development now is that to make the robot run forward for 3 sec and wait for the pumping command and move forward again after.

The designated stops(pots) are positioned such that the robot will stop there after 3 seconds of moving forward now. The only use of the proximity sensor now is to make sure that there is a plant after moving forward. :)

I forgot to mention that the robot is facing sideways.

That makes more sense.

Why detect a pot after moving and stopping ? What will you do if there is not one there ? Better to move until you detect a post, water it and move on I would have thought. If you have X pots then do this X times with maybe a failsafe that if a pot is not detected within 10 seconds then the robot powers down and stays where it is because it is lost.

Is the robot going to move back to the start after watering or do it in reverse next time ? Either way, how will you keep it straight ?

How is the robot powered and does it carry its own water supply or is it fed from a hose which could drag on it and pull it off course. Can you paint a line on the ground for the robot to follow ? Maybe add markers for each pot position and detect them instead of the pots.

Why detect a pot after moving and stopping ?

Now that I've thought about it.

Might as well do it like this:
--PC will send 'A' (This acts as a switch to make the robot start with the routine)
-receive 'A'
-start moving forward for 3 seconds. (assumed to be straight) and stop then send 'C' to my PC
--My PC will Automatically send 'R' to Arduino
-Robot will pump the water ( the pump and the water container is attached to my Robot)

  • move forward again for 3 seconds. stop again and send C. (do this 9 more times depending on the number of plants)

Now after the last pot it will move forward again and if the collision sensor did not sense any pot for like 6 seconds and there is no character sent by my PC, it will stop.

How is the robot powered and does it carry its own water supply or is it fed from a hose which could drag on it and pull it off course. Can you paint a line on the ground for the robot to follow ? Maybe add markers for each pot position and detect them instead of the pots.

the robot is powered by a 12 volts 7.2Ah rechargeable battery and the water is carried by a car washer pump. :slight_smile: So Generally, its autonomous and wirelessly operated.

Sorry but i cant put any line on the floor that's why I removed the thought of using a line sensor as a marker and for moving.

Why involve the PC at all once the watering cycle has started ?
Come to that, why involve the PC at all ?

Why involve the PC at all once the watering cycle has started ?
Come to that, why involve the PC at all ?

Because I created a program with Matlab that tells arduino how much water will be watered to a certain plant by controlling the time that the pump will be on. (not all plants need the same amount of water and some doesn't even need to everyday)
This way plants will be watered accordingly depending on what I'll input to my program.

So in the arduino code I have cases with different characters thrown at the arduino

case 'M'
//one second pump only
break;

case 'N'
//2 second pump only
break;

case 'O'
//skip and just move to the next
break;

Furthermore, I'm planning to add Plant Recognition Image Processing in the future which will require the use of a PC. So this is actually a step towards that. :slight_smile: