Go Down

Topic: 2WD robot code (Read 3848 times) previous topic - next topic

AWOL

#15
Dec 09, 2012, 11:14 pm Last Edit: Dec 09, 2012, 11:15 pm by AWOL Reason: 1
Quote
issues?

You tell us - you're the one with the eyeballs on the project.

Code: [Select]
digitalWrite (13, HIGH); //Establishes forward motion of Channel B right motor
A simple sensible pin name would have been shorter and more descriptive than the comment.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

HazardsMind

#16
Dec 09, 2012, 11:31 pm Last Edit: Dec 09, 2012, 11:33 pm by HazardsMind Reason: 1
I should have mentioned before, my code works with a basic H-Bridge driver that I built myself, it use 2 pins to control a single motor. I label my pins as
left(L) forward(F) motor1, for the left motor drive forward .
........reverse(R) motor1 , for left motor drive reverse.

Likewise for the right motor.

Now which ever pin is high, tells which direction the motor is going, forward or reverse.

@AWOL
It does not need to return anything, it just has to go forward or backward based on which pin is HIGH.
My code can also use PWM, just change it to analogWrite( pin, 0-255), and the other to LOW or 0.

This can work with his motor shield, he just to change up the pins a little.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

#17
Dec 10, 2012, 02:32 am Last Edit: Dec 10, 2012, 02:34 am by HazardsMind Reason: 1
Here, this is my old testing code, you might be able to learn something from it and apply it to your project.
It is very primitive, and improper, but it does work. As and added bonus, this code also uses a Parallax ultrasonic ping sensor. (the 4 pin sensor)

Code: [Select]
/*
simple keyboard control test
*/
long duration, inches, cm;
const int ping = 7;//Sensor (trigger)
const int echo = 8;//Sensor (echo)
int distance;

char val;         // variable to receive data from the serial port
byte M1L = 3;// PWM
byte M1R = 5;// PWM
byte M2L = 9;// PWM
byte M2R = 6;// PWM

void setup()
{
  pinMode(M1L, OUTPUT);                               
  pinMode(M1R, OUTPUT);
  pinMode(M2L, OUTPUT);                               
  pinMode(M2R, OUTPUT);
  Serial.begin(9600);       // start serial communication at 9600bps

}

void loop() {
  if( Serial.available() )       // if data is available to read
  {
    val = Serial.read();
   
    if( val == 'W' || val == 'w' )//forwards               
    {
      //Serial.println("Moving Forwards"); 
      digitalWrite(M1L, HIGH); digitalWrite(M2L, HIGH);
      digitalWrite(M1R, LOW); digitalWrite(M2R, LOW);                 
    }
    else if( val == 'S' || val == 's' )//backwards             
    {
      //Serial.println("Moving Backwards");
      digitalWrite(M1L, LOW);  digitalWrite(M2L, LOW);
      digitalWrite(M1R, HIGH); digitalWrite(M2R, HIGH);                   
    }
    else if( val == 'A' || val == 'a' )//Left         
    {
      //Serial.println("LEFT");
      digitalWrite(M1L, LOW);  digitalWrite(M2L, HIGH);   
      digitalWrite(M1R, HIGH); digitalWrite(M2R, LOW);             
    }
   else if( val == 'D' || val == 'd' )             
    {
      //Serial.println("RIGHT");
      digitalWrite(M1L, HIGH); digitalWrite(M2L, LOW);
      digitalWrite(M1R, LOW);  digitalWrite(M2R, HIGH);                 
    }   
    else { //full stop
      digitalWrite(M1L, LOW); digitalWrite(M2L, LOW);
      digitalWrite(M1R, LOW); digitalWrite(M2R, LOW); 
    }
   if( val == 'R' || val == 'r' ){ //search mode
      getping();} 
  }
}

void getping()
{
  Serial.print("R is not pressed ");
  while(val == 'r')
  {
    Serial.print("R is pressed ");
    pinMode(ping, OUTPUT);
    digitalWrite(ping, LOW);
    delayMicroseconds(2);
    digitalWrite(ping, HIGH);
    delayMicroseconds(5);
    digitalWrite(ping, LOW);

    pinMode(echo, INPUT);
    duration = pulseIn(echo, HIGH);

    // convert the time into a distance
    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);

    Serial.print(inches);
    Serial.print("in, ");
    Serial.print(cm);
    Serial.print("cm ");
    Serial.println();

    delay(100);


    //------Search for object with in range------

    if(inches > 12)
    {
      Serial.println("Searching for object");
      analogWrite(M1R, 100);
      digitalWrite(M1L, LOW);
      analogWrite(M2L, 100);
      digitalWrite(M2R, LOW);
    }
    else if(inches >= 4 && inches <= 12)
    {
      analogWrite(M1R, 90);
      digitalWrite(M1L, LOW);
      analogWrite(M2L, 90);
      digitalWrite(M2R, LOW);
     
      Serial.println("Object Found!!!");//Move forward to object
      if(inches <= 6){ distance = 450;}
      else distance = 300;
     
      delay(distance);
      Serial.println("Going to object"); //is within 5 inches
      while(inches != 3) //go to up object and stop when object
      {
        Serial.println(inches);
        pinMode(ping, OUTPUT);
        digitalWrite(ping, LOW);
        delayMicroseconds(2);
        digitalWrite(ping, HIGH);
        delayMicroseconds(5);
        digitalWrite(ping, LOW);
        pinMode(echo, INPUT);
        duration = pulseIn(echo, HIGH);
        inches = microsecondsToInches(duration);

        analogWrite(M1L, 100);
        digitalWrite(M1R, LOW);
        analogWrite(M2L, 100);
        digitalWrite(M2R, LOW);
      }
      digitalWrite(M1L, LOW); //all motors stop
      digitalWrite(M1R, LOW);
      digitalWrite(M2L, LOW);
      digitalWrite(M2R, LOW);

      delay(500);
      return;
    }
   
    if(val=='x')
    {
      Serial.print("Returning to prog ");
      return;
    }
    else
    {
      Serial.print("X not pressed,");
    }
  }
}


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



My GitHub:
https://github.com/AndrewMascolo?tab=repositories

evmaker

The magic in this code occurs in the analogWrite lines. There are small inconsistencies in all electric motors. Even if they are "IDENTICAL". but the faster motor will need a slightyly smaller value than 255 "the max". so take a few tries and slow the faster motor by 5 or so each time until you get them running as close to the same as possible. Let us say for example that motor a is the sluggish one and the robot wanders in a circle to the a motor side when going forward. Also when we give motor b an analog write command of only 200 the robot goes very nearly straight. Then we know that motor b must be skewed by 55 at full throttle forward. This can be easily done with the map function described in the docs. map motor a 0 to 255 forward and motor b 0 to 200 and it works relatively well. However not perfect. Now the flip side is that it is likely that in reverse the faster motor may be the other one or not. To make a robot go straight really you will need an input device of some sort that monitors actual position and changes the values to correct on the fly. There is a lipoly bord available at cute digi wit a gps chip that can tell a robot where it is with in 15 feet or so this robot would need a large out door playing field but they are a hoot to watch run around finding there targets.

Check this post i have it working with a ping sensor for object avoidance, a piezo for sounds, a robot has to make sounds :D, and 2 motors. i use a uno r3 and a
motorsheild r3 same as yours. just trying to get a led to light when the piezo beeps. robot has to have blinky lights also. :D http://arduino.cc/forum/index.php/topic,115743.0.html

evmaker

wow, very cool !!

No my motor shield is very different. My beta 2 version can be viewed under construction at www.evmaker.tumblr.com. If you care to sort back through time you can see some of the development of beta 1 and some clips of his short one month life. My robot is pretty big and disguised as a ford truck. Also requires piloting that is the fun part.


SoniC73

So i would need a program for 2 motors im new with arduino so  one is steer motor and another one is for going foward and backward he gets signal from ping distance sensor help pls

Go Up