Need a little help with a bot.

Right here goes,

I have been playing with arduino for a little while now gathering parts and bits of knowlege from everyone on the forum, Thanks to all that post and to you coders who can make sense of the mess that us noobs spew out.

On to the real point…

I am using,
DFRduino mega 1280
DFRduino motor drive
Unknown brand Ultrasonic Sensor HC-SRo4
Micro servo
and i am using Tracks and chassis from an ebay £10 transit damaged RC Tank

I have had everything working, Tracks, Mega and Motor drive with buttons, pots and on “auto” with bump sensors no problems.
I have also had the Ultrasonic Sensor working with the LCD to output the distance in CM.

Heres my first post of code:

include “Ultrasonic.h”

#include <Servo.h>

Ultrasonic ultrasonic (8,9);
Servo scanservo;

const int RTMPin = 5 ;
const int RTRPin = 4 ;
const int LTMPin = 6 ;
const int LTRPin = 7 ;

const int ledPin = 13;

void setup() {

pinMode (RTMPin, OUTPUT);
pinMode (RTRPin, OUTPUT);
pinMode (LTMPin, OUTPUT);
pinMode (LTRPin, OUTPUT);

pinMode (ledPin, OUTPUT);

scanservo.attach(9);
analogWrite(RTMPin, 50);
analogWrite(LTMPin, 50);

}

void loop()
{

scanservo.write(120);
delay(500);

if (ultrasonic.Ranging(CM)<15){
digitalWrite(RTRPin, HIGH);
delay(500);
digitalWrite(RTRPin, LOW);
}
delay(500);
scanservo.write(90);
delay(500);
scanservo.write(60);
delay(500);

if (ultrasonic.Ranging(CM)<15){
digitalWrite(LTRPin, HIGH);
delay(500);
digitalWrite(LTRPin, LOW);
}

delay(500);
scanservo.write(90);
delay(500);
}


I have tried the analogWrite in the loop tried it digital made the delays longer, I had it previously switching Pin 13 when i was testing the Servo and Sensor, What do you think??

What is the problem you want help with?

I was too busy trying to get all the details i and forgot to add the problem, It was that the motors were not reversing.

My bad had the pins switched from test program.

Here is it fixed, I also added reverse if it finds something too close when it centers

Here it is:

#include “Ultrasonic.h”

#include <Servo.h>

Ultrasonic ultrasonic (11,12);
Servo scanservo;

const int RTMPin = 5 ;
const int RTRPin = 4 ;
const int LTMPin = 6 ;
const int LTRPin = 7 ;

const int ledPin = 13;

void setup() {

pinMode (RTMPin, OUTPUT);
pinMode (RTRPin, OUTPUT);
pinMode (LTMPin, OUTPUT);
pinMode (LTRPin, OUTPUT);

pinMode (ledPin, OUTPUT);

scanservo.attach(9);

analogWrite(RTMPin, 100);
analogWrite(LTMPin, 100);
}

void loop()
{

scanservo.write(120);
delay(500);

if (ultrasonic.Ranging(CM)<30){
digitalWrite(RTRPin, HIGH);
delay(1500);
digitalWrite(RTRPin, LOW);
delay(50);
}
delay(500);
scanservo.write(90);
delay(500);

if (ultrasonic.Ranging(CM)<20){
digitalWrite(LTRPin, HIGH);
digitalWrite(RTRPin, HIGH);
delay(3000);
digitalWrite(LTRPin, LOW);
digitalWrite(RTRPin, HIGH);
delay(50);
}

scanservo.write(60);
delay(500);

if (ultrasonic.Ranging(CM)<30){
digitalWrite(LTRPin, HIGH);
delay(1500);
digitalWrite(LTRPin, LOW);
delay(50);
}

delay(500);
scanservo.write(90);
delay(500);
}

Let me know what you think and how i can refine the code.

Well, you could add some comments. Another thing you could do is wrap the void loop in code like the Blink Without Delay program so you could monitor all the sensors quicker without being stuck 3 seconds at a time just sitting there.

unsigned long currentMillis = 0;
unsigned long previousMilllis = 0;
unsigned interval = 50; // check every 50mS

void loop (){
currentMillis  = millis();
if ( (currentMilllis-previousMillis) >= interval){
// check this stuff every 50 milliseconds, 
// or every 1500 ms (30 intervals)
// or every 3000 ms (60 intervals)
50mS_count = 1;
1500mS_count = 1500mS_count +1;
3000mS_count = 3000mS_count +1;
}

if (50mS_count == 1){

// code that does stuff

// code that does other stuff

// done with the code

50mS_count = 0; // reset for next pass
if (1500mS_count == 30) {1500mS_count = 0;}  // reset for next time
if (3000mS_count == 60) {3000mS_count = 0;}  // reset for next time
} // end if interval passed loop
} // end void loop