Overview

In this lesson, you will build on lesson 3, adding the facility to control the LEDs from your computer using the Arduino Serial Monitor. The serial monitor is the 'tether' between the computer and your ESPcopter- it lets you send and receive text messages, handy for debugging and also controlling the Arduino from a keyboard! For example, you will be able to send commands from your computer to change RGB LEDs. In this lesson, you should a command by using Arduino Serial Monitor. The command format should be redValue,GreenValu,BlueValue. For example, for red 1023,0,0 or for white color 1023,1023,1023 . 
 

Code

Serial RGB controller Reads a serial input string looking for three comma-separated integers with a newline at the end. Values should be between 0 and 1023.
*/

String inString = ""; // string to hold input
int currentColor = 0;
int red, green, blue = 0;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// send an intro:
Serial.println("\n\nString toInt() RGB:");
Serial.println();
// set LED cathode pins as outputs:
pinMode(2, OUTPUT);
pinMode(10, OUTPUT);
pinMode(16, OUTPUT);
// turn on pin 13 to power the LEDs:

}

void loop() {
int inChar;

// Read serial input:
if (Serial.available() > 0) {
inChar = Serial.read();
}

if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}

// if you get a comma, convert to a number,
// set the appropriate color, and increment
// the color counter:
if (inChar == ',') {
// do something different for each value of currentColor:
switch (currentColor) {
case 0: // 0 = red
red = inString.toInt();
// clear the string for new input:
inString = "";
break;
case 1: // 1 = green:
green = inString.toInt();
// clear the string for new input:
inString = "";
break;
}
currentColor++;
}
// if you get a newline, you know you've got
// the last color, i.e. blue:
if (inChar == '\n') {
blue = inString.toInt();

// set the levels of the LED.
// subtract value from 255 because a higher
// analogWrite level means a dimmer LED, since
// you're raising the level on the anode:
analogWrite(10, blue);
analogWrite(2, PWMRANGE-green);
analogWrite(16, red);

// print the colors:
Serial.print("Red: ");
Serial.print(red);
Serial.print(", Green: ");
Serial.print(green);
Serial.print(", Blue: ");
Serial.println(blue);

// clear the string for new input:
inString = "";
// reset the color counter:
currentColor = 0;
}

}

Processing 

There are the multiple ways to change the RGB led color. One of them is processing control. You should connect ESPcopter to your computer and start the below program.

Code

Here's a Processing sketch that will draw a color wheel and send a serial
string with the color you click on:
*/
import processing.serial.*;

int segs = 12;
int steps = 6;
float rotAdjust = TWO_PI / segs / 2;
float radius;
float segWidth;
float interval = TWO_PI / segs;

Serial myPort;

void setup() {
size(200, 200);
background(127);
smooth();
ellipseMode(RADIUS);
noStroke();
// make the diameter 90% of the sketch area
radius = min(width, height) * 0.45;
segWidth = radius / steps;

// swap which line is commented out to draw the other version
// drawTintWheel();
drawShadeWheel();
// open the first serial port in your computer's list
myPort = new Serial(this, Serial.list()[0], 9600);
}
void drawShadeWheel() {
for (int j = 0; j < steps; j++) {
color[] cols = {
color(255-(255/steps)*j, 255-(255/steps)*j, 0),
color(255-(255/steps)*j, (255/1.5)-((255/1.5)/steps)*j, 0),
color(255-(255/steps)*j, (255/2)-((255/2)/steps)*j, 0),
color(255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j, 0),
color(255-(255/steps)*j, 0, 0),
color(255-(255/steps)*j, 0, (255/2)-((255/2)/steps)*j),
color(255-(255/steps)*j, 0, 255-(255/steps)*j),
color((255/2)-((255/2)/steps)*j, 0, 255-(255/steps)*j),
color(0, 0, 255-(255/steps)*j),
color(0, 255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j),
color(0, 255-(255/steps)*j, 0),
color((255/2)-((255/2)/steps)*j, 255-(255/steps)*j, 0)
};
for (int i = 0; i < segs; i++) {
fill(cols[i]);
arc(width/2, height/2, radius, radius,
interval*i+rotAdjust, interval*(i+1)+rotAdjust);
}
radius -= segWidth;
}
}
void drawTintWheel() {
for (int j = 0; j < steps; j++) {
color[] cols = {
color((255/steps)*j, (255/steps)*j, 0),
color((255/steps)*j, ((255/1.5)/steps)*j, 0),
color((255/steps)*j, ((255/2)/steps)*j, 0),
color((255/steps)*j, ((255/2.5)/steps)*j, 0),
color((255/steps)*j, 0, 0),
color((255/steps)*j, 0, ((255/2)/steps)*j),
color((255/steps)*j, 0, (255/steps)*j),
color(((255/2)/steps)*j, 0, (255/steps)*j),
color(0, 0, (255/steps)*j),
color(0, (255/steps)*j, ((255/2.5)/steps)*j),
color(0, (255/steps)*j, 0),
color(((255/2)/steps)*j, (255/steps)*j, 0)
};
for (int i = 0; i < segs; i++) {
fill(cols[i]);
arc(width/2, height/2, radius, radius,
interval*i+rotAdjust, interval*(i+1)+rotAdjust);
}
radius -= segWidth;
}
}

void draw() {
// nothing happens here
}

void mouseReleased() {
// get the color of the mouse position's pixel:
color targetColor = get(mouseX, mouseY);
// get the component values:
int r = int(red(targetColor));
int g = int(green(targetColor));
int b = int(blue(targetColor));
// make a comma-separated string:
String colorString = r + "," + g + "," + b + "\n";
// send it out the serial port:
myPort.write(colorString );


Blynk

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
SimpleTimer timer;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

int bluePin = 10; 
int redPin = 16; 
int greenPin = 2; // This pin is workink reverse because of circuit design..!!



BLYNK_WRITE(V0)
{
 analogWrite(bluePin, param.asInt()*4);
}
BLYNK_WRITE(V1)
{
 analogWrite(redPin, param.asInt()*4);
}
BLYNK_WRITE(V2)
{
 analogWrite(greenPin, PWMRANGE-param.asInt()*4);
}

void setup()
{
 pinMode(2, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(16, OUTPUT);
 Serial.begin(115200);
 Blynk.begin(auth, ssid, pass);
}

void loop()
{
 Blynk.run();
 timer.run(); // Initiates SimpleTimer
}