mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-30 16:24:09 +03:00
Updating examples.
This commit is contained in:
46
build/shared/dist/examples/Communication/ASCIITable/ASCIITable.pde
vendored
Normal file
46
build/shared/dist/examples/Communication/ASCIITable/ASCIITable.pde
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
// ASCII Table
|
||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
// prints title with ending line break
|
||||
Serial.println("ASCII Table ~ Character Map");
|
||||
|
||||
// wait for the long string to be sent
|
||||
delay(100);
|
||||
}
|
||||
|
||||
int number = 33; // first visible character '!' is #33
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.print(number, BYTE); // prints value unaltered, first will be '!'
|
||||
|
||||
Serial.print(", dec: ");
|
||||
Serial.print(number); // prints value as string in decimal (base 10)
|
||||
// Serial.print(number, DEC); // this also works
|
||||
|
||||
Serial.print(", hex: ");
|
||||
Serial.print(number, HEX); // prints value as string in hexadecimal (base 16)
|
||||
|
||||
Serial.print(", oct: ");
|
||||
Serial.print(number, OCT); // prints value as string in octal (base 8)
|
||||
|
||||
Serial.print(", bin: ");
|
||||
Serial.println(number, BIN); // prints value as string in binary (base 2)
|
||||
// also prints ending line break
|
||||
|
||||
// if printed last visible character '~' #126 ...
|
||||
if(number == 126) {
|
||||
// loop forever
|
||||
while(true) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
number++; // to the next character
|
||||
|
||||
delay(100); // allow some time for the Serial data to be sent
|
||||
}
|
74
build/shared/dist/examples/Communication/Dimmer/Dimmer.pde
vendored
Normal file
74
build/shared/dist/examples/Communication/Dimmer/Dimmer.pde
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Dimmer
|
||||
* by David A. Mellis
|
||||
*
|
||||
* Demonstrates the sending data from the computer to the Arduino board,
|
||||
* in this case to control the brightness of an LED. The data is sent
|
||||
* in individual bytes, each of which ranges from 0 to 255. Arduino
|
||||
* reads these bytes and uses them to set the brightness of the LED.
|
||||
*
|
||||
* http://www.arduino.cc/en/Tutorial/Dimmer
|
||||
*/
|
||||
|
||||
int ledPin = 9;
|
||||
|
||||
void setup()
|
||||
{
|
||||
// begin the serial communication
|
||||
Serial.begin(9600);
|
||||
pinMode(ledPin, OUTPUT);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
byte val;
|
||||
|
||||
// check if data has been sent from the computer
|
||||
if (Serial.available()) {
|
||||
// read the most recent byte (which will be from 0 to 255)
|
||||
val = Serial.read();
|
||||
// set the brightness of the LED
|
||||
analogWrite(ledPin, val);
|
||||
}
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
// Dimmer - sends bytes over a serial port
|
||||
// by David A. Mellis
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
Serial port;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(256, 150);
|
||||
|
||||
println("Available serial ports:");
|
||||
println(Serial.list());
|
||||
|
||||
// Uses the first port in this list (number 0). Change this to
|
||||
// select the port corresponding to your Arduino board. The last
|
||||
// parameter (e.g. 9600) is the speed of the communication. It
|
||||
// has to correspond to the value passed to Serial.begin() in your
|
||||
// Arduino sketch.
|
||||
port = new Serial(this, Serial.list()[0], 9600);
|
||||
|
||||
// If you know the name of the port used by the Arduino board, you
|
||||
// can specify it directly like this.
|
||||
//port = new Serial(this, "COM1", 9600);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
// draw a gradient from black to white
|
||||
for (int i = 0; i < 256; i++) {
|
||||
stroke(i);
|
||||
line(i, 0, i, 150);
|
||||
}
|
||||
|
||||
// write the current X-position of the mouse to the serial port as
|
||||
// a single byte
|
||||
port.write(mouseX);
|
||||
}
|
||||
*/
|
105
build/shared/dist/examples/Communication/Graph/Graph.pde
vendored
Normal file
105
build/shared/dist/examples/Communication/Graph/Graph.pde
vendored
Normal file
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Graph
|
||||
*
|
||||
* A simple example of communication from the Arduino board to the computer:
|
||||
* the value of analog input 0 is printed. We call this "serial"
|
||||
* communication because the connection appears to both the Arduino and the
|
||||
* computer as an old-fashioned serial port, even though it may actually use
|
||||
* a USB cable.
|
||||
*
|
||||
* You can use the Arduino serial monitor to view the sent data, or it can
|
||||
* be read by Processing, Flash, PD, Max/MSP, etc. The Processing code
|
||||
* below graphs the data received so you can see the value of the analog
|
||||
* input changing over time.
|
||||
*
|
||||
* http://www.arduino.cc/en/Tutorial/Graph
|
||||
*/
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println(analogRead(0));
|
||||
delay(100);
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
|
||||
// Graph
|
||||
// by David A. Mellis
|
||||
//
|
||||
// based on Analog In
|
||||
// by <a href="http://itp.jtnimoy.com">Josh Nimoy</a>.
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
Serial port;
|
||||
String buff = "";
|
||||
int NEWLINE = 10;
|
||||
|
||||
// Store the last 64 values received so we can graph them.
|
||||
int[] values = new int[64];
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(512, 256);
|
||||
|
||||
println("Available serial ports:");
|
||||
println(Serial.list());
|
||||
|
||||
// Uses the first port in this list (number 0). Change this to
|
||||
// select the port corresponding to your Arduino board. The last
|
||||
// parameter (e.g. 9600) is the speed of the communication. It
|
||||
// has to correspond to the value passed to Serial.begin() in your
|
||||
// Arduino sketch.
|
||||
port = new Serial(this, Serial.list()[0], 9600);
|
||||
|
||||
// If you know the name of the port used by the Arduino board, you
|
||||
// can specify it directly like this.
|
||||
//port = new Serial(this, "COM1", 9600);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(53);
|
||||
stroke(255);
|
||||
|
||||
// Graph the stored values by drawing a lines between them.
|
||||
for (int i = 0; i < 63; i++)
|
||||
line(i * 8, 255 - values[i], (i + 1) * 8, 255 - values[i + 1]);
|
||||
|
||||
while (port.available() > 0)
|
||||
serialEvent(port.read());
|
||||
}
|
||||
|
||||
void serialEvent(int serial)
|
||||
{
|
||||
if (serial != NEWLINE) {
|
||||
// Store all the characters on the line.
|
||||
buff += char(serial);
|
||||
} else {
|
||||
// The end of each line is marked by two characters, a carriage
|
||||
// return and a newline. We're here because we've gotten a newline,
|
||||
// but we still need to strip off the carriage return.
|
||||
buff = buff.substring(0, buff.length()-1);
|
||||
|
||||
// Parse the String into an integer. We divide by 4 because
|
||||
// analog inputs go from 0 to 1023 while colors in Processing
|
||||
// only go from 0 to 255.
|
||||
int val = Integer.parseInt(buff)/4;
|
||||
|
||||
// Clear the value of "buff"
|
||||
buff = "";
|
||||
|
||||
// Shift over the existing values to make room for the new one.
|
||||
for (int i = 0; i < 63; i++)
|
||||
values[i] = values[i + 1];
|
||||
|
||||
// Add the received value to the array.
|
||||
values[63] = val;
|
||||
}
|
||||
}
|
||||
*/
|
90
build/shared/dist/examples/Communication/PhysicalPixel/PhysicalPixel.pde
vendored
Normal file
90
build/shared/dist/examples/Communication/PhysicalPixel/PhysicalPixel.pde
vendored
Normal file
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Physical Pixel
|
||||
* by David A. Mellis
|
||||
*
|
||||
* An example of using the Arduino board to receive data from the
|
||||
* computer. In this case, the Arduino boards turns on an LED when
|
||||
* it receives the character 'H', and turns off the LED when it
|
||||
* receives the character 'L'.
|
||||
*
|
||||
* The data can be sent from the Arduino serial monitor, or another
|
||||
* program like Processing (see code below), Flash (via a serial-net
|
||||
* proxy), PD, or Max/MSP.
|
||||
*
|
||||
* http://www.arduino.cc/en/Tutorial/PhysicalPixel
|
||||
*/
|
||||
|
||||
int outputPin = 13;
|
||||
int val;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
pinMode(outputPin, OUTPUT);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (Serial.available()) {
|
||||
val = Serial.read();
|
||||
if (val == 'H') {
|
||||
digitalWrite(outputPin, HIGH);
|
||||
}
|
||||
if (val == 'L') {
|
||||
digitalWrite(outputPin, LOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
|
||||
// mouseover serial
|
||||
// by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
|
||||
|
||||
// Demonstrates how to send data to the Arduino I/O board, in order to
|
||||
// turn ON a light if the mouse is over a rectangle and turn it off
|
||||
// if the mouse is not.
|
||||
|
||||
// created 13 May 2004
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
Serial port;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
noStroke();
|
||||
frameRate(10);
|
||||
|
||||
// List all the available serial ports in the output pane.
|
||||
// You will need to choose the port that the Arduino board is
|
||||
// connected to from this list. The first port in the list is
|
||||
// port #0 and the third port in the list is port #2.
|
||||
println(Serial.list());
|
||||
|
||||
// Open the port that the Arduino board is connected to (in this case #0)
|
||||
// Make sure to open the port at the same speed Arduino is using (9600bps)
|
||||
port = new Serial(this, Serial.list()[0], 9600);
|
||||
}
|
||||
|
||||
// function to test if mouse is over square
|
||||
boolean mouseOverRect()
|
||||
{
|
||||
return ((mouseX >= 50)&&(mouseX <= 150)&&(mouseY >= 50)&(mouseY <= 150));
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(#222222);
|
||||
if(mouseOverRect()) // if mouse is over square
|
||||
{
|
||||
fill(#BBBBB0); // change color
|
||||
port.write('H'); // send an 'H' to indicate mouse is over square
|
||||
} else {
|
||||
fill(#666660); // change color
|
||||
port.write('L'); // send an 'L' otherwise
|
||||
}
|
||||
rect(50, 50, 100, 100); // draw square
|
||||
}
|
||||
*/
|
89
build/shared/dist/examples/Communication/VirtualColorMixer/VirtualColorMixer.pde
vendored
Normal file
89
build/shared/dist/examples/Communication/VirtualColorMixer/VirtualColorMixer.pde
vendored
Normal file
@ -0,0 +1,89 @@
|
||||
int redPin = 0;
|
||||
int greenPin = 1;
|
||||
int bluePin = 2;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.print("R");
|
||||
Serial.println(analogRead(redPin));
|
||||
Serial.print("G");
|
||||
Serial.println(analogRead(greenPin));
|
||||
Serial.print("B");
|
||||
Serial.println(analogRead(bluePin));
|
||||
delay(100);
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
|
||||
// Color Mixer
|
||||
// by David A. Mellis
|
||||
//
|
||||
// Created 2 December 2006
|
||||
//
|
||||
// based on Analog In
|
||||
// by <a href="http://itp.jtnimoy.com">Josh Nimoy</a>.
|
||||
//
|
||||
// Created 8 February 2003
|
||||
// Updated 2 April 2005
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
String buff = "";
|
||||
int rval = 0, gval = 0, bval = 0;
|
||||
int NEWLINE = 10;
|
||||
|
||||
Serial port;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
|
||||
// Print a list in case COM1 doesn't work out
|
||||
println("Available serial ports:");
|
||||
println(Serial.list());
|
||||
|
||||
//port = new Serial(this, "COM1", 9600);
|
||||
// Uses the first available port
|
||||
port = new Serial(this, Serial.list()[0], 9600);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
while (port.available() > 0) {
|
||||
serialEvent(port.read());
|
||||
}
|
||||
background(rval, gval, bval);
|
||||
}
|
||||
|
||||
void serialEvent(int serial)
|
||||
{
|
||||
// If the variable "serial" is not equal to the value for
|
||||
// a new line, add the value to the variable "buff". If the
|
||||
// value "serial" is equal to the value for a new line,
|
||||
// save the value of the buffer into the variable "val".
|
||||
if(serial != NEWLINE) {
|
||||
buff += char(serial);
|
||||
} else {
|
||||
// The first character tells us which color this value is for
|
||||
char c = buff.charAt(0);
|
||||
// Remove it from the string
|
||||
buff = buff.substring(1);
|
||||
// Discard the carriage return at the end of the buffer
|
||||
buff = buff.substring(0, buff.length()-1);
|
||||
// Parse the String into an integer
|
||||
if (c == 'R')
|
||||
rval = Integer.parseInt(buff);
|
||||
else if (c == 'G')
|
||||
gval = Integer.parseInt(buff);
|
||||
else if (c == 'B')
|
||||
bval = Integer.parseInt(buff);
|
||||
// Clear the value of "buff"
|
||||
buff = "";
|
||||
}
|
||||
}
|
||||
*/
|
Reference in New Issue
Block a user