Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Daniel Pok
Created October 20, 2015

A Light Drawing Machine

It's a drawing machine that draws in colored lights.

IntermediateShowcase (no instructions)369
A Light Drawing Machine

Things used in this project

Story

Read more

Schematics

longexposure.jpg

Code

lightstepper.ino

C/C++
const int grey = 4;
const int green = 3;
const int yellow = 2;
const int red = 1;
const int DELAY = 6;
const unsigned long MILLI_PER_REV = 1230 * 1.75;
const double DEG_PER_MILLI = 360.0 / (double) MILLI_PER_REV;
const double MILLI_PER_DEG = (double) MILLI_PER_REV / 360.0;
int direction = 1;

int currentState;
unsigned long switchTime;
unsigned long startTime;
unsigned long stopTime;

// Rainbow
int pattern1[12] = {1,1,1,1,1,1,0,0,0,0,0,0};
int pattern2[12] = {0,0,1,1,1,1,1,1,0,0,0,0};
int pattern3[12] = {0,0,0,0,0,0,1,1,1,1,1,1};

// Discrete
//int pattern1[12] = {1,1,0,0,0,0,0,0,1,1,1,1};
//int pattern2[12] = {0,0,1,1,0,0,1,1,1,1,0,0};
//int pattern3[12] = {0,0,0,0,1,1,1,1,0,0,1,1};

// Flashes
//int pattern1[12] = {1,1,0,0,0,0,0,0,0,0,0,0};
//int pattern2[12] = {0,0,0,0,1,1,0,0,0,0,0,0};
//int pattern3[12] = {0,0,0,0,0,0,0,0,1,1,0,0};

void setup()  {
  pinMode(grey, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  digitalWrite(13, LOW);
  currentState = 0;
  moveToState();
  switchTime = millis();
  startTime = millis();
  stopTime = startTime + MILLI_PER_REV;
}

void loop() {
  unsigned long now = millis();
  if (now >= stopTime){
    if (direction == 1){
      direction = -1;
      startTime = now;
      stopTime = now + MILLI_PER_REV;
      switchTime = now;
      currentState = 0;
      moveToState();
    } else {
      direction = 1;
      startTime = now;
      stopTime = now + MILLI_PER_REV;
      switchTime = now;
      currentState = 0;
      moveToState();
    }
    return;
  } else if (now >= switchTime){
    currentState += direction + 4;
    currentState %= 4;
    moveToState();
    switchTime = now + DELAY;
  }
  getPatternValue();
}

void moveToState(){
  switch(currentState){
    case 0:
      digitalWrite(grey, LOW);
      digitalWrite(green, HIGH);
      digitalWrite(yellow, LOW);
      digitalWrite(red, HIGH);
      break;
    case 1:
       digitalWrite(grey, LOW);
       digitalWrite(green, HIGH);
        digitalWrite(yellow, HIGH);
        digitalWrite(red, LOW);
        break;
     case 2:
       digitalWrite(grey, HIGH);
       digitalWrite(green, LOW);
       digitalWrite(yellow, HIGH);
       digitalWrite(red, LOW);
       break;
    case 3:
      digitalWrite(grey, HIGH);
       digitalWrite(green, LOW);
      digitalWrite(yellow, LOW);
      digitalWrite(red, HIGH);
      break;
    default:
      digitalWrite(grey, LOW);
       digitalWrite(green, LOW);
      digitalWrite(yellow, LOW);
      digitalWrite(red, LOW);
  }
}

 double getDegree(){
    return (millis() - startTime) * DEG_PER_MILLI;
 }

 double getPatternValue(){ 
    double deg = getDegree();
    int bucket = (int)(deg / 30.0) % 12;
    digitalWrite(13, (pattern1[bucket] == 1 ? HIGH : LOW));
    digitalWrite(12, (pattern2[bucket] == 1 ? HIGH : LOW));
    digitalWrite(11, (pattern3[bucket] == 1 ? HIGH : LOW));
 }

Credits

Daniel Pok

Daniel Pok

6 projects • 1 follower

Comments