Skip to content

Instantly share code, notes, and snippets.

@corinnas
Created October 13, 2010 00:56
Show Gist options
  • Save corinnas/623208 to your computer and use it in GitHub Desktop.
Save corinnas/623208 to your computer and use it in GitHub Desktop.
/*
* Mechanisms and Movement
*
* A device that uses two forms of mechanical movement,
* driven by a DC motor:
* it translates rotary motion (DC motor)
* to a rocking motion (4-bar linkage)
* to intermittent motion (dowel on pivot)
*
*
* Created by Corinna Sherman
* October 10, 2010
*/
int enableSwitchPin = 8; // enable switch input pin to turn device ON/OFF
int enablePin1 = 9; // H-bridge enable pin 1 (1,2EN)
int motorPin1 = 3; // motor 1 logic pin 1 on H-bridge (1A)
int motorPin2 = 5; // motor 1 logic pin 2 on H-bridge (2A)
void setup() {
// Initialize serial communication.
Serial.begin(9600);
// Set enable switch pin and touch sensor pins as input pins.
pinMode(enableSwitchPin, INPUT);
// Set other pins as output pins.
pinMode(enablePin1, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Go forward by default.
goForward();
}
void loop() {
// If the switch is on, the motor turns in one direction.
if (digitalRead(enableSwitchPin) == HIGH) {
// Set enablePins to HIGH to turn on both motors.
Serial.println("go");
enable();
}
else {
Serial.println("stop");
disable();
}
}
void enable() {
// Set enablePins to HIGH to turn on both motors.
digitalWrite(enablePin1, HIGH);
}
void disable() {
// Set enablePins to LOW to turn off both motors.
digitalWrite(enablePin1, LOW);
}
void goForward() {
// Set motor's direction
digitalWrite(motorPin1, LOW); // Set H-bridge 1A pin to low
digitalWrite(motorPin2, HIGH); // Set H-bridge 2A pin to high
}
void goBackward() {
// Set motor's direction
digitalWrite(motorPin1, HIGH); // Set H-bridge 1A pin to high
digitalWrite(motorPin2, LOW); // Set H-bridge 2A pin to low
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment