Components and supplies
1
RGB Diffused Common Cathode
1
Arduino Mini Pro - ATmega168 5V
1
Tilt Switch
1
SparkFun USB Mini-B Breakout
Project description
Code
Mincraft Cube Nightlight
c_cpp
1int redpin = 9; //select the pin for the red LED 2int greenpin = 10;// select the pin for the green LED 3int bluepin = 11; // select the pin for the blue LED 4const int buttonPin = 2; // the number of the pushbutton pin 5int buttonState; // the current reading from the input pin 6int count = 0; 7int r = 0; 8int g = 0; 9int b = 0; 10 11void setup() { 12 pinMode(buttonPin, INPUT_PULLUP); 13 pinMode(redpin, OUTPUT); 14 pinMode(bluepin, OUTPUT); 15 pinMode(greenpin, OUTPUT); 16 Serial.begin(115200); 17 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); // Set intial state to all off 18 } 19 20void loop() { 21 22 // read the state of the switch into a local variable: 23 int buttonState = digitalRead(buttonPin); 24 if(buttonState == LOW){ 25 delay(500); 26 count++; 27 } 28 if (count >=13){ 29 count=0; 30 } 31 if (count==0){ 32 blacktodiamond(); 33 count=1; 34 } 35 if (count==1){ 36 diamond(); 37 } 38 if (count==2){ 39 diamondtoiron(); 40 count=3; 41 } 42 if (count==3){ 43 iron(); 44 } 45 if (count==4){ 46 irontoemerald(); 47 count=5; 48 } 49 if (count==5){ 50 emerald(); 51 } 52 if (count==6){ 53 emeraldtolapis(); 54 count=7; 55 } 56 if (count==7){ 57 lapis(); 58 } 59 if (count==8){ 60 lapistoredstone(); 61 count=9; 62 } 63 if (count==9){ 64 redstone(); 65 } 66 if (count==10){ 67 redstonetogold(); 68 count=11; 69 } 70 if (count==11){ 71 gold(); 72 } 73 if (count==12){ 74 goldtodiamond(); 75 count=13; 76 } 77 if (count==13){ 78 diamond(); 79 } 80 delay(200); 81} 82 83 84// Preset Colours 85void blacktodiamond(){ 86 for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 2) { 87 // sets the value (range from 0 to 255): 88 r=r+2; 89 if(r>=255){r=255;} 90 g=g+2; 91 if(g>=200){g=200;} 92 b=b+2; 93 if(b>=255){b=255;} 94 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 95 // wait for 30 milliseconds to see the dimming effect 96 delay(30); 97 } 98} 99 100// Blue/White Diamond 101void diamond(){ 102 r=255; g=200; b=255; 103 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 104} 105 106void diamondtoiron(){ 107 for (int fadeValue = 0 ; fadeValue <= 254; fadeValue += 5) { 108 // sets the value (range from 0 to 255): 109 b=b-3; 110 if(b<=100){b=100;} 111 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 112 // wait for 30 milliseconds to see the dimming effect 113 delay(30); 114 } 115} 116 117// White Iron 118void iron(){ 119 r=255; g=200; b=100; 120 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 121} 122void irontoemerald(){ 123 for (int fadeValue = 0 ; fadeValue <= 254; fadeValue += 5) { 124 // sets the value (range from 0 to 255): 125 r=r-5; 126 g=g+5; if(g>=255){g=255;} 127 b=b-5; if(b<=0){b=0;} 128 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 129 // wait for 30 milliseconds to see the dimming effect 130 delay(30); 131 } 132} 133// Green Emerald 134void emerald(){ 135 r=0; g=255; b=0; 136 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 137} 138void emeraldtolapis(){ 139 for (int fadeValue = 0 ; fadeValue <= 254; fadeValue += 5) { 140 // sets the value (range from 0 to 255): 141 g=g-5; 142 b=b+5; 143 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 144 // wait for 30 milliseconds to see the dimming effect 145 delay(30); 146 } 147} 148// Blue Lapis 149void lapis(){ 150 r=0; g=0; b=255; 151 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 152} 153void lapistoredstone(){ 154 for (int fadeValue = 0 ; fadeValue <= 254; fadeValue += 5) { 155 // sets the value (range from 0 to 255): 156 r=r+5; 157 b=b-5; 158 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 159 // wait for 30 milliseconds to see the dimming effect 160 delay(30); 161 } 162} 163 164// Red Redstone 165void redstone(){ 166 r=255; g=0; b=0; 167 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 168} 169void redstonetogold(){ 170 for (int fadeValue = 0 ; fadeValue <= 100; fadeValue += 5) { 171 // sets the value (range from 0 to 255): 172 g=g+5; 173 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 174 // wait for 30 milliseconds to see the dimming effect 175 delay(30); 176 } 177} 178 179// Yellow Gold 180void gold(){ 181 r=255; g=100; b=0; 182 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 183} 184 185void goldtodiamond(){ 186 for (int fadeValue = 0 ; fadeValue <= 254; fadeValue += 5) { 187 // sets the value (range from 0 to 255): 188 g=g+3; 189 if(g>=200){g=200;} 190 b=b+5; 191 if(b>=255){b=255;} 192 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 193 // wait for 30 milliseconds to see the dimming effect 194 delay(30); 195 } 196} 197 198 199void fadetoblack(){ 200 for (int fadeValue = 0 ; fadeValue <= 254; fadeValue += 5) { 201 // sets the value (range from 0 to 255): 202 r=r-5; 203 g=g-5; 204 b=b-5; 205 analogWrite(9, r); analogWrite(10, g); analogWrite(11, b); 206 // wait for 30 milliseconds to see the dimming effect 207 delay(30); 208 } 209} 210
Comments
Only logged in users can leave comments