// led strip postition to ultrasonic distance int SDI = 2; //Red wire (not the red 5V wire!) int CKI = 3; //Green wire // red 5+ / blue GND int ledPin = 13; //On board LED #define ECHOPIN 6 #define TRIGPIN 7 #define STRIP_LENGTH 32 //32 LEDs on this strip long view_buffer[STRIP_LENGTH]; int echo_timer = 0; float distance; typedef struct _pixel { int bri; int col; int dir; uint32_t color; }; _pixel pixel[32]; int app_timer = 0; int serial_on = 0; void setup() { if (serial_on == 1) { Serial.begin(115200); } init_pin(); init_echo(); clear_buffer(); randomSeed(analogRead(0)); } void init_pin() { pinMode(SDI, OUTPUT); pinMode(CKI, OUTPUT); pinMode(ledPin, OUTPUT); } void init_echo() { pinMode(ECHOPIN, INPUT); pinMode(TRIGPIN, OUTPUT); } void loop() { // works_cycle(); // winkle_cycle(); loop_echo(); pixel_render(); // trailer_cycle(); // yellow_cycle(); //loop_test(); post_frame(); } void loop_echo() { echo_timer++; if (echo_timer == 2) { echo_timer = 0; digitalWrite(TRIGPIN, LOW); delayMicroseconds(2); digitalWrite(TRIGPIN, HIGH); delayMicroseconds(10); digitalWrite(TRIGPIN, LOW); distance = pulseIn(ECHOPIN, HIGH); distance = distance / 58; //if (serial_on == 1) { // Serial.println(distance); //} } } void loop_test() { //Pre-fill the color array with known values view_buffer[0] = 0xFF0000; //Bright Red view_buffer[1] = 0x00FF00; //Bright Green view_buffer[2] = 0x0000FF; //Bright Blue view_buffer[3] = 0x010000; //Faint red view_buffer[4] = 0x800000; //1/2 red (0x80 = 128 out of 256) post_frame(); //Push the current color frame to the strip delay(2000); while(1){ //Do nothing addRandom(); post_frame(); //Push the current color frame to the strip digitalWrite(ledPin, HIGH); // set the LED on delay(25); // wait for a second digitalWrite(ledPin, LOW); // set the LED off delay(25); // wait for a second } } //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--) view_buffer[x] = view_buffer[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. } view_buffer[0] = new_color; //Add the new random color to the strip view_buffer[0] = 16; } //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 = view_buffer[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 } void clear_buffer() { memset(view_buffer, 0, 32); } void shift_pixel(int dir) { int e; for (uint16_t i = 0; i < STRIP_LENGTH; i++) { e = i + dir; if (e > -1 && e < STRIP_LENGTH) { //view_buffer[i] = view_buffer[e]; pixel[i] = pixel[e]; } } } uint32_t Color(byte r, byte g, byte b) { uint32_t c; c = r; c <<= 8; c |= g; c <<= 8; c |= b; return c; } void pixel_render() { int bri; int echo; // float a; echo = distance; if (echo > STRIP_LENGTH * 2) { echo = STRIP_LENGTH * 2; } else if (echo < 0) { echo = 0; } else { }; echo = (STRIP_LENGTH * 2) - echo; // echo = echo; if (serial_on == 1) { Serial.println(echo); } for (uint16_t i = 0; i < STRIP_LENGTH; i++) { // a = (i / 2) + (millis() / 250.0); // bri = sin(a) * 127.0; // bri = sin(a) * echo; // bri = echo; // bri = echo; // view_buffer[i] = Color(bri, bri, 0); if (i * 2 < echo) { // view_buffer[i] = Color(echo, 0, 0); if (i % 2 == 0) { view_buffer[i] = Color(0, echo * 2, 0); } else { view_buffer[i] = Color(0, 0, echo * 2); } } else { view_buffer[i] = Color(0, 0, 0); } } }