rhd
0
- Joined
- Dec 7, 2010
- Messages
- 8,469
- Points
- 0
Dramatically rewrote the code, and it works great! I even added a rapid cycling mode.
If there's any problem (and I can live with this for now), it's that there's piss poor entropy to the randomness approach (probably because storing a value of 1-255 to use as the random seed just doesn't produce the needed entropy). I can detect the patterns.
If there's any problem (and I can live with this for now), it's that there's piss poor entropy to the randomness approach (probably because storing a value of 1-255 to use as the random seed just doesn't produce the needed entropy). I can detect the patterns.
Code:
#include <EEPROM.h> // include the EEPROM Memory library
boolean pinOptions[8][3] = { // Array of pin combinations (1 = on, 0 = off)
{1,1,1},
{1,0,0},
{1,1,0},
{0,1,0},
{0,1,1},
{0,0,1},
{1,0,1},
{0,0,0}
};
byte ButtonPin = 3; // see pinout
byte ColorOnePin = 0; // see pinout
byte ColorTwoPin = 1; // see pinout
byte ColorThreePin = 2; // see pinout
byte MemVar;
byte RandVar;
byte MemoryLocation = 1;
byte RandLocation = 0;
byte ButtonPinState;
byte ColorChoice;
void setup (){ // this subroutine runs once upon startup
pinMode (0, OUTPUT); // set pin 0 to output
pinMode (1, OUTPUT); // set pin 1 to output
pinMode (2, OUTPUT); // set pin 2 to output
pinMode (3, INPUT); // set pin 3 to input
// Read memvar
MemVar = 1;
MemVar = EEPROM.read(MemoryLocation);
if (MemVar > 9){
MemVar = 1; //
}
if (MemVar == 8){
RandVar = EEPROM.read(RandLocation);
randomSeed(RandVar);
setRandomColor();
}
else if (MemVar == 9){
RandVar = EEPROM.read(RandLocation);
randomSeed(RandVar);
ColorChoice = random(2, 7);
setColor();
}
else
{
ColorChoice = MemVar;
setColor();
}
}
void loop(){ // this subroutine runs continuously after setup finishes
ButtonPinState = digitalRead(ButtonPin); // read ButtonPin to ButtonPinState
if (ButtonPinState == HIGH){ // when ButtonPinState is HIGH...
++ MemVar; // increment memory variable
if (MemVar > 9){ // on 9th button press...
MemVar = 1; // reset color
}
EEPROM.write(MemoryLocation, MemVar);
// Normal mode for colors 1 through 7
if (MemVar < 8){
ColorChoice = MemVar;
setColor();
delay(300);
}
// Random color mode (no cycling)
else if (MemVar == 8){
blinkAll();
randomSeed(millis());
setRandomColor();
}
// Random color mode (rapid cycling) - Indicate that we're entering the mode
else if (MemVar == 9){
blinkAll();
}
}
// Random color mode (rapid cycling)
if (MemVar == 9){
randomSeed(millis());
ColorChoice = random(2, 7);
setColor();
delay(50);
}
}
void blinkAll(){
for (int i=0; i <= 4; i++){
ColorChoice = 8;
setColor();
delay(20);
ColorChoice = 1;
setColor();
delay(20);
}
}
void setRandomColor(){
ColorChoice = random(2, 7);
setColor();
RandVar = random(1, 255);
EEPROM.write(RandLocation, RandVar);
}
void setColor(){
digitalWrite(ColorOnePin, pinOptions[ColorChoice-1][0]);
digitalWrite(ColorTwoPin, pinOptions[ColorChoice-1][1]);
digitalWrite(ColorThreePin, pinOptions[ColorChoice-1][2]);
}
Last edited: