4/5/17

The assignment yesterday (4/4/17) was to program 4 LED’s flashing independently from each other with different fading effects. I’m currently stuck on only getting the first LED in the chain lit. We wrote some complicated (to me) code to control the timing instead of just using a simpler but less efficient delay(); function. I do not understand the workaround code at all and can only get one light to turn on (pin 5), but still feel like I’m writing this out properly. I’m about to check each light to see if I’m just experiencing a hardware malfunction… and back. All of the LED’s are functioning, so I’m going to reset my whole breadboard… and back! Ok so the breadboard must have not been set up right. I am now able to get all the lights on, but only with the delay(); function.

This is the key to my problems, I have to figure out how to control this section:

int rTime = millis();
if (rTime – redTime > 1000) {
if (toggle) {
digitalWrite(ledRed, HIGH);
toggle = false;
} else {
digitalWrite(ledRed, LOW);
toggle = true;
}
redTime = rTime;
}

This breadboard doesn’t have a proper ground wire bridging the gap. Derp!

4/9/17

I found out that my breadboard’s ground column is split in 2 sections, thats why most of my lights weren’t working, I wasn’t bridging the gap with ground wire! We went further into our code today, creating new functions with arrays to control multiple LEDs through PWM contacts on the Feather. There are 7 contacts that can accept PWM signals: 3, 5, 6, 9, 10, 11, 13.

Our new assignment was to run at least 6 flashing LEDs and one digital on/off LED in a fashion that represents a narrative. The confusing aspect to me was how to create more than one looping scene that kept accurate time of each scene in sequence. I woke up with the answer in my head Saturday morning,  I realized I had to create different variables that represented specific milliseconds since my timeline is being measured in millis(); I wrapped my forLoops in an if statement that checks the currentTime and activates each loop in order (tenSec, twentySec, ect). My code is really heavy and I know it can be simplified, so I’m looking forward to learning how to package this up neatly. This is the work in progress:

 

Here’s the final code! It’s a loose interpretation of someone channel surfing, so each scene is interrupted by a quick strobing flash before moving to the next scene.

unsigned long currentTime = 0;

unsigned long stage1 = 7000; //Writing the name of your variable literally can create bias and establish boundaries
unsigned long stage2 = 7500;
unsigned long stage3 = 15000;
unsigned long stage4 = 15500;
unsigned long stage5 = 20000;
unsigned long stage6 = 20500;
unsigned long stage7 = 23000;
unsigned long stage8 = 26000;
unsigned long stage9 = 26500;
unsigned long stage10 = 38000;
unsigned long stage11 = 42000;
//Digital ON/OFF variables
int green = A5;
bool toggle = true;
unsigned long greenTime = 0;

//PWM LED variables
const int numLeds = 7;
int pin [numLeds] = {3, 5, 6, 9, 10, 11, 13};
int brightness [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int fadeIn [numLeds] = {3, 2, 2, 1, 2, 2, 3};
int fadeOut [numLeds] = { -1, -2, -2, -3, -2, -2, -1};
bool fadeUp [numLeds] = {1, 1, 1, 1, 1, 1, 1};
unsigned long lastTime [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int flckrSpd [numLeds] = {6, 4, 2, 8, 2, 4, 6};

//PWM LED variables 2
int pin2 [numLeds] = {3, 5, 6, 9, 10, 11, 13};
int brightness2 [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int fadeIn2 [numLeds] = {10, 10, 10, 10, 10, 10, 10};
int fadeOut2 [numLeds] = { -1, -1, -2, -3, -4, -4, -4};
bool fadeUp2 [numLeds] = {1, 1, 1, 1, 1, 1, 1};
unsigned long lastTime2 [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int flckrSpd2 [numLeds] = {1, 2, 3, 4, 5, 5, 5};

//PWM LED variables 3
int pin3 [numLeds] = {3, 5, 6, 9, 10, 11, 13};
int brightness3 [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int fadeIn3 [numLeds] = {3, 3, 3, 3, 3, 3, 3};
int fadeOut3 [numLeds] = { -10, -1, -10, -1, -10, -1, -10};
bool fadeUp3 [numLeds] = {1, 1, 1, 1, 1, 1, 1};
unsigned long lastTime3 [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int flckrSpd3 [numLeds] = {30, 10, 30, 10, 30, 10, 30};

//PWM LED variables 4
int pin4 [numLeds] = {3, 5, 6, 9, 10, 11, 13};
int brightness4 [numLeds] = {1, 1, 1, 1, 1, 1, 1};
int fadeIn4 [numLeds] = {1, 1, 1, 1, 1, 1, 1};
int fadeOut4 [numLeds] = { -8, -3, -4, -5, -6, -7, -8};
bool fadeUp4 [numLeds] = {1, 1, 1, 1, 1, 1, 1};
unsigned long lastTime4 [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int flckrSpd4 [numLeds] = {10, 3, 1, 10, 1, 3, 10};

//PWM LED variables 5
int pin5 [numLeds] = {3, 5, 6, 9, 10, 11, 13};
int brightness5 [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int fadeIn5 [numLeds] = {1, 1, -1, 1, -1, 1, 1};
int fadeOut5 [numLeds] = { -5, -1, -2, -3, -2, -1, -5};
bool fadeUp5 [numLeds] = {1, 1, 1, 1, 1, 1, 1};
unsigned long lastTime5 [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int flckrSpd5 [numLeds] = {15, 15, 15, 15, 15, 15, 15};

//PWM LED variables 6
int pin6 [numLeds] = {3, 5, 6, 9, 10, 11, 13};
int brightness6 [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int fadeIn6 [numLeds] = {1, 1, 1, 1, 1, 1, 1};
int fadeOut6 [numLeds] = { -1, -1, -1, -1, -1, -1, -1};
bool fadeUp6 [numLeds] = {1, 1, 1, 1, 1, 1, 1};
unsigned long lastTime6 [numLeds] = {0, 0, 0, 0, 0, 0, 0};
int flckrSpd6 [numLeds] = {30, 30, 30, 30, 30, 30, 30};

void setup() {

pinMode(green, OUTPUT);

// for loop initializes each pin as an output:
for (int i = 0; i < numLeds; i++) {
pinMode(pin[i], OUTPUT);
}

for (int j = 0; j < numLeds; j++) {
pinMode(pin[j], OUTPUT);
}

for (int k = 0; k < numLeds; k++) {
pinMode(pin[k], OUTPUT);
}

for (int l = 0; l < numLeds; l++) {
pinMode(pin[l], OUTPUT);
}

for (int m = 0; m < numLeds; m++) {
pinMode(pin[m], OUTPUT);
}
}

void loop() {
currentTime = millis();
// Run stage 1
if (currentTime < stage1) {
for (int i = 0; i < numLeds; i++) {
fader(i);
}
blinker();
}
// Run stage 2
if ((currentTime > stage1) && (currentTime < stage2)) {
for (int j = 0; j < numLeds; j++) {
fader2(j);
}
blinker2();
}
// Run stage 3
if ((currentTime > stage2) && (currentTime < stage3)) {
for (int k = 0; k < numLeds; k++) {
fader3(k);
}
blinker3();
}
// Run stage 4
if ((currentTime > stage3) && (currentTime < stage4)) {
for (int l = 0; l < numLeds; l++) {
fader2(l);
}
blinker2();
}
// Run stage 5
if ((currentTime > stage4) && (currentTime < stage5)) {
for (int m = 0; m < numLeds; m++) {
fader4(m);
}
blinker4();
}

// Run stage 6
if ((currentTime > stage5) && (currentTime < stage6)) {
for (int m = 0; m < numLeds; m++) {
fader2(m);
}
blinker2();
}

// Run stage 7
if ((currentTime > stage6) && (currentTime < stage7)) {
for (int m = 0; m < numLeds; m++) {
fader5(m);
}
blinker5();
}

// Run stage 8
if ((currentTime > stage7) && (currentTime < stage8)) {
for (int m = 0; m < numLeds; m++) {
fader3(m);
}
blinker3();
}

// Run stage 9
if ((currentTime > stage8) && (currentTime < stage9)) {
for (int m = 0; m < numLeds; m++) {
fader2(m);
}
blinker2();
}

// Run stage 10
if ((currentTime > stage9) && (currentTime < stage10)) {
for (int m = 0; m < numLeds; m++) {
fader5(m);
}
blinker5();
}
// Run stage 11
if ((currentTime > stage10) && (currentTime < stage11)) {
for (int m = 0; m < numLeds; m++) {
fader6(m);
}
blinker2();
}
}
void fader(int LED) {
if (currentTime – lastTime[LED] > flckrSpd[LED]) {
analogWrite(pin[LED], brightness[LED]);
// change the brightness for next time through the loop:
if (fadeUp[LED]) {
brightness[LED] = brightness[LED] + fadeIn[LED];
} else {
brightness[LED] = brightness[LED] + fadeOut[LED];
}

// reverse the direction of the fading at the ends of the fade:
if (brightness[LED] <= 0) {
fadeUp[LED] = true;
}
else if (brightness[LED] >= 255) {
fadeUp[LED] = false;
}
lastTime[LED] = currentTime;
}
}

void fader2(int LED) {
if (currentTime – lastTime2[LED] > flckrSpd2[LED]) {
analogWrite(pin2[LED], brightness2[LED]);
// change the brightness for next time through the loop:
if (fadeUp2[LED]) {
brightness2[LED] = brightness2[LED] + fadeIn2[LED];
} else {
brightness2[LED] = brightness2[LED] + fadeOut2[LED];
}

// reverse the direction of the fading at the ends of the fade:
if (brightness2[LED] <= 0) {
fadeUp2[LED] = true;
}
else if (brightness2[LED] >= 60) {
fadeUp2[LED] = false;
}
lastTime2[LED] = currentTime;
}
}

void fader3(int LED) {
if (currentTime – lastTime3[LED] > flckrSpd3[LED]) {
analogWrite(pin3[LED], brightness3[LED]);
// change the brightness for next time through the loop:
if (fadeUp3[LED]) {
brightness3[LED] = brightness3[LED] + fadeIn3[LED];
} else {
brightness3[LED] = brightness3[LED] + fadeOut3[LED];
}

// reverse the direction of the fading at the ends of the fade:
if (brightness3[LED] <= 0) {
fadeUp3[LED] = true;
}
else if (brightness3[LED] >= 70) {
fadeUp3[LED] = false;
}
lastTime3[LED] = currentTime;
}
}

void fader4(int LED) {
if (currentTime – lastTime4[LED] > flckrSpd4[LED]) {
analogWrite(pin4[LED], brightness4[LED]);
// change the brightness for next time through the loop:
if (fadeUp4[LED]) {
brightness4[LED] = brightness4[LED] + fadeIn4[LED];
} else {
brightness4[LED] = brightness4[LED] + fadeOut4[LED];
}

// reverse the direction of the fading at the ends of the fade:
if (brightness4[LED] <= 0) {
fadeUp4[LED] = true;
}
else if (brightness2[LED] >= 255) {
fadeUp4[LED] = false;
}
lastTime4[LED] = currentTime;
}
}

void fader5(int LED) {
if (currentTime – lastTime5[LED] > flckrSpd5[LED]) {
analogWrite(pin5[LED], brightness5[LED]);
// change the brightness for next time through the loop:
if (fadeUp2[LED]) {
brightness5[LED] = brightness5[LED] + fadeIn5[LED];
} else {
brightness5[LED] = brightness5[LED] + fadeOut5[LED];
}

// reverse the direction of the fading at the ends of the fade:
if (brightness5[LED] <= 0) {
fadeUp5[LED] = true;
}
else if (brightness5[LED] >= 10) {
fadeUp5[LED] = false;
}
lastTime5[LED] = currentTime;
}
}

void fader6(int LED) {
if (currentTime – lastTime6[LED] > flckrSpd6[LED]) {
analogWrite(pin5[LED], brightness5[LED]);
// change the brightness for next time through the loop:
if (fadeUp6[LED]) {
brightness6[LED] = brightness6[LED] + fadeIn6[LED];
} else {
brightness5[LED] = brightness6[LED] + fadeOut6[LED];
}

// reverse the direction of the fading at the ends of the fade:
if (brightness6[LED] <= 0) {
fadeUp6[LED] = true;
}
else if (brightness5[LED] >= 255) {
fadeUp6[LED] = false;
}
lastTime6[LED] = currentTime;
}
}

void blinker() {
if (currentTime – greenTime > 1500) {
if (toggle) {
digitalWrite(green, HIGH);
toggle = false;
} else {
digitalWrite(green, LOW);
toggle = true;
}
greenTime = currentTime;
}
}

void blinker2() {
if (currentTime – greenTime > 50) {
if (toggle) {
digitalWrite(green, HIGH);
toggle = false;
} else {
digitalWrite(green, LOW);
toggle = true;
}
greenTime = currentTime;
}
}

void blinker3() {
if (currentTime – greenTime > 270) {
if (toggle) {
digitalWrite(green, HIGH);
toggle = false;
} else {
digitalWrite(green, LOW);
toggle = true;
}
greenTime = currentTime;
}
}

void blinker4() {
if (currentTime – greenTime > 80) {
if (toggle) {
digitalWrite(green, HIGH);
toggle = false;
} else {
digitalWrite(green, LOW);
toggle = true;
}
greenTime = currentTime;
}
}

void blinker5() {
if (currentTime – greenTime > 400) {
if (toggle) {
digitalWrite(green, HIGH);
toggle = false;
} else {
digitalWrite(green, LOW);
toggle = true;
}
greenTime = currentTime;
}
}