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.



