Skip to content

Instantly share code, notes, and snippets.

@Naozumi520
Forked from kellyegan/Zoetrope.ino
Created June 14, 2022 01:54
Show Gist options
  • Save Naozumi520/05d03807426bb1c6950d656c3c39a16d to your computer and use it in GitHub Desktop.
Save Naozumi520/05d03807426bb1c6950d656c3c39a16d 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