Pages: 1 [2]   Go Down
Author Topic: 2WD robot code  (Read 3110 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26194
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
« Last Edit: December 09, 2012, 05:15:53 pm by AWOL » Logged

"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.

Queens, New York
Online Online
Faraday Member
**
Karma: 98
Posts: 3560
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 09, 2012, 05:33:32 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Queens, New York
Online Online
Faraday Member
**
Karma: 98
Posts: 3560
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
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;
}



« Last Edit: December 09, 2012, 08:34:24 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Georgia
Offline Offline
Newbie
*
Karma: 0
Posts: 28
I get by with lots of help from my friends
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check this post i have it working with a ping sensor for object avoidance, a piezo for sounds, a robot has to make sounds smiley-grin, 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. smiley-grin http://arduino.cc/forum/index.php/topic,115743.0.html
Logged

Georgia
Offline Offline
Newbie
*
Karma: 0
Posts: 28
I get by with lots of help from my friends
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

2 words!! REMOTE CONTROL
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: 1 [2]   Go Up
Jump to: