Skip to content

Instantly share code, notes, and snippets.

@SageStarCodes
Created October 11, 2016 17:56
Show Gist options
  • Save SageStarCodes/b29153ecc20313a7e23022c57aa1b616 to your computer and use it in GitHub Desktop.
Save SageStarCodes/b29153ecc20313a7e23022c57aa1b616 to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include "SH1106_SPI.h"
//#define USE_FRAME_BUFFER
#ifdef USE_FRAME_BUFFER
SH1106_SPI_FB lcd;
#else
SH1106_SPI lcd;
#endif
// R B G
byte led[] = {3,5,6};
bool ledStat = false;
byte ledColor[3] = {0,0,0};
unsigned long lastMillis[4];
unsigned short ledAnimTime = 1000;
unsigned long currentMillis;
float factor;
void runLED() {
if(currentMillis - lastMillis[3] > 0) {
if(currentMillis - lastMillis[0] <= ledAnimTime) {
factor = (float)(currentMillis - lastMillis[0])/(float)ledAnimTime;
analogWrite(led[0], (int)((float)ledColor[0] * factor));
analogWrite(led[1], (int)((float)ledColor[1] * factor));
analogWrite(led[2], (int)((float)ledColor[2] * factor));
} else if(currentMillis - lastMillis[0] <= ledAnimTime*2) {
factor = 1.0f - (float)(currentMillis - lastMillis[0])/(float)(ledAnimTime*2);
analogWrite(led[0], (int)((float)ledColor[0] * factor));
analogWrite(led[1], (int)((float)ledColor[1] * factor));
analogWrite(led[2], (int)((float)ledColor[2] * factor));
} else {
lastMillis[0] = currentMillis;
}
lastMillis[3] = currentMillis;
}
}
void runDisplay() {
if(currentMillis - lastMillis[1] >= 100) {
lcd.clear();
lcd.print(F("Hello world!"));
lcd.gotoXY(0,1);
lcd.print(millis());
lcd.gotoXY(0,2);
lcd.print(F("Factor "));
lcd.print(factor);
lastMillis[1] = currentMillis;
}
}
void runCheck() {
if(currentMillis - lastMillis[2] >= 8000) {
ledColor[0] = random(0,256);
ledColor[1] = random(0,256);
ledColor[2] = random(0,256);
lastMillis[2] = currentMillis;
}
}
void setup(void)
{
Serial.begin(9600);
pinMode(led[0], OUTPUT);
pinMode(led[1], OUTPUT);
pinMode(led[2], OUTPUT);
lcd.begin(false, true, 255, 3);
lastMillis[0] = millis();
lastMillis[1] = lastMillis[0];
lastMillis[2] = lastMillis[0];
lastMillis[3] = lastMillis[0];
ledColor[0] = random(0,256);
ledColor[1] = random(0,256);
ledColor[2] = random(0,256);
}
void loop(void)
{
//check for time overflow
if (millis() < lastMillis[0]) {
//reset clocks
lastMillis[0] = millis();
lastMillis[1] = lastMillis[0];
lastMillis[2] = lastMillis[0];
lastMillis[3] = lastMillis[0];
}
currentMillis = millis();
runCheck();
runDisplay();
runLED();
}
@SageStarCodes
Copy link
Author

SageStarCodes commented Oct 11, 2016

L20-L37 There is a very sudden drop in the brightness when fading out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment