Created
June 6, 2014 16:12
-
-
Save peppy/a4d6a95e9b0be3aeada1 to your computer and use it in GitHub Desktop.
This file contains 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
#include <SPI.h> | |
/* Pro Micro Test Code | |
by: Nathan Seidle | |
modified by: Jim Lindblom | |
SparkFun Electronics | |
date: January 20, 2012 | |
license: Public Domain - please use this code however you'd like. | |
It's provided as a learning tool. | |
This code is provided to show how to control the SparkFun | |
ProMicro's TX and RX LEDs within a sketch. It also serves | |
to explain the difference between Serial.print() and | |
Serial1.print(). | |
*/ | |
int RXLED = 17; // The RX LED has a defined Arduino pin | |
#define MAX_PIXELS 50 | |
uint16_t _LEDs = MAX_PIXELS; | |
uint8_t _CSPIN = 9; | |
uint8_t _patterns = 4; | |
uint8_t _pixels[MAX_PIXELS * 3]; | |
void setupBitmap() { | |
for (int i = 0; i < _LEDs * 3; i++) { | |
_pixels[i] = 0; | |
} | |
} | |
void outputStrip(uint8_t r, uint8_t g, uint8_t b) { | |
int index = 0, length = _LEDs * 3; | |
while(index < length) { | |
_pixels[index] = b; | |
_pixels[index + 1] = g; | |
_pixels[index + 2] = r; | |
index += 3; | |
} | |
} | |
void setup() | |
{ | |
pinMode(_CSPIN, OUTPUT); | |
digitalWrite(_CSPIN, LOW); | |
SPI.begin(); | |
randomSeed(analogRead(0)); | |
setupBitmap(); | |
} | |
uint8_t aimR = 0, aimG = 0, aimB = 0; | |
uint8_t currentR = 0, currentG = 0, currentB = 0; | |
uint8_t brightness = 255; | |
bool aiming = true; | |
void loop() | |
{ | |
while (true) | |
{ | |
if (aimR == currentR && aimG == currentG && aimB == currentB) | |
{ | |
aimR = random(brightness); | |
aimG = random(brightness); | |
aimB = random(brightness); | |
} | |
else | |
{ | |
if (currentR != aimR) currentR = aimR > currentR ? currentR + 1 : currentR - 1; | |
if (currentG != aimG) currentG = aimG > currentG ? currentG + 1 : currentG - 1; | |
if (currentB != aimB) currentB = aimB > currentB ? currentB + 1 : currentB - 1; | |
} | |
outputStrip(currentR, currentG, currentB); | |
int i, length = _LEDs * 3; | |
digitalWrite(_CSPIN, HIGH); | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setDataMode(SPI_MODE0); | |
SPI.setClockDivider(SPI_CLOCK_DIV8); | |
for(i = 0; i < length; i++) | |
{ | |
SPDR = _pixels[i]; | |
while(!(SPSR & (1<<SPIF))); | |
} | |
digitalWrite(_CSPIN, LOW); | |
delay(100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment