Advertisement
Guest User

RGB Code ATTiny85

a guest
Apr 30th, 2015
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. const int RED=4;
  2. const int GREEN=0;
  3. const int BLUE=1;
  4. const int SENSE=3;
  5. const int threshold=4;
  6. const int rate=120;
  7. boolean dark=false;
  8. float cycle_time=float(rate)*768/3600000.0; //rate*768 is ms in rainbow, 3600000.0 is ms in hour
  9. float timeout_hours=8; //must be divided by x.0 (e.g. "1/15.0")to get accurate result if not an integer
  10. int cycles_to_run=int(timeout_hours/cycle_time)+1;
  11. int cycle_counter=0;
  12.  
  13. void setup() {
  14. pinMode(RED,OUTPUT);
  15. pinMode(GREEN,OUTPUT);
  16. pinMode(BLUE,OUTPUT);
  17. changeColor(0,0,0);
  18. }
  19.  
  20. void loop() {
  21. if ((analogRead(SENSE)<threshold)&&(!dark)) {
  22. dark=true;
  23. }
  24. else if ((analogRead(SENSE)>=threshold)&&(dark)) {
  25. dark=false;
  26. cycle_counter=0;
  27. }
  28. if ((dark)&&(cycle_counter<cycles_to_run)) {
  29. for (int i=0;i<256;i++) {
  30. changeColor(255-i,i,0);
  31. delay(rate);
  32. }
  33. for (int j=0;j<256;j++) {
  34. changeColor(0,255-j,j);
  35. delay(rate);
  36. }
  37. for (int k=0;k<256;k++) {
  38. changeColor(k,0,255-k);
  39. delay(rate);
  40. }
  41. cycle_counter++;
  42. }
  43. else {
  44. changeColor(0,0,0);
  45. delay(50);
  46. }
  47. }
  48.  
  49. void changeColor(int r,int g,int b) {
  50. analogWrite(RED,255-r);
  51. analogWrite(GREEN,255-g);
  52. analogWrite(BLUE,255-b);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement