Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- PWM Networked LEDFader v0.8
- 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
- Set to run with Chinese Arduino Nano v3
- */
- #include <math.h>
- #include <LEDFader.h>
- int PWR1, PWR2, PWR3, PWR4, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12;
- int hundreds, tens, ones;
- int Dtens, Dones;
- char *h, *t, *o, *Dt, *Do;
- LEDFader led1 = LEDFader(12);
- LEDFader led2 = LEDFader(11);
- LEDFader led3 = LEDFader(10);
- LEDFader led4 = LEDFader(9);
- void setup() {
- led1.fade(15, 6000);
- led2.fade(15, 6000);
- led3.fade(15, 6000);
- led4.fade(15, 6000);
- // Initialize the digital PWM pins as outputs.
- pinMode(12, OUTPUT); // Set pin as output
- pinMode(11, OUTPUT); // Set pin as output
- pinMode(10, OUTPUT); // Set pin as output
- pinMode(9, OUTPUT); // Set pin as output
- Serial.begin(115200);
- }
- //******************************************************************************
- // The main loop just watches for 12 characters string from Serial
- // It also keeps the LED fade alive if running
- //******************************************************************************
- void loop() {
- led1.update();
- led2.update();
- led3.update();
- led4.update();
- if (Serial.available()>=12) {
- readloop();
- }
- delay(10);
- // Serial.flush();
- }
- //******************************************************************************
- // This loop reads reads and processes the values it receives through Serial
- //******************************************************************************
- void readloop() {
- hundreds = Serial.read();
- *h = hundreds;
- x1 = atoi(h);
- tens = Serial.read();
- *t = tens;
- x2 = atoi(t);
- ones = Serial.read();
- *o = ones;
- x3 = atoi(o);
- hundreds = Serial.read();
- *h = hundreds;
- x4 = atoi(h);
- Dtens = Serial.read();
- *Dt = Dtens;
- x5 = atoi(Dt);
- Dones = Serial.read();
- *Do = Dones;
- x6 = atoi(Do);
- hundreds = Serial.read();
- *h = hundreds;
- x7 = atoi(h);
- Dtens = Serial.read();
- *Dt = Dtens;
- x8 = atoi(Dt);
- Dones = Serial.read();
- *Do = Dones;
- x9 = atoi(Do);
- hundreds = Serial.read();
- *h = hundreds;
- x10 = atoi(h);
- Dtens = Serial.read();
- *Dt = Dtens;
- x11 = atoi(Dt);
- Dones = Serial.read();
- *Do = Dones;
- x12 = atoi(Do);
- Serial.read();
- // Serial.read();
- PWR1 = 100*x1 + 10*x2 + x3; // Compiling the PWM signal
- PWR2 = 100*x4 + 10*x5 + x6;
- PWR3 = 100*x7 + 10*x8 + x9;
- PWR4 = 100*x10 + 10*x11 + x12;
- // 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.
- led1.fade(PWR1, 6000);
- led2.fade(PWR2, 6000);
- led3.fade(PWR3, 6000);
- led4.fade(PWR4, 6000);
- // Output values it has received
- Serial.println("Value is: ");
- Serial.println(PWR1);
- Serial.println(PWR2);
- Serial.println(PWR3);
- Serial.println(PWR4);
- // Flush the serial buffer to accept new input
- Serial.flush();
- }
Advertisement
Add Comment
Please, Sign In to add comment