Comments (8)
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.
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.
@mrakuaku the GTCCR activates PWM on the pin, OCR1B = value; is basicly analogWrite(ledB, value);
from attiny.
I fixed this for Arduino 1.5.x with 4284860
from attiny.
@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.
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.
@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.
Fixed for 1.0.6 too.
from attiny.
Related Issues (20)
- Not working with Arduino IDE 1.8.x HOT 6
- delay() is taking a lot of time when setting 8MHZ and 16MHZ internal HOT 1
- Wire.h fails HOT 4
- Attiny library using in my project
- ATtiny45 doesn't connect to Uno HOT 1
- Error Uploading to Arduino
- Error while burning bootloader HOT 3
- add attiny26 HOT 1
- Pin Defines for direct port. EG. PB1, PA3 HOT 6
- Server down
- Board in Arduino IDE doesn't show the hardware
- Naming and confusion with similar projects HOT 1
- Invalid device signature HOT 1
- I2C not working corretly on ATTiny85 clone
- disable external reset HOT 1
- Version 1.0.2 gives "Error resolving FQBN: board"
- installation guide not available HOT 1
- I am getting the following error when i am compiling the code by selecting the board as attiny85, and if i compile the same code by selecting board as arduino nano ,i am getting no error. please help
- avrdude.conf file missing
- ATtiny Arduino IDE Port greyed out 230422
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from attiny.