Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ledcube.pde - Example sketch for controlling an LED cube.
- Created by Gamaiel Zavala (gzip), 2009-2012
- MIT License. See accompanying LICENSE file for terms.
- */
- #include <LedCube.h>
- #ifndef LedCube_h
- #define LedCube_h
- #if defined(ARDUINO) && ARDUINO >= 100
- #include "Arduino.h"
- #else
- #include "WProgram.h"
- #endif
- struct cubeFrame {
- unsigned int size;
- unsigned int delay;
- byte *sequence;
- };
- class LedCube
- {
- public:
- LedCube(byte size, byte levelPins[], byte colPins[]);
- ~LedCube();
- byte getCols(){ return cols; }
- byte getLevels(){ return levels; }
- byte getNumLights(){ return num; }
- void light(byte level, byte col, byte val);
- void lightOn(byte level, byte col);
- void lightOff(byte level, byte col);
- void lightPulse(byte level, byte col, unsigned int wait = 5);
- void lightSequence(byte seq[], byte length, unsigned int time = 5, byte gap = 1);
- void lightLevel(byte r, unsigned int wait = 50);
- void lightRow(byte r, byte level, unsigned int wait = 50);
- void lightPlane(byte r, unsigned int wait = 50);
- void lightColumn(byte col, unsigned int wait = 50);
- void lightDrop(byte col, unsigned int wait = 50);
- void lightPerimeter(byte level, byte rotations, unsigned int wait = 50);
- void randomLight(byte numLights, unsigned int wait = 50);
- void randomColumn(byte numColumns = 1, unsigned int wait = 50);
- void lightsOut(unsigned int wait = 5);
- cubeFrame* createFrame(byte sequence[], unsigned int length, unsigned int delay);
- void destroyFrame(cubeFrame* frame);
- void lightFrames(cubeFrame* frames[], unsigned int length);
- void enableBuffer(boolean enable = true);
- void invertBuffer(boolean invert = true);
- void clearBuffer();
- void fillBuffer();
- void drawBuffer(unsigned int wait = 1);
- byte getBufferAt(byte lv, byte col);
- private:
- byte levels;
- byte cols;
- byte num;
- byte **buffer;
- byte *colPins;
- byte *levelPins;
- boolean bufferEnabled;
- boolean bufferInverted;
- void setBuffer (byte val);
- };
- #endif
- #define SIZE 3
- #define COLS (SIZE*SIZE)
- byte levelPins[SIZE] = {
- 11,12,13};
- byte colPins[COLS] = {
- 2,3,4,5,6,7,8,9,10};
- LedCube cube(SIZE, levelPins, colPins);
- int Pot = A2;
- int potValue = 0;
- void setup (){
- pinMode (Pot,INPUT);
- }
- void loop (){
- potValue = analogRead(Pot);
- delay(10);
- if (potValue <= 5) {
- for(byte level=0; level<cube.getLevels(); level++)
- {
- for(byte col=0; col<cube.getCols(); col++)
- {
- cube.lightPulse(level, col, 100);
- }
- }
- }
- if (potValue >= 6 && potValue <= 100) {
- // light one level at a time, increasing speed each time
- for(byte d=25; d>2; d-=2)
- {
- for(byte l=1; l <= cube.getLevels(); l++)
- {
- cube.lightLevel(l, d);
- }
- }
- }
- if (potValue >= 101 && potValue <= 200) {
- // light each row on each level
- for(byte level=1; level<=cube.getLevels(); level++)
- {
- for(byte row=1; row<=cube.getLevels()*2; row++)
- {
- cube.lightRow(row, level);
- }
- }
- }
- if (potValue >= 201 && potValue <= 300) {
- // light each plane
- for(byte i=3; i; i--)
- {
- for(byte row=1; row<=cube.getLevels()*2; row++)
- {
- cube.lightPlane(row, 10*i);
- }
- }
- }
- if (potValue >= 301 && potValue <= 400) {
- // single random light at a time
- cube.randomLight(random(25,100),100);
- // random column drop
- for(byte x=0; x<=15; x++)
- {
- cube.lightDrop(random(0,cube.getCols()), random(50,150));
- }
- }
- if (potValue >= 401 && potValue <= 500) {
- // circle around cube at a random level
- for(byte x=0; x<=5; x++)
- {
- cube.lightPerimeter(random(0,cube.getLevels()), random(1,5), random(25,100));
- }
- }
- if (potValue >= 501 && potValue <= 600) {
- // light each face
- byte planes[] = {
- cube.getLevels()+1,cube.getLevels(),cube.getLevels()*2,1 };
- for(byte i=5; i; i--)
- {
- for(byte p=0; p<sizeof(planes); p++)
- {
- cube.lightPlane(planes[p], 5*i);
- }
- }
- }
- if (potValue >= 601 && potValue <= 700) {
- // random columns
- cube.randomColumn(25);
- }
- if (potValue >= 701 && potValue <= 800) {
- // turn off a single column randomly
- cube.enableBuffer();
- for(byte c=0; c<30; c++)
- {
- cube.fillBuffer();
- cube.invertBuffer();
- cube.randomColumn();
- cube.drawBuffer(7);
- }
- cube.enableBuffer(false);
- }
- if (potValue >= 801 && potValue <= 900) {
- // cols in and out
- for(byte c=1, d=0; c<=10; c++)
- {
- if(c%2 == 0)
- {
- for(d=0; d<20; d++)
- {
- cube.lightColumn(2,1);
- cube.lightColumn(4,1);
- cube.lightColumn(6,1);
- cube.lightColumn(8,1);
- }
- }
- else if(c%4 == 1)
- {
- for(d=0; d<30; d++)
- {
- cube.lightColumn(1,1);
- cube.lightColumn(3,1);
- cube.lightColumn(7,1);
- cube.lightColumn(9,1);
- }
- }
- else
- {
- for(d=0; d<70; d++)
- {
- cube.lightColumn(5,1);
- }
- }
- }
- }
- if (potValue >= 901 && potValue <= 950) {
- // diamond and box
- byte diamond[] = {
- 0,4, 1,1, 1,3, 1,4, 1,5, 1,7, 2,4 };
- byte box[] = {
- 2,0, 2,1, 2,2, 2,3, 2,5, 2,6, 2,7, 2,8,
- 1,0, 1,2, 1,6, 1,8,
- 0,0, 0,1, 0,2, 0,3, 0,5, 0,6, 0,7, 0,8
- };
- cube.lightSequence(box, sizeof(box), 200);
- cube.lightSequence(diamond, sizeof(diamond), 400);
- }
- if (potValue >= 951 && potValue <= 980) {
- // helicopter effect
- byte topSeq[8] = {
- 0,3,6,7,8,5,2,1 };
- byte botSeq[8] = {
- 8,5,2,1,0,3,6,7 };
- for(byte loops = 0, delay = 50; loops<=8; loops++)
- {
- for(byte s=0; s<8; s++)
- {
- byte seq[] = {
- 2,topSeq[s], 1,4, 0,botSeq[s] };
- cube.lightSequence(seq, sizeof(seq), delay);
- }
- if(loops < 5) delay-=10;
- else delay += 10;
- }
- }
- if (potValue >= 981&& potValue <= 1000) {
- // turn off one light at a time
- cube.enableBuffer();
- cube.fillBuffer();
- cube.drawBuffer(25);
- for(byte w=0, l, c, max = cube.getNumLights(); w<max; )
- {
- // lower bound is inclusive, upper is exclusive
- l = random(0, cube.getLevels());
- c = random(0, cube.getCols());
- if(cube.getBufferAt(l,c) == HIGH)
- {
- cube.lightOff(l,c);
- cube.drawBuffer(5);
- w++;
- }
- }
- }
- cube.enableBuffer(false);
- }
- .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement