Part 1: Digital I/O with LEDs
Instructions: Build a circuit with at least 2 digital inputs (use two momentary push-buttons, or try a few different kinds of switches). Add 2 standard LEDs as digital outputs, and be sure to include current limiting resistors for each LED. After you build the circuit, program your microcontroller so that each input or combination of inputs has a unique output. Record a video of this working.
For the circuit, the two inputs will be two push buttons and the two outputs will be 2 LEDs. One LED will be red and the other will be green. I assigned each button and LED to a port on the microcontroller and ground. I also added a pull-down resistor of 1k ohms for both buttons. For the LEDs, I gave both of them a 100-ohm resistor to prevent too much current from flowing through them. According to SparkFun, both green and red LEDs can have a forward voltage of 2V and a forward current of 20mA. With that, I got 150 ohms as the resistance and the closest resistor value I had to that was 100 ohms.
For my circuit, I wanted each LED to have its own button so that when you press the button, the assigned LED will turn on, and when you release the button, the LED will turn off. I wanted to spice up the code, so I then made it so that if you press both buttons and the same time, both LEDs will blink for half a second and be off for 1/5 of a second. Once you release both buttons, the blinking loop will then turn off.
I used Ardunio's example code of button (file -> examples -> digital -> button) to help me write my code for part one.
// delcaring ports
const int button1 = 2;
const int button2 = 3;
const int redLED = 4;
const int greenLED = 5;
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
// setting port pin modes
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
Serial.begin(9600);
}
void loop() {
//getting button state
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
//checking button state
if (buttonState1 == HIGH) { //button pressed
digitalWrite(redLED, HIGH); //turning on LED
Serial.println("Button 1 Pressed");
} else { //button not pressed
digitalWrite(redLED, LOW); //LED off
}
if (buttonState2 == HIGH) {
digitalWrite(greenLED, HIGH);
Serial.println("Button 2 Pressed");
} else {
digitalWrite(greenLED, LOW);
}
//blink
if(buttonState1 == HIGH && buttonState2 == HIGH) { //both buttons pressed
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(500);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
delay(200);
}
}
Part 2: Digital I/O with a Programmable LED Strip
Instructions: Build a circuit with at least 2 digital inputs (momentary push buttons, or maintained switches). Leave the switches but remove the LEDs from Part 1 and replace them with an LED strip. Connect wires to a Neopixel strip. Write a program where each button interaction (or sequence of button presses) controls a unique visual output on the LED strip. The interaction should be easy to understand with reassuring feedback.
For this part of the lab, I used the wheel example for the neo pixels and this YouTube video: https://www.youtube.com/watch?v=mliaJrkme2U&t=599s
I like the idea of having each pixel a different color, so I wanted to implement that into the loop. Within the example, I changed some colors, brightness, and timing of sequences.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 12
int numPixels = 5;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin(); //initializes the NeoPixel Library
strip.setBrightness(30);
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// Some example procedures showing how to display to the pixels:
colorWipe(strip.Color(100, 0, 100), 30);
colorWipe(strip.Color(0, 250, 200), 20);
colorWipe(strip.Color(120, 230, 0), 45);
theaterChase(strip.Color(127, 127, 127), 50);
theaterChase(strip.Color(127, 0, 0), 20);
theaterChase(strip.Color(0, 0, 127), 35);
oneTime(5000);
//time for each segment
rainbow(30);
}
void oneTime(uint32_t wait){
strip.setPixelColor(0, strip.Color(123, 231, 0));
strip.setPixelColor(1, strip.Color(0, 111, 243));
strip.setPixelColor(2, strip.Color(132, 0, 244));
strip.setPixelColor(3, strip.Color(0, 222, 0));
strip.setPixelColor(4, strip.Color(123, 231, 123));
strip.show();
delay(wait);
}
// 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.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
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);
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
// 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);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
Part 3: Soldered Breakout Boards
Instructions: Use two standard LEDs. You’ll be able to use this board as an output in future labs and/or projects. The arrangement of the LEDs is up to you (they can be either series or parallel), both must be illuminated when the output is HIGH. This board will have 2 wires coming from it – signal and ground. To use the board, connect – and the signal wire to a digital i/o pin on the microcontroller. Test your breakout board with your microcontroller and write an Arduino program where the input board (switch) will turn on an output (either a standard LED or Neopixel).
For my breakout board, I decided to do a button with a pulldown resistor of 10k ohms. I chose the button because I felt like I was going to use it more often in the future. After choosing my button, getting my 10k ohm resistor, and three wires, I began soldering. I only used one side of the button and connected one wire to one leg of the button which will connect to the 5V. Then, the middle wire is connected to the other leg of the button plus the resistor which will be connected to the microcontroller. Lastly, the third wire is only connected to the other end of the resistor which goes to ground. After I soldered, I connected it to my breadboard to make sure that it worked before I hot-glued the back to make sure that it wouldn't fall apart later. I don't have a picture of the breakout board before the hot glue went over it. But here is what it looks like as itself:
For the code, I just used the same code as part one but made it so that if you press the button, both lights would blink and when you let go of the button, then they would turn off.
// delcaring ports
const int button1 = 8;
const int redLED = 4;
const int greenLED = 5;
int buttonState1 = 0;
void setup() {
// setting port pin modes
pinMode(button1, INPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
Serial.begin(9600);
}
void loop() {
//getting button state
buttonState1 = digitalRead(button1);
//blink
if(buttonState1 == HIGH) { // button pressed
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(500);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
delay(200);
}
}