Saturday, December 13, 2014

Arduino RGB strip

Knight Rider ...

keep track of misc arduino snippets here.

Get yourself some RGB LED light strip like this one.
These are WS2812B type of chip are in a 5050 package including controller to control every single "pixel" i.e. the RGB LED on the strip and set each color individually.

I always wanted one of those :)
Shipping was from Hong-Kong to Germany a pretty reasonable amount of time (less then three weeks).

So what to do? you start with some examples you'll find on the web.
Good starting point is this page. As this page explains there's basically two major arduino libs for controlling this kind of strip the Neopixel one from Adafruit and FastLed.


From there I took another example code found here:

This gives you some "knight rider" effect.

I broaden the moving lights a bit and added some code to control the speed via a simple poti.


#include "FastLED.h"
//#include <OneSheeld.h>

// How many leds are in the strip?
#define NUM_LEDS 60
// Data pin that led data will be written out over
#define DATA_PIN 6
// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

const int poti = A1;
const int button8 = 8;


// This function sets up the ledsand tells the controller about them
void setup() {
  delay(3000);

  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

}

int duration;
void loop() {
  int potiVal = analogRead(poti);
  float voltage = potiVal * (20.0 / 1023.0);
  int duration = (int)voltage;

  byte hue = 8; // some red

  larsonScanner(NUM_LEDS, hue, 255, duration);

}

void larsonScanner(byte ledcount, byte hue, byte sat, byte duration) {
  static unsigned long previousMillis = millis();
  static int i; // Schrittzaehler
  static int led = 0;
  static boolean reverse = false;
  byte dim;

  if(i > 8) {
    i = 0;

    if (reverse) led--;
    else led++;

    // Anfang erreicht
    if (led < 0) {
      led = 0;
      reverse =! reverse;
    }

    // Ende erreicht
    if (led >= ledcount) {
      led = ledcount-1;
      reverse =! reverse;
    }
  }

  if (millis()-previousMillis > duration) {
    previousMillis = millis();
    memset(leds, 0, ledcount*3); // LED Array zuruecksetzen
    dim = i*4;
    i++;

    // Aktuelle Led-4
    if (led-5>=0 && !reverse) {
      leds[led-5] = CHSV(hue, sat, 32-dim); // FadeDown
    }
    else if (led-5>=0 && reverse) {
      leds[led-5] = CHSV(hue, sat, dim); // FadeUp
    }

    // Aktuelle Led-3
    if (led-4>=0 && !reverse) {
      leds[led-4] = CHSV(hue, sat, 64-dim); // FadeDown
    }
    else if (led-4>=0 && reverse) {
      leds[led-4] = CHSV(hue, sat, 32+dim); // FadeUp
    }

    // Aktuelle Led-2
    if (led-3>=0 && !reverse) {
      leds[led-3] = CHSV(hue, sat, 128-dim); // FadeDown
    }
    else if (led-3>=0 && reverse) {
      leds[led-3] = CHSV(hue, sat, 64+dim); // FadeUp
    }

    // Aktuelle Led-1
    if (led-2>=0 && !reverse) {
      leds[led-2] = CHSV(hue, sat, 255-dim); // FadeDown
    }
    else if (led-2>=0 && reverse) {
      leds[led-2] = CHSV(hue, sat, 127+dim); // FadeUp
    }

    // Aktuelle Led
    leds[led-1] = CHSV(hue, sat, 255);
    leds[led] = CHSV(hue, sat, 255);
    leds[led+1] = CHSV(hue, sat, 255);

    // Aktuelle Led+1
    if (led+2<ledcount && !reverse) {
      leds[led+2] = CHSV(hue, sat, 127+dim); // FadeUp
    }
    else if (led+2<ledcount && reverse) {
      leds[led+2] = CHSV(hue, sat, 255-dim); // FadeDown
    }

    // Aktuelle Led+2
    if (led+3<ledcount && !reverse) {
      leds[led+3] = CHSV(hue, sat, 64+dim); // FadeUp
    }
    else if (led+3<ledcount && reverse) {
      leds[led+3] = CHSV(hue, sat, 128-dim); // FadeDown
    }

    // Aktuelle Led+3
    if (led+4<ledcount && !reverse) {
      leds[led+4] = CHSV(hue, sat, 32+dim); // FadeUp
    }
    else if (led+4<ledcount && reverse) {
      leds[led+4] = CHSV(hue, sat, 64-dim); // FadeDown
    }

    // Aktuelle Led+4
    if (led+5<ledcount && !reverse) {
      leds[led+5] = CHSV(hue, sat, dim); // FadeUp
    }
    else if (led+5<ledcount && reverse) {
      leds[led+5] = CHSV(hue, sat, 32-dim); // FadeDown
    }

    FastLED.show();
  }
}


--

The potentiometer is connected to analog 1 and the digital in of the light strip in dio6.


Sunday, November 30, 2014

strava node.js api

How to get shoe mileage on the command line


make sure you have node.js and node package manager (npm) installed.
$ sudo apt-get install node.js npm

then follow this quickstart instructions  from here: https://github.com/ajsnapshots/node-strava-v3

get strava developer api access from here
access your application info and settings here: https://app.strava.com/settings/api
the api doc is at: http://strava.github.io/api/

now you should be able to run the example program from the quickstart guide.


you directory structure should look something like this:


user@host:~/Programming/node.js/strava$ tree -L 2
.
├── data
│   └── strava_config
├── node_modules
│   └── strava-v3
└── shoes.js

3 directories, 2 files

now edit the shoes.js file and put in this code:
user@host:~/Programming/node.js/strava$ cat shoes.js 

var strava = require('strava-v3');

strava.athlete.get({},function(err,payload) {
    if(!err) {          
for (var i=0; i<payload.shoes.length; i++) {
   console.log(payload.shoes[i].name + " - " +
payload.shoes[i].distance/1000 + " km");
}

    }   
    else {
console.log("ERROR:");
        console.log(err);
    }
});

if you run it you should get a listing of your active shoes from strava together with the mileage (here in km)

$ node shoes.js 
Salomon Fellraiser - 481.693 km
Inov-8 Roclite 315 - 383.47 km
Brooks T6 Racer - 401.136 km
Inov-8 Trailroc 255 - 656.62 km
Saucony Fastwitch 5 ( blue ) - 463.049 km
Barefoot (No Shoes) - 5.219 km
Aldi "barfuss" ?? blau - 136.115 km
Saucony Peregrine 3.0 - 479.282 km
Hoka One One Mafata 2 - 1119.308 km
New Balance Minimum Trail 00 - 403.544 km
Inov-8 Trailrc 245 - 338.791 km
Vibram FF EL-X - 46.919 km


that's it for now.

Sunday, February 9, 2014