Quindor

LEDdimmer_v0.9

Dec 23rd, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. /*
  2. PWM Networked LEDFader v0.8
  3. Watches for 12 character serial communication - first 3 characters are PWM signal for pin 12, second set of 3 for pin 11, third for pin 10 and last set for pin 09. Values of 000-255 are accepted
  4. Set to run with Chinese Arduino Nano v3
  5. */
  6.  
  7. #include <math.h>
  8. #include <LEDFader.h>
  9. int PWR1, PWR2, PWR3, PWR4, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12;
  10. int hundreds, tens, ones;
  11. int Dtens, Dones;
  12. char *h, *t, *o, *Dt, *Do;
  13. LEDFader led1 = LEDFader(12);
  14. LEDFader led2 = LEDFader(11);
  15. LEDFader led3 = LEDFader(10);
  16. LEDFader led4 = LEDFader(9);
  17.  
  18.  
  19. void setup() {
  20. led1.fade(15, 6000);
  21. led2.fade(15, 6000);
  22. led3.fade(15, 6000);
  23. led4.fade(15, 6000);
  24. // Initialize the digital PWM pins as outputs.
  25. pinMode(12, OUTPUT); // Set pin as output
  26. pinMode(11, OUTPUT); // Set pin as output
  27. pinMode(10, OUTPUT); // Set pin as output
  28. pinMode(9, OUTPUT); // Set pin as output
  29. Serial.begin(115200);
  30. }
  31.  
  32.  
  33. //******************************************************************************
  34. // The main loop just watches for 12 characters string from Serial
  35. // It also keeps the LED fade alive if running
  36. //******************************************************************************
  37. void loop() {
  38. led1.update();
  39. led2.update();
  40. led3.update();
  41. led4.update();
  42. if (Serial.available()>=12) {
  43. readloop();
  44. }
  45. delay(10);
  46. // Serial.flush();
  47. }
  48.  
  49.  
  50. //******************************************************************************
  51. // This loop reads reads and processes the values it receives through Serial
  52. //******************************************************************************
  53. void readloop() {
  54. hundreds = Serial.read();
  55. *h = hundreds;
  56. x1 = atoi(h);
  57. tens = Serial.read();
  58. *t = tens;
  59. x2 = atoi(t);
  60. ones = Serial.read();
  61. *o = ones;
  62. x3 = atoi(o);
  63. hundreds = Serial.read();
  64. *h = hundreds;
  65. x4 = atoi(h);
  66. Dtens = Serial.read();
  67. *Dt = Dtens;
  68. x5 = atoi(Dt);
  69. Dones = Serial.read();
  70. *Do = Dones;
  71. x6 = atoi(Do);
  72. hundreds = Serial.read();
  73. *h = hundreds;
  74. x7 = atoi(h);
  75. Dtens = Serial.read();
  76. *Dt = Dtens;
  77. x8 = atoi(Dt);
  78. Dones = Serial.read();
  79. *Do = Dones;
  80. x9 = atoi(Do);
  81. hundreds = Serial.read();
  82. *h = hundreds;
  83. x10 = atoi(h);
  84. Dtens = Serial.read();
  85. *Dt = Dtens;
  86. x11 = atoi(Dt);
  87. Dones = Serial.read();
  88. *Do = Dones;
  89. x12 = atoi(Do);
  90. Serial.read();
  91. // Serial.read();
  92.  
  93.  
  94. PWR1 = 100*x1 + 10*x2 + x3; // Compiling the PWM signal
  95. PWR2 = 100*x4 + 10*x5 + x6;
  96. PWR3 = 100*x7 + 10*x8 + x9;
  97. PWR4 = 100*x10 + 10*x11 + x12;
  98.  
  99.  
  100. // Enage LEDfader library to fade LEDs to the desired PWM value. It will automatically determine the amount of steps to take within the given amount of ms.
  101. led1.fade(PWR1, 6000);
  102. led2.fade(PWR2, 6000);
  103. led3.fade(PWR3, 6000);
  104. led4.fade(PWR4, 6000);
  105. // Output values it has received
  106. Serial.println("Value is: ");
  107. Serial.println(PWR1);
  108. Serial.println(PWR2);
  109. Serial.println(PWR3);
  110. Serial.println(PWR4);
  111. // Flush the serial buffer to accept new input
  112. Serial.flush();
  113. }
Advertisement
Add Comment
Please, Sign In to add comment