ademosh

v0.00001

May 2nd, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #define NUM_LEDS 4
  2. #include<SoftwareSerial.h>
  3. SoftwareSerial s(3, 1);
  4. #include "FastLED.h"
  5. #define PIN 13
  6. CRGB leds[NUM_LEDS];
  7. int red = 255;
  8. int green = 255;
  9. int blue = 255;
  10. int del = 1000;
  11. int temp = 0;
  12. int buf = 0;
  13. int count = 1000;
  14.  
  15. //Вспомогательные функции
  16.  
  17. int asciiToInt (int ascii) {
  18. //АСКИ код нуля - 48
  19. return ascii - 48;
  20. }
  21.  
  22.  
  23. int serialToComand () {
  24. int tempSerial = Serial.read();
  25. int controlcommand = 0;
  26. while (tempSerial != -1) {
  27. controlcommand = controlcommand * 10 + asciiToInt(tempSerial);
  28. tempSerial = Serial.read();
  29. Serial.println("");
  30. Serial.print("tempSerial()=");
  31. Serial.print(tempSerial);
  32. Serial.println("");
  33. }
  34. return controlcommand;
  35. }
  36.  
  37.  
  38. CRGB ledRGB (CRGB leds, int red, int green, int blue)
  39. {
  40. leds.r = red;
  41. leds.g = green;
  42. leds.b = blue;
  43. return leds;
  44. }
  45. void setup() {
  46. FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  47. FastLED.setBrightness(50);
  48. pinMode(13, OUTPUT);
  49. Serial.begin(9600);
  50. }
  51. //Режим радуги
  52.  
  53.  
  54. //Режим какого-то определенного цвета
  55. void flaslight_mode_0 () {
  56. long inputColors=serialToComand;
  57. blue=inputColors%1000;
  58. inputColors=inputColors/1000;
  59. green=inputColors%1000;
  60. inputColors=inputColors/1000;
  61. red=inputColors;
  62. Serial.println(inputColors);
  63. for (int i = 0; i < NUM_LEDS; i++ ) {
  64. leds[i] = ledRGB(leds[i], red, green, blue);
  65. }
  66. FastLED.show();
  67. delay(100);
  68. }
  69.  
  70.  
  71. void flaslight_mode_1 () {
  72. byte counter;
  73. for (int i = 0; i < NUM_LEDS; i++ ) {
  74. leds[i] = ledRGB(leds[i], red, green, blue);
  75. }
  76. FastLED.show();
  77. delay (del);
  78. for (int i = 0; i < NUM_LEDS; i++ ) {
  79. leds[i] = ledRGB(leds[i], 0, 0, 0);
  80. }
  81. FastLED.show();
  82. delay(del); // скорость движения радуги
  83. }
  84.  
  85.  
  86.  
  87. void loop() {
  88. int tempSerial = Serial.read();
  89. if (tempSerial != -1) {
  90. Serial.println("");
  91. Serial.print("tempSerial()=");
  92. Serial.print(tempSerial);
  93. Serial.println("");
  94. int controlcommand = 0;
  95. while (tempSerial != -1) {
  96. controlcommand = controlcommand * 10 + asciiToInt(tempSerial);
  97. tempSerial = Serial.read();
  98. Serial.println("");
  99. Serial.print("tempSerial()=");
  100. Serial.print(tempSerial);
  101. Serial.println("");
  102. }
  103. tempSerial = controlcommand;
  104. }
  105. if (tempSerial != -1) Serial.println(tempSerial);
  106. if ((buf != tempSerial) && (tempSerial >= 0)) {
  107. buf = tempSerial;
  108. Serial.println("");
  109. Serial.println("Buf=");
  110. Serial.print(buf);
  111. Serial.println("tempSerial()=");
  112. Serial.print(tempSerial);
  113. Serial.println("");
  114. }
  115. switch (buf) {
  116. case 100:
  117. Serial.println("Mode 0");
  118. flaslight_mode_0();
  119. break;
  120. case 101:
  121. Serial.println("Mode 1");
  122. flaslight_mode_1();
  123. break;
  124. }
  125. }
Add Comment
Please, Sign In to add comment