2009年4月22日 星期三

Smart Car API for smart tiles



int switchPin = 2; // switch input
int motor1go = 3; // H-bridge leg 1 (pin 2, 1A)
int motor1back = 4; // H-bridge leg 2 (pin 7, 2A)
int motor2go = 5; // H-bridge leg 1 (pin 2, 1A)
int motor2back = 6; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9; // H-bridge enable pin
int ledPin = 13; // LED

void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);

// set all the other pins you're using as outputs:
pinMode(motor1go, OUTPUT);
pinMode(motor1back, OUTPUT);
pinMode(motor2go, OUTPUT);
pinMode(motor2back, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);

// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);

// blink the LED 3 times. This should happen only once.
// if you see the LED blink three times, it means that the module
// reset itself,. probably because the motor caused a brownout
// or a short.
blink(ledPin, 3, 100);
}

void loop() {
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1go, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor1back, HIGH); // set leg 2 of the H-bridge high

digitalWrite(motor2go, HIGH); // set leg 1 of the H-bridge low
digitalWrite(motor2back, LOW); // set leg 2 of the H-bridge high

}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1go, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor1back, LOW ); // set leg 2 of the H-bridge low

digitalWrite(motor2go, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2back, HIGH); // set leg 2 of the H-bridge high
}
}

/*
blinks an LED
*/
void blink(int whatPin, int howManyTimes, int milliSecs) {
int i = 0;
for ( i = 0; i < howManyTimes; i++) {
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}

References
ITP Physical Computing
Tamiya 70068 Wall-Hugging Mouse Kit
http://letsmakerobots.com/node/2278
http://akashxav.com/2009/04/18/arduino-l293d-dc-motor/

沒有留言: