Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1.  
  2. #define R 9
  3. #define G 10
  4. #define B 11
  5. #define B1 4
  6. #define B2 5
  7. #define POT A0
  8. int s0 = 0;
  9. int s1 = 1;
  10. int c[3] = {0, 0, 0};
  11.  
  12. class Color {
  13.   public:
  14.     int r, g, b;
  15.     void update(int r, int g, int b);
  16.     Color(int r, int g, int b);
  17. };
  18.  
  19. class Ledstring {
  20.   public:
  21.     int r_pin, g_pin, b_pin; //attached pin
  22.     int r, g, b; //colors
  23.     int start = 0;
  24.     int in = 0;
  25.     Ledstring(int, int, int);
  26.     void off();
  27.     //ALERT:these functions just load the value in the buffer
  28.     //call the show function to show off the buffer values
  29.     void write(int, int, int);
  30.     void write(Color*);
  31.     void show();
  32.     void init();
  33.  
  34.     //these are premade animations (very simple ones)
  35.     void naivefade(int);
  36.     void blink();//blink uses the buffer color
  37.     void plasma();
  38.     void fixed();
  39.     void autome();
  40.  
  41.     //some utilities
  42.     void b2C(Color*);
  43. };
  44.  
  45. Color black(0, 0, 0);
  46. Color white(255, 255, 255);
  47.  
  48. Ledstring strip(9, 10, 11);
  49.  
  50.  
  51.  
  52. void setup() {
  53.  
  54.   strip.init();
  55.   pinMode(B1, INPUT_PULLUP);
  56.   pinMode(B2, INPUT_PULLUP);
  57. }
  58.  
  59. void loop() {
  60.  
  61.   strip.autome();
  62.   if (digitalRead(B1) == LOW) {
  63.     while (digitalRead(B1) == LOW) {}
  64.     if (s0 < 4) {
  65.       s0++;
  66.     }
  67.     else s0 = 0;
  68.   }
  69.   if (digitalRead(B2) == LOW) {
  70.     while (digitalRead(B2) == LOW) {}
  71.     if (s1 < 2) {
  72.       s1++;
  73.     }
  74.     else s1 = 0;
  75.     c[s1] = map(analogRead(POT), 0, 1024, 0, 255);
  76.   }
  77.  
  78.  
  79. }
  80.  
  81.  
  82. void Color::update(int r, int g, int b) {
  83.   this->r = r;
  84.   this->g = g;
  85.   this->b = b;
  86. }
  87. Color::Color(int r, int g, int b) {
  88.   this->update(r, g, b);
  89. }
  90. Ledstring::Ledstring(int r, int g, int b) {
  91.   this->r_pin = r;
  92.   this->g_pin = g;
  93.   this->b_pin = b;
  94. }
  95. void Ledstring::init() {
  96.   pinMode(this->r_pin, OUTPUT);
  97.   pinMode(this->g_pin, OUTPUT);
  98.   pinMode(this->b_pin, OUTPUT);
  99. }
  100. void Ledstring::write(int r, int g, int b) {
  101.   this->r = r;
  102.   this->g = g;
  103.   this->b = b;
  104. }
  105. void Ledstring::write(Color* col) {
  106.   this->r = col->r;
  107.   this->g = col->g;
  108.   this->b = col->b;
  109. }
  110. void Ledstring::show() {
  111.   analogWrite(this->r_pin, this->r);
  112.   analogWrite(this->g_pin, this->g);
  113.   analogWrite(this->b_pin, this->b);
  114. }
  115. void Ledstring::off() {
  116.   this->write(&black);
  117.   this->show();
  118. }
  119.  
  120.  
  121. void Ledstring::fixed() {
  122.   this->show();
  123. }
  124. void Ledstring::blink() {
  125.  
  126.   if (this->start < millis()) {
  127.     if (this->in) {
  128.       this->off();
  129.       this->in = 0;
  130.     }
  131.     else {
  132.       this->write(c[0], c[1], c[2]);
  133.       this->show();
  134.       this->in = 1;
  135.     }
  136.     this->start = millis() + 300;
  137.   }
  138.  
  139. }
  140. void Ledstring::autome() {
  141.   switch (s0) {
  142.     case 0:
  143.       this->fixed();
  144.       break;
  145.     case 1:
  146.       //plasma();
  147.       break;
  148.     case 2:
  149.       this->blink();
  150.       break;
  151.   }
  152.  
  153. }
  154.  
  155.   void Ledstring::b2C(Color * col) {
  156.     col->r = this->r;
  157.     col->g = this->g;
  158.     col->n = this->b;
  159.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement