Need help with easy coding

I've managed to wire a 2 wheel drive little rover with an L293 Hbridge and now I need help programming it.

Any help is welcome, this is the layout: http://shallowsky.com/arduino/class/hbridge-dual-motor_bb.jpg

I just need a very basic code for Arduino's IDE and be able to make calls for the movement.

ANY HELP IS APPRECIATED!

thanks

What are you trying to do? Change direction, or speed? or both?

Direction, I dont mind about speed.

Direction is quite easy then. I actually made an Instructable about this, but I'll spare you the trouble.

int in1pin = 6; // pins to h-bridge
int in2pin = 7; 

int leftButton = 8;
int rightButton = 9; // buttons

void setup() {

  pinMode(in1pin, OUTPUT);
  pinMode(in2pin, OUTPUT); // outputs

  pinMode(leftButton, INPUT_PULLUP);
  pinMode(rightButton, INPUT_PULLUP); // inputs w internal pullup resistors
  
}

void loop() {

  int leftPinState = digitalRead(leftButton);
  int rightPinState = digitalRead(rightButton); // set value names for read data

  if (leftPinState == LOW) { // if left button is pressed ... 

    digitalWrite(in1pin, HIGH); // make motor go one way
    digitalWrite(in2pin, LOW);
    
  }

  else if (rightPinState == LOW) { // if right button is pressed ...

    digitalWrite(in1pin, LOW);
    digitalWrite(in2pin, HIGH); // make motor go other way

    
  }

  else { // if neither button is pressed ...

    digitalWrite(in1pin, LOW); // nothing happens
    digitalWrite(in2pin, LOW); 
    
  }
  
}

That is a code controlling one DC motor with an H-Bridge that I found in my old sketchbook. While you press one button, the motor will turn one way. If you press the other button, it will turn the other way. Here is also a diagram of my setup.

I dont have buttons, So I was hoping just to make it move and alter the delays to make it navigate trough a course

GarrisonFord:
I dont have buttons, So I was hoping just to make it move and alter the delays to make it navigate trough a course

You can do that easily, just ditch the IF/ELSE statements use delays, something like this:

void loop() {

  // section 1

  digitalWrite(in1pin, HIGH);
  delay(500);
  digitalWrite(in1pin, LOW);
  digitalWrite(in2pin, HIGH);
  delay(750);

}

or something like that. Also, delays aren't the best thing to use, but I suppose for this project it is fine if all you are doing is controlling those motors.

Yes, Now what about the voidsetup?

I really forgot alot about programming, I just took a small course last year.

All you need in void setup() is to set the pins you connected to your h-bridge as outputs. I'd suggest to give good names so you can tell which pin does what -

int M1clockwise = 6;
int M1cclockwise = 7; // motor 1

int M2clockwise = 8;
int M2cclockwise = 9; // motor 2

void setup() {

  pinMode(M1clockwise, OUTPUT);
  pinMode(M1cclockwise, OUTPUT);

  pinMode(M2clockwise, OUTPUT);
  pinMode(M2cclockwise, OUTPUT);

}

Obviously your code needs to match the pins that you are actually connecting, of course.

Also, the two white wires in your diagram going to the arduino board actually can just lead to the power rails, since you aren't interested in changing speed at all.

chummer1010:
You can do that easily, just ditch the IF/ELSE statements use delays, something like this:

void loop() {

// section 1

digitalWrite(in1pin, HIGH);
 delay(500);
 digitalWrite(in1pin, LOW);
 digitalWrite(in2pin, HIGH);
 delay(750);

}




or something like that. Also, delays aren't the best thing to use, but I suppose for this project it is fine if all you are doing is controlling those motors.

Do you really want both in2pin and in1pin HIGH after the 750ms delay?

.

LarryD:
Do you really want both in2pin and in1pin HIGH after the 750ms delay?

.

It was a quick example of what part of the sketch might look like that I typed up in a few seconds for the OP to get an idea of what to do.

good catch, however!

chummer1010:
I actually made an Instructable about this, but I'll spare you the trouble.

Caught red handed.

How did you power the Arduino.
Did you calculate for the 2-3volt drop of the L293 (2-3volt left for the motor).
Is the stall current of that motor going to overheat or damage the onboard 5volt regulator or trip the USB fuse.
Not wise to power a motor from the 5volt pin (= MCU supply), unless it's a tiny one.
Leo..

chummer1010:
All you need in void setup() is to set the pins you connected to your h-bridge as outputs. I'd suggest to give good names so you can tell which pin does what -

int M1clockwise = 6;

int M1cclockwise = 7; // motor 1

int M2clockwise = 8;
int M2cclockwise = 9; // motor 2

void setup() {

pinMode(M1clockwise, OUTPUT);
 pinMode(M1cclockwise, OUTPUT);

pinMode(M2clockwise, OUTPUT);
 pinMode(M2cclockwise, OUTPUT);

}




Obviously your code needs to match the pins that you are actually connecting, of course. 

Also, the two white wires in your diagram going to the arduino board actually can just lead to the power rails, since you aren't interested in changing speed at all.

Alright so I got this inside the code now, what should go inside of the loop so I can start making the delays and changing direction?

Wawa:
Caught red handed.

How did you power the Arduino.
Did you calculate for the 2-3volt drop of the L293 (2-3volt left for the motor).
Is the stall current of that motor going to overheat or damage the onboard 5volt regulator or trip the USB fuse.
Not wise to power a motor from the 5volt pin (= MCU supply), unless it's a tiny one.
Leo..

I actually used a 9V battery, however I failed to mention that I see.

@GarrisonFord , here is an example of what your code could look like, I tried to explain things as best as I could.

int M1clockwise = 6;
int M1cclockwise = 7; // motor 1

int M2clockwise = 8;
int M2cclockwise = 9; // motor 2

void setup() {

  pinMode(M1clockwise, OUTPUT);
  pinMode(M1cclockwise, OUTPUT);

  pinMode(M2clockwise, OUTPUT);
  pinMode(M2cclockwise, OUTPUT); // set the pins as outputs

}

void loop() {

  digitalWrite(M1clockwise, HIGH); // part 1
  delay(500);
  digitalWrite(M1clockwise, LOW);   
  // make one motor turn clockwise for .5 secs, then turn off
  
  digitalWrite(M1cclockwise, HIGH); // part 2
  delay(150);
  digitalWrite(M1cclockwise, LOW); 
  // make same motor turn for .15 secs in counterclockwise, then turn around
  
  digitalWrite(M2cclockwise, HIGH); // part 3
  delay(600);
  digitalWrite(M2cclockwise, LOW);
  // make other motor turn counterclockwise for .6 secs, then turn off

  /* note that since we're in void loop, after 'part 3', 
   * part 1 will happen right after, 
   * and this process will be looped forever
   * until a power supply is cut, 
   * or there is something within the code to stop it
   */
}

Wawa:
Caught red handed.

How did you power the Arduino.
Did you calculate for the 2-3volt drop of the L293 (2-3volt left for the motor).
Is the stall current of that motor going to overheat or damage the onboard 5volt regulator or trip the USB fuse.
Not wise to power a motor from the 5volt pin (= MCU supply), unless it's a tiny one.
Leo..

I actually built this robot the same way a year ago and it worked perfectly with 4 AA Batteries powering up both the arduino and the motors.

I actually built this robot the same way a year ago and it worked perfectly with 4 AA Batteries powering up both the arduino and the motors

So you are an expert then.

.

LarryD:
So you are an expert then.

.

Did he say or even try to imply that?

He/she did say "it worked perfectly". :wink:

.

A 9volt smoke alarm battery to power an Arduino and a motor.
Double fail.

4AA batteries Which ones.
If you connect the 4AA batteries directly to the motor chip and to the 5volt pin of the Arduino, then the motor current is not coming from or flowing through the Arduino.
Leo..

Wawa:
A 9volt smoke alarm battery to power an Arduino and a motor.
Double fail.

plugging a 9V into the Vin pin on the arduino and the h bridge to 5V works fine for me. it's also how i was instructed to work with the motor I have. Of course, all motors are different, aren't they?

How should I have done it? It'd be helpful to know why I was wrong instead of just knowing that I'm wrong and feeling dumb about it. :slight_smile:

LarryD:
So you are an expert then.

.

Allow me to clarify, when I said that "I built it" I meant that I built it in that course I previously talked about, getting help to code and wire it up. Now, a year after, I do not remember everything in programming or wiring.