Let's Make Robots!

Kinda stuck again

OK I have re-writen my code and now I am having a problem where my left side motors are not being driven.I know it is not a hardware problem. I upload an older version of code with out the scanning portion and everthing works.I have included LEDS on the proto board to see if I am getting comands from my uC and right side is working but not getting anything for the left. the new code is basicly an addition to the old. I just added the servo and varibles so it can look left and right. If anyone can see why I lost leftside drive and give me an idea what I screwed up any help would be appreciated. I have posted video on my robots page so you can see what is happening.

 

#include <Servo.h>
Servo myservo;

#define motorlf 9
#define motorlr 10
#define motorrf 6
#define motorrr 5

int pingPin = 4;
int pos = 0;

int val2=0;
int val3=0;
int val4=0;

void setup()
{
  pinMode(motorlf, OUTPUT);
  pinMode(motorlr, OUTPUT);
  pinMode(motorrf, OUTPUT);
  pinMode(motorrr, OUTPUT);
  pinMode(pingPin, OUTPUT);
  myservo.attach(3);
}

void Ping()
{
  long duration,cm;
  pinMode (pingPin, OUTPUT);
  digitalWrite(pingPin,LOW);
  delayMicroseconds,(2);
  digitalWrite(pingPin,HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin,LOW);
  pinMode(pingPin,INPUT);
  duration=pulseIn (pingPin,HIGH);
  cm= microsecondsToCentimeters(duration);
 val4=cm;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds/29/2;
}
 
 void foward()
 {
   analogWrite(motorlf,250);
   analogWrite(motorrf,250);
   analogWrite(motorlr,0);
   analogWrite(motorrr,0);
   }
 
 void reverse()
 {
   analogWrite(motorlr,250);
   analogWrite(motorrr,250);
   analogWrite(motorlf,0);
   analogWrite(motorrf,0);
   delay(500);
 }
 
 void halt()
 {
   analogWrite(motorlf,0);
   analogWrite(motorrf,0);
   analogWrite(motorlr,0);
   analogWrite(motorrr,0);
 }
 
 void turnright()
 {
   analogWrite(motorlf,250);
   analogWrite(motorrr,250);
   analogWrite(motorlr,0);
   analogWrite(motorrf,0);
   delay(700);
 }
 
 void turnleft()
 {
   analogWrite(motorlr,250);
   analogWrite(motorrf,250);
   analogWrite(motorlf,0);
   analogWrite(motorrr,0);
   delay(700);
 }
 
 void check()
 {
      myservo.write(0);
     delay (375);
      Ping();
   val2=val4;
    myservo.write(180);
     delay(700);
   Ping();
   val3=val4;
   myservo.write(90);
   delay(375);
 }
 void loop()
 {
   myservo.write(90);
   delay(375);
    Ping();
   if(val4>=35){foward();}
   else
   {
     halt();
     check();
     if (val2>val3){turnleft();}
     else
     {
       if (val3>val2){turnright();}
       else
       {
         if(val2<20&&val3<20){reverse();}}}}

         }

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Geir Andersen's picture

I can’t see where the problem is, but I can give you some pointers on writing better (at least more readable) code.
So I took the liberty of rewriting your code.

It compiles, but I don’t know if it works.
The things I did;

  • Got rid of your val2, val3 & val4 integers as they are hard to figure out
  • Moved the servo position into the Ping function and it returns an int
  • Pulled out the check function as it’s now part of the main loop function
  • Gave new names to your motorlf, motorrl…
  • Rearanged the sequence of the motor settings in your movement functions so they come in the same order.

#include <Servo.h>
Servo myservo;

#define MotorLeftForward 9
#define MotorLeftReverse 10
#define MotorRightForward 6
#define MotorRightReverse 5
#define pingPin 4

void setup()
{
  pinMode(MotorLeftForward, OUTPUT);
  pinMode(MotorLeftReverse, OUTPUT);
  pinMode(MotorRightForward, OUTPUT);
  pinMode(MotorRightReverse, OUTPUT);
  pinMode(pingPin, OUTPUT);
  myservo.attach(3);
}

void loop()
{
  int ForwardPing, RightPing, LeftPing;
  ForwardPing = Ping(90);
 
  if(ForwardPing >= 35)
  {
    foward();
  }
  else
  {
    halt();
    RightPing = Ping(180);
    LeftPing = Ping(0);
    if (LeftPing > RightPing)
    {
      turnleft();
    }
    else
    {
    if (RightPing > LeftPing)
      {
        turnright();
       }
      else
        {
          if((RightPing < 20) && (LeftPing < 20))
        {
          reverse();
        }
      }
    }
  }
}

int Ping(int Pos)
{
  long duration,cm;
  myservo.write(Pos);
  delay (20); // 20ms is probably enough to get the servo into position
 
  pinMode (pingPin, OUTPUT);
  digitalWrite(pingPin,LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin,HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin,LOW);
  pinMode(pingPin,INPUT);
  duration=pulseIn (pingPin,HIGH);
  cm= microsecondsToCentimeters(duration);
  return (int) cm;
}

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

 void foward()
 {
   analogWrite(MotorLeftForward,250);
   analogWrite(MotorRightForward,250);
   analogWrite(MotorLeftReverse,0);
   analogWrite(MotorRightReverse,0);
   }
 
 void reverse()
 {
   analogWrite(MotorLeftForward,0);
   analogWrite(MotorRightForward,0);
   analogWrite(MotorLeftReverse,250);
   analogWrite(MotorRightReverse,250);
   delay(500);
 }
 
 void halt()
 {
   analogWrite(MotorLeftForward,0);
   analogWrite(MotorRightForward,0);
   analogWrite(MotorLeftReverse,0);
   analogWrite(MotorRightReverse,0);
 }
 
 void turnright()
 {
   analogWrite(MotorLeftForward,250);
   analogWrite(MotorRightForward,0);
   analogWrite(MotorLeftReverse,0);
   analogWrite(MotorRightReverse,250);
   delay(700);
 }
 
 void turnleft()
 {
   analogWrite(MotorLeftForward,0);
   analogWrite(MotorRightForward,250);
   analogWrite(MotorLeftReverse,250);
   analogWrite(MotorRightReverse,0);
   delay(700);
 }

jlevous's picture

The Servo.h library disables PWM functionality on pins 9 and 10, whether or not there is a servo on those pins.

Try using pins 11 & 3 for the left motor, and attach the servo to pin 9 or 10. 

The Timer PWM Cheatsheet is a handy referrence, bookmark it or print it out.

Also, take the time to read and understand the Data Sheet for the ATmega µController.

 

Although I do like the Arduino IDE for quick and dirty testing and development, you really need a good understanding of the underlying architecture if you hope to develop projects of any complexity.

 

Disclosure statement: Keep in mind that my prefered programing language is Assembly... I'm always leary of using libraries without knowing what they are doing below the surface. Always look for the man behind the curtain.

bdugh's picture

I thank you I just put up another post/plee for help but this is the answer I think I have been looking for. I will keep you posted

bdugh's picture

With a few more adjustments it lives. I thank you again for taking your time to help out a noob like me.

birdmun's picture

I never would have thought to look at the included library.

bdugh's picture

Geir,

I thank you you code is beautiful after a few adjustments to the servo delay time everything seems to work but I still don't have left side drive. I am going to take video of it shortly but I am not getting any outputs on pins 9&10. I upload my old code which doesn't have the servo or varibles of any kind and it all works. I thank you for your time and patience and I hope to one day be able to acomplish the quality of projects that you have

birdmun's picture

What motor shield are you using?

bdugh's picture

I built my own motor driver using a couple BA6886n chips. I know its not ideal but I have been on a budget. I have recently gotten a SN54410 from pololu which is going to be my next project. I also got the better replacement motors for the tamyia gearbox from pololu too. so I am not pulling to much amprage from the chips.

bdugh's picture

I keep missing that but it compiles and uploads.I checked my old code and it is in there too. The Ping is working and sending info and the right track is working as it should. i am wondering if there is some issue with the servo that might be cutting off pins 9 and 10.

bdugh's picture

I fixed the comma and no change in anything