Instructables

Step 3: Program the chip

Picture of Program the chip
Go here and it will show you how to connect the ATtiny85 to the Arduino and program it. Once you are ready to upload the code to the ATtiny85, paste it into the Arduino IDE and click upload.



//ATtiny85 RGB color fading Mood Light NOW WITH LIGHT SENSING CAPABILITIES!!!

const int redPin = 2; 
const int grnPin = 1; 
const int bluPin = 0;
const int sensor = 3;


void setup()
{
  pinMode(redPin, OUTPUT);    
  pinMode(grnPin, OUTPUT);    
  pinMode(bluPin, OUTPUT);
  pinMode(sensor, INPUT);
}

void loop() {
  if (analogRead(sensor) <= 200)
  {
    redtoyellow();
    yellowtogreen();
    greentocyan();
    cyantoblue();
    bluetomagenta();
    magentatored();
  }
  else if (analogRead(sensor) >= 201)
  {
    digitalWrite(redPin, LOW);
    digitalWrite(grnPin, LOW);
    digitalWrite(bluPin, LOW);
  }
}

void redtoyellow()
{
  digitalWrite(redPin, HIGH);
  digitalWrite(bluPin, LOW);

  // fade up green
  for(byte i=1; i<100; i++) {
    byte on  = i;
    byte off = 100-on;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(grnPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(grnPin, LOW);
      delayMicroseconds(off);
    }
  }
}



void yellowtogreen()
{
  digitalWrite(grnPin, HIGH);
  digitalWrite(bluPin, LOW);

  // fade down red
  for(byte i=1; i<100; i++) {
    byte on  = 100-i;
    byte off = i;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(redPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(redPin, LOW);
      delayMicroseconds(off);
    }
  }
}


void greentocyan()
{
  digitalWrite(grnPin, HIGH);
  digitalWrite(redPin, LOW);

  // fade up blue
  for(byte i=1; i<100; i++) {
    byte on  = i;
    byte off = 100-on;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(bluPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(bluPin, LOW);
      delayMicroseconds(off);
    }
  }
}



void cyantoblue()
{
  digitalWrite(bluPin, HIGH);
  digitalWrite(redPin, LOW);

  // fade down green
  for(byte i=1; i<100; i++) {
    byte on  = 100-i;
    byte off = i;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(grnPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(grnPin, LOW);
      delayMicroseconds(off);
    }
  }
}


void bluetomagenta()
{
  digitalWrite(bluPin, HIGH);
  digitalWrite(grnPin, LOW);

  // fade up red
  for(byte i=1; i<100; i++) {
    byte on  = i;
    byte off = 100-on;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(redPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(redPin, LOW);
      delayMicroseconds(off);
    }
  }
}



void magentatored()
{
  digitalWrite(redPin, HIGH);
  digitalWrite(grnPin, LOW);

  // fade down blue
  for(byte i=1; i<100; i++) {
    byte on  = 100-i;
    byte off = i;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(bluPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(bluPin, LOW);
      delayMicroseconds(off);
    }
  }
}
codongolev1 year ago
go where to find out how to connect to the arduino?

Go here to see how to program a ATtiny45/85 with an Arduino

https://www.youtube.com/watch?v=30rPt802n1k

I've improved this code. New features include:

Easier to read and understand
Generalized fadeUp and fadeDown functions
Arbitrary speed control
Easy control of light threshold

Here is the new code:

const int redPin = 2;
const int greenPin = 1;
const int bluePin = 0;
const int sensor = 3;
int lightThresh = 550; // Light threshold. 0 - 1023. Lower number = activation at lower light level.
int time = 50; // Speed control. Higher number = slower fades.


void setup()
{
  pinMode(redPin, OUTPUT);   
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin, OUTPUT);
  pinMode(sensor, INPUT);
}
//fadeUp & fadeDown syntax: (color to fade, color to turn on, color to turn off)
void loop() {
  if (analogRead(sensor) <= lightThresh)
  {
    fadeUp(greenPin, redPin, bluePin);   //red to yellow
    fadeDown(redPin, greenPin, bluePin); //yellow to green
    fadeUp(bluePin, greenPin, redPin);   //green to cyan
    fadeDown(greenPin, bluePin, redPin); //cyan to blue
    fadeUp(redPin, bluePin, greenPin);   //blue to purple
    fadeDown(bluePin, redPin, greenPin); //purple to red
  }
  else if (analogRead(sensor) > lightThresh)
  {
    //turn all colors off
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }
}

void fadeUp(int fadePin, int onPin, int offPin) {
  //set constant colors
  digitalWrite(onPin, HIGH);
  digitalWrite(offPin, LOW);
  //set current brightness out of 1000
  for(int bright = 1; bright < 1000; bright = bright + 10) {
    //set PWM lengths
    int on = bright;
    int off = 1000 - bright;
    //software PWM for 'time' ms
    for(int run = 0; run < time; run++) {
      digitalWrite(fadePin, HIGH);
      delayMicroseconds(on);
      digitalWrite(fadePin, LOW);
      delayMicroseconds(off);
    }
  }
}

void fadeDown(int fadePin, int onPin, int offPin) {
  //set constant colors
  digitalWrite(onPin, HIGH);
  digitalWrite(offPin, LOW);
  //set current brightness out of 1000
  for(int bright = 1; bright < 1000; bright = bright + 10) {
    //set inverse PWM lengths
    int on = 1000 - bright;
    int off = bright;
    //software PWM for 'time' ms
    for(int run = 0; run < time; run++) {
      digitalWrite(fadePin, HIGH);
      delayMicroseconds(on);
      digitalWrite(fadePin, LOW);
      delayMicroseconds(off);
    }
  }
}


I would recommend setting your internal ATtiny85 clock to 8 MHz, instead of the default 1 MHz. This is optional, but it provides you with more accurate control of the fade time, as the chip isn't limited by its slow clock speed. This is easy to do within the Arduino programming environment, and it just as easily reversed. However, it does take more power, and will drain a battery faster. I am running mine on USB 5V, so it isn't a concern. If you are interested, just go here to see how to do it.
mikroskeem1 year ago
Where can i get another version of it? i don't use Adruino IDE, since i program with parralel port