Giter Site home page Giter Site logo

Comments (8)

oddstr13 avatar oddstr13 commented on May 31, 2024

After a few hours of pulling my hair, I've managed to figure this out.
Finally, three PWM outputs!
Here is a Random fading RGB LED, now finally fading also it's B from the Attiny85.
Based on the fade example sketch that comes with the Arduino.

/*
 Fade

 This example shows how to fade an LED on pin 9
 using the analogWrite() function.

 This example code is in the public domain.
 */

int ledR = 0;           // the pin that the LED is attached to
int ledG = 1;
int ledB = 4;

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 9 to be an output:
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);
  pinMode(ledB, OUTPUT);
  randomSeed(analogRead(0));
  //Serial.begin(115200);
  GTCCR |= 0b01010000;
  GTCCR &= 0b11011111;
} 

byte valueR = 0;
byte valueG = 0;
byte valueB = 0;
byte targetR = 0;
byte targetG = 0;
byte targetB = 0;

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 9:
  analogWrite(ledR, valueR);
  analogWrite(ledG, valueG);
  //analogWrite(ledB, valueB);
  OCR1B = valueB;
  if (valueR == targetR && valueG == targetG && valueB == targetB) {
      //Serial.println("Change done.");
      //delay(30);
      targetR = (byte) random(255);
      targetG = (byte) random(255);
      targetB = (byte) random(255);

      /*
      Serial.print("0x");
      char hexR[3];
      char hexG[3];
      char hexB[3];
      sprintf(hexR, "%02x", targetR);
      sprintf(hexG, "%02x", targetG);
      sprintf(hexB, "%02x", targetB);
      Serial.print(hexR);
      Serial.print(hexG);
      Serial.print(hexB);
      Serial.println();
      */
  }
  if (valueR < targetR) {
      valueR += 1;
  } else if (valueR > targetR) {
      valueR -= 1;
  }
  if (valueG < targetG) {
      valueG += 1;
  } else if (valueG > targetG) {
      valueG -= 1;
  }
  if (valueB < targetB) {
      valueB += 1;
  } else if (valueB > targetB) {
      valueB -= 1;
  }

  // Adjust delay to change cycling speed
  delay(10);                            
}

I hope this saves the next poor soul coming along from a few hours of pulling their hair :)
Also please correct me if i missunderstood the GTCCR flags, but it seems to be working for me

from attiny.

mrakuaku avatar mrakuaku commented on May 31, 2024

Thanks so much @oddstr13! Works great for me! and yes it saved me from going bald :) Which is the actual line that is activating PB4 as PWM? is it
" GTCCR |= 0b01010000;
GTCCR &= 0b11011111;"
or
"OCR1B = valueB;"
... or another one?

from attiny.

oddstr13 avatar oddstr13 commented on May 31, 2024

@mrakuaku the GTCCR activates PWM on the pin, OCR1B = value; is basicly analogWrite(ledB, value);

from attiny.

damellis avatar damellis commented on May 31, 2024

I fixed this for Arduino 1.5.x with 4284860

from attiny.

mrakuaku avatar mrakuaku commented on May 31, 2024

@oddstr13, the red and green LEDs never seem to go on at the same time. Could this be because the PWM's are interfering with each other somehow? I tried another colour fading code, which I modified to include
"GTCCR |= 0b01010000;
GTCCR &= 0b11011111;"
and
"OCR1B = valueB;"
and noticed that they can fade in and out by them selves, and with the blue, but not with each other.
Do you have any suggestions on how to fix this?

Thanks

from attiny.

damellis avatar damellis commented on May 31, 2024

I believe Arduino 1.0.6 adds support for initVariant(), so it should be possible to get the third PWM working there too (along with 1.5.x). Anyone want to take a shot at it?

from attiny.

oddstr13 avatar oddstr13 commented on May 31, 2024

@mrakuaku First off, let me appologize for my late reply.
Make sure you use individual resistors for each of the colors, not one on the common lead. Using just one resistor results in erratic behavior. This puzzled me for a while, and I should probably have mentioned that in the original message.

I should also note that I have not worked on this since my initial experiments a year ago.

from attiny.

damellis avatar damellis commented on May 31, 2024

Fixed for 1.0.6 too.

from attiny.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.