Help with code for a project

Hello,
I need a help with code for a personal project I am working on a automatic watering robot.
The robot is made of 2 parts. On is a reused car and the second is a flower pot. I basically finished the all project only trouble I am having is a code part the code most of the time doesn't work. I manage to make it work once and that was only that time it worked. Heres a
little summery on how the project works.

The car will receive a signal on HC-05 from the Pot's HC-05, then the car will start going in circles looking for a IR signal from the IR receiver HX1838 module that's on the front of the car. The pot has 4 IR transmitters (just a IR led with a 100 om resister on anode) so when the car gets a ir signal it will stop and go straight until it stoped by ultrasonic sensor it will water the pot and go back.

Heres the code for the car:

#include <SoftwareSerial.h>
#include <IRremote.h>

#define PUMP 12
#define MOTOR_A_FORWARD 5
#define MOTOR_A_BACK 4
#define MOTOR_B_FORWARD 2  
#define MOTOR_B_BACK 3
#define RX 10
#define TX 11
#define IR_RECEIVER 7  
#define TRIG 8
#define ECHO 6

IRrecv irrecv(IR_RECEIVER);
decode_results results;

SoftwareSerial nodeCommunication(RX, TX);

void setup() {
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);

  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);

  pinMode(PUMP, OUTPUT);

  pinMode(MOTOR_A_FORWARD, OUTPUT);
  pinMode(MOTOR_A_BACK, OUTPUT);
  pinMode(MOTOR_B_FORWARD, OUTPUT);
  pinMode(MOTOR_B_BACK, OUTPUT);

  nodeCommunication.begin(38400);  
  irrecv.enableIRIn();

}

void loop() {
  long duration, distance;
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration / 2) / 29.1; 

  if (nodeCommunication.available() > 0) {
    char signal = nodeCommunication.read();

    if (signal == '1') {  

      while (!irrecv.decode(&results)) {

        digitalWrite(MOTOR_A_FORWARD, HIGH);
        digitalWrite(MOTOR_A_BACK, LOW);
        digitalWrite(MOTOR_B_FORWARD, LOW);
        digitalWrite(MOTOR_B_BACK, HIGH);
        delay(500);  

       
      }
 irrecv.resume();  
     
      digitalWrite(MOTOR_A_FORWARD, LOW);
      digitalWrite(MOTOR_A_BACK, LOW);
      digitalWrite(MOTOR_B_FORWARD, LOW);
      digitalWrite(MOTOR_B_BACK, LOW);
delay(3000);
      digitalWrite(MOTOR_A_FORWARD, LOW);
      digitalWrite(MOTOR_A_BACK, HIGH);
      digitalWrite(MOTOR_B_FORWARD, HIGH);
      digitalWrite(MOTOR_B_BACK, LOW);

      if (distance < 5) {
  
        digitalWrite(MOTOR_A_FORWARD, LOW);
        digitalWrite(MOTOR_A_BACK, LOW);
        digitalWrite(MOTOR_B_FORWARD, LOW);
        digitalWrite(MOTOR_B_BACK, LOW);
  
        digitalWrite(PUMP, HIGH);
        delay(2000); 
        digitalWrite(PUMP, LOW);
  
        digitalWrite(MOTOR_A_FORWARD, LOW);
        digitalWrite(MOTOR_A_BACK, HIGH);
        digitalWrite(MOTOR_B_FORWARD, LOW);
        digitalWrite(MOTOR_B_BACK, HIGH);
        delay(3000);

        digitalWrite(MOTOR_A_FORWARD, LOW);
        digitalWrite(MOTOR_A_BACK, LOW);
        digitalWrite(MOTOR_B_FORWARD, LOW);
        digitalWrite(MOTOR_B_BACK, LOW);

delay(10000);
      }

    }
  }
}

the pot code:

#include <SoftwareSerial.h>

#define RX 10
#define TX 11
#define IR 3
#define Soil A0

SoftwareSerial nodeCommunication(RX, TX);

void setup() {
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
  pinMode(IR, OUTPUT);

  nodeCommunication.begin(38400);  
}

void loop() {
   int soilMoisture = analogRead(Soil);
   if (soilMoisture < 500) {
    nodeCommunication.write('1'); 
    tone(IR, 38000);  
  } else {
    nodeCommunication.write('0'); 
    notone(IR, 38000);
  delay(1000); 
}

When I give power to the car and pot the moment it receives a signal from the HC-05 the motors start spinning but when i bring the IR signa basically in front of HX1838 the HX1838 diode light up but nothing happens (I had it work one time and that was it). I need help pls if you know what the problem is. Is it the code or the wiring i will include a photo of the car and the pot.




put the car on blocks so it will not be moving on the table. Connect the USB back to your PC and begin to add serial.Print() statements to your code until you track down the errors.

1 Like

What is the version of the IRremote.h library do you use?

According to the line above, you used a code examples for the old version of the library.

With a such many delays of 2, 3 and even 10s it would be amazing if the code worked.

I tried that and it just keep repeating the same thing I think the IR is the main problem

I am using the IRremote lib 4.4.1 I know this is an old code but its the only one that works I written and tried many different codes and non of them worked on this code at least the hx1838 led lights up but still doesn't work.
I put up delay because i wasn't sure if the code is looping or not(but whats wrong with delays in code ??)

Look at IR Remotes Revisited - 2023 | DroneBot Workshop and you will understand what might be the problem with your IR signal

Hi @aquilus ,

the delays are influencing the reactivity of your code ...

It would be better (if not absolutely necessary) to change the code to e.g. a finite state machine. That sounds more dramatic than it is. A state machine allows to create the sequences as you want them to be while not slowing down time critical functions.

Good luck!
ec2021

Your code is for version 2.x of the library, it shouldn't work with a v 4.4.1
Read the Converting-your-2x-program-to-the-4x-version section of the library README file

It would be better to start from the library examples

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.