Go Down

Topic: ATtiny that's arduino compatable with 3x independent PWM channels (Read 3516 times) previous topic - next topic

Riva

Using parametric search on Atmel site I'm looking for a arduino compatible ATtiny that has 3 independent PWM channels to run the simplest of code below for a mood lamp. I can use a spare ATmega8 I have but it seems such a waste of chip and PCB space. I'm not bothered about a bootloader as long as I can program the chip using ArduinoISP. Is the 8 pin ATtiny85 suitable or any suggestions please?

Code: [Select]
// HLSMAX BEST IF DIVISIBLE BY 6. RGBMAX, HLSMAX must each fit in a byte.
#define HLSMAX 240                              // H,L, and S vary over 0-HLSMAX
#define RGBMAX 255                              // R,G, and B vary over 0-RGBMAX

#define RedPin 3
#define GreenPin 6
#define BluePin 9

const long mainDelay = 25;                      // Delay between each colour change

byte R,G,B;                                     // Target RGB colour
byte Ro,Go,Bo;                                  // Current RGB colour

void setup() {

// Serial.begin(9600);

    pinMode(RedPin,OUTPUT);
    pinMode(GreenPin,OUTPUT);
    pinMode(BluePin,OUTPUT);

}

void loop() {

    for (word Hue=0; Hue <= HLSMAX; Hue++){     // Once around the circle
        HLS2RGB(Hue,HLSMAX,HLSMAX/2);           // Get target R,G,B colour
// Serial.print (Hue);
// Serial.print (" = ");
// Serial.print (R,16);
// Serial.print (",");
// Serial.print (G,16);
// Serial.print (",");
// Serial.println (B,16);
        boolean OK = false;                     //Flag colours don't match
        while (OK == false){
            long loopTime = millis();           //Read timer
            while ((millis() - loopTime) < mainDelay) {}    // Delay
            OK = true;                          // Flag colours match

            if (R > Ro){                        // Target R greater than current Ro
                Ro++;                           // Increment current
                OK = false;                     // Flag colours will change
            }
            if (R < Ro){                        // Target R less than current Ro
                Ro--;                           // Decrement current
                OK = false;                     // Flag colours will change
            }
            if (G > Go){
                Go++;
                OK = false;
            }
            if (G < Go){
                Go--;
                OK = false;
            }
            if (B > Bo){
                Bo++;
                OK = false;
            }
            if (B < Bo){
                Bo--;
                OK = false;
            }
            analogWrite(RedPin,Ro);
            analogWrite(GreenPin,Go);
            analogWrite(BluePin,Bo);
        }
    }
}

void HLS2RGB(word hue, word sat, word lum) {
   
    word M1,M2;                                 // calculated numbers

    sat = constrain(sat, 1, HLSMAX);            // Don't allow sat<=0 or sat>HLSMAX
    lum = constrain(lum, 0, HLSMAX/2);          // Don't allow lum<0 or lum>(HLSMAX/2)

    if (lum <= (HLSMAX/2))
        M2 = (lum*(HLSMAX + sat) + (HLSMAX/2))/HLSMAX;
    else
        M2 = lum + sat - ((lum*sat) + (HLSMAX/2))/HLSMAX;

    M1 = 2*lum-M2;
   
    // get RGB, change units from HLSMAX to RGBMAX
    R = (Hue2RGB(M1,M2,hue+(HLSMAX/3))*RGBMAX +(HLSMAX/2))/HLSMAX;
    G = (Hue2RGB(M1,M2,hue)*RGBMAX + (HLSMAX/2)) / HLSMAX;
    B = (Hue2RGB(M1,M2,hue-(HLSMAX/3))*RGBMAX +(HLSMAX/2))/HLSMAX;

// Serial.print (R,16);
// Serial.print (",");
// Serial.print (G,16);
// Serial.print (",");
// Serial.println (B,16);

    //return(RGB(R,G,B));
}
// Utility routine for HLS2RGB
byte Hue2RGB(word m1, word m2, word hue) {
    // range check: note values passed add/subtract thirds of range
    if (hue < 0)
        hue += HLSMAX;

    if (hue > HLSMAX)
        hue -= HLSMAX;

    // return r,g, or b value from this tridrant
    if (hue < (HLSMAX/6))
        return ( m1 + (((m2-m1)*hue+(HLSMAX/12))/(HLSMAX/6)));

    if (hue < (HLSMAX/2))
        return ( m2 );

    if (hue < ((HLSMAX*2)/3))
        return ( m1 + (((m2-m1)*(((HLSMAX*2)/3)-hue)+(HLSMAX/12))/(HLSMAX/6)));
    else
        return ( m1 );
}
I might seem like an idiot but remember it takes one to know one.
Forum user generally fall into three categories (the good, the bad & the a**hole) what category do you fit in?

olof_n

#1
Oct 07, 2012, 04:46 pm Last Edit: Oct 07, 2012, 04:56 pm by olof_n Reason: 1
Hi!

Attiny85 will work fine.
I use a Attiny85 in a mood lamp I made for my kids. 3*PWM pins to control the RGB-LED.
Used a extra photoresistor to sense when it is dark/light in the room to automatically turn on/off the lamp.

You can program the chip using ArduinoISP.

/Olof

fungus


Using parametric search on Atmel site I'm looking for a arduino compatible ATtiny that has 3 independent PWM channels to run the simplest of code below for a mood lamp. I can use a spare ATmega8 I have but it seems such a waste of chip and PCB space. I'm not bothered about a bootloader as long as I can program the chip using ArduinoISP. Is the 8 pin ATtiny85 suitable or any suggestions please?


The ATtiny85 should work perfectly.
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Riva

Does the ATtiny85 have 3 independent PWM channels? This image from one of the core suppliers only shows 2

The parametric search says it has 6 PWM's but does the Arduino cores support more than 2.
I might seem like an idiot but remember it takes one to know one.
Forum user generally fall into three categories (the good, the bad & the a**hole) what category do you fit in?

Coding Badly

Does the ATtiny85 have 3 independent PWM channels?


Yes.

Quote
This image from one of the core suppliers only shows 2


The image is wrong.

Quote
The parametric search says it has 6 PWM's but does the Arduino cores support more than 2.


This core supports all three...
http://code.google.com/p/arduino-tiny/

fungus


Does the ATtiny85 have 3 independent PWM channels? This image from one of the core suppliers only shows 2

The parametric search says it has 6 PWM's but does the Arduino cores support more than 2.


The Tiny85 has two 8-bit timers so it can do 4 PWMs. I don't know where the number 6 comes from.


No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Riva

Thanks Guys,
ATtiny85's ordered. I might be back later if I'm having problems though :D
I might seem like an idiot but remember it takes one to know one.
Forum user generally fall into three categories (the good, the bad & the a**hole) what category do you fit in?

Riva


Does the ATtiny85 have 3 independent PWM channels? This image from one of the core suppliers only shows 2


For anyone referencing this thread in the future the link Coding Badly supplied http://code.google.com/p/arduino-tiny/ for a ATtiny85 core supports 3x PWM on digital pins 0,1 & 4
I might seem like an idiot but remember it takes one to know one.
Forum user generally fall into three categories (the good, the bad & the a**hole) what category do you fit in?

fungus


Does the ATtiny85 have 3 independent PWM channels? This image from one of the core suppliers only shows 2



That image has a lot of missing info...



The three PWM outputs are OC0A 0C1A and OC1B.
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy