Commit 5ca24c4d authored by Steffen Weber's avatar Steffen Weber

Added ino file

parents
/*
Nathan Seidle
SparkFun Electronics 2011
This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
Controlling an LED strip with individually controllable RGB LEDs. This stuff is awesome.
The SparkFun (individually controllable) RGB strip contains a bunch of WS2801 ICs. These
are controlled over a simple data and clock setup. The WS2801 is really cool! Each IC has its
own internal clock so that it can do all the PWM for that specific LED for you. Each IC
requires 24 bits of 'greyscale' data. This means you can have 256 levels of red, 256 of blue,
and 256 levels of green for each RGB LED. REALLY granular.
To control the strip, you clock in data continually. Each IC automatically passes the data onto
the next IC. Once you pause for more than 500us, each IC 'posts' or begins to output the color data
you just clocked in. So, clock in (24bits * 32LEDs = ) 768 bits, then pause for 500us. Then
repeat if you wish to display something new.
This example code will display bright red, green, and blue, then 'trickle' random colors down
the LED strip.
You will need to connect 5V/Gnd from the Arduino (USB power seems to be sufficient).
For the data pins, please pay attention to the arrow printed on the strip. You will need to connect to
the end that is the begining of the arrows (data connection)--->
If you have a 4-pin connection:
Blue = 5V
Red = SDI
Green = CKI
Black = GND
If you have a split 5-pin connection:
2-pin Red+Black = 5V/GND
Green = CKI
Red = SDI
*/
#include <SD.h>
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// Buttons on analog in:
int nothing_pressed = 1023;
int up = 141;
int down = 325;
int left = 502;
int right = 0;
int select = 740;
int SDI = 2; //Red wire (not the red 5V wire!)
int CKI = 3; //Green wire
// Colors
long RED = 0x800000;
// SD
const int chipSelect = 10;
//Sd2Card card;
//SdVolume volume;
//SdFile root;
File img;
File root;
int state = 0;
bool textureMode = false;
bool pixelMode = false;
#define STRIP_LENGTH 64 //32 LEDs on this strip
#define LONG_SIZE 4
long strip_colors[STRIP_LENGTH];
void setup() {
// serial port
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("initialize!");
lcd.display();
// Clear out the array
memset(strip_colors, 0, STRIP_LENGTH * LONG_SIZE);
// setup SD-Card
pinMode(SDI, OUTPUT);
pinMode(CKI, OUTPUT);
pinMode(10, OUTPUT); // SD
Serial.print("\nInitializing SD card...");
setState(1);
//Init
if (SD.begin(chipSelect)) {
Serial.println("Wiring is correct and a card is present.");
setState(2);
}
}
void loop() {
int delayMs = 100;
delay(2000);
lcd.setCursor(0, 0); // top left
lcd.print("select file");
delay(1000);
root = SD.open("/");
img = root.openNextFile();
int imgIndex = 0;
bool fileSelected = false;
while(!fileSelected)
{
int sensorValue = analogRead(A0);
if (abs(sensorValue - select) < 10)
fileSelected = true;
if (
(abs(sensorValue - up) < 10)
||
(abs(sensorValue - down) < 10)
)
{
delay(200);
img = root.openNextFile();
imgIndex++;
}
//img = SD.open("default.out");
if (!img)
{
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.print("end of list");
lcd.setCursor(0, 1); // bottom left
lcd.print("to start...");
delay(1000);
// no more files
root.rewindDirectory();
img = root.openNextFile();
imgIndex = 0;
}
delay(50);
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.print("select file");
lcd.setCursor(0, 1); // bottom left
lcd.print(String(imgIndex, DEC) + ": " + img.name());
}
setState(3);
lcd.setCursor(0, 0); // top left
lcd.print("select time!");
delay(300);
bool timeSelected = false;
while(!timeSelected)
{
int sensorValue = analogRead(A0);
if (abs(sensorValue - select) < 10)
timeSelected = true;
if (abs(sensorValue - right) < 10)
delayMs += 5;
if (abs(sensorValue - up) < 10)
delayMs += 10;
if (abs(sensorValue - left) < 10)
delayMs -= 5;
if (abs(sensorValue - down) < 10)
delayMs -= 10;
if (delayMs < 5)
delayMs = 5;
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.print("select time!");
lcd.setCursor(0, 1); // bottom left
lcd.print(String(String(delayMs, DEC) + "ms"));
delay(250);
}
delay(300);
// Texture MEnu
bool textureSelected = false;
while(!textureSelected)
{
int sensorValue = analogRead(A0);
if (abs(sensorValue - select) < 10)
textureSelected = true;
if (abs(sensorValue - up) < 10)
textureMode = true;
if (abs(sensorValue - down) < 10)
textureMode = false;
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.print("texture mode?");
lcd.setCursor(0, 1); // bottom left
if (textureMode)
lcd.print("YES");
else
lcd.print("NO");
delay(250);
}
// Pixel Menu
bool pixelModeSelected = false;
while(!pixelModeSelected)
{
int sensorValue = analogRead(A0);
if (abs(sensorValue - select) < 10)
pixelModeSelected = true;
if (abs(sensorValue - up) < 10)
pixelMode = true;
if (abs(sensorValue - down) < 10)
pixelMode = false;
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.print("pixel mode?");
lcd.setCursor(0, 1); // bottom left
if (pixelMode)
lcd.print("YES");
else
lcd.print("NO");
delay(250);
}
setState(4);
lcd.clear();
lcd.print("OK");
int x;
for(x = 8; x >= 0 ; x--)
{
setState(x);
lcd.clear();
lcd.setCursor(0, 0); // top left
lcd.print(String("starting in " + String(x, DEC)));
/*
if (x == 1)
lcd.noDisplay();
*/
}
lcd.clear();
lcd.print("GO");
while(1){ //Do nothing
//addRandom();
readFromFile();
post_frame(); //Push the current color frame to the strip
if (pixelMode) {
delay(delayMs/2); // wait for a second
memset(strip_colors, 0, STRIP_LENGTH * LONG_SIZE);
post_frame();
delay(delayMs/2);
}
else {
delay(delayMs);
}
}
}
void readFromFile(void) {
if (img) {
int x;
for(x = (STRIP_LENGTH - 1) ; x >= 0 ; x--) {
if(img.available()) {
strip_colors[x] = (img.read() * 0x010000) + (img.read() * 0x000100) + (img.read() * 0x000001);
}
else
if (textureMode)
img.seek(0);
else
strip_colors[x] = 0;
}
}
}
void setState(int newState) {
state = newState;
int x;
for(x = (STRIP_LENGTH - 1) ; x >= 0 ; x--) {
strip_colors[x] = 0x000000;
}
for(x = 0; x < state ; x++) {
strip_colors[x] = RED;
}
post_frame();
delay(999);
}
//Throws random colors down the strip array
void addRandom(void) {
int x;
//First, shuffle all the current colors down one spot on the strip
for(x = (STRIP_LENGTH - 1) ; x > 0 ; x--)
strip_colors[x] = strip_colors[x - 1];
//Now form a new RGB color
long new_color = 0;
for(x = 0 ; x < 3 ; x++){
new_color <<= 8;
new_color |= random(0xFF); //Give me a number from 0 to 0xFF
//new_color &= 0xFFFFF0; //Force the random number to just the upper brightness levels. It sort of works.
}
strip_colors[0] = new_color; //Add the new random color to the strip
}
//Takes the current strip color array and pushes it out
void post_frame (void) {
//Each LED requires 24 bits of data
//MSB: R7, R6, R5..., G7, G6..., B7, B6... B0
//Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
//Pulling the clock low for 500us or more causes the IC to post the data.
for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) {
long this_led_color = strip_colors[LED_number]; //24 bits of color data
for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
//Feed color bit 23 first (red data MSB)
digitalWrite(CKI, LOW); //Only change data when clock is low
long mask = 1L << color_bit;
//The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.
if(this_led_color & mask)
digitalWrite(SDI, HIGH);
else
digitalWrite(SDI, LOW);
digitalWrite(CKI, HIGH); //Data is latched when clock goes high
}
}
//Pull clock low to put strip into reset/post mode
digitalWrite(CKI, LOW);
delayMicroseconds(500); //Wait for 500us to go into reset
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment