Code to translate to a Flora project.

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
searchone
 
Posts: 2
Joined: Thu Nov 21, 2013 2:13 am

Code to translate to a Flora project.

Post by searchone »

There is this code that i want to work with my Flora. My project is setup as the Ampli-tie project wiring (Mic at D9, LED Data at D6) can someone help me translate this to be used on the flora? Thanks so much in advance. After all my research, i still have a hard time understanding the language. Thanks.
Link: http://BANNED.com/XkpnK5Ph

or this is the code if that's preferable.

Code: Select all

#include <avr/io.h>
#include <util/delay.h>
#include <math.h>
#include "WS2811.h"
 
#define BIT(B)           (0x01 << (uint8_t)(B))
#define SET_BIT_HI(V, B) (V) |= (uint8_t)BIT(B)
#define SET_BIT_LO(V, B) (V) &= (uint8_t)~BIT(B)
 
#define PAUSE  1000     // msec
#define DELAY    10     // msec
#define CONST_E 2.71828
 
// Define the output function, using pin 0 on port b.
DEFINE_WS2811_FN(WS2811RGB, PORTB, 0)
 
void setup()
{
    // Configure pin for output.
    SET_BIT_HI(DDRB, 0);
    SET_BIT_LO(PORTB, 0);
//      Serial.begin(9600);
}
 
float   cR = 1,
                cG = 0,
                cB = 0;
 
// Drive the three pixels in an infinit loop.
void loop(void)
{
    // off->red, off->green, off->blue
    RGB_t rgb[240] = {
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
        {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}
        };
        const int Elements = ARRAYLEN(rgb);
 
    WS2811RGB(rgb, Elements);
    _delay_ms(PAUSE);
 
        int col = 0;
       
    // loop forever.
    for (;;) {
                for(int k=0; k<10; k++) {
                        for(int i = Elements - 1; i >= 1; i--)
                        {
                                rgb[i].r = rgb[i - 1].r;
                                rgb[i].g = rgb[i - 1].g;
                                rgb[i].b = rgb[i - 1].b;
                        }
                       
                        long micLevel = analogRead(0);
                        int log4Level = log(micLevel) / log(4) * 12.5;
                        int squareLevel = micLevel * micLevel * micLevel / ((long)4 * 1024 * 1024);
                        float sCurveLevel = (int)(sCurve(micLevel, 4) * 255);
                       
                        float fMicLevel = micLevel / 4.0f / 200.0f;
                        float quadraticLevel = pow(fMicLevel, 2);
                       
                        //Serial.println(squareLevel);
                       
                        const float one = 1.f;
                       
                        rgb[0].r = rgb[0].g = rgb[0].b = 0;
                       
                        setRgb(&(rgb[0]), one);
                       
                        //if(log4Level > squareLevel && log4Level > sCurveLevel)
                                //rgb[0].r = log4Level;
                        //else if(squareLevel > log4Level && squareLevel > sCurveLevel)
                                //rgb[0].g = quadraticLevel * 255;
                        //else if(sCurveLevel > log4Level && sCurveLevel > squareLevel)
                                //rgb[0].b = sCurveLevel;
                               
                        WS2811RGB(rgb, Elements);
                        //_delay_ms(5);
                        fadeRgb();
                }
               
                //_delay_ms(500);
               
                col = ++col % 6;
                //cycleRgb(col);
    }
}
 
const int phaseLength = 30;
const int period = phaseLength * 6;
int iteration = 0;
void fadeRgb() {
       
        // Determine which phase we are in
        int phase = iteration / phaseLength;
        int step = iteration % phaseLength;
       
        switch(phase)
        {
                case 0: // Red on, green increasing, blue off
                cR = 1;
                cG = step / ((float)phaseLength);
                cB = 0;
                break;
               
                case 1: // Red decreasing, green on, blue off
                cR = (phaseLength - step) / ((float)phaseLength);
                cG = 1;
                cB = 0;
                break;
               
                case 2: // Red off, green on, blue increasing
                cR = 0;
                cG = 1;
                cB = step / ((float)phaseLength);
                break;
               
                case 3: // Red off, green decreasing, blue on
                cR = 0;
                cG = (phaseLength - step) / ((float)phaseLength);
                cB = 1;
                break;
               
                case 4: // Red increasing, green off, blue on
                cR = step / ((float)phaseLength);
                cG = 0;
                cB = 1;
                break;
               
                case 5: // Red on, green off, blue decreasing
                cR = 1;
                cG = 0;
                cB = (phaseLength - step) / ((float)phaseLength);
                break;
        }
       
        iteration = ++iteration % period;
}
 
void cycleRgb(int col) {
        switch(col)
        {
                case 0:
                cR = 1;
                cG = 0;
                cB = 0;
                break;
               
                case 1:
                cR = 0.5;
                cG = 0.5;
                cB = 0;
                break;
               
                case 2:
                cR = 0;
                cG = 1;
                cB = 0;
                break;
               
                case 3:
                cR = 0;
                cG = 0.5;
                cB = 0.5;
                break;
               
                case 4:
                cR = 0;
                cG = 0;
                cB = 1;
                break;
               
                case 5:
                cR = 0.5;
                cG = 0;
                cB = 0.5;
                break;
        }
}
 
void setRgb(RGB_t* rgb, float val)
{
        if (val > 1.0f) val = 1.0f;
        rgb->r = (int)(val * cR * 255);
        rgb->g = (int)(val * cG * 255);
        rgb->b = (int)(val * cB * 255);
}
 
float sCurve(int micInput, float power)
{
        float normalised = micInput / 1024.0f;
       
        return normalised / (pow(1 + pow(normalised, power), 1.0f / power) );
}
Last edited by adafruit_support_rick on Sat Dec 21, 2013 11:53 am, edited 1 time in total.
Reason: please use Code tags when posting code

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”