Society of Robots - Robot Forum
Electronics => Electronics => Topic started by: Fiery Duck 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=394 (http://://www.toddholoubek.com/classes/pcomp/?page_id=394)Without using a switch? How could I do it automatically?
-
Base on the code, the switch alternate the control pins to change the direction of the motor.
/* 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.
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
}
-
Yes, but I am looking for a way to wire it without a switch. Sorry if my question wasn't clear. :-\
-
If you are talking about the switch statement, then you can replace it with "if else if" Branching.
/* 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://gd.tuwien.ac.at/languages/c/programming-bbrown/c_025.htm)
http://www.tutorialspoint.com/ansi_c/c_control_statements.htm (http://www.tutorialspoint.com/ansi_c/c_control_statements.htm)
-
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.
-
I want to use this on the 50$ robot tutorial because I am cheap. How would I do this? :-[