PWM (“analog output”) on all five i/o pins of an ATtiny using software PWM!
The software PWM code was written by Ernst Christensen, I found it in the following Arduino forum thread:
>> http://arduino.cc/forum/index.php/topic,75334.0.html
Example of fabric circuit using software PWM code >> http://www.kobakant.at/DIY/?p=3395
My slightly edited version of Ernst’s code:
// fades LEDs on all five pins on and off using software PWM
#define fadeSpeed 20
void setup(){
for(int pin=0;pin<5;pin++) pinMode(pin, OUTPUT);
}
void loop(){
for(int pin=0;pin<5;pin++) {
for(int fade=1;fade<254;fade++) { //fade on
softPWM(pin, fade, fadeSpeed);
}
for(int fade=254;fade>1;fade–) { //fade off
softPWM(pin, fade, fadeSpeed);
}
}
}
void softPWM(int pin, int freq, int sp) { // software PWM function that fakes analog output
digitalWrite(pin,HIGH); //on
delayMicroseconds(sp*freq);
digitalWrite(pin,LOW); //off
delayMicroseconds(sp*(255-freq));
}
I’m curious as to how one can use spwm and count on this chip, since these commands are not supported by the AT Tiny…? What are the limitations on which commands I can program? Can I make a random number generator to choose which led gets lit?
“++” is a mathematical expression and “spwm()” is a function included in the code, so both these things will run on the ATtiny chips. i don’t think the “random()” function will work though. for a full list of the supported Arduino commands look at the bottom of this page:
>> http://hlt.media.mit.edu/?p=1695
Thanks! I wonder then, is there another way to generate random numbers on the AT 45 or 85? I would like to scramble the order of the fading here, for example.
I feel like there may be a way more professional way, and not even sure if this would work, but you could try simply reading an analog value from one of the pins and doing some math with the number you read to result in one of the LEDs?
I’m having difficulty getting this code to compile. I get an error stray ‘\’, which as I understand means I have a unicode character in there, but I see nothing strange. I have copied your code exactly and pasted, but it won’t compile. Is it me? I tried Ernie’s original code and it runs, but I really like your version better for effect! 🙂
here is a link to the code version of the sketch:
>> https://github.com/plusea/CODE/blob/master/WORKSHOP CODE/SOFTandTINY/a_softPWMfade5/a_softPWMfade5.ino
your a_softPWMfade5.ino download link is broken, it should be
https://github.com/plusea/CODE/blob/master/WORKSHOP CODE/SOFTandTINY/a_softPWMfade5/a_softPWMfade5.ino
Anyway, can you show me your sketch which is same as your video demo?
Very funny. I had corrected your download link. But it come back to the broken link again after click on Submit Comment button.
Him Jim:
Erase this line ==> for(int fade=254;fade>1;fade–) { //fade off
and key in manually. add a – behind fade- too.
thanks for all your comments smching! i don’t know what to do about the broken link, just recommend that people copy paste the whole link, rather than click on it. and thanks for fixing the error that comes from copy pasting the code!
// Pimped Ernst’s code, I did it on an 2313 (1 Mhz)
//
// code (PIN’s) for ATtiny 2313
// fades LEDs on all five pins on and off using software PW
//
// ATMEL ATTINY2313
//
// -\/-
// PA2 1| |29 VCC
// RX (D 0) PD0 2| |19 PB7 (D 16)
// TX (D 1) PD1 3| |18 PB6 (D 15)
// (D 2) PA1 4| |17 PB5 (D 14) R=330 Ohm LED
// (D 3) PA0 5| |16 PB4 (D 13) —||||———Red—–|
// INT0 (D 4) PD2 6| |15 PB3 (D 12) —||||——–Yellow—|
// INT1 (D 5) PD3 7| |14 PB2 (D 11) —||||——–Green—-|
// (D 6) PD4 8| |13 PB1 (D 10) —||||——– Red—–|
// *(D 7) PD5 9| |12 PB0 (D 9) —||||——–Yellow—|
// GND 10| |11 PD6 (D 8)
// —-
//
// * indicates PWM port
#define fadeSpeed 1
int fade=0;
void setup()
{
for(int pin=9;pin<14;pin )
pinMode(pin, OUTPUT);
}
void loop(){
for(int pin=9;pin<14;pin )
{
for(fade=1;fade1;–fade)
{
softPWM(pin, fade, fadeSpeed);
}
}
for(int pin=12;pin>9;–pin)
{
for(fade=1;fade1;–fade)
{
softPWM(pin, fade, fadeSpeed);
}
}
}
void softPWM(int pin, int freq, int sp)
{
digitalWrite(pin,HIGH); //on
delayMicroseconds(sp*freq);
digitalWrite(pin,LOW); //off
delayMicroseconds(sp*(255-freq));
}