First Project Code Fail - Help Please - Kids RC Car

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 :blush: 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 }

IMG_2599[1].JPG

All those "digitalWrite (10.." or "digitalWrite (12..." are a maintenance nightmare in waiting.

Give the pins name ("const byte brakeRpin = 12;" or whatever) and stick with them.

Thank you for replying, and i will rewrite the code to use different terms, but I dont believe the terms used, are at the heart of my current issue. Correct? That is, your suggested scheme change, won't fix the lack of working logic flow in my code. Do you have any suggestions on what I need to add/change so the distance reading is used by the Arduino to make adjustments to its travel?

It isn't a problem...yet.

You have some serial debug prints - what do they tell you?

JackHBarnes:
while my bot is calculating a range distance, it is not taking action based on that event.

Okay, what does the code say it SHOULD do when it sees something?

void loop()
{
// get a reading- sounds like a fine start

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 
// we have a reading, now what?

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);
  } 

// we did some serial prints, we flashed some LEDs, we did some delays
// unless I'm missing something we didn't actually do anything with the motors

AWOL

The serial messages are as follows... Hammer Down, Slowdown 32, slowdown 12, slowdown 4, As it gets close to an object (my hand getting closer to the sensor)

BIGRED

That perfectly sums up my problem, I don't know what i should do next. I have a pile of working test software, but I am not a coder or even code masher, such that I can figure out the next logical step.

How would I get the 'distance' to be "used" by the Arduino to actually avoid walls. I realize the code is missing its primary logic process. I just don't know code well enough to hack someone elses into place.

Any chance you would help?

I found two sources of code and have mashed them into a semi crashing, semi working version. Definitely need to figure out the actual back up and turn around coding part next. It currently works better in my hands then on the ground. Need to work on the logic of the actions now.

Here is the code with SRO4 sensor, Seeed Motor Controller, Uno
Edit changed code to represent a quick change. Now working 85% of the time. Even backed up in a dead end hallway.

//Albert Code 0.25
//Albert Code 0.25
//Now with working motor and sonic range finding.

int led2 = 2; // Green (quick blink when object noticed)
int led1 = 3; // White blinks at the end of each time the code cycles. 
int echoPin = 5;
int trigPin = 6;
int duration, distance, inches, cm;


const int motorA2 = 8;  // set L298 input 1 as motorA2 on Arduino Pin 8
const int motorA1 = 11; // set L298 input 2 as motorA1 on Arduino Pin 11
const int motorA = 9;   // set L298 output 1 as motorA on Arduino PWM pin 9 (drive motor)
const int motorB2 = 12; // set L298 input 3 as motorB2 on Arduino Pin 12
const int motorB1 = 13; // set L298 input 4 as motorB1 on Arduino Pin 13
const int motorB = 10;  // set L298 output 2 as motorB on Arduino PWM pin 10 (turn motor)

void setup(){
  Serial.begin (115200);//start serial
  // declare LED as output
  pinMode(led1, OUTPUT);      
  pinMode(led2, OUTPUT);
  //sonic
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //motor
  pinMode(motorA1, OUTPUT);  // set motor pins as output
  pinMode(motorA2, OUTPUT);
  pinMode(motorA, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);
  pinMode(motorB, OUTPUT);
}

void loop()
{
  int duration, distance;//defining values
  digitalWrite(trigPin, HIGH);//signal sent to sensor
  delayMicroseconds(2000);//for 2000 microseconds
  digitalWrite(trigPin, LOW);//then switched off
  duration = pulseIn(echoPin, HIGH);//time taken for signal to echo back
  distance = (duration/2) / 29.1;//converting the duriation of echo to a distance in cm
  if (distance >= 400){
    Serial.print(distance);
    Serial.println("Hammer Down");//if no echo is recieved
    digitalWrite(motorA, 200);
    digitalWrite(motorB, LOW);
    digitalWrite(motorA1, LOW);
    digitalWrite(motorA2, HIGH);
  }
  else {
    Serial.print(distance);//print the distance
    Serial.println(" Oh Shit");//in cm to the serial monitor
    analogWrite(motorA, 100);
    digitalWrite(motorB, LOW);
    digitalWrite(motorA1, LOW);
    digitalWrite(motorA2, HIGH);
    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(20); 
  }


  if (distance <=50 ){//If the car is closer than 30cm to an object

    analogWrite(motorA, 50);
    digitalWrite(motorB, LOW);
    digitalWrite(motorA1, LOW);
    digitalWrite(motorA2, HIGH);
    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 (500);//wait .5 seconds

    if(distance <=20 ){//double check there is nothing new in the way
      //stop and back up while turning right for a half second, then stop again
      digitalWrite(motorA, LOW);
      digitalWrite(motorB, LOW);
      delay(1000);
      analogWrite(motorA, 200);
      analogWrite(motorB, 170);
      digitalWrite(motorA1, HIGH);
      digitalWrite(motorA2, LOW);
      digitalWrite(motorB1, HIGH);
      digitalWrite(motorB2, LOW);
      delay(1000);
      digitalWrite(motorA, LOW);
      digitalWrite(motorB, LOW);
      delay(100);

    }
   analogWrite(motorA, 20);
    digitalWrite(motorB, LOW);
    digitalWrite(motorA1, LOW);
    digitalWrite(motorA2, HIGH);

  }
    digitalWrite(motorA, 200);
    digitalWrite(motorB, LOW);
    digitalWrite(motorA1, LOW);
    digitalWrite(motorA2, HIGH);
  
  
  
  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(20);
}

Moderator edit: CODE TAGS AGAIN.

Sorry Mod, I will remember next time. (I hope). First project, first time using this forum, idiot at keyboard.