/* Final Project for Making Things Interactive Carnegie Mellon University Fall 2010 by Paulo Coelho */ // Info about the TLC chip here: http://www.arduino.cc/playground/Learning/TLC5940 #include "Tlc5940.h" // Pins that control the servo motor #define motorPin1 4 #define motorPin2 5 #define motorPin3 6 #define motorPin4 7 // Statics to help direct the motor #define FORWARD 0 #define BACKWARD 1 // Stepper motor finetuning #define STEPPER_SPEED 733 #define MAX_CYCLES 60 // Handy little helper variables, general usage int i = 0; int j = 0; // array to draw. This is the where the information is // so that the software can then do its magic to display // it using the hardware byte image[40][32] ={{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; byte current40 = 0; byte current32 = 0; int inByte; void setup(){ // Define the pinmodes for the motor pinMode(motorPin1,OUTPUT); pinMode(motorPin2,OUTPUT); pinMode(motorPin3,OUTPUT); pinMode(motorPin4,OUTPUT); // Init the TLC stuff Tlc.init(); // Serial connect, just for debug Serial.begin(9600); } void loop(){ if (Serial.available() > 0) { inByte = Serial.read(); image[current40][current32] = inByte; current32++; if(current32>31){ current32 = 0; current40++; displayNumber(current40); } if(current40>39){ current32 = 0; current40 = 0; printFullArray(); } } } ///////// FUNCTION GROUP : TLC ///////// // The TLCs were not properly mounted. This function corrects that. int swtlc(int i){ if(i<0) return -1; else if(i<=15) return i+16; else if(i>31) return -1; else if(i>15) return i-16; } void turnOffAllLights(){ for (int i = 0; i < NUM_TLCS * 16; i++) { Tlc.set(i, 0); } Tlc.update(); } ///////// FUNCTION GROUP : STEPPER MOTOR ///////// void nsequence(int dir, int n, int t){ if(dir==FORWARD){ for(int j=0 ; j 0; j--) { Tlc.set(swtlc(j), random(2)*1000); } Tlc.update(); } shutdown(); turnOffAllLights(); delay(10000); for(i=0 ; i<40 ; i++){ nsequence(BACKWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); for (int j = (NUM_TLCS * 16)-1; j > 0; j--) { Tlc.set(swtlc(j), random(2)*1000); } Tlc.update(); } turnOffAllLights(); delay(10000); } void fullPower(){ for(i=0 ; i<40 ; i++){ nsequence(FORWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); for (int j = 0; j < NUM_TLCS * 16; j++) { Tlc.set(swtlc(j), 1000); } Tlc.update(); } shutdown(); turnOffAllLights(); delay(10000); for(i=0 ; i<40 ; i++){ nsequence(BACKWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); for (int j = 0; j < NUM_TLCS * 16; j++) { Tlc.set(j, 1000); } Tlc.update(); } turnOffAllLights(); delay(10000); } void printFullArray(){ for(i=0 ; i<40 ; i++){ nsequence(FORWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); for (int j = (NUM_TLCS * 16)-1; j > 0; j--) { if(image[i][j]<50){ Tlc.set(swtlc(-(j-31)),4000); }else if(image[i][j]<100){ Tlc.set(swtlc(-(j-31)),300); }else{ Tlc.set(swtlc(-(j-31)),0); } } Tlc.update(); } delay(500); for(i=39 ; i>-1 ; i--){ nsequence(BACKWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); for (int j = (NUM_TLCS * 16)-1; j > 0; j--) { if(image[i][j]<50){ Tlc.set(swtlc(-(j-31)),4000); }else if(image[i][j]<100){ Tlc.set(swtlc(-(j-31)),300); }else{ Tlc.set(swtlc(-(j-31)),0); } } Tlc.update(); } shutdown(); turnOffAllLights(); turnOffAllLights(); turnOffAllLights(); turnOffAllLights(); } void dumpArray(){ for(i=0 ; i<40 ; i++){ nsequence(FORWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); for (int j = (NUM_TLCS * 16)-1; j > 0; j--) { Tlc.set(swtlc(-(j-31)), image[i][j]*1000); } Tlc.update(); } shutdown(); delay(10000); for(i=0 ; i<40 ; i++){ nsequence(BACKWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); for (int j = (NUM_TLCS * 16)-1; j > 0; j--) { Tlc.set(swtlc(j), image[i][j]*1000); } Tlc.update(); } delay(10000); } void testStepper(){ for(i=0 ; i<40 ; i++){ nsequence(FORWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); delay(2); } shutdown(); delay(100); for(i=0 ; i<40 ; i++){ nsequence(BACKWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); delay(2); } delay(10000); } void testLights(){ for (int i = 0; i < NUM_TLCS * 16; i++) { Tlc.set(swtlc(i), 1000); Tlc.update(); delay(1); Tlc.set(swtlc(i), 400); Tlc.set(swtlc(i-1), 150); Tlc.set(swtlc(i-2), 50); Tlc.set(swtlc(i-3), 25); Tlc.set(swtlc(i-4), 0); Tlc.update(); //delay(1); } for (int i = (NUM_TLCS * 16)-1 ; i > 0; i--) { Tlc.set(swtlc(i), 1000); Tlc.update(); delay(1); Tlc.set(swtlc(i), 400); Tlc.set(swtlc(i+1), 150); Tlc.set(swtlc(i+2), 50); Tlc.set(swtlc(i+3), 25); Tlc.set(swtlc(i+4), 0); Tlc.update(); //delay(1); } /* for(int k = 0; k < 100 ; k++){ for (int j = 0; j < NUM_TLCS * 16; j++) { Tlc.set(j, random(2,10)*70); Tlc.update(); delay(1); } }*/ } void testSync(){ int activeLED = 0; int previousLED = -1; for(i=0 ; i<40 ; i++){ nsequence(FORWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); Tlc.set(swtlc(activeLED), 1000); Tlc.set(swtlc(previousLED), 0); activeLED++; previousLED++; if(activeLED > 32) activeLED = 0; if(previousLED > 32) previousLED = -1; Tlc.update(); } shutdown(); delay(100); for(i=0 ; i<40 ; i++){ nsequence(BACKWARD,MAX_CYCLES,STEPPER_SPEED); shutdown(); Tlc.set(swtlc(activeLED), 1000); Tlc.set(swtlc(previousLED), 0); activeLED++; previousLED++; if(activeLED > 32) activeLED = 0; if(previousLED > 32) previousLED = -1; Tlc.update(); } delay(10000); }