Skip to content

Instantly share code, notes, and snippets.

@kellyegan
Created October 8, 2014 12:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kellyegan/759160b77aa87860dec1 to your computer and use it in GitHub Desktop.
Save kellyegan/759160b77aa87860dec1 to your computer and use it in GitHub Desktop.
3D Zoetrope Arduino Code
// Zoetrope
// Copyright 2014 Kelly Egan
//
// 3D Strobescopic Zoetrope for Makerfaire 2014
#define BUTTON_PIN 12 //Button to turn zoetrope on and off
#define POT_PIN 0 //Potentiometer used to set speed of motor
#define INDICATOR_PIN 13 //Indicator LED
#define HALL_PIN 3 //Hall effect sensor to determine motor speed
#define STROBE_PIN 5 //High-power LED used as strobe
#define MOTOR_PIN 6 //Motor control for spinning zoetrope
//Variables used in the interrupt function should be marked volatile
volatile unsigned long rotationStartTime;
volatile unsigned long rotationDuration;
volatile unsigned long rotations;
volatile boolean strobeState;
int potValue;
boolean buttonState, lastButtonState; //Button state and software debounce
unsigned long lastDebounce;
boolean zoetropeOn;
unsigned long lastFrameTime;
unsigned long frameDuration;
float rpm;
int numFrames = 12;
void setup() {
pinMode(STROBE_PIN, OUTPUT);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(INDICATOR_PIN, OUTPUT);
pinMode(HALL_PIN, INPUT);
digitalWrite(STROBE_PIN, LOW);
digitalWrite(MOTOR_PIN, LOW);
attachInterrupt(0, newRotation, FALLING); //Interrupt 0 is on pin 3, Hall Effect sensor
rotationStartTime = millis();
rotationDuration = 5000;
Serial.begin(9600);
zoetropeOn = true;
buttonState = false;
lastButtonState = false;
}
void loop() {
buttonState = digitalRead( BUTTON_PIN );
if( buttonState != lastButtonState ) {
if( buttonState == HIGH )
zoetropeOn = !zoetropeOn;
}
lastButtonState = buttonState;
//Checks for divide by zero
rpm = rotationDuration > 0 ? 60000 / rotationDuration : 0;
frameDuration = rotationDuration / numFrames;
if( zoetropeOn ) {
potValue = analogRead(POT_PIN);
analogWrite( MOTOR_PIN, potValue / 4 );
//If the time ellapsed since the last strobe is
//greater than the frameduraciton turn on the strobe
if( micros() - lastFrameTime > frameDuration ) {
lastFrameTime = micros();
digitalWrite( STROBE_PIN, HIGH );
delay( 1 ); //Strobe is on for millisecond (1/1000) of a second
digitalWrite( STROBE_PIN, LOW );
}
} else {
//If zoetrope off turn off motor
analogWrite( MOTOR_PIN, 0 );
}
}
// Hall Effect Interupt function
void newRotation() {
//Wait for first rotation before making measurement
if(rotations > 0) {
rotationDuration = millis() - rotationStartTime;
}
rotationStartTime = millis();
rotations++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment