Pages: [1]   Go Down
Author Topic: ATtiny that's arduino compatable with 3x independent PWM channels  (Read 2575 times)
0 Members and 1 Guest are viewing this topic.
Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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 );
}
Logged

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Sweden
Offline Offline
Full Member
***
Karma: 1
Posts: 159
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: October 07, 2012, 09:56:47 am by olof_n » Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4537
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

No, I don't answer questions sent in private messages...

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 08, 2012, 03:37:55 am by Riva » Logged

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4537
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

No, I don't answer questions sent in private messages...

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Guys,
ATtiny85's ordered. I might be back later if I'm having problems though smiley-grin
Logged

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4537
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

No, I don't answer questions sent in private messages...

Pages: [1]   Go Up
Jump to: