Lesson 4. Analog Inputs

 

Overview

In this lesson, you will rewrite Blink code. However, your delay function change according to your analog value. To do this lesson, you need a 10k potentiometer. The potentiometer will be connected to the ESPcopter’ analog pin which is 2. You can find out this pin by using ESPcopter’ pin out diagram.

Code

Analog Input

*/

int sensorPin = A0; // select the input pin for the potentiometer
 int ledPin = 10; // select the pin for the LED
 int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
 // declare the ledPin as an OUTPUT:
 pinMode(ledPin, OUTPUT);
 }

void loop() {
 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 // turn the ledPin on
 digitalWrite(ledPin, HIGH);
 // stop the program for <sensorValue> milliseconds:
 delay(sensorValue);
 // turn the ledPin off:
 digitalWrite(ledPin, LOW);
 // stop the program for for <sensorValue> milliseconds:
 delay(sensorValue);
 }

 


Lesson 3. The Serial Monitor

 

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
}

 


}[/vc_message]

Lesson 2. RGB LEDs

 

Overview

In this lesson, you will learn how to use a RGB (Red Green Blue) LED with an Espcopter.

At first glance, RGB (Red, Green, Blue) LEDs look just like regular LEDs, however, inside the usual LED package, there are actually three LEDs, one red, one green and yes, one blue. By controlling the brightness of each of the individual LEDs you can mix pretty much any color you want.
You will use the analogWrite function of Arduino to control the color of the LED.

Code

/*
 RGB
 ESPcopter
 By Metehan Emlik
 */

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

// the setup function runs once when you press reset or power the board
 void setup() {
 // initialize digital pin 13 as an output.
 pinMode(bluePin, OUTPUT);
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 }

// the loop function runs over and over again forever
 void loop() {


 digitalWrite(bluePin, HIGH); // turn the LED on (HIGH is the voltage level)
 digitalWrite(greenPin, HIGH); // turn the LED on (HIGH is the voltage level)
 digitalWrite(redPin, LOW); // turn the LED on (HIGH is the voltage level)
 delay(1000); // wait for a second
 digitalWrite(bluePin, LOW); // turn the LED on (HIGH is the voltage level)
 digitalWrite(greenPin, LOW); // turn the LED on (HIGH is the voltage level)
 digitalWrite(redPin, LOW); // turn the LED on (HIGH is the voltage level)
 delay(1000); // wait for a second
 digitalWrite(bluePin, LOW); // turn the LED on (HIGH is the voltage level)
 digitalWrite(greenPin, HIGH); // turn the LED on (HIGH is the voltage level)
 digitalWrite(redPin, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(1000); // wait for a second
 }

 


Lesson 1. Blink

Overview

 

In this lesson, you will learn how to program your Espcopter to make the Esp8266’s built-in LED blink.

Code

/*
 Blink
 ESPcopter 
 By Metehan Emlik
 */
 
 int bluePin = 10;



// the setup function runs once when you press reset or power the board
void setup() {
 // initialize digital pin 13 as an output.
 pinMode(bluePin, OUTPUT);

}

// the loop function runs over and over again forever
void loop() {
 digitalWrite(bluePin, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(1000); // wait for a second
 digitalWrite(bluePin, LOW); // turn the LED off by making the voltage LOW
 delay(1000); // wait for a second
}