Hi,
I made a little 4wd robotic platform using Arduino for the brains who controls the 4 dc motors (Lego PF).
From trial and error I managed and learned to write a sketch what controls the movements of the bot.
It goes neetly back and forward, makes a turn etc. The next step I took was to add an IR Obstacle Avoiding Sensor to it.
After some experiments, I got the sensor working fine and mounted it on the bot. The next thing I want is that the bot
can roam my room freely without hitting obstacles. I started a fresh new sketch, but i can't get wrap my mind about what
would be the next step, this is my sketch:
//Loading libraries and stating pins
#include <legopowerfunctions.h> // Lego PF Library
LEGOPowerFunctions lego(13); // Pin for Lego PF IR Output
//Set up obstacle avoiding sensor
int sensorPinEN = 7; // Common pin for module
int sensorPinLO = 6; // Left Object detected output pin
int sensorPinRO = 5; // Right Object detected output pin
int ledPin = 13; // select the pin for the infrared control LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// Initialize Arduino Serial which will be used to communicate
Serial.begin(9600);
// Set the pins of the sensor correctly
pinMode(sensorPinEN, OUTPUT);
pinMode(sensorPinLO, INPUT);
pinMode(sensorPinRO, INPUT);
// ready the sensor
digitalWrite(sensorPinEN, HIGH);
}
void loop() {
lego.ComboPWM(PWM_REV4, PWM_FWD4, CH4); // 50% speed forward
if (digitalRead(sensorPinRO) == HIGH) // obstacle on the right side
lego.ComboPWM(PWM_FLT, PWM_FLT, CH4); // complete stop
delay(100);
lego.ComboMode(RED_FWD, BLUE_FWD, CH4); // turn left
delay(10);
if (digitalRead(sensorPinRO) == LOW) lego.ComboPWM(PWM_REV4, PWM_FWD4, CH4); // continue forward
}
I tried different commands, but i keep stuck in how to proceed.
I also did try to create a sketch this way:
//Loading libraries and stating pins
#include <legopowerfunctions.h> // Lego PF Library
LEGOPowerFunctions lego(13); // Pin for Lego PF IR Output
//Set up obstacle avoiding sensor
int sensorPinEN = 7; // Common pin for module
int sensorPinLO = 6; // Left Object detected output pin
int sensorPinRO = 5; // Right Object detected output pin
int ledPin = 13; // select the pin for the infrared control LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// Initialize Arduino Serial which will be used to communicate
Serial.begin(9600);
// Set the pins correctly
pinMode(sensorPinEN, OUTPUT);
pinMode(sensorPinLO, INPUT);
pinMode(sensorPinRO, INPUT);
// ready the sensor
digitalWrite(sensorPinEN, HIGH);
}
void loop() {
if (digitalRead(sensorPinLO && sensorPinRO == LOW))
lego.ComboMode(PWM_FWD2, PWM_FWD4, CH4);
if (digitalRead(sensorPinLO == HIGH)) lego.ComboMode(RED_FWD, BLUE_FWD,CH4);
}
The code above only makes one side run normal, the other set of wheels operate in short pulses.
So my question is, what is the right way to go? The first or second scetch? Something completely different?
Do i need to ad something like a time out function?
The kind of behaviour i'm looking for is: Robot drives slow around, detects obstacle sensor is HIGH, stops, turns wheels until sensor is LOW again, continue forward.
I would like to have some help to point me and my robot in the right direction
M