Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 30th, 2013  |  syntax: None  |  size: 4.95 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // led strip postition to ultrasonic distance  
  2.  
  3. int SDI = 2; //Red wire (not the red 5V wire!)
  4. int CKI = 3; //Green wire
  5. // red 5+ / blue GND
  6.  
  7. int ledPin = 13; //On board LED
  8.  
  9. #define ECHOPIN 6
  10. #define TRIGPIN 7
  11.  
  12. #define STRIP_LENGTH 32 //32 LEDs on this strip
  13. long view_buffer[STRIP_LENGTH];
  14.  
  15. int echo_timer = 0;
  16. float distance;
  17.  
  18. typedef struct _pixel {
  19.         int bri;
  20.         int col;
  21.         int dir;
  22.         uint32_t color;
  23. };
  24. _pixel pixel[32];
  25.  
  26. int app_timer = 0;
  27. int serial_on = 0;
  28.  
  29. void setup() {
  30.         if (serial_on == 1) {
  31.                 Serial.begin(115200);
  32.         }
  33.         init_pin();    
  34.         init_echo();
  35.         clear_buffer();
  36.         randomSeed(analogRead(0));
  37. }
  38.  
  39. void init_pin() {
  40.         pinMode(SDI, OUTPUT);
  41.         pinMode(CKI, OUTPUT);
  42.         pinMode(ledPin, OUTPUT);
  43. }
  44.  
  45. void init_echo() {
  46.   pinMode(ECHOPIN, INPUT);
  47.   pinMode(TRIGPIN, OUTPUT);
  48. }
  49.  
  50. void loop() {
  51. //      works_cycle();
  52. //      winkle_cycle();
  53.         loop_echo();
  54.         pixel_render();
  55. //      trailer_cycle();
  56. //      yellow_cycle();
  57.         //loop_test();
  58.         post_frame();
  59. }
  60.  
  61. void loop_echo() {
  62.         echo_timer++;
  63.         if (echo_timer == 2) {
  64.                 echo_timer = 0;
  65.                 digitalWrite(TRIGPIN, LOW);
  66.                 delayMicroseconds(2);
  67.                 digitalWrite(TRIGPIN, HIGH);
  68.                 delayMicroseconds(10);
  69.                 digitalWrite(TRIGPIN, LOW);
  70.                 distance = pulseIn(ECHOPIN, HIGH);
  71.                 distance = distance / 58;
  72.                 //if (serial_on == 1) {
  73.                 //      Serial.println(distance);
  74.                 //}
  75.         }
  76. }
  77.  
  78. void loop_test() {
  79.   //Pre-fill the color array with known values
  80.   view_buffer[0] = 0xFF0000; //Bright Red
  81.   view_buffer[1] = 0x00FF00; //Bright Green
  82.   view_buffer[2] = 0x0000FF; //Bright Blue
  83.   view_buffer[3] = 0x010000; //Faint red
  84.   view_buffer[4] = 0x800000; //1/2 red (0x80 = 128 out of 256)
  85.   post_frame(); //Push the current color frame to the strip
  86.  
  87.   delay(2000);
  88.  
  89.   while(1){ //Do nothing
  90.     addRandom();
  91.     post_frame(); //Push the current color frame to the strip
  92.  
  93.     digitalWrite(ledPin, HIGH);   // set the LED on
  94.     delay(25);                  // wait for a second
  95.     digitalWrite(ledPin, LOW);    // set the LED off
  96.     delay(25);                  // wait for a second
  97.   }
  98. }
  99.  
  100. //Throws random colors down the strip array
  101. void addRandom(void) {
  102.   int x;
  103.  
  104.   //First, shuffle all the current colors down one spot on the strip
  105.   for(x = (STRIP_LENGTH - 1) ; x > 0 ; x--)
  106.     view_buffer[x] = view_buffer[x - 1];
  107.    
  108.   //Now form a new RGB color
  109.   long new_color = 0;
  110.   for(x = 0 ; x < 3 ; x++){
  111.     new_color <<= 8;
  112.     new_color |= random(0xFF); //Give me a number from 0 to 0xFF
  113.     //new_color &= 0xFFFFF0; //Force the random number to just the upper brightness levels. It sort of works.
  114.   }
  115.  
  116.   view_buffer[0] = new_color; //Add the new random color to the strip
  117.  
  118.   view_buffer[0] = 16;
  119. }
  120.  
  121. //Takes the current strip color array and pushes it out
  122. void post_frame (void) {
  123.   //Each LED requires 24 bits of data
  124.   //MSB: R7, R6, R5..., G7, G6..., B7, B6... B0
  125.   //Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
  126.   //Pulling the clock low for 500us or more causes the IC to post the data.
  127.  
  128.   for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) {
  129.     long this_led_color = view_buffer[LED_number]; //24 bits of color data
  130.  
  131.     for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
  132.       //Feed color bit 23 first (red data MSB)
  133.      
  134.       digitalWrite(CKI, LOW); //Only change data when clock is low
  135.      
  136.       long mask = 1L << color_bit;
  137.       //The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.
  138.      
  139.       if(this_led_color & mask)
  140.         digitalWrite(SDI, HIGH);
  141.       else
  142.         digitalWrite(SDI, LOW);
  143.  
  144.       digitalWrite(CKI, HIGH); //Data is latched when clock goes high
  145.     }
  146.   }
  147.  
  148.   //Pull clock low to put strip into reset/post mode
  149.   digitalWrite(CKI, LOW);
  150.   delayMicroseconds(500); //Wait for 500us to go into reset
  151. }
  152.  
  153. void clear_buffer() {
  154.         memset(view_buffer, 0, 32);
  155. }
  156.  
  157. void shift_pixel(int dir) {
  158.         int e;
  159.  
  160.         for (uint16_t i = 0; i < STRIP_LENGTH; i++) {
  161.                 e = i + dir;
  162.                 if (e > -1 && e < STRIP_LENGTH) {
  163.                         //view_buffer[i] = view_buffer[e];
  164.                         pixel[i] = pixel[e];
  165.                 }
  166.         }
  167. }
  168.  
  169. uint32_t Color(byte r, byte g, byte b) {
  170.         uint32_t c;
  171.         c = r;
  172.         c <<= 8;
  173.         c |= g;
  174.         c <<= 8;
  175.         c |= b;
  176.         return c;
  177. }
  178.  
  179. void pixel_render() {
  180.         int bri;
  181.         int echo;
  182. //      float a;
  183.        
  184.         echo = distance;
  185.         if (echo > STRIP_LENGTH * 2) {
  186.                 echo = STRIP_LENGTH * 2;
  187.         } else if (echo < 0) {
  188.                 echo = 0;
  189.         } else {
  190.         };
  191.         echo = (STRIP_LENGTH * 2) - echo;
  192. //      echo = echo;
  193.         if (serial_on == 1) {
  194.                 Serial.println(echo);
  195.         }
  196.         for (uint16_t i = 0; i < STRIP_LENGTH; i++) {
  197.         //      a = (i / 2) + (millis() / 250.0);
  198. //              bri = sin(a) * 127.0;
  199. //              bri = sin(a) * echo;
  200. //              bri = echo;
  201.         //      bri =  echo;
  202.         //      view_buffer[i] = Color(bri, bri, 0);
  203.                 if (i * 2 < echo) {
  204. //                      view_buffer[i] = Color(echo, 0, 0);
  205.                         if (i % 2 == 0) {
  206.                                 view_buffer[i] = Color(0, echo * 2, 0);
  207.                         } else {
  208.                                 view_buffer[i] = Color(0, 0, echo * 2);
  209.                         }
  210.                 } else {
  211.                         view_buffer[i] = Color(0, 0, 0);
  212.                 }
  213.         }
  214. }
clone this paste RAW Paste Data