#define F_CPU 9600000
#include <ctype.h>
#include <avr/io.h>
//#include <avr/iom8.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/wdt.h>
#include <avr/delay.h>
#include <stdlib.h>
#include "usiTwiSlave.h"
#define outp(a, b) b = a
#define i2cAddress 0x18
#define DDRB_MASK (1<< PB1)|(1<< PB3)|(1<< PB4)
#define MAX 511
#define TIME 1
int red = 0;
int green = 0;
int blue = 0;
int h = 0;
int brightness = 100;
void hue2rgb(int hue){
int hd = hue / 42; // 42 == 252/6, 252 == H_MAX
int hi = hd % 6; // gives 0-5
int f = hue % 42;
int fs = f * 6;
switch( hi ) {
case 0:
red = 252; green = fs; blue = 0;
break;
case 1:
red = 252-fs; green = 252; blue = 0;
break;
case 2:
red = 0; green = 252; blue = fs;
break;
case 3:
red = 0; green = 252-fs; blue = 252;
break;
case 4:
red = fs; green = 0; blue = 252;
break;
case 5:
red = 252; green = 0; blue = 252-fs;
break;
}
}
void delayms(uint16_t millis) {
uint16_t loop;
while ( millis ) {
_delay_ms(1);
millis--;
}
}
void setup(){
DDRB = DDRB_MASK;
TCCR0A=_BV(WGM02)|_BV(COM0B1);
TCCR0B=_BV(CS00)|_BV(COM0A1);
}
int main(void) {
setup();
int i = 0;
uint8_t rxData;
while(1) {
for(int j = 0; j<256; j++){
hue2rgb(j);
OCR0B=red * (brightness/100);
OCR1B=green * (brightness/100);
delayms(1);
}
}
return 0;
}
/*
for(i=0;i<MAX;i++){
OCR1A=i; //works for blue, PB3
OCR1B=i; //green
OCR0A=i/4;
delayms(TIME);
}
for(i=MAX;i>0;i--){
OCR1A=i; //works for blue, PB3
OCR1B=i;
OCR0A=i/4;
delayms(TIME);
}
*/