Iron shield

Voici un petit montage DIY pour un cosplay d’IronMan.

Il est inspiré de celui du site adafruit, auquel j’ai rajouté un bouton afin de changer les modes de marche.

J’ai donc utilisé un trinket 5v, et un anneau NeoPixel afin de réaliser l’anneau lumineux d’IronMan.

ironshield

Au menu plusieurs modes de pulsation, un défilement de leds, et des dégradés.

Le programme est inspiré de plusieurs bouts code utilisant le neopixel, je les ai intégrés et adaptés au trinket.

Code Source

//Superhero Power Plant
//fades all pixels subtly
//code by Tony Sherwood for Adafruit Industries
 
#include <Adafruit_NeoPixel.h>
 
#define PIN 0
const int buttonPin = 1;     // the number of the pushbutton pin
 
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(26, PIN, NEO_GRB + NEO_KHZ800);
 
int alpha; // Current value of the pixels
int dir = 1; // Direction of the pixels... 1 = getting brighter, 0 = getting dimmer
int flip; // Randomly flip the direction every once in a while
int minAlpha = 25; // Min value of brightness
int maxAlpha = 100; // Max value of brightness
int alphaDelta = 5; // Delta of brightness between times through the loop

short redVal;
short blueVal;
short greenVal;
short fade;

int index =0;
int numLoop=0;
int buttonState = 0;         // variable for reading the pushbutton status
int mode =0;
int dep=0;
long time=0;

void setup() {
  pinMode(buttonPin, INPUT);   
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
 
void loop() 
{
  buttonState = digitalRead(buttonPin);
  
  if(buttonState==HIGH)
  {
    delay(500);
    mode++;
    if (mode >5)
    {
      mode=0;
    }
   }
    switch (mode) 
    {
    case 0:
      
      // Change the line below to alter the color of the lights
      // The numbers represent the Red, Green, and Blue values
      // of the lights, as a value between 0(off) and 1(max brightness)
      //
      // EX:
      // colorWipe(strip.Color(alpha, 0, alpha/2)); // Pink
      alpha=getAlpha();
      colorWipe(strip.Color(0, 0, alpha)); // Blue
      
      break;
    case 1:
     alpha=getAlpha();
      colorWipe(strip.Color(alpha, alpha, alpha)); // White
      
      
      break;
      
       case 2:
        alpha=getAlpha();
        for(uint16_t i=0; i<strip.numPixels(); i++) {
          strip.setPixelColor(i,alpha,0,0 );
          strip.show();
        }
       
       
      
      break;
      
       case 3:
          colorSwipe(0,0,100);
      
      break;
      
      case 4:
        rainbowCycle();
      
      break;
      
      case 5:
          rainbow();
      break;
      
    default: 
      // if nothing else matches, do the default
      // default is optional
      break;
  }
      
      
}
 
// Fill the dots one after the other with a color
void colorWipe(uint32_t c) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
  }
}
  
  int getAlpha()
  {
      flip = random(32);
      if(flip > 20) 
      {
        dir = 1 - dir;
      }
      
      // Some example procedures showing how to display to the pixels:
      if (dir == 1) {
        alpha += alphaDelta;
      }
      if (dir == 0) {
        alpha -= alphaDelta;
      }
      if (alpha < minAlpha) {
        alpha = minAlpha;
        dir = 1;
      }
      if (alpha > maxAlpha) {
        alpha = maxAlpha;
        dir = 0;
      }
      return alpha;
}

// Fill the dots one after the other with a color
void colorSwipe(int red, int green, int blue) {
  int charge []={5,20,50,70,100,70,50,20,5};
  for(uint16_t i=0; i<strip.numPixels()-1; i++) {
    if(i>=index && i<index+9)
    {
      strip.setPixelColor(i, 0, 0, charge[i-index]);       
      //strip.setPixelColor(i, red/charge[index+i], green/charge[index+i], blue/charge[index+i]);
    }     
    else
    {
      strip.setPixelColor(i,0,0,0);
    }
    if(index>17) dep=index-17;
    else dep=0;  
    if(i<dep && dep!=0)
    {
      strip.setPixelColor(i, 0, 0, charge[(i+index)%9]);       
      //strip.setPixelColor(i, red/charge[index+i], green/charge[index+i], blue/charge[index+i]);
    }  
  }
  strip.setPixelColor(25, 0, 0, getAlpha()); 

  if(millis()-time>50)
      {
      index=(index+1)%25;
      time=millis();
      }
  
      //numLoop=(numLoop+1)%30000;
      strip.show();
      
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} 
else if(WheelPos < 170) {
      WheelPos -= 85;
      return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} 
else {
        WheelPos -= 170;
        return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
       }
}
  
void rainbow() {
uint16_t i, j;
 
for(j=0; j<256; j=j) {
  for(i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, Wheel((i+j) & 255));
  }
  strip.show();
  if(millis()-time>30)
        {
          time=millis();
          j++;
        }
        if(digitalRead(buttonPin)==HIGH) j=256;
  }
}

void rainbowCycle() {
uint16_t i, j;
 
for(j=0; j<256; j++) { // 5 cycles of all colors on wheel
  for(i=0; i< strip.numPixels(); i++) {
  strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  }
  strip.show();
    if(millis()-time>30)
        {
          time=millis();
          j++;
        }
        if(digitalRead(buttonPin)==HIGH) j=256;
  }
  
}
Ironshield