Hello everyone,
I have recently built a robot that can be controlled using a TV remote. I took a remote, decoded some of it's buttons and programmed the robot to respond to each press of certain buttons. The robot worked perfectly and i was able to control it. Afterwards i even added some Leds and a buzzer.
after I played with it for a while I've decided i want to add an "autonomous" mode, using a ping sensor, that will be activated by the press of a determined button on the remote. In another words, a press of a button (The play button, for example) will make the robot avoid obstacles.
So i added a ping sensor.
Unfortunately, I have a small problem with the code.
#include <pitches.h>
#include <Servo.h>
#include <IRremote.h>
Servo SERVO_1;
Servo SERVO_2;
int RECV_PIN = 11;
int redled = 1;
int blueled = 2;
int greenled = 3;
#define trigPin 8
#define echoPin 9
IRrecv irrecv(RECV_PIN);
decode_results results;
#define UP 807FD02F
#define DOWN 807F50AF
#define RIGHT 807F52AD
#define LEFT 807F10EF
#define ENTER 807FCA35
#define ONE 807FA05F
#define TWO 807FA25D
#define THREE 807F22DD
void setup()
{
Serial.begin(9600);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode (8, OUTPUT);
pinMode (9, INPUT);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
SERVO_1.attach(5);
SERVO_2.attach (6);
SERVO_1.write (93);
SERVO_2.write (93);
irrecv.enableIRIn();
// Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
irrecv.resume();
if (results.value == 0x807FD02F){ // 0xtype your IR remote button value note down in the previous step
SERVO_1.write (180);
SERVO_2.write (0);
}
else if(results.value == 0x807F50AF){
SERVO_1.write (0);
SERVO_2.write (180);
}
else if(results.value == 0x807F52AD){
SERVO_1.write (0);
SERVO_2.write (0);
} else if(results.value == 0x807F10EF){
SERVO_1.write (180);
SERVO_2.write (180);
}else if(results.value == 0x807FCA35){
SERVO_1.write (95);
SERVO_2.write (95);
}
else if(results.value == 0x807FA25D){
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(3, LOW); // green
delay(500);
digitalWrite(3, HIGH);
digitalWrite(2, LOW); // blue
delay(500);
digitalWrite(2, HIGH);
digitalWrite(1, LOW); // red
delay(500);
digitalWrite(3, LOW); // yellow
delay(500);
digitalWrite(1, HIGH);
digitalWrite(2, LOW); // Cyan (Light Blue)
delay(500);
digitalWrite(3, HIGH);
digitalWrite(1, LOW); // Magenta (purple)
delay(500);
digitalWrite(3, LOW); // White
delay(500);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
}
else if(results.value == 0x807FA05F){
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 25) { // This is where the LED On/Off happens
Serial.println ("Close Obstacle detected!" );
Serial.print ( distance);
Serial.print ( " CM!");
SERVO_1.write (0);
SERVO_2.write (0);
}
else {
Serial.println ("No obstacle detected. going forward");
SERVO_1.write (180);
SERVO_2.write (0);
}
}
}
}
The problem in the code is obvious. When the button is pressed, the obstacle avoiding string starts, but in order for it to work correctly, it needs to loop itself. in another words, whenever the string ends it needs to start from the beginning and scan again, but it doesn't do it, because it is a part of a "if-else" condition. My question is, How can I make the obstacle avoidance string loop itself whenever i press that button? does "Switch-case" loop the cases' results ? Is that even possible to make a loop inside the loop ? (I think not, since the arduino doesn't have core functioning).
If anyone has any suggestions about how can i accomplish this task, i would like to hear it.
Thanks,
Roy.
BTW, I have already built obstalce avoiding robots in the past, But i haven't built ones that have an obstacle avoiding "Mode".