Output Devices

The last assignment before starting with our final project was to develop a program that uses an output device. To complete this we had to connect an output devices to one of our Fab Lab made microcontroller board, upload the program and show that the output device works.An Output device is any peripheral, such as a speaker, monitor, and printer, among most other devices, that can be plugged in to your computer. The term refers to any equipment that captures data from your machine and converts it into a new medium.

RGB NeoPixel LED Strip

For this assignment I choose to use a Adafruit RGB NeoPixel LED Strip Sensor. The LED's are digitally-addressable. You can set the color of each LED's red, green and blue (or even mix colors), and you can change also the brightness. Only 1 digital out pin is required to send data. The PWM is built into each LED-chip so once you set the color you can stop talking to the strip and it will continue to PWM all the LEDs for you.

Creating the code

Below you can see the code I created. Everytime you push the button, the emergency light will turn off and the trafic light will turn on. Moreover it writes a debug message to the serial connection.

#include <Adafruit_NeoPixel.h>  // Neopixel Library to use the LED Strip
#ifdef __AVR__
#include <avr/power.h>
#endif


#define PIN            PB3     // The pin that is connected to the ATtiny45
#define Button         4       // I connected the Button on the ATtiny45 Pin 3 
int state = 0;                 // int to be able to switch between the collors by using the button multiple times
int buttonstate;               // int that reads the value of the Button

// How many NeoPixels are attached to the Board
#define NUMPIXELS      24

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
Adafruit_NeoPixel Strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code
  // initialize digital BUTTON pins as an input
  pinMode(Button, INPUT);
  // This initializes the NeoPixel library.
  Strip.begin(); 
  Strip.show();
  // start function rainbow with a time between the color changes of 50 millisec. 
  rainbow(50);

}

void loop() {
// reads the button pin and saves the value in buttonState:
  buttonstate = digitalRead(Button);
 
  if ( buttonstate == 0 && state == 2) {
     state = 0;
     delay(500); 
  } else if ( buttonstate == 0 && state == 0) {
    state = 1;
    delay(500);
  } else if ( buttonstate == 0 && state == 1) {
    state = 2;
    delay(500);
  }

  if ( state == 0 ) {
     
     
     // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    Strip.setPixelColor(i, Strip.Color(255,0,0)); // Moderately bright red color.

    Strip.show(); // This sends the updated pixel color to the hardware.
    
     }
  } else if ( state == 1) {
    for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    Strip.setPixelColor(i, Strip.Color(0,0,255)); // Moderately bright blue color.

    Strip.show(); // This sends the updated pixel color to the hardware.

     }
  } else if ( state == 2 ) {
     for(int i=0;i<NUMPIXELS;i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    Strip.setPixelColor(i, Strip.Color(0,255,0)); // Moderately bright green color.

    Strip.show(); // This sends the updated pixel color to the hardware.

     }

  }
  
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<Strip.numPixels(); i++) {
      Strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    Strip.show();
    delay(wait);
  }
}

// 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) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return Strip.Color(255 - WheelPos * 3, 0, WheelPos * 3,0);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return Strip.Color(0, WheelPos * 3, 255 - WheelPos * 3,0);
  }
  WheelPos -= 170;
  return Strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0,0);
}

uint8_t red(uint32_t c) {
  return (c >> 16);
}
uint8_t green(uint32_t c) {
  return (c >> 8);
}
uint8_t blue(uint32_t c) {
  return (c);
}
					

Before starting to connect my FabLab board to the arduino, I uploaded the ArduinoISP code onto the Arduino Uno. To communicate through the Arduino Software with the ATtiny45 board, I had to check the pinout of the ATtiny45. As you can see below pin 7 on the ATtiny45 is addressed by using pin number 2 in the Arduino Software.

Now I started to connect the Arduion with my board to upload the code.

After I uploaded the code, I attached the strip to the actual pin and voila!

You can do a lot of stuff with these LED's, because you are able to adress each single one of them and make them act how ever you like. In another project I created a wordclock using these LED's and each one of them was assigned to a specific number or letter. Below you can see a picture of my wordclock.