Skip to content

Instantly share code, notes, and snippets.

@paultag
Created October 31, 2010 22:41
Show Gist options
  • Save paultag/657268 to your computer and use it in GitHub Desktop.
Save paultag/657268 to your computer and use it in GitHub Desktop.
The RGB Throbber
/*
* "Throb" effect ( Part Deux )
*
* Paul Tagliamonte, 2010. ( Peace, Love and C++, baby )
*
* This code is released under the Bacon license.
* If you like this code enough, consider sending me bacon.
*
* That or GPL-3+.
*
*/
int incomingByte = -1;
int lred = 3;
int lgreen = 5;
int lblue = 6;
int highMask = 3;
int lowMask = 0;
int adder = 1; // how much to jump by.
// This will be inverted later.
int slope = 4; // y = (slope) x
int accumulator = 0; // part of bresie's algorithm
int cycle = 15; // duty ( hehehehe, doodie ) cycle
int on = 10; // How long "on" is per cycle.
int bottom = 0; // What "off" is
void setup() {
pinMode(lred, OUTPUT);
pinMode(lgreen, OUTPUT);
pinMode(lblue, OUTPUT);
}
void loop() {
/*
* Credit where credit is due:
* The following is an adaption of Bresenham's line algorithm.
*
* To future CS students, as well as current CS students:
* Please pay attention in class. You never know when the most
* inconsequential algorithm for the most obscure and backwards
* example will come in handy. This algorithm was originally
* to draw lines.
*
* Now, the reason I use this is one big reason and one big reason
* only: It does not use division or floating point numbers to do
* computation. This was by design ( since such operations would
* bog down a CPU by a large factor when done over 10,000,000,000
* times ). Since the Arduino sucks with floating point numbers,
* and since it's a cute ARM processor, this is a perfect fit.
*
* Stay in school, kids. Listen to your parents.
*
* Oh yes, and go to class. For the love of christ, go to class.
*
*/
accumulator = accumulator + 1; // on cycle length
if ( accumulator > slope ) { // if we've gone far enough X to jump a Y
accumulator = 0; // then: - reset the counter
on = on + adder; // - pop the "line" up one "Y"
if ( on >= cycle || on <= bottom ) {
adder = -adder; // if we're at the border, invert the adder for the next go-around.
}
}
if ( on <= bottom )
highMask = random(1,7);
int rgbMask = highMask;
int rHigh = rgbMask % 2;
rgbMask = rgbMask / 2;
int gHigh = rgbMask % 2;
rgbMask = rgbMask / 2;
int bHigh = rgbMask % 2;
rgbMask = rgbMask / 2;
if ( rHigh == 1 )
digitalWrite(lred, HIGH);
else
digitalWrite(lred, LOW);
if ( gHigh == 1 )
digitalWrite(lgreen, HIGH);
else
digitalWrite(lgreen, LOW);
if ( bHigh == 1 )
digitalWrite(lblue, HIGH);
else
digitalWrite(lblue, LOW);
delay(on); // Wait for it!
if ( on >= cycle ) {
delay( 1000 );
}
rgbMask = lowMask;
int rLow = rgbMask % 2;
rgbMask = rgbMask / 2;
int gLow = rgbMask % 2;
rgbMask = rgbMask / 2;
int bLow = rgbMask % 2;
rgbMask = rgbMask / 2;
if ( rLow == 1 )
digitalWrite(lred, HIGH);
else
digitalWrite(lred, LOW);
if ( gLow == 1 )
digitalWrite(lgreen, HIGH);
else
digitalWrite(lgreen, LOW);
if ( bLow == 1 )
digitalWrite(lblue, HIGH);
else
digitalWrite(lblue, LOW);
delay(cycle - on); // Wait for it!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment