Hi,
My first project is a 2 motor bot made out of a Radio Shack RC car I cut the receiver out of. I have installed an Uno with Seeed Studio Motor Shield, a SR04 Sonic range finder, and two LEDs. The idea is to learn the code on this bot (picture attached below) so I could build a couple of bots for friends and family.
I have pieced together other peoples code, and the motor spins, the wheels will turn, the Range Finder outputs its distance to the serial monitor. LED1 flashes each time the code base finishes running. LED2 flashes when the Distance calculated is less than 50 CM. That's the good news.
While I have been around computers my whole life, I don't code. I started this project so i could learn to code (yes even at my advanced age My issue, and I am sure this forum see's this same problem every month or so, is that while my bot is calculating a range distance, it is not taking action based on that event. I honestly have no idea how to adjust my code to fix this problem.
I originally had more sensors in my code base, but I have now chopped the code down to the bare minimum I need, or at least what I think I need.
Could someone please review this code and help me make my kids smile? I cant start on their bot projects until I figure out how to get my basic autonomous bot code working.
Here is my basic Code, I have hacked it together out of other peoples working examples. All mistakes are mine. My goal is to show the girls, how the car drives safer then they will.
//Albert Code 0.1
//2 Motors (M+1/-1 Forward/Reverse, M2+/- L/R Front), 1 Sonic SR04, , 2 LEDs,
//everything is hooked up and working via tests of each sub module. However, bot currently does not pay attention to 'distance' output
//just drives forward. I need help getting the bot to react to its environment.
//Now to get this to work.
int led1 = 3; // White blinks at the end of each time the code cycles.
int led2 = 2; // Green (quick blink when object noticed)
int echoPin = 5;
int trigPin = 6;
int duration, distance, inches, cm;
void setup() {
pinMode(led1, OUTPUT); // declare LED as output
pinMode(led2, OUTPUT);
pinMode(trigPin, OUTPUT);//Sonic Range Finder Routine
pinMode(echoPin, INPUT);
//establish motor direction toggle pins
pinMode(12, OUTPUT); //drive motor -- HIGH = forwards and LOW = backwards
pinMode(13, OUTPUT); //turn motor -- HIGH = left and LOW = right
//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) the drive motor
pinMode(10, OUTPUT); //brake (disable) the turn motor
//Turns brake off for drive motor
digitalWrite(9, LOW);
//Turns brake on for turn motor
digitalWrite(10, HIGH);
//Sets initial speed of drive motor
analogWrite(3, 200);
//Sets initial direction of drive motor
if (distance < 12){
//brake drive motor and pause 1/10 second
digitalWrite(9, HIGH);
delay(100);
//setting turn motor
//turn off brake for turn motor
digitalWrite(8, LOW);
//set turn motor direction
digitalWrite(13, HIGH);
//activate turn motor
analogWrite(11, 255);
//setting drive motor
//turn off brake of drive motor
digitalWrite(9, LOW);
//set drive motor backwards direction
digitalWrite(12, LOW);
//activate the drive motor
analogWrite(3, 200);
//backup for 2 seconds
delay(2000);
//stopping
//brake both motors
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
}
//when nothing is within 12"
//the robot simply drives forwards
else{
//Setting drive motor
//set drive motor forward direction
digitalWrite(12, HIGH);
//turn off brake of drive motor
digitalWrite(9, LOW);
//activate drive motor
analogWrite(3, 200);
}
delay(100);
digitalWrite(12, HIGH);
Serial.begin(115200);
}
void loop(){
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 50 || distance <= 0){
Serial.println("Hammer Down");
}
else {
Serial.print(distance);
Serial.println(" slowdown");
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(10); // wait for .01 second
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(200);
}
delay(10); // wait for .01 second
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(10); // wait for .01 second
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(200);
}
Moderator edit: { sob } CODE TAGS { sob }