Project "bacon"

P1010125.JPG (3167910Bytes)
P1010127.JPG (2995501Bytes)

Hi ,Im new to this whole arena of microprocessors and C-code. My previous experience more than 25Yrs ago, was with an extended form of Basic on a Tandy 128K Color Computer III ,then again in school with 5in floppy's and Dos or Pre-Dos..It was a long time ago.. Ive completed two projects so far, a 4 channel thermometer using DS18B20 ,, and a little scanner thing for my Honda.. Each project on a standalone Atmega Arduino. So heres the spawn of my labors on this robot .. its mostly code I found and currently trying to make work...Its not pretty. As soon as I figure out how to post Code on this forum ..I'll put out what Ive got. For now .. Its a kit of got of Ebay,use's a L298N motor controller, It sports and UNO with a sensor shield.. Im running the motors of a 9.6 volt Nimh pack and a 6volt Nimh pack for the UNO and sensors..

Just a few comments.

This post seems to be more of a blog post than a question. The forum posts are usually questions of some nature. Your post is fine as is, just mentioning for next time.

I too learned to program on CoCos. My first was a CoCo 1 with BASIC, 16k of RAM, and a tape drive. It finally got upgraded to 64k and Extended Color BASIC. :stuck_out_tongue:

Looking at the bottom of your robot it seems the encoder wheel on the left motor is pushed on a bit far.

I am pretty sure the NewPing lib has a function that will return the sensed distance in CM or IN. You can even cut the number of pins required to drive your 4 pin US sensor with 3 pins.

In your first program listing you could remove the if statement from the else if. You have already asked if the distance is > 30 or 0. Anything else will be < 30 or > 0.

In your test sketch you have 4 delays that are set at 200 with a remark saying delay 1 sec. 200 != 1 second unless your oscillator is not set correctly.

 

#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();
  }
  {//what is this for???
  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);
  }
  }//this is the what is this for’s partner
}

 

How?

Did you post the code so neatly?

And thanks for your observation’s…

The Timing and delay…this started out with 1sec delay… but I turned it up a notch, 200uS is about as fast as my servo will go to complete one sweep 0-90 etc., the… anything  faster and the servo cannot complete  the sweep . I was thinking of looking into “blink without delay” for these function’s.

Im not using the encoder’s , just motor drive …I put the encoders on the spindles so I wouldnt lose them. I have a few OPTO sensors from VCR’s that I think may work well here…but first the code …

 

 

Re: pretty posted code.

I cheated. :stuck_out_tongue:

In the arduino IDE if you highlight code you want to post, you will see an option to Copy to HTML. Once that is done you then click on the HTML “button” above the text entry box, 3rd from the right. A new window will open and you can paste the HTML in that window where you want it. If it is the first thing you post, you may want to add <p></p> to the very end so you can add text in this font instead of the font the code is pasted in.

On another note, why do you have the extra braces in your code? I added comments to the two extra braces so they would stand out.

Thank you!I didnt even

Thank you!

I didnt even notice the braces…Im still wading through the code…I keep having to check definitions of things…

To try and understand error’s Ive been putting error’s in code to see what message it generates to better understand how this all works. In other words ,I cant make enough mistakes on my own that I have to make some…nothing worse than having no idea what an error means…Its amazing how a simple “/” left in a line …issues a string of errors from top to bottom…but at least now I can identify some of the errors may be two or three…

So this gets it moving

So this gets it moving around

 

 

 

#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){
  halt;
  delay(250);
  reverse();
  delay(800);
  rightturn();
  delay(550);
  leftturn();
  delay(650);
}
}
 
 
 
  
 


 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,150);
 analogWrite (ENA,150);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,HIGH);
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
 }

void reverse(){
 analogWrite (ENA,110);
 analogWrite (ENB,110);
 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);
}

 

 

Im starting to try and write a code to check left , right, forward  

 

/
Arduino Servo Test sketch
/
#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);          // Wait 1 second
   servoMain.write(75);  // Turn Servo back to center position (90 degrees)
   delay(200);          // Wait 1 second
             // Wait 1 second
   servoMain.write(170); // Turn Servo Right to 180 degrees
   delay(200);          // Wait 1 second
   servoMain.write(75);  // Turn Servo back to center position (90 degrees)
   delay(200);          // Wait 1 second  
}

 

 

Im using this to check my direction seeking …just turn on a led for which direction it choose’s and then get that to work with the Motor’s, head servo ,ping …

 

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

 

Ohh and then its onto the flame sensors…if its gonna wander around the house , it may as well be doing something…

I flirted with another

I flirted with another avenue. Taking some advice…I put Serial.print in the motor direction lines and a series of “if”'s with differential measurements…just to see how it all run’s…

The one thing I cant get a grip on is how to get the servo to do its scan thing seperate from everything else thats running…

 

Ohhh.and inadvertantly stumbled on some useful ping sensor info…milli-seconds vs microseconds…now the blink with out delay makes sense.

 

 

 

 

#include <Servo.h>
  
  Servo servoMain; // Define Servo
  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 5 (output pwm)
  int IN3=4;//connected to arduino’s port 4
  int IN4=7;//connected to arduino’s port 7
   const int trigPin = 12;
  const int echoPin = 11;
 
  
 void loop()
{
  
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration,cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode (trigPin, OUTPUT);
  digitalWrite (trigPin, LOW);
  delayMicroseconds(29);
  digitalWrite (trigPin, HIGH);
  delayMicroseconds(29);
  digitalWrite (trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode (echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  
  cm = microsecondsToCentimeters(duration);
  
    
  
  Serial.print(cm);
  Serial.print(“cm”);
  Serial.println();
  
  if ( cm < 10) {
  analogWrite (ENA,110); //moving reverse
  analogWrite (ENB,110);
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,LOW);
  digitalWrite(IN4,HIGH);
Serial.print(“reverse”);}
  

 if (cm>25){
  analogWrite (ENA,150); //moving forward
 analogWrite (ENA,150);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,HIGH);
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
 Serial.print(“forward”);}
 
 if (cm>15){
 analogWrite (ENA,125);//turning left
 analogWrite (ENB,125);
 digitalWrite(IN1,HIGH);
 digitalWrite(IN2,LOW);
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
 Serial.print(“Left”);}

 if(cm>20){
 analogWrite(ENA,125);//turning right
 analogWrite(ENB,125);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,HIGH);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);
 Serial.print(“right”);
 }

  long microsecondsToCentimeters(long microseconds)
   {
    return microseconds / 29 / 2;
    }
 

 

 

Your program as pasted, will not work.

The setup(){} is where one usually sets pinMode for OUTPUT pins. You only set the trigger pin to OUTPUT and you do it every time you run through the loop().

You might want to look over http://playground.arduino.cc/Code/NewPing

You will notice there is a method in the library that will return cm. It is called ping_cm. “You” could call it, if you were using it here, by cm = sonar.ping_cm()

You don’t have Serial.begin(baud) in your program at all. You will not get any information back.

Just for fun, I took a shot at writing something that might work

for you.

http://pastebin.com/E9uRJYSy

Posting full programs in forum posts gets a bit crazy after a while.

I won’t tell you that it will work. The code should be “close enough for Rock’n’Roll”. Let me know how/if it works.

I should mention, some of the numbers are just “from the hip.” Notably:

#define farLeft 10 // set min travel for servo
#define center 89 // set center for servo
#define farRight 175 // set max travel for servo

byte servoStep = 5; // global variable setting servo step

#define fast 100
#define med 50
#define slow 25

Starting at line 55:

  if (servoDir) {
    servoPos += servoStep;
    if (servoPos < farRight) {
      servoMain.write(servoPos);
    } else if (servoPos >= farRight) {
      servoMain.write(farRight);
      servoDir = false;
    }
  } else {

if (servoDir) just means if (servoDir == true). Remember servoDir is a boolean, only true or false.
servoPos += servoStep means servoPos = servoPos + servoStep. The inverse goes for later where servoPos -= servoStep.
The next line checks that servoPos is still less than max servo travel. Otherwise, the servo moves to max travel and sets servoDir to false. Next time through the loop() the servo should start moving the other direction.

The robotMove function is one I should have written for my robot. (I haven’t gotten there yet. :P) It takes 3 ints left motor speed, right motor speed, and a delay. mogul suggested I improve my original robotMove function with + and - 0 to 100 ints. With respect to the final few lines of the robotMove function, I will point your eyes at http://arduino.cc/en/Reference/Map . Let me know if I have made almost no sense. :smiley:

My bad …I left the setup()

My bad …I left the setup() in a tab. (a note from Oddbot)

I put Serial print in the motor commands to see whats happening with the motor’s (a note from chickenparmi )

Im trying to develop some sort of hierarchy. To try and keep organised in my head. I have a vision of how this “should” work…

Im gonna go with what you wrote and see if I can use it

Ive een reluctant to ask for code,nothing is learned if something is given. Something Im trying to teach my own two kids and my buddies teenager…

I appreciate your time and effort here…

 

void setup() {
  servoMain.attach(9);
  Serial.begin(9600);// initialize serial communication:
  pinMode(ENA,OUTPUT);//output
  pinMode(ENB,OUTPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
}

I offered the code to show how I might code your project.

I will gladly explain how/why/what I did.

Off the top of my head, one thing I changed was some of your declarations.

#define VARIABLENAME value

and

const int VARIABLENAME = value; // int could be any valid variable type

will both declare constants. Be careful that you don’t insert and = and ; in #define statements. The const keyword stands for constant. If I was on top of my game, I would have made sure that all my constants were all caps. Oops.

If you really want to learn, you might try making a forum post with a list of questions about the hows/whys/whats you don’t quite understand. There are a number of programmers around here that put my work to shame. :slight_smile: Be careful you don’t ask something that can be answered by a quick google search. You might get publicly flogged. :stuck_out_tongue:

LOL…throughout all of

LOL…

throughout all of this,Google has been my weapon of choice.Back in my day I only had a set of enclclopedia’s and the local libary…

 

Ive run your code,and Im currently working with it…its a little more complex than my simple logic…so this will take a second…maybe a day to get my head around it…

 

 

 

I’m right there with you.

I am 42 for the next few months. I have just spent more time learning to code. :smiley:

all things aside…The

all things aside…The placement of the sensor is a problem…If the machine swings around ,and is in close proximity of another object,particularly some thing with foam, it doesn’t see it, more often than a wall or something solid…

Im going to have to move the sensor or install a bumper , The idea would be to allow minimum ,5 cm in front of the sensor…it appears to alieve the close proximity errors…

You have run across a common issue.

I once read a book that suggested graceful failure. It boiled down to making use of multiple sensors. Ultrasonics for solid objects, IR for things that US won’t likely catch, and bumpers when those sensors fail.