Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buckinghamk/9e82191496d70e6014c45525c0384d15 to your computer and use it in GitHub Desktop.
Save buckinghamk/9e82191496d70e6014c45525c0384d15 to your computer and use it in GitHub Desktop.
LightCube Arduino Code
#include <Servo.h>
/*
LightCube
Kristjan Buckingham
DIGF 6037: Creation & Computation
OCAD University
Created on October 19, 2020
Based on:
Creation & Computation - Digital Futures, OCAD University
blinkMultipleLEDs_loopANDarrays, Kate Hartman / Nick Puckett
Fade an LED using a timer
Based on Basic Arduino Fade example, but updated to remove the delay
*Display 3 modes of lighting effects based on amount of light detected
*Housed in a box with holes, labels, and cutouts to create a lantern
*/
int ledPins[] = {2, 3, 5, 6, 9, 10, 11}; //Pin that the led is attached to - check that it is a PWM pin
int fadeRates[] = {7, 8, 2, 3, 0, 0, 0}; //the time between updates in milliseconds
int minBrightVal[] = {0, 0, 0, 0, 0, 0, 0}; //sets the low point of the fade range
int maxBrightVal[] = {150, 150, 150, 150, 150, 150, 150}; //sets the high point of the fade range
int fadeIncrement[] = {1, 1, 1, 1, 1, 1, 1}; // how much to change the brightness each cycle
int ledBrightness[] = {10, 10, 10, 10, 0, 0, 0};
int ledTotal = sizeof(ledPins) / sizeof(int);
int sensor1Pin = A0;
int sensor1Val;
long lastTimeYouChanged[] = {0, 0, 0, 0, 0, 0, 0}; //this stores the time of the last change
void setup()
{
Serial.begin(9600);
for (int i = 0; i < ledTotal; i++)
{
pinMode(ledPins[i], OUTPUT); //Set the pin to output mode
ledBrightness[i] = minBrightVal[i];
}
}
void loop()
{
sensor1Val = analogRead(sensor1Pin);
Serial.print("light1: ");
Serial.println(sensor1Val);
if (sensor1Val >= 250 && sensor1Val < 700)
{
for (int i = 0; i < ledTotal; i++)
{
if (millis() - lastTimeYouChanged[i] >= fadeRates[i]) //this very simple statement is the timer,
{ //it subtracts the value of the moment in time the last blink happened, and sees if that number is larger than your set blinking value
analogWrite(ledPins[i], ledBrightness[i]);
ledBrightness[i] += fadeIncrement[i];
if (ledBrightness[i] <= minBrightVal[i] || ledBrightness[i] >= maxBrightVal[i])
{
fadeIncrement[i] = -fadeIncrement[i];
}
lastTimeYouChanged[i] = millis(); //save the value in time that this switch occured, so we can use it again.
}
}
ledBrightness[2] = 0;
ledBrightness[3] = 0;
ledBrightness[4] = 0;
ledBrightness[5] = 0;
ledBrightness[6] = 0;
} else if (sensor1Val >= 700)
{
for (int i = 0; i < ledTotal; i++)
{
if (millis() - lastTimeYouChanged[i] >= fadeRates[i]) //this very simple statement is the timer,
{ //it subtracts the value of the moment in time the last blink happened, and sees if that number is larger than your set blinking value
analogWrite(ledPins[i], ledBrightness[i]);
ledBrightness[i] += fadeIncrement[i];
if (ledBrightness[i] <= minBrightVal[i] || ledBrightness[i] >= maxBrightVal[i])
{
fadeIncrement[i] = -fadeIncrement[i];
}
lastTimeYouChanged[i] = millis(); //save the value in time that this switch occured, so we can use it again.
}
}
ledBrightness[0] = 0;
ledBrightness[1] = 0;
ledBrightness[4] = 0;
ledBrightness[5] = 0;
ledBrightness[6] = 0;
} else if (sensor1Val < 250)
{
ledBrightness[0] = 0;
ledBrightness[1] = 0;
ledBrightness[2] = 0;
ledBrightness[3] = 0;
ledBrightness[4] = 150;
ledBrightness[5] = 150;
ledBrightness[6] = 150;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment