Badge hack
From MiniSoOnCon Wiki
Contents |
Intro
Here's a single page for all of this magic.
What you've got is a very simple and yet very awesome electronic badge. It's a collaborative effort amongst the various spaces. A little bit of plastic work from Toronto and KW, impromptu software from Montreal and some awesome electronics hardware from Hamilton.
As far as the arduino environment is concerned, it's a Lilypad with the ATMEGA328P running at 8MHz.
There are LEDs on each of the PWM channels and a button on Pin 8 -- which requires that you set the internal pull-up resistor on the board.
Generic Debounce Code
Here is some reliable code to read the S1 button from the badge
/* Global/static vars */ const int BUTTON_PIN = 8; const int BOUNCE_DELAY = 10; int bounceFilter = 0; int buttonState = LOW;
/* In loop() */
if (bounceFilter == 0) {
if (buttonState != digitalRead(BUTTON_PIN)) {
buttonState = (buttonState == HIGH ? LOW : HIGH);
bounceFilter = BOUNCE_DELAY;
if (buttonState == LOW) {
/* handle button press */
} else {
/* handle button release */
}
}
} else {
bounceFilter--;
}
The technique is to simply ignore the button for a certain amount of time after any state change. A delay of 100ms seems to work well.
The ideal value for BOUNCE_DELAY will depend on the timing of your loop. If that is unpredictable, you might need to keep time some other way.
Software
This is currently loaded on all badges as of Friday night - it has issues. Sorry.
int mode = 0;
int reading;
//debouncing variables
int buttonstate;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
// setup the number of available programs
int programQuant = 4; // variable to keep the actual value
// set initial state to "off"
int programMode = 0; // program mode: 0 is off mode, higher than 0 is on mode
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
// set up the pins
int Red2 = 9; // light connected to digital pin 9
int Red1 = 3;
int Green2 = 10;
int Green1 = 5;
int Blue2 = 11;
int Blue1 = 6;
int inputPin = 8; // choose the input pin (for a pushbutton)
// end pin setup
// set up for Randomizer
int rval = 0; // placeholder value for the new red led brightness
int gval = 0; // placeholder value for the new green led brightness
int bval = 0; // placeholder value for the new blue led brightness
int oldrval = 0; //placeholders for the old red led brightness
int oldgval = 0; //placeholders for the old green led brightness
int oldbval = 0; //placeholders for the old blue led brightness
int counter = 0; // a counter for loops
int fadetime = 1000; // the amount of time it takes to fade
// from one colour to another in ms
int steps = 10; // the number of steps between each colour
//end Randomizer setup
// set up for Jamie's programs
int value1 = LOW;
int value = 0;
// end Jamie's setup
//check for button press and change mode
void pincheck(){
int reading = digitalRead(inputPin);
if( reading != lastButtonState ) {
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay ){
buttonState = reading;
}
if ( lastButtonState && !buttonState ){
mode = ( mode + 1 ) % 3;
Serial.println(mode , DEC);
}
lastButtonState = reading;
/*
delay(2);
int second = digitalRead(inputPin);
if( !first && !second ){
mode = (mode + 1) % 8;
Serial.println(mode , DEC);
}
*/
}
void mode0(){
//Array of values to output to leds
int a[6];
for( int fill = 0; fill < 6; fill ++ ){
a[fill]=fill*42;
}
//Fade LEDS in order
for( int i = 0; i < 256; i++ ){
for( int fill = 0; fill < 6; fill ++ ){
a[fill]=(a[fill]+i)%256;
}
analogWrite( Red1, a[0] );
analogWrite( Green1, a[1] );
analogWrite( Blue1, a[2] );
analogWrite( Red2, a[3] );
analogWrite( Green2, a[4] );
analogWrite( Blue2, a[5] );
pincheck();
}
}
void setup()
{
Serial.begin(9600);
// setup various pin modes
pinMode(Red1, OUTPUT);
pinMode(Red2, OUTPUT);
pinMode(Green1, OUTPUT);
pinMode(Green2, OUTPUT);
pinMode(Blue1, OUTPUT);
pinMode(Blue2, OUTPUT);
pinMode(inputPin, INPUT); // declare pushbutton as input
digitalWrite(inputPin, HIGH); // activate ATMEGA328P internal pull-up resistors
// from http://www.arduino.cc/en/Reference/Random:
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
delay(1000);
/*
//Enable interrupts
GICR |= ( 1 < < INT0);
// Signal high triggers interrupt
MCUCR |= ( 1 << ISC00);
MCUCR |= ( 0 << ISC01);
EICRA |= ( 1 << ISC00);
EICRA |= ( 0 << ISC01);
*/
}
void loop()
{
switch(mode){
case 0:
mode0();
break;
case 1:
mode1();
break;
case 2:
mode2();
break;
}
}
void mode2(){
// Weld my ass...
digitalWrite(Red1, LOW);
digitalWrite(Red2, LOW);
digitalWrite(Green1, LOW);
digitalWrite(Green2, LOW);
analogWrite(Blue1, random(120)+135);
// analogWrite(Green1, random(120)+135);
analogWrite(Blue2, random(120)+135);
// analogWrite(Green2, random(120)+135);
int d = random(50);
for( int i = 0; i < d; i++ ){
pincheck();
delay(2);
}
//delay(random(100));
}
void mode1(){
digitalWrite(Red1, LOW);
digitalWrite(Red2, LOW);
digitalWrite(Green1, LOW);
digitalWrite(Green2, LOW);
analogWrite(Blue1, random(120)+135);
analogWrite(Green1, random(120)+135);
analogWrite(Blue2, random(120)+135);
analogWrite(Green2, random(120)+135);
int d = random(500);
for( int i = 0; i < d; i++ ){
pincheck();
delay(2);
}
}
