Author Topic: Motor Driver  (Read 2670 times)

0 Members and 1 Guest are viewing this topic.

Offline Fiery DuckTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Motor Driver
« on: March 06, 2010, 07:26:28 PM »
How could I use the tutorial in this link:http://www.toddholoubek.com/classes/pcomp/?page_id=394Without using a switch? How could I do it automatically?

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: Motor Driver
« Reply #1 on: March 06, 2010, 08:06:28 PM »
Base on the code, the switch alternate the control pins to change the direction of the motor.

Code: [Select]
/* simpleMotor using th L293
* —————-
*/

//the H bridge takes two outputs from the Arduino to control the motor.
int motorPin0 = 2;
int motorPin1 = 3;
// there is one switch
int switchPin = 4;
//declare the state variable
int state = 0;

void setup() {
//the motor control wires are outputs
pinMode(motorPin0, OUTPUT);
pinMode(motorPin1, OUTPUT);
//the switch is an input
pinMode(switchPin, INPUT);
}

void loop() {
//read the switch
state = digitalRead(switchPin);
//based on the state of the switch alternate the control pins to change the direction of the motor.
switch (state){
case 0:
digitalWrite(motorPin0, HIGH);
digitalWrite(motorPin1, LOW);
break;
case 1:
digitalWrite(motorPin0, LOW);
digitalWrite(motorPin1, HIGH);
break;
}
}

You can change the code to this, it will change direction every 2 seconds.
Code: [Select]

void loop() {
//read the switch
//state = digitalRead(switchPin);
//based on the state of the switch alternate the control pins to change the direction of the motor.
switch (state){
case 0:
digitalWrite(motorPin0, HIGH);
digitalWrite(motorPin1, LOW);
break;
case 1:
digitalWrite(motorPin0, LOW);
digitalWrite(motorPin1, HIGH);
break;
}
if(state==0) {
 state=1;
}
else {
state = 0;}
delay(2000);                  // waits for a 2 seconds

}

Offline Fiery DuckTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Re: Motor Driver
« Reply #2 on: March 06, 2010, 09:59:15 PM »
Yes, but I am looking for a way to wire it without a switch. Sorry if my question wasn't clear. :-\

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: Motor Driver
« Reply #3 on: March 07, 2010, 03:33:07 AM »
If you are talking about the switch statement, then you can replace it with "if else if" Branching.
Code: [Select]
/* simpleMotor using th L293
* —————-
*/

//the H bridge takes two outputs from the Arduino to control the motor.
int motorPin0 = 2;
int motorPin1 = 3;
// there is one switch
int switchPin = 4;
//declare the state variable
int state = 0;

void setup() {
//the motor control wires are outputs
pinMode(motorPin0, OUTPUT);
pinMode(motorPin1, OUTPUT);
//the switch is an input
pinMode(switchPin, INPUT);
}

void loop() {
//read the switch
state = digitalRead(switchPin);
//based on the state of the switch alternate the control pins to change the direction of the motor.
if(state==0){
digitalWrite(motorPin0, HIGH);
digitalWrite(motorPin1, LOW);
}
else if(state==1) {
digitalWrite(motorPin0, LOW);
digitalWrite(motorPin1, HIGH);
}
}


http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_025.htm
http://www.tutorialspoint.com/ansi_c/c_control_statements.htm

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
    • Ro-Bot-X Designs
Re: Motor Driver
« Reply #4 on: March 07, 2010, 06:51:42 AM »
You can replace the toggle switch with any other sensor of your choice. For instance, you can use a contact switch as a bumper, a IR sensor and set a threshold when the state changes it's value. For the IR sensor you will have to remove the resistor and wires and connect the sensor to an analog pin, for instance A0.
Check out the uBotino robot controller!

Offline Fiery DuckTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Re: Motor Driver
« Reply #5 on: March 07, 2010, 10:11:09 AM »
I want to use this on the 50$ robot tutorial because I am cheap. How would I do this? :-[

 

SMF spam blocked by CleanTalk