FIRSTIES
[russ-button.git] / sketch_oct25a / sketch_oct25a.ino
1 //USBtinyISP(ATTinyCore) upload using programmer
2 //Atinny841(Optiboot)
3
4 const int RED = 4;     //LED row 1
5 const int GREEN = 3;     //LED row 2    
6 const int BLUE = 2;     //LED row 3
7
8 static uint16_t hue = 0; // 0-359
9 unsigned long previousMillis = 0;
10 int shift = 0;
11
12 void setup() {
13   previousMillis = millis() + 5000;
14   pinMode(RED, OUTPUT);
15   pinMode(GREEN, OUTPUT);
16   pinMode(BLUE, OUTPUT);
17
18 }
19
20 void loop() {
21   randomSeed(analogRead(0));
22   if (shift==1) {
23     for (int hue=0; hue<360; hue++) {
24       setLedColorHSV(hue,random(1, 5),random(1, 5));
25       delay(random(1, 25));
26     }
27   }
28   if (shift==0) {
29     for (int hue=360; hue>0; hue--) {
30       setLedColorHSV(hue,random(1, 5),random(1, 5));
31       delay(random(1, 25));
32     }
33   }
34   if (((signed long)(millis() - previousMillis)) > 0) {
35     previousMillis = millis() + 3000;
36     long dice = random(1000);
37     if (dice > 900) {
38       glowgreen();
39       shift=1;
40       RGB_color(0,100,0);
41     } else if (dice<100) {
42       glowblue();
43       shift=0;
44       RGB_color(100,0,0);
45     }
46   }
47 }
48
49 void RGB_color(int red_light_value, int green_light_value, int blue_light_value) {
50   analogWrite(RED, red_light_value);
51   analogWrite(GREEN, green_light_value);
52   analogWrite(BLUE, blue_light_value);
53 }
54
55 void setLedColorHSV(int h, double s, double v) {
56   //this is the algorithm to convert from RGB to HSV
57   double r=0; 
58   double g=0; 
59   double b=0;
60
61   double hf=h/60.0;
62
63   int i=(int)floor(h/60.0);
64   double f = h/60.0 - i;
65   double pv = v * (1 - s);
66   double qv = v * (1 - s*f);
67   double tv = v * (1 - s * (1 - f));
68
69   switch (i)
70   {
71   case 0: //rojo dominante
72     r = v;
73     g = tv;
74     b = pv;
75     break;
76   case 1: //verde
77     r = qv;
78     g = v;
79     b = pv;
80     break;
81   case 2: 
82     r = pv;
83     g = v;
84     b = tv;
85     break;
86   case 3: //azul
87     r = pv;
88     g = qv;
89     b = v;
90     break;
91   case 4:
92     r = tv;
93     g = pv;
94     b = v;
95     break;
96   case 5: //rojo
97     r = v;
98     g = pv;
99     b = qv;
100     break;
101   }
102
103   //set each component to a integer value between 0 and 255
104   int red=constrain((int)255*r,0,255);
105   int green=constrain((int)255*g,0,255);
106   int blue=constrain((int)255*b,0,255);
107
108   RGB_color(red,green,blue);
109 }
110
111 void glowgreen() {
112   for(uint16_t t=0; t<255; t++) {
113     RGB_color(0,t,0);
114     delay(random(1, 25));
115   }
116   for(uint16_t t=255; t>0; t--) {
117     RGB_color(0,t,0);
118     delay(random(1, 25));
119   }
120 }
121
122 void glowblue() {
123   for(uint16_t t=0; t<255; t++) {
124     RGB_color(0,0,t);
125     delay(random(1, 25));
126   }
127   for(uint16_t t=255; t>0; t--) {
128     RGB_color(0,0,t);
129     delay(random(1, 25));
130   }
131 }