I’m an Arduino-head

For Hallowe’en just gone I printed out an Elvis Skull with blue PLA from Ultimachine. I had some trouble with missed steps early on due to my SFACT not being well calibrated and causing the printer to push out more filament than needed… but I tried to correct the position a few times… with mixed results. Nevertheless it was usable so I drilled out some holes in the eyes/nose and popped in some blue LED’s that were lying around. I had modified the arduino fade sketch to control some laser diodes so I put it to use to make the LEDs glow in a slightly spooky fashion. The kids loved it!

The LEDs pulse 6 times with slightly longer on-times for each successive pulse and at the end they rapidly flash a few times individually. Lather, rinse, repeat.  Here’s the modified code:

/*
This is a variation of Fade from http://www.arduino.cc/en/Tutorial
The no of pulses, their ramping up/down and 'full-on' times can be set.
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:

int brightness = 0; // how bright the LED is

/***** main pulse parameters here: pulse ramping, on-time and no of pulses *****/
int pulses = 6; //no of pulses per burst
int maxtime = 100; // time pulse is at max value: 'full-on'
int upduration = 1020; //pulse ramp-up duration in ms
int dnduration = 255; //pulse ramp-down duration in ms

// parameters for ramping output up to max from zero
int upfadeStep = 5; // how many points to fade by
int upfadeAmount = upfadeStep;
int upsteps = (255/upfadeAmount);
int updelaytime = upduration / upsteps;// delay to give above pulse duration

// parameters for ramping output from max back down to zero
int dnfadeStep = 5; // how many points to fade by
int dnfadeAmount = dnfadeStep;
int dnsteps = (255/dnfadeAmount);
int dndelaytime = dnduration / dnsteps;// delay to give above pulse duration

void setup() {
// initialize the LED pin as an output:
// pinMode(ledPin, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);

}

void loop(){
brightness = 5;
// turn LED on:
digitalWrite(ledPin, HIGH);
maxtime = 500;

for(int p=0;p< pulses; p++){

brightness = 5;
upfadeAmount = upfadeStep;
for (int i=0; i< upsteps; i++){
analogWrite(9, brightness);
analogWrite(10, brightness);
analogWrite(11, brightness);

// change the brightness for next time through the loop:
brightness = brightness + upfadeAmount;

// reverse the direction of the fading at the ends of the fade:
// if (brightness == 0 || brightness == 255) {
// fadeAmount = -fadeAmount ;
//}
// wait for 30 milliseconds to see the dimming effect
delay(updelaytime);
}
brightness = 255;
analogWrite(9, brightness);
analogWrite(10, brightness);
analogWrite(11, brightness);
delay(maxtime);
dnfadeAmount = dnfadeStep;
for (int j=0; j< dnsteps; j++){

// reverse the direction of the fading at the ends of the fade:
if (brightness == 5 || brightness == 255) {
dnfadeAmount = -dnfadeAmount ;
}
// change the brightness for this time through the loop:
brightness = brightness + dnfadeAmount;

analogWrite(9, brightness);
analogWrite(10, brightness);
analogWrite(11, brightness);
// wait for 30 milliseconds to see the dimming effect
delay(dndelaytime);
}

brightness = 5;
analogWrite(9, brightness);
analogWrite(10, brightness);
analogWrite(11, brightness);
maxtime = maxtime * 1.5;
}
// code to flash LEDs individually at end of fading cycle
for (int k=0; k< 5; k++){
analogWrite(9, 255);
delay(100);
analogWrite(9, 0);
delay(25);
}
analogWrite(9, 5);

for (int l=0; l< 5; l++){
analogWrite(10, 255);
delay(100);
analogWrite(10, 0);
delay(25);
}
analogWrite(10, 5);

for (int m=0; m< 5; m++){
analogWrite(11, 255);
delay(100);
analogWrite(11, 0);
delay(25);
}
analogWrite(9, 0);
analogWrite(10, 0);
analogWrite(11, 0);
delay(3000);
}

I imagine there are neater ways to code it but it does the job. I’ve been messing with controlling it from an Android smartphone using Python (SL4A) and a bluesmirf bluetooth module. More on that in a future post.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: