Last active
November 24, 2019 01:02
-
-
Save daniloc/5c12565f7940d85ac172431e21966068 to your computer and use it in GitHub Desktop.
Using a shift register to pulse LEDs for a model warp core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************************************************************************ | |
Fade in LED's one by one using ShiftPWM with one shift register | |
************************************************************************************************************************************/ | |
// You can choose the latch pin yourself. | |
const int ShiftPWM_latchPin=8; | |
#define SHIFTPWM_NOSPI | |
const int ShiftPWM_dataPin = 11; | |
const int ShiftPWM_clockPin = 12; | |
// If your LED's turn on if the pin is low, set this to true, otherwise set it to false. | |
const bool ShiftPWM_invertOutputs = false; | |
// You can enable the option below to shift the PWM phase of each shift register by 8 compared to the previous. | |
// This will slightly increase the interrupt load, but will prevent all PWM signals from becoming high at the same time. | |
// This will be a bit easier on your power supply, because the current peaks are distributed. | |
const bool ShiftPWM_balanceLoad = false; | |
#include <ShiftPWM.h> // include ShiftPWM.h after setting the pins! | |
// Here you set the number of brightness levels, the update frequency and the number of shift registers. | |
// These values affect the load of ShiftPWM. | |
// Choose them wisely and use the PrintInterruptLoad() function to verify your load. | |
// There is a calculator on my website to estimate the load. | |
unsigned char maxBrightness = 255; | |
unsigned char pwmFrequency = 60; | |
int numRegisters = 1; | |
int numRGBleds = numRegisters*8/3; | |
int delayInterval = 250; | |
int brightnessFloor = 25; | |
int offset = 1; | |
void setup(){ | |
Serial.begin(9600); | |
Serial.println("We up"); | |
// Sets the number of 8-bit registers that are used. | |
ShiftPWM.SetAmountOfRegisters(numRegisters); | |
// SetPinGrouping allows flexibility in LED setup. | |
// If your LED's are connected like this: RRRRGGGGBBBBRRRRGGGGBBBB, use SetPinGrouping(4). | |
//ShiftPWM.SetPinGrouping(3); //This is the default, but I added here to demonstrate how to use the funtion | |
ShiftPWM.Start(pwmFrequency,maxBrightness); | |
} | |
void loop() | |
{ | |
readSerial(); | |
// Turn all LED's off. | |
ShiftPWM.SetAll(brightnessFloor); | |
//ShiftPWM.SetOne(0, 255); | |
int maxPins = 7; | |
// For every led | |
for(int pin = 0; pin <= maxPins - offset; pin++){ | |
readSerial(); | |
int nextPin = pin + offset; | |
// Fade in | |
for(unsigned char brightness = brightnessFloor; brightness < 255; brightness++){ | |
ShiftPWM.SetOne(pin, brightness); | |
ShiftPWM.SetOne(nextPin, brightness); | |
delayMicroseconds(delayInterval); | |
} | |
// Fade out | |
for(unsigned char brightness = 255; brightness > brightnessFloor; brightness--){ | |
ShiftPWM.SetOne(pin, brightness); | |
ShiftPWM.SetOne(nextPin, brightness); | |
delayMicroseconds(delayInterval); | |
} | |
} | |
} | |
void readSerial() { | |
while (Serial.available() > 0) { | |
// look for the next valid integer in the incoming serial stream: | |
int minimumBrightness = Serial.parseInt(); | |
// do it again: | |
int interval = Serial.parseInt(); | |
// do it again: | |
int newOffset = Serial.parseInt(); | |
// look for the newline. That's the end of your sentence: | |
if (Serial.read() == '\n') { | |
brightnessFloor = minimumBrightness; | |
delayInterval = interval; | |
offset = newOffset; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment