So I'm trying to create a cleaning robot using Arduino. My goal is to essentially make it run by making it go in like a S motion across the room where once it encounters a wall, it will make a U-turn to its left then once it encounters another wall, a U-turn to its right. Currently this part works if I only use the Ultrasonic Sensor and DC Motor. Since it's hard to chase around whenever I need it to stop, I decided to add a Infrared Receiver so that I can just press a button on an Infrared Remote to make it stop or use it to manually control it instead. But whenever I add the code for the Infrared Receiver, only one of the robot's two wheels end up spinning and the Ultrasonic Sensor seems to stop working as the instructions that were programmed to it do not happen. How might I solve this issue?
#include <IRremote.h>
#include <AFMotor.h>
#include <NewPing.h>
#define TRIGGER_PIN A0 // Arduino Pin connected to Trigger Pin on the Ultrasonic Sensor
#define ECHO_PIN A1 // Arduino Pin connected to Echo Pin on the Ultrasonic Sensor
#define MAX_DISTANCE 200 // Maximum Distance we want to ping for (in centimeters)
#define Button_1 0xFF6897
#define Button_2 0xFFB04F
#define Button_3 0xFFA25D
#define Button_4 0xFF629D
#define Button_5 0xFF18E7
#define Button_6 0xFF10EF
#define Button_7 0xFF4AB5
#define Button_8 0xFF5AA5
#define Button_9 0xFF38C7
#define Button_10 0xFF9867
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance
//DC Motor Back-Left
AF_DCMotor motor1(3);
//DC Motor Back-Right
AF_DCMotor motor2(2);
int distance = 100;
int speedSet = 0;
int countUp = 0;
int sensor = 0;
int receiver = A4;
uint32_t Previous;
IRrecv irrecv(receiver); //create a new instance of receiver
decode_results results;
void setup() {
Serial.begin(115200);
//turn on motors
motor1.setSpeed(235);
motor1.run(RELEASE);
motor2.setSpeed(255);
motor2.run(RELEASE);
irrecv.enableIRIn(); //start the receiver
}
void loop() {
if (distance <= 20) {
taph();
delay(1000);
backward();
delay(750);
taph();
delay(1000);
if (countUp % 2 == 0) {
leftward();
delay(1250);
forward();
countUp++;
Serial.println(countUp);
}
else {
rightward();
delay(1250);
forward();
countUp++;
Serial.println(countUp);
}
}
else {
forward();
}
if (sensor == 1){
distance = readPing();
}
if (irrecv.decode(&results)) { //if we have received an IR signal
if (results.value==0xFFFFFFFF) {
results.value=Previous;
}
switch(results.value) {
case Button_1 : // Press * on Remote to Stop
motor1.setSpeed(0);
motor1.run(RELEASE);
motor2.setSpeed(0);
motor2.run(RELEASE);
break;
case Button_2 : // Press # on Remote to Continue
motor1.setSpeed(235);
motor1.run(RELEASE);
motor2.setSpeed(255);
motor2.run(RELEASE);
break;
case Button_3 : // Press 1 on Remote to Turn Left First
countUp = 0;
break;
case Button_4 : // Press 2 on Remote to Turn Right First
countUp = 1;
break;
case Button_5 : // Press Up on Remote to Move Forward
forward();
break;
case Button_6 : // Press Left on Remote to Move Leftward
leftward();
break;
case Button_7 : // Press Down on Remote to Move Backward
backward();
break;
case Button_8 : // Press Right on Remote to Move Rightward
rightward();
break;
case Button_9 : // Press OK on Remote to Enable Manual Mode
sensor = 0;
break;
case Button_10 : // Press 0 on Remote to Enable Automatic Mode
sensor = 1;
break;
}
Serial.println (results.value, HEX); //display HEX results
irrecv.resume(); //next value
}
Previous=results.value;
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0)
{
cm = 250;
}
return cm;
}
void forward() {
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void backward() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
}
void leftward() {
motor1.run(RELEASE);
motor2.run(FORWARD);
}
void rightward() {
motor1.run(FORWARD);
motor2.run(RELEASE);
}
void taph() {
motor1.run(RELEASE);
motor2.run(RELEASE);
}
Note:
-
That one 9V Battery below the DC Motor on the left is powering the Arduino.
-
Though the connection for the infrared receiver in the diagram is different from that in the code, I made sure that it is connected the same so that it will work.
Can you provide a list of parts WITH LINKS to data sheets?
What batteries are you using?
Arduino Uno - https://docs.arduino.cc/resources/datasheets/A000066-datasheet.pdf
L293D Motor Driver Shield - https://5.imimg.com/data5/PX/UK/MY-1833510/l293d-based-arduino-motor-shield.pdf
Ultrasonic Sensor - https://www.handsontec.com/dataspecs/HC-SR04-Ultrasonic.pdf
DC Motor - https://www.makerlab-electronics.com/product/dc-gear-motor-6v-77rpm-sgm25-370/
Infrared Receiver - https://www.epitran.it/ebayDrive/datasheet/45.pdf
LM2596 DC-DC Buck Converter - https://www.ti.com/product/LM2596
1: Your code is full of delays which wont help, you may wish to look at the "blink without delay" example and change to using millis() for timing.
2: I'd suggest you simplify your code and use srial prints to see what values you are getting; particularly for
results.value
and take out every case except for "Button_1"
Also I'm puzzled. Why would you use 2 weedy PP3 batteries to generate 18V, then drop it to 5V to drive your motors? when the motor shield datasheet says use 6-15V?
It would be better to use 4 -6 AA size NiMH cells to give your motor supply.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.