Skip to content

Instantly share code, notes, and snippets.

@smanning29
Created November 13, 2019 16:11
Show Gist options
  • Save smanning29/f1258d26e9b873088048bed398c38212 to your computer and use it in GitHub Desktop.
Save smanning29/f1258d26e9b873088048bed398c38212 to your computer and use it in GitHub Desktop.
Object Lab 5 Motors
const int switchPin = 2; // switch input
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9; // H-bridge enable pin
const int potPin = 0; //potentiometer input
void setup() {
// put your setup code here, to run once:
// set the switch as an input:
pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
Serial.begin(9600);
}
void loop() {
int potVal = analogRead(potPin);
Serial.println(potVal);
// put your main code here, to run repeatedly:
// if the switch is high, motor will turn on one direction
if(digitalRead(switchPin) == HIGH){
analogWrite(motor2Pin, potVal);
digitalWrite(motor1Pin, LOW);
}
// else (which means the switch is low), motor will turn in the other direction
else{
analogWrite(motor1Pin, potVal);
digitalWrite(motor2Pin, LOW);
}
}
#include <Stepper.h>
const int stepsPerRevolution = 514; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment