Hello dear People!
Since my Girlfriends birthday is about to come up, I decided to build her a neat little mood lamp that automaticaly fades through all the different colours of the rainbow. I am using a RGB LED Strip and I want to controle it with my attiny45 since i have still a couple of those lying around.
I ordered an RGB Strip but I wnated to have the attiny ready when it it arrives so i bought a cheap RGB LED at my local electronic store.
I managed to make my LED fade through all the colours. The blending works great except when red changes to blue and green changes to red. Then it's everything else but smooth. It just switches to blue or red really fast.
Here's my code:
#define GREEN 0
#define BLUE 1
#define RED 2
#define delayTime 40
void setup()
{
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(RED, HIGH);
}
int redVal=0;
int blueVal=255;
int greenVal=255;
void loop()
{
for(int i = 0; i < 255; i += 1){
redVal +=1;
blueVal -=1;
delay(delayTime);
update();
}
for(int i = 0; i < 255; i += 1){
blueVal +=1;
greenVal -=1;
delay(delayTime);
update();
}
for(int i = 0; i < 255; i += 1){
greenVal +=1;
redVal -=1;
delay(delayTime);
update();
}
}
void update(){
analogWrite(RED,redVal);
analogWrite(GREEN,greenVal);
analogWrite(BLUE,blueVal);
}
I'd appreciate your help and hope i've made myself clear. If niot so, pleas feel free to ask any further questions.
Thank you in advance.
Jocobes
Looks like colour variables is allowed to either overflow or underflow.
They shoul not be allowed to become less than 0 (negative) or roll over 255.
Throw a sharp eye on the value of these variables, when you start the for loop, and when you exit the for loop.
If one of them is = 1 when you enter a count up loop, it will overflow.
Try use some code for your monitor and write out the values of your variables for debugging.
In your setup() insert :
Serial.begin(115200);
(check that your monitor is set to same baudrate.)
In your update() insert these monitor print lines :
Serial.print("redval, greenval, blueval ="); Serial.print("\t");
Serial.print(redval); Serial.print("\t");
Serial.print(greenval); Serial.print("\t");
Serial.println(blueval);
Your varables should continue to count up/down smoothly between 0 & 255, with no sudden shift from 0 to 255 or 255 to 0.
And of course never run over 255 or under 0.
#define GREEN 0
#define BLUE 1
#define RED 2
#define delayTime 40
int redVal=0;
int blueVal=255;
int greenVal=255;
void setup()
{
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(RED, HIGH);
Serial.begin(115200);
}
void loop()
{
for(int i = 0; i < 255; i += 1){
redVal +=1;
blueVal -=1;
delay(delayTime);
update();
}
for(int i = 0; i < 255; i += 1){
blueVal +=1;
greenVal -=1;
delay(delayTime);
update();
}
for(int i = 0; i < 255; i += 1){
greenVal +=1;
redVal -=1;
delay(delayTime);
update();
}
} // end loop
void update()
{
analogWrite(RED,redVal);
analogWrite(GREEN,greenVal);
analogWrite(BLUE,blueVal);
// debug
Serial.print("redVal, greenVal, blueVal ="); Serial.print("\t");
Serial.print(redVal); Serial.print("\t");
Serial.print(greenVal); Serial.print("\t");
Serial.println(blueVal);
}
PS : I really like the idea of giving your beloved such a gift 
Hey there!
Thanks for your quick reply.
I am relatively new to all of this so I'm struggling with setting up my serial connection with my attiny. The baud-rate matches my monitor but the only output I get is a huge array of "á"'s.
any suggestions?
cheers
Try a lower baudrate : 9600
Also I tried making sure that each of the colour variables has the right value before entering the for loop. However, I still get thess hard jumps from red to blue and green to red.
#define GREEN 0
#define BLUE 1
#define RED 2
#define delayTime 40
int redVal=0;
int blueVal=255;
int greenVal=255;
void setup()
{
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(RED, HIGH);
Serial.begin(9600);
}
void loop()
{
for(int i = 0; i < 255; i += 1){
redVal +=1;
blueVal -=1;
delay(delayTime);
update();
}
redVal=255;
blueVal=0;
greenVal=255;
for(int i = 0; i < 255; i += 1){
blueVal +=1;
greenVal -=1;
delay(delayTime);
update();
}
redVal=255;
blueVal=255;
greenVal=0;
for(int i = 0; i < 255; i += 1){
greenVal +=1;
redVal -=1;
delay(delayTime);
update();
}
redVal=0;
blueVal=255;
greenVal=255;
} // end loop
void update()
{
analogWrite(RED,redVal);
analogWrite(GREEN,greenVal);
analogWrite(BLUE,blueVal);
// debug
Serial.print("redVal, greenVal, blueVal ="); Serial.print("\t");
Serial.print(redVal); Serial.print("\t");
Serial.print(greenVal); Serial.print("\t");
Serial.println(blueVal);
}
Something else is garbaging your monitor data.
Do you get the straight text "redVal, greenVal, blueVal =" on the monitor ?
You are using outpin pins 0, 1, 2
eeeerr ...
0 and 1 are also used as RX, TX for the serial communication.
Thats why the serial monitor test dont work.
Next, check which pins are PWM output capable on your ATtiny.
Then correct your colour output pins accordingly.
I am confused now. THere are 8 PIns on my attiny, 2 of which are Ground and ACC and one reset PIN (never quite understodd what this one is for). How about the other 6 Pins? They all have to numbers assigned to them. Maybe you could help me out? Remember I am a newbie!
It depends on how you configure the pins in your setup(), and how you use the pins in your code.
Attiny will use the code to select the function on the addressed pin.
Looking in Atmega datasheet, there seems to be more options on the pin functions, than you have submitted here ?
You are defining the pins as digital output, which is correct.
But you shoud use AnalogWrite to the pins, instead of DigitalWrite, to make the pins function as PWM outputs.
0 and 1 are also used as RX, TX for the serial communication
I don't think the Attiny85 has any hardware serial capabilities. I think serial communication can be accomplished but it heeds to be done through software. - Scotty
scottyjr:
I don't think the Attiny85 has any hardware serial capabilities. I think serial communication can be accomplished but it heeds to be done through software. - Scotty
You are rigth scotty, 0 & 1 are not dedicated to RX, TX on Attiny.
It depends on the code and the ISP.
Lets skip the monitor output for now, and concentrate on the problem : do we have access to 3 PWM output ports on Attiny45, and are they addressed as so ?
Would you mind just telling me where I need to plug in my LED'S and how to define them in my code? I'm kinda stuck here...
page 60 in this document gives a table on the pin functions :
Complte Attiny45 datasheet (pdf)
The pin functions are addressed by their port address, not by their physical pin number on the chip, just like all other Arduino family members.
This means you should try define your 3 LEDs on these ouput pins :
#define GREEN 0
#define BLUE 1
#define RED 3
Because you have timer/counter 0 output on 0 & 1, and timer/counter 1 ouputs on 3 & 4.
Timer/counter pins have the PWM capability, which fits the shortform Attiny info of 4 outputs with PWM capability.
The physical connections would then be :
GREEN 0 = Attiny chip pin 5
BLUE 1 = Attiny chip pin 6
RED 3 = Attiny chip pin 3
I'm sorry for the confusion, as I also had to learn how Attiny is build ...
Well also I am really sorry that I am not much of a help. I'm a bit overwhelmed by the amount of knowledge I am trying to underhand here
Alright thank you very much!
I will try the build as soon as I am home.
But can I still use my code? (With correction of the pin value off)
As I see it, your problem is the one pin that has to be redefined.
And then use AnalogWrite, instead of DigtialWrite.
Delete the monitor serial junk introduced by me.
- then you should be up and running.
Feedback pls, as I'm still learning too.
Alright, so I did everything according to your instructions and voilá blue and green are fading perfectly smooth. However, the Red LED doesn't vene light up. I've checked connections andd everything.. any ideas?.
Here's my code once again
#define GREEN 0
#define BLUE 1
#define RED 3
#define delayTime 40
int redVal=0;
int blueVal=255;
int greenVal=255;
void setup()
{
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
analogWrite(GREEN, HIGH);
analogWrite(BLUE, HIGH);
analogWrite(RED, HIGH);
}
void loop()
{
for(int i = 0; i < 255; i += 1){
redVal +=1;
blueVal -=1;
delay(delayTime);
update();
}
for(int i = 0; i < 255; i += 1){
blueVal +=1;
greenVal -=1;
delay(delayTime);
update();
}
for(int i = 0; i < 255; i += 1){
greenVal +=1;
redVal -=1;
delay(delayTime);
update();
}
} // end loop
void update()
{
analogWrite(RED,redVal);
analogWrite(GREEN,greenVal);
analogWrite(BLUE,blueVal);
}
For double checking hardware connections :
GREEN 0 = P0
BLUE 1 = P1
RED 3 = P3
If its all ok, then try redefine RED :
RED 4 = P4
oh yeah i actuall yhad it cionnectied to the physical pin 3...
so now it works, however, i still do not get a smooth fade 