Skip to content

Instantly share code, notes, and snippets.

@chtzvt
Last active April 21, 2017 20:08
Show Gist options
  • Save chtzvt/d577d73b83f59ba55a8b3732bfa80e14 to your computer and use it in GitHub Desktop.
Save chtzvt/d577d73b83f59ba55a8b3732bfa80e14 to your computer and use it in GitHub Desktop.
/* Documentation:
* Assuming an Arduino Mega with the Adafruit Motor Shield and a
* stepper motor with a 1.8deg resolution connected to M1 and M2
* Your IDE should have the AFMotor library installed before compilation.
* This software manages the movement of a dial connected to a stepper motor,
* for use in the demonstration of a caesar cipher (see https://en.wikipedia.org/wiki/Caesar_cipher)
* How movement is calculated:
* Steps per revolution: (360/motorResolution) + optional stepOffset
* Steps to move: (stepsPerRevolution/dialSections) * shiftAmount
* Examples:
* Clock: dialSections = 4
* Alphabet: dialSections = 26
* Where:
* - motorResolution is the degree value the motor itself moves per
* step (usually written on the motor somewhere),
* - motorRPM is the desired speed of the motor, in revolutions/min,
* - motorPort is the port the stepper is connected (default 1 for outputs
* M1 and M2),
* - dialSections is the number of sections on the dial which have an
* equal width in degrees,
* - shiftAmount is the number of letters to shift the cipher by (e.g. the
* number of full letters to move the dial by.
* - stepOffset - If your motor has some sort of abnormalities, use this for
* fine-tuning.
*/
#include <AFMotor.h>
float motorResolution = 1.8;
int motorRPM = 20;
int motorPort = 1;
float dialSections = 26;
int shiftAmount = 1;
float stepOffset = 0;
float stepsPerRevolution = (360/motorResolution) + stepOffset;
AF_Stepper dial(stepsPerRevolution, motorPort);
void setup() {
dial.setSpeed(motorRPM);
moveDial(shiftAmount);
}
void loop() {}
int moveDial(int shift){
dial.step((stepsPerRevolution/dialSections) * shift, FORWARD, SINGLE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment