Advertisement
dioxik

minecraft kostka

May 6th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1.  
  2.  const int ledPin =  LED_BUILTIN;// the number of the LED pin
  3. int redl,greenl,bluel;
  4. int redPin = 11;
  5. int greenPin = 10;
  6. int bluePin = 9;
  7. const int buttonPin = 8;
  8. int buttonState = 0;
  9. const long interval = 30;
  10. const long longlight = 60000;
  11. const long clicktime = 15000;
  12. const long colortime = 5000;
  13. int fade = 0;    // how bright the LED is
  14. int fadeAmount = 5;
  15. int clickcount = 0;
  16. unsigned long previousMillis = 0;
  17. unsigned long longMillis = 0;
  18. unsigned long clickMillis = 0;
  19. unsigned long colorMillis = 0;
  20. boolean animate = false;
  21. boolean light = false;
  22. //#define COMMON_ANODE
  23.  
  24. void setup()
  25. {
  26.   pinMode(ledPin, OUTPUT);
  27.   pinMode(redPin, OUTPUT);
  28.   pinMode(greenPin, OUTPUT);
  29.   pinMode(bluePin, OUTPUT);  
  30.   pinMode(buttonPin, INPUT_PULLUP);
  31.   digitalWrite(ledPin, LOW);
  32.   redl = 255; greenl = 0; bluel = 0;//set red default
  33. }
  34.  
  35. void loop()
  36. {
  37. //  setColor(255, 0, 0);  // red
  38. //  setColor(0, 255, 0);  // green
  39. //  setColor(0, 0, 255);  // blue
  40. //  setColor(255, 255, 0);  // yellow
  41. //  setColor(80, 0, 80);  // purple
  42. //  setColor(0, 255, 255);  // aqua
  43. buttonState = digitalRead(buttonPin); //odczytanie guzika
  44. unsigned long currentMillis = millis();
  45.  
  46.  
  47.  
  48. if(animate ){
  49.  
  50.   if (currentMillis - longMillis <= longlight){
  51. if (currentMillis - previousMillis >= interval) { // animate fade
  52.   previousMillis = currentMillis;
  53.   fade = fade + fadeAmount;
  54.   if (fade == 0 || fade == 255) {
  55.                                   fadeAmount = -fadeAmount ;
  56.                                   }  
  57.     setColorfade(redl, greenl,bluel,fade);                              
  58. }  //fade animation
  59. }else{animate = false;setColor(0,0,0);}}//do animate
  60.  
  61.  
  62. if(light ){
  63.  
  64. while (digitalRead(buttonPin) == HIGH){
  65.  
  66. unsigned long currentMillis = millis();
  67. if (currentMillis - previousMillis >= interval) { // animate fade
  68.   previousMillis = currentMillis;
  69.   fade = fade + fadeAmount;
  70.   if (fade == 0 || fade == 255) {
  71.                                   fadeAmount = -fadeAmount ;
  72.                                   }  
  73.     setColorfade(redl, greenl,bluel,fade);                              
  74. }  //fade animation
  75. }
  76. clickcount = 0;
  77. light = false;setColor(0,0,0);
  78. while (digitalRead(buttonPin) == LOW){}
  79. delay(1000);
  80. }//do light
  81.  
  82.  
  83.  
  84. if (buttonState == LOW) {
  85.   colorMillis =currentMillis ;
  86. while (digitalRead(buttonPin) == LOW){
  87. // TU NAPISZ FUNKCJĘ ZMIANY KOLORÓW
  88. unsigned long currentMillis = millis();
  89. //  setColor(255, 0, 0);  // red
  90. //  setColor(0, 255, 0);  // green
  91. //  setColor(0, 0, 255);  // blue
  92. //  setColor(255, 255, 0);  // yellow
  93. //  setColor(80, 0, 80);  // purple
  94. //  setColor(0, 255, 255);  // aqua
  95. //if (currentMillis - colorMillis >= colortime ) {
  96.   int sec =0;
  97.   int var = currentMillis - colorMillis;
  98.  
  99.  
  100.   sec = sec + 5000;
  101. if(var >sec && var < sec+5000){redl = 255; greenl = 0; bluel = 0; setColor(redl, greenl,bluel);}//SET red  
  102.   sec = sec + 5000;
  103. if(var >sec && var < sec+5000){redl = 0; greenl = 255; bluel = 0; setColor(redl, greenl,bluel);}//SET green  
  104.   sec = sec + 5000;
  105. if(var >sec && var < sec+5000){redl = 0; greenl = 0; bluel = 255; setColor(redl, greenl,bluel);}//SET blue  
  106.   sec = sec + 5000;
  107. if(var >sec && var < sec+5000){redl = 255; greenl = 255; bluel = 0; setColor(redl, greenl,bluel);}//SET yellow  
  108.   sec = sec + 5000;
  109. if(var >sec && var < sec+5000){redl = 80; greenl = 0; bluel = 80; setColor(redl, greenl,bluel);}//SET purple  
  110.   sec = sec + 5000;
  111. if(var >sec && var < sec+5000){redl = 0; greenl = 255; bluel = 255; setColor(redl, greenl,bluel);}//SET aqua  
  112.   sec = sec + 5000;
  113. if(var >sec && var < sec+5000){colorMillis =currentMillis-5000 ;}//start over    
  114. //}
  115. }//loop button
  116.  
  117. setColor(0, 0,0);
  118. colorMillis =0;
  119. clickcount = clickcount + 1;
  120. clickMillis = currentMillis;
  121. } //button on
  122.  
  123. if (clickcount == 1 && !animate){animate = true;longMillis = currentMillis;}
  124. if (clickcount == 2 ){light = true;animate = false;}
  125.  
  126.  
  127. if (currentMillis - clickMillis <= clicktime ){
  128. //clickMillis = currentMillis;
  129.   }else{clickcount = 0;digitalWrite(ledPin, HIGH);}
  130.  
  131. } //loop
  132.  
  133.  
  134.  
  135.  
  136. void setColor(int red, int green, int blue)
  137. {
  138.   #ifdef COMMON_ANODE
  139.     red = 255 - red;
  140.     green = 255 - green;
  141.     blue = 255 - blue;
  142.   #endif
  143.   analogWrite(redPin, red);
  144.   analogWrite(greenPin, green);
  145.   analogWrite(bluePin, blue);  
  146. }
  147. void setColorfade(int red, int green, int blue,int fade)
  148. {
  149.   #ifdef COMMON_ANODE
  150.     red = 255 - red;
  151.     green = 255 - green;
  152.     blue = 255 - blue;
  153.   #endif
  154.   analogWrite(redPin, (red > 0 ) ? (fade>red)?0:red-fade : red);
  155.   analogWrite(greenPin, (green > 0) ? (fade>green)?0:green-fade : green);
  156.   analogWrite(bluePin, (blue>0)?(fade>blue )?0:blue-fade:blue);  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement