attiny45 rgb fader

Hallo liebe Bastler,
nachdem meine ersten Schritte endlich geklappt haben wollte ich gerne einen rgb fader mit dem Attiny45 bauen. Leider scheine ich an den Pins des Controllers zu scheitern.

Ich benutze den folgenden Code:

Die Farben rot (0) und grün (1) funktionieren wie gewünscht mit einem sanften Farbübergang, allerdings habe ich noch Probleme mit der blauen (2), welche sich quasi nur direkt an und wieder abschaltet.

Meine Vermutung ist, dass Pin 2 nicht als PWM deklariert ist. Lässt sich das Problem für den Attiny45 lösen oder muss dafür etwas Anderes her?

Vielen Dank im Voraus und beste Grüße!

Wenn du dir das Pinout des ATtiny45 ansiehst, dann erkennst du, dass nur 2 Pins (0 + 1) PWM tauglich sind.
Der ATtiny44 hat da mehr (4) PWM-Pins.

Hi

Was sagen die Debug-Ausgaben?
Du kannst den Code auch auf einem Arduino 'Probelaufen' lassen - eben um zu sehen, ob die Variablen 'sauber' laufen.

Die Pin-Nummern wirst Du ja angepasst haben - hätte noch keinen Code für einen ATtiny45 compiliert, wo die Pin-Nummern falsch sind - kA, ob Das durch geht.

Das PinOut eines jeden µC findest Du, wenn interessiert, hier:
http://www.pighixxx.com/test/pinoutspg/processors/#prettyPhoto[gallery]/4/
Durch die eckigen Klammern geht wohl der URL-Tag nicht - also kopieren und 'in neuem Tab öffnen'.

MfG

agmue:
Knapp vorbei, nur 0 und 1 können PWM:

Ja, stimmt.
Mist, da habe ich doch im Web das falsche Pinout gefunden.

Vielen Dank für die Antworten bisher.

Auf youtube habe ich beispielsweise das hier für einen attiny13 gefunden welcher ebenfalls nur 2 pwm an der 0 und der 1 hat.

Kann der Effekt ggf. irgendwie mit 2 pwm erreicht werden?

Aus dem Datenblatt:

"Port B, Bit 4- XTAL2/CLKO/ADC2/OC1B/PCINT4
• OC1B: Output Compare Match output: The PB4 pin can serve as an external output for the
Timer/Counter1 Compare Match B when configured as an output (DDB4 set). The OC1B pin
is also the output pin for the PWM mode timer function."

Da könnte doch was gehen :slight_smile:

Also es gibt unterschiedliche Pinouts im Web.
Mein erstes gefundenes hatte 2, nach agmues Beitrag weiter gesucht und andere mit 4 gefunden.
Einfach selbst mal googeln.

Hi

Man könnte einen Timer-Interrupt generieren und damit einen Software-PWM erstellen.
Da könntest Du dann auch 5 PWM-Ausgänge machen (6, wenn Du RESET weg konfigurierst)

MfG

Erneut vielen Dank für die Antworten!

Der Hinweis mit dem pin 4 hat mich auf den folgenden Thread gebracht:

https://www.reddit.com/r/arduino/comments/1kdh8t/finally_got_my_first_attiny85_project_working/

Ich habe den Code entsprechend angepasst:

// Output
int redPin = 4;   // Red LED,   connected to digital pin 9
int grnPin = 1;  // Green LED, connected to digital pin 10
int bluPin = 0;  // Blue LED,  connected to digital pin 11

// Color arrays
int black[3]  = { 0, 0, 0 };
int white[3]  = { 100, 100, 100 };
int red[3]    = { 100, 0, 0 };
int green[3]  = { 0, 100, 0 };
int blue[3]   = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };
int dimWhite[3] = { 30, 30, 30 };
// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1]; 
int bluVal = black[2];

int wait = 5;      // 10ms internal crossFade delay; increase for slower fades
int hold = 0;       // Optional hold when a color is complete, before the next crossFade
int DEBUG = 0;      // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)
int j = 0;          // Loop counter for repeat

// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;

// Set up the LED outputs
void setup()
{
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT); 
}

// Main program: list the order of crossfades
void loop()
{
  crossFade(red);
  crossFade(green);
  crossFade(blue);
  crossFade(yellow);

  if (repeat) { // Do we loop a finite number of times?
    j += 1;
    if (j >= repeat) { // Are we there yet?
      exit(j);         // If so, stop.
    }
  }
}

/* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
* 
* The program works like this:
* Imagine a crossfade that moves the red LED from 0-10, 
*   the green from 0-5, and the blue from 10 to 7, in
*   ten steps.
*   We'd want to count the 10 steps and increase or 
*   decrease color values in evenly stepped increments.
*   Imagine a + indicates raising a value by 1, and a -
*   equals lowering it. Our 10 step fade would look like:
* 
*   1 2 3 4 5 6 7 8 9 10
* R + + + + + + + + + +
* G   +   +   +   +   +
* B     -     -     -
* 
* The red rises from 0 to 10 in ten steps, the green from 
* 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
* 
* In the real program, the color percentages are converted to 
* 0-255 values, and there are 1020 steps (255*4).
* 
* To figure out how big a step there should be between one up- or
* down-tick of one of the LED values, we call calculateStep(), 
* which calculates the absolute gap between the start and end values, 
* and then divides that gap by 1020 to determine the size of the step  
* between adjustments in the value.
*/

int calculateStep(int prevValue, int endValue) {
  int step = endValue - prevValue; // What's the overall gap?
  if (step) {                      // If its non-zero, 
    step = 1020/step;              //   divide by 1020
  } 
  return step;
}

/* The next function is calculateVal. When the loop value, i,
*  reaches the step size appropriate for one of the
*  colors, it increases or decreases the value of that color by 1. 
*  (R, G, and B are each calculated separately.)
*/

int calculateVal(int step, int val, int i) {

  if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
    if (step > 0) {              //   increment the value if step is positive...
      val += 1;           
    } 
    else if (step < 0) {         //   ...or decrement it if step is negative
      val -= 1;
    } 
  }
  // Defensive driving: make sure val stays in the range 0-255
  if (val > 255) {
    val = 255;
  } 
  else if (val < 0) {
    val = 0;
  }
  return val;
}

/* crossFade() converts the percentage colors to a 
*  0-255 range, then loops 1020 times, checking to see if  
*  the value needs to be updated each time, then writing
*  the color values to the correct pins.
*/

void crossFade(int color[3]) {
  // Convert to 0-255
  int R = (color[0] * 255) / 100;
  int G = (color[1] * 255) / 100;
  int B = (color[2] * 255) / 100;

  int stepR = calculateStep(prevR, R);
  int stepG = calculateStep(prevG, G); 
  int stepB = calculateStep(prevB, B);

  for (int i = 0; i <= 1020; i++) {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);

    analogWrite(redPin, redVal);   // Write current values to LED pins
    analogWrite(grnPin, grnVal);      
    analogWrite(bluPin, bluVal); 

    delay(wait); // Pause for 'wait' milliseconds before resuming the loop

  }
  // Update current values for next loop
  prevR = redVal; 
  prevG = grnVal; 
  prevB = bluVal;
  delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}

Ich werde den Code noch weiter auf meine Wünsche anpassen, doch er funktioniert bereits so.

Besten Dank an Alle!