3D Turn-o-trope – Arduino Code

[<– previous section]

0. Introduction

1. Basic Operation

2. The Conversion Overview

3. The Conversion Details

4. Main Components

5. Schematics

6. Arduino Code

/*
zoetrope controller

to do:
  - implement auto calibration mode for optical cartridge

v3.0  2017-7-23 include diagnostics
V2.11 2017-5-25 clean up button code (test for max & min)
V2.1  2017-5-23 added workaround for malfunctioning A2 digital in
V2.0  2017-5-20 replaced mode button by toggle switch, simplified code
V0.2  2017-5-16 added red/blue mode indicators
V0.1  2017-5-16 initial

inputs:
  (A0) strobeFrequencyPot
  (A1) cartSensor - connect to A1 & +5V and 10k 
resistor A1 & GND
  (A2) modeButton

outputs:
  (5) redLED = mode 0, syncStrobe (variable)
  (6) blueLED = mode 1, variableStrobe (variable)
  (8) strobeRelay (on/off)
  (9) cartLED (variable)
*/

// user can play with these for fine tuning

// optical cartridge settings
int cartLedBrightness = 200;    // cartridge LED brightness

int sensorThresholdLow = 450;   // sensor at black spot

int sensorThresholdHigh = 500;  // sensor at white spot

int modeLedBrightness = 160;  // mode indicator LED brightness

int strobeHoldTime = 5;      // how long the strobe signal is ON

//------------------------------------------

// set pin numbers inputs

const byte potPin = 0;      // (A0) reads strobe frequency pot

const byte lightSensor = 1;  // (A1) cartridge light sensor

const byte buttonPin = 2;   // (2) number for modeSwitch

// outputs

const byte mode0LedPin = 5;  // (5) syncStrobe mode LED, red

const byte mode1LedPin = 6; // (6) variableStrobe mode LED, blue

const byte strobePin = 8;   // (8) to relay triggering the strobe

const byte ledPin = 9;      // (9)) variable power to cartridge LED

int photocellReading;

int buttonReading = 0;

// other inits and defaults

byte buttonState = 0;

byte mode = 0;  // modes: 0=syncStrobe, 1=variableStrobe

byte cycleFlag = 0;  // used in syncStrobe

// for rpm calculation in mode 1

int frameRangeMin = 10;

int frameRangeMax = 24;

int frameRange = frameRangeMax - frameRangeMin;

int potRangeMin = 0;

int potRangeMax = 1023;

int timeSlice = potRangeMax/frameRange;

int frameFrequency;

int frequencyPot;


void setup()

{

  pinMode(ledPin, OUTPUT);

  pinMode(mode0LedPin, OUTPUT);

  pinMode(mode1LedPin, OUTPUT);

  pinMode(strobePin, OUTPUT);

  

  pinMode(buttonPin, INPUT);   

  

  analogWrite(ledPin,cartLedBrightness);

  

  Serial.begin(9600);

}


void loop()

{

  /*

  buttonState = digitalRead(buttonPin);

  Serial.println(buttonState);

  */

  

  // workaround for arduino not recognizing DIGITAL INPUT

  buttonReading = analogRead(buttonPin);

  //Serial.println(buttonRead);

  

  if (buttonReading > 1000) {

    buttonState = HIGH;

  }

  

  if (buttonReading < 10) {

    buttonState = LOW;

  }

  

  

  

  if (buttonState == HIGH) {

    mode = 1;

    analogWrite(mode0LedPin,0);

    analogWrite(mode1LedPin,modeLedBrightness);

    analogWrite(ledPin,0);

  } else {

    mode = 0;

    analogWrite(mode0LedPin,modeLedBrightness);

    analogWrite(mode1LedPin,0);

    analogWrite(ledPin,cartLedBrightness);

  }

  // decide which operations mode we’re in 

  if (mode == 0) {

    // syncStrobe mode 

    

    photocellReading = analogRead(lightSensor);

    //Serial.println(photocellReading);

    if (photocellReading < sensorThresholdLow && cycleFlag == 0)

  {

    Serial.print(“photocellReading: “);

    Serial.println(photocellReading);

    Serial.println();

    

    

    digitalWrite(strobePin, HIGH);

    delay(strobeHoldTime);

    digitalWrite(strobePin, LOW);

    

    

    cycleFlag = 1;

  

  }

  

  if (photocellReading < sensorThresholdLow && cycleFlag == 1)

  {

    //DO NOTHING

  }

  

  if (photocellReading > sensorThresholdHigh && cycleFlag == 1)

  {

    cycleFlag = 0;

    Serial.print(“photocellReading: “);

    Serial.println(photocellReading);

    Serial.println();

    

  }

    

  } else {

    // variableStrobe mode

    frequencyPot = analogRead(potPin);

    

    frameFrequency = (frequencyPot/timeSlice)+frameRangeMin;  // calculate frequency from spin time

    

    Serial.println(frameFrequency);  // diagnostics

  

    // fire strobe

    digitalWrite(strobePin, HIGH);

    delay(strobeHoldTime);

    digitalWrite(strobePin, LOW);

    delay(1000/frameFrequency);

  }

  

}

[<– previous section]