My final project requires almost no output devices except for a servo motor. Instead, I'm planning on incorporating multiple input devices (sensors) into my design. It is for this reason that I gave myself an easier time for this assignment and spent the majority of my efforts on testing for my final project.
The stepper motor is an output device that I haven't worked with before. It provides absolute positioning like the servo, but in "steps" and not angle. I built a circuit that controls the stepper motor's speed and orientation with a potentiometer.
Here is the code I used for this.
/*
This code is based on the Stepper motor code that Tom Igoe wrote on 30 Nov. 2009
*/
#include <Stepper.h>
const int stepsPerRevolution = 200; // 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, 11, 10, 9, 8);
int stepCount = 0; // number of steps the motor has taken
int potPin = A0; // define potentiometer pin
void setup() {
// initialize the serial port:
Serial.begin(9600);
// setup potentiometer pin as input
pinMode(potPin, INPUT);
}
void loop() {
int motorDirection;
//read and transform potentiometer reading
int analogReading = analogRead(potPin);
int motorSpeed = map(analogReading, 0, 1023, -50, 50); // map the 10 bit analog reading to the speed of the motor (which will be the length of the delay)
if (motorSpeed > 0) { // get direction of the motor
motorDirection = -1;
}
else if (motorSpeed < 0) {
motorDirection = 1;
}
else {
motorDirection = 0;
}
// step one step in the specified direction.
myStepper.step(motorDirection);
// I have to tranform the speed a bit since the bigger the delay, the slower the speed
motorSpeed = 51 - abs(motorSpeed)
Serial.print("motor speed:");
Serial.println(motorSpeed);
// the delay determines the speed of the motor
delay(motorSpeed);
}
And here is the final result.