My Robot,My journey

I've been messing around with an obstacle avoidance robot. I picked up a lil'kit of Ebay for the 2WD version,It came with motors an a motor shield(L298N). I have a UNO, sensor shieldV4.0 , a couple ultrasound sensors(4pin type)

Playing with various bits of code ,I found nothing to fit.

So.I started to mess around on my own.
I guess the first thing a robot needs to do is move...of course...So I established the various motion patterns.I put a "halt" pattern in ,because or house is multilevel and I don't need this bot driving over the stairs.

//establish relative motion and turn routine


int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=7;//connected to Arduino's port 7
#define leftturn
#define rightturn
#define forward
#define reverse
#define halt

void setup()
{
 pinMode(ENA,OUTPUT);//output
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);

}
void loop()
{
  analogWrite(ENA,255);//start driving motorA
  analogWrite(ENB,255);//start driving motorB
  rightturn;
 digitalWrite(ENA,LOW);
 digitalWrite(ENB,LOW);//stop driving
 digitalWrite(IN1,LOW); 
 digitalWrite(IN2,HIGH);//setting motorA's directon
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);//setting motorB's directon
 delay (3000);
 
 leftturn;
 digitalWrite(ENA,LOW);
 digitalWrite(ENB,LOW);//stop driving
 digitalWrite(IN1,HIGH); 
 digitalWrite(IN2,LOW);//setting motorA's directon
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);//setting motorB's directon
 delay (3000);
 
forward;
digitalWrite(ENA,LOW);
digitalWrite(ENA,LOW);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
delay(3000);

reverse;
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
delay (3000);

halt;
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);

delay (3000);

}

My guess is that this bit of code will not be in the loop but in setup with out delays when it comes time to get this thing looking for walls and drop off's

I don't need this bot driving over the stairs.

Of course you do! That's the fun part....

You should read about functions here.

So I established the various motion patterns.

Now you need to put them into individual functions, and eliminate the delays, otherwise your robot will never see the stairs in time to avoid them.

Something Like this?

int distance = 0;
int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=7;//connected to Arduino's port 7
#define leftturn
#define rightturn
#define forward
#define reverse
#define halt

void setup()
{
 pinMode(ENA,OUTPUT);//output
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);

}
void loop()
{
  analogWrite(ENA,255);//start driving motorA
  analogWrite(ENB,255);//start driving motorB
  
                             
 
  void rightturn();
  {
 digitalWrite(ENA,LOW);
 digitalWrite(ENB,LOW);//stop driving
 digitalWrite(IN1,LOW); 
 digitalWrite(IN2,HIGH);//setting motorA's directon
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);//setting motorB's directon
}
 
 void leftturn();
 {
 digitalWrite(ENA,LOW);
 digitalWrite(ENB,LOW);//stop driving
 digitalWrite(IN1,HIGH); 
 digitalWrite(IN2,LOW);//setting motorA's directon
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);//setting motorB's directon
 }
 
void forward();{
digitalWrite(ENA,LOW);
digitalWrite(ENA,LOW);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}

void reverse();{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}

void halt();{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}

}

Well...After wading around waste deep in syntax , only to find myself running half naked through a thorn ridden Boolean Rainforest.. I managed to make a led turn on and of using the NewPing sensor..

I really do think that this well help with not hitting things.

#include <NewPing.h>

  
  #define LED_PIN    4
  #define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
  #define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
  #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters).
  NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  boolean trig = false;
 
 

  void setup(){
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

   pinMode(LED_PIN,OUTPUT);
   analogWrite(LED_PIN,0);
   delay(1000); 
   }
   
    void Engaged(){
   digitalWrite(LED_PIN,HIGH);
    Serial.println("This Hurt!!!");}
   
   
   void loop(){
   delay(50);                        // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
   unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
   Serial.print("Ping: ");
   Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
   Serial.println("cm");
     if(trig == true){
       Engaged();}
     {
    delay(50);
    unsigned int uS = sonar.ping();
    unsigned int distance = uS / US_ROUNDTRIP_CM;
     
    if(distance<10){
    trig = true;}
    delay(50);
    if(distance>10){
      trig = false;
    digitalWrite(LED_PIN,LOW);}}}

Does any body know why I cant get rid of this loop I find myself in..
I added the delays to test my theory that nothing was stopped ,just loopy.
.I've tried various times and cannot get clear of the motor function running in a loop..
Here's the nightmare.I shoved everything into the loop...its a joke really.I dont need it fixed..advice would help. :wink:

#include <NewPing.h>

int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=7;//connected to Arduino's port 7
#define leftturn
#define rightturn
#define forward
#define reverse
#define halt
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-1000cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup(){
  


 Serial.begin(9600);
 pinMode(ENA,OUTPUT);//output
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);}

 void loop(){
 
 delay (50);
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
  
   
    delay(50);

    unsigned int distance = uS / US_ROUNDTRIP_CM;
    
if ( distance >10);{ 
  forward;}

 if (distance<10);{
  rightturn;}


 void rightturn();
 digitalWrite(ENA,LOW);
 digitalWrite(ENB,LOW);
 digitalWrite(IN1,LOW); 
 digitalWrite(IN2,HIGH);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);
 delay(100);

 void leftturn();
 digitalWrite(ENA,LOW);
 digitalWrite(ENB,LOW);
 digitalWrite(IN1,HIGH); 
 digitalWrite(IN2,LOW);
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
 delay(100);
 

 void forward();
 digitalWrite(ENA,LOW);
 digitalWrite(ENA,LOW);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,HIGH);
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
 delay(100);

void reverse();
 digitalWrite(ENA,LOW);
 digitalWrite(ENB,LOW);
 digitalWrite(IN1,HIGH);
 digitalWrite(IN2,LOW);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);
  delay(100);

void halt();
 digitalWrite(ENA,LOW);
 digitalWrite(ENB,LOW);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,LOW);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,LOW);
 delay(100);
}

This

#define leftturn
#define rightturn
#define forward
#define reverse
#define halt

does not define functions, and even if it did, this
forward; wouldn't call a function, and neither would this void forward();

Look at some of the examples provided with the IDE and work through them - play with them and modify them.

Im daft....
Im sitting here and I realized Im not sure if Im trying to create a string ,an array ,or bacon..

Chris1448:
Im daft....
Im sitting here and I realized Im not sure if Im trying to create a string ,an array ,or bacon..

Go with bacon then, if there's any doubt. Everything is better with bacon.

Its alive..well sort of..first time I turned my back he drove over the stairs..then ran into everything..I tweaked the delay..seems to give a higher resolution and smoother turning.
I figured out the speed..I was using digitalWrite instead of analogWrite..and remove the jumpers from ENA and ENB on the motor board..
Im going to put two lasers on the head ..just to drive my neighbour's cats mental
What I dont understand is ..why did the #define essentially paralyze the sketch?..that was my problem ..
I dont know how this will work out when I add the "looking servo" and flame sensors..but at least" its" something
Its bacon

#include <NewPing.h>

int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=7;//connected to Arduino's port 7

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-1000cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup(){
   Serial.begin(9600);
 pinMode(ENA,OUTPUT);//output
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);}

 void loop(){
 delay(35);
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
if ( uS / US_ROUNDTRIP_CM > 30 || uS / US_ROUNDTRIP_CM == 0) {
  forward();
}
else if (uS / US_ROUNDTRIP_CM < 30){
  
  rightturn();
  delay(35);
}
}
 
 
  
   
  


 void rightturn(){
 analogWrite(ENA,125);
 analogWrite(ENB,125);
 digitalWrite(IN1,LOW); 
 digitalWrite(IN2,HIGH);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);
 }

 void leftturn(){
 analogWrite (ENA,125);
 analogWrite (ENB,125);
 digitalWrite(IN1,HIGH); 
 digitalWrite(IN2,LOW);
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
 }
 

 void forward(){
 analogWrite (ENA,125);
 analogWrite (ENA,145);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,HIGH);
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
 }

void reverse(){
 analogWrite (ENA,125);
 analogWrite (ENB,145);
 digitalWrite(IN1,HIGH);
 digitalWrite(IN2,LOW);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);
}

void halt(){
 analogWrite (ENA,0);
 analogWrite (ENB,0);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,LOW);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,LOW);
}

tweaked the delay.

You should have deleted them. You don't walk down a hall, only opening your eyes once in a while, do you? Of course not. You are constantly looking left and right, and turning when there is an obstacle in front of you. Otherwise you keep going at a constant speed. There is never a delay() in how you move. There should not be one in how the robot moves or looks or falls down the stairs.

Well, maybe there should be a delay() before it falls down the stairs, but only after it halt()s first.

PaulS:
There is never a delay() in how you move.

This is true..but like everyone ,we have spatial awareness....bacon dosent.

Your right however..the last delay ..I mean for the sake of 35 milliseconds....i get your point.

Head sweep ...just looking left and right.
The numbers look alittle silly ..but like any servo it has to be tweeked to find center and setup end point volume
Im using a 9g servo ..its quite obvious the the gears need to be set properly , its out about 5-6 teeth one way..while this would be a problem for an aircraft ..its reconcilable on the Arduino platform.

#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
   servoMain.attach(9); // servo on digital pin 10
}

void loop()
{
       
   servoMain.write(9);   // Turn Servo Left to 0 degrees
   delay(200); 
  
   servoMain.write(75);  /
   delay(200);          
             
   servoMain.write(170); // Turn Servo Right to 180 degrees
   delay(200);
   
   servoMain.write(75);  /
   delay(200);             
}

The case is transparent,
Its clear that the potentiometer is at endpoint one way ,and the pot-gear has some left over,the other way theres nothing left..and .the pot-gear is clearly not centered....the sweep is clearly short one way..
I was making servo winch's back in the 80's