Close

Arduino Code

A project log for Animal Interaction

What if you could experience and interact with the world like animals do.

Drap & DesignDrap & Design 08/20/2014 at 22:530 Comments

Below is the code we used to make the Aninteractions jacket work. We started working with the code for the Florabrella (https://learn.adafruit.com/florabrella/code) found on the Adafruit website, and modified and removed this to make it suitable for our use. We also had to download the Adafruit libraries:

Color sensor library: https://github.com/adafruit/Adafruit_TCS34725

Neopixel library: https://learn.adafruit.com/adafruit-neopixel-uberguide

Basically the sensors perform continuous readings and send these to the LED-strips. If the readings indicate "black" or as good as pitch dark, the LED-strips fade out. If the sensors are in mid-air or sense an "ambient" reading, the previous reading (color or off) is maintained until either a color or darkness is picked up again.

The code uses one main sketch with three function sketches. Not really necessary, but we started doing it this way to have a better overview and flexibility (bear in mind, we are designers, not programmers).


--------------------------------------------------------------------------------------------------------------------------------

Main Code:

--------------------------------------------------------------------------------------------------------------------------------

#include <Wire.h>

#include "Adafruit_TCS34725.h"

#include <Adafruit_NeoPixel.h>

#define PINA 6

#define PINB 5

#define TPIXELA 80 //The total amount of pixel's/led's in your connected strip/stick (Default is 60)

#define TPIXELB 80 //The total amount of pixel's/led's in your connected strip/stick (Default is 60)

//int switchPin = 10; // switch is connected to pin 10

//int val; // variable for reading the pin status

//int val2;

//int buttonState; // variable to hold the button state

//int lightMode = 0; // how many times the button has been pressed

Adafruit_NeoPixel strip_a = Adafruit_NeoPixel(TPIXELA, PINA, NEO_GRB + NEO_KHZ800);

Adafruit_NeoPixel strip_b = Adafruit_NeoPixel(TPIXELB, PINB, NEO_GRB + NEO_KHZ800);

// our RGB -> eye-recognized gamma color

byte gammatable[256];

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup() {

}

void loop(){

Serial.begin(9600); // Set up serial communication at 9600bps

//pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input

pinMode(PINA, OUTPUT);

pinMode(PINB, OUTPUT);

//strip.setBrightness(200); //adjust brightness here

//buttonState = digitalRead(switchPin); // read the initial state

strip_a.begin();

strip_a.show(); // Initialize all pixels to 'off'

strip_b.begin();

strip_b.show(); // Initialize all pixels to 'off'

if (tcs.begin()) {

Serial.println("Found sensor");

} else {

Serial.println("No TCS34725 found ... check your connections");

while (1); // halt!

}

// thanks PhilB for this gamma table!

// it helps convert RGB colors to what humans see

for (int i=0; i<256; i++) {

float x = i;

x /= 255;

x = pow(x, 2.5);

x *= 255;

gammatable[i] = x;

//Serial.println(gammatable[i]);

}

uint16_t clear, red, green, blue;

tcs.setInterrupt(false); // turn on LED

delay(200); // takes 50ms to read

tcs.getRawData(&red, &green, &blue, &clear);

tcs.setInterrupt(true); // turn off LED

Serial.print("C:\t"); Serial.print(clear);

Serial.print("\tR:\t"); Serial.print(red);

Serial.print("\tG:\t"); Serial.print(green);

Serial.print("\tB:\t"); Serial.print(blue);

// Figure out some basic hex code for visualization

uint32_t sum = red;

sum += green;

sum += blue;

sum = clear;

float r, g, b;

r = red; r /= sum;

g = green; g /= sum;

b = blue; b /= sum;

r *= 256; g *= 256; b *= 256;

Serial.print("\t");

Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);

Serial.println();

//Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" "); Serial.println((int)b );

//colorWipe(strip.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]), 0);

if(clear <= 150){

function1();

}

if(clear <= 1000){

function2();

}

else{

function3();

Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" "); Serial.println((int)b );

colorWipe(strip_a.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]), 0),(strip_b.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]), 0);

}

}

// Fill the dots one after the other with a color

void colorWipe(uint32_t c, uint8_t wait) {

for(uint16_t i=0; i<strip_a.numPixels(), i<strip_b.numPixels(); i++) {

strip_a.setPixelColor(i, c),strip_b.setPixelColor(i, c);

strip_a.show(),strip_b.show();

delay(30);

}

}


--------------------------------------------------------------------------------------------------------------------------------

function1 (Fade out when sensor reading is "black"):

--------------------------------------------------------------------------------------------------------------------------------

void function1(){

int R = 0;

int G = 0;

int B = 0;

int foutCount=5;

int waitT = 5;

//Fade Out

while(1){ //using an inf loop to be more custom.

//Protect the strand from higher then 255 values

if(R>255 || G>255 || B>255) { break; } //DO NOT DELETE OR ALTER THIS LINE.

//break the inf loop if the color is off

if (R<0 && G<0 && B<0) {

//ReSet the RGB to 0 values.

R=0;

G=0;

B=0;

break;

}

//update the strip

for(int j=0; j<strip_a.numPixels(); j++) {

strip_a.setPixelColor(j, strip_a.Color(R, G, B)),strip_b.setPixelColor(j, strip_a.Color(R, G, B));

strip_a.show(),strip_b.show();

delay(30);

}

//Decrease by the set amount

R=R-foutCount;

G=G-foutCount;

B=B-foutCount;

delay(waitT);

}

}


--------------------------------------------------------------------------------------------------------------------------------

function2 (Keep last color when sensor reading is ambient / mid air):

--------------------------------------------------------------------------------------------------------------------------------

void function2(){

uint32_t color; strip_a.getPixelColor(0),strip_b.getPixelColor(0);

}


--------------------------------------------------------------------------------------------------------------------------------

function3 (Use sensor reading to change color):

--------------------------------------------------------------------------------------------------------------------------------

void function3(){

strip_a.setBrightness(255); //adjust brightness here

strip_b.setBrightness(255); //adjust brightness here

}

Discussions