mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-30 16:24:09 +03:00
Run new astyle formatter against all the examples
This commit is contained in:
@ -1,78 +1,78 @@
|
||||
/*
|
||||
ASCII table
|
||||
|
||||
Prints out byte values in all possible formats:
|
||||
|
||||
Prints out byte values in all possible formats:
|
||||
* as raw binary values
|
||||
* as ASCII-encoded decimal, hex, octal, and binary values
|
||||
|
||||
|
||||
For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII
|
||||
|
||||
|
||||
The circuit: No external hardware needed.
|
||||
|
||||
|
||||
created 2006
|
||||
by Nicholas Zambetti
|
||||
by Nicholas Zambetti
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
<http://www.zambetti.com>
|
||||
|
||||
<http://www.zambetti.com>
|
||||
|
||||
*/
|
||||
void setup() {
|
||||
//Initialize serial and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
void setup() {
|
||||
//Initialize serial and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for Leonardo only
|
||||
}
|
||||
|
||||
// prints title with ending line break
|
||||
Serial.println("ASCII Table ~ Character Map");
|
||||
}
|
||||
|
||||
// prints title with ending line break
|
||||
Serial.println("ASCII Table ~ Character Map");
|
||||
}
|
||||
|
||||
// first visible ASCIIcharacter '!' is number 33:
|
||||
int thisByte = 33;
|
||||
int thisByte = 33;
|
||||
// you can also write ASCII characters in single quotes.
|
||||
// for example. '!' is the same as 33, so you could also use this:
|
||||
//int thisByte = '!';
|
||||
//int thisByte = '!';
|
||||
|
||||
void loop() {
|
||||
// prints value unaltered, i.e. the raw binary version of the
|
||||
// byte. The serial monitor interprets all bytes as
|
||||
// ASCII, so 33, the first number, will show up as '!'
|
||||
Serial.write(thisByte);
|
||||
void loop() {
|
||||
// prints value unaltered, i.e. the raw binary version of the
|
||||
// byte. The serial monitor interprets all bytes as
|
||||
// ASCII, so 33, the first number, will show up as '!'
|
||||
Serial.write(thisByte);
|
||||
|
||||
Serial.print(", dec: ");
|
||||
Serial.print(", dec: ");
|
||||
// prints value as string as an ASCII-encoded decimal (base 10).
|
||||
// Decimal is the default format for Serial.print() and Serial.println(),
|
||||
// so no modifier is needed:
|
||||
Serial.print(thisByte);
|
||||
Serial.print(thisByte);
|
||||
// But you can declare the modifier for decimal if you want to.
|
||||
//this also works if you uncomment it:
|
||||
|
||||
// Serial.print(thisByte, DEC);
|
||||
// Serial.print(thisByte, DEC);
|
||||
|
||||
|
||||
Serial.print(", hex: ");
|
||||
Serial.print(", hex: ");
|
||||
// prints value as string in hexadecimal (base 16):
|
||||
Serial.print(thisByte, HEX);
|
||||
Serial.print(thisByte, HEX);
|
||||
|
||||
Serial.print(", oct: ");
|
||||
Serial.print(", oct: ");
|
||||
// prints value as string in octal (base 8);
|
||||
Serial.print(thisByte, OCT);
|
||||
Serial.print(thisByte, OCT);
|
||||
|
||||
Serial.print(", bin: ");
|
||||
// prints value as string in binary (base 2)
|
||||
Serial.print(", bin: ");
|
||||
// prints value as string in binary (base 2)
|
||||
// also prints ending line break:
|
||||
Serial.println(thisByte, BIN);
|
||||
Serial.println(thisByte, BIN);
|
||||
|
||||
// if printed last visible character '~' or 126, stop:
|
||||
if(thisByte == 126) { // you could also use if (thisByte == '~') {
|
||||
// if printed last visible character '~' or 126, stop:
|
||||
if (thisByte == 126) { // you could also use if (thisByte == '~') {
|
||||
// This loop loops forever and does nothing
|
||||
while(true) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
while (true) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// go on to the next character
|
||||
thisByte++;
|
||||
}
|
||||
thisByte++;
|
||||
}
|
||||
|
@ -1,24 +1,24 @@
|
||||
/*
|
||||
Dimmer
|
||||
|
||||
|
||||
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.
|
||||
|
||||
|
||||
The circuit:
|
||||
LED attached from digital pin 9 to ground.
|
||||
Serial connection to Processing, Max/MSP, or another serial application
|
||||
|
||||
|
||||
created 2006
|
||||
by David A. Mellis
|
||||
modified 30 Aug 2011
|
||||
by Tom Igoe and Scott Fitzgerald
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Dimmer
|
||||
|
||||
|
||||
*/
|
||||
|
||||
const int ledPin = 9; // the pin that the LED is attached to
|
||||
@ -47,35 +47,35 @@ void loop() {
|
||||
// Dimmer - sends bytes over a serial port
|
||||
// by David A. Mellis
|
||||
//This example code is in the public domain.
|
||||
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
@ -83,7 +83,7 @@ void loop() {
|
||||
*/
|
||||
|
||||
/* Max/MSP v5 patch for this example
|
||||
|
||||
|
||||
----------begin_max5_patcher----------
|
||||
1008.3ocuXszaiaCD9r8uhA5rqAeHIa0aAMaAVf1S6hdoYQAsDiL6JQZHQ2M
|
||||
YWr+2KeX4vjnjXKKkKhhiGQ9MeyCNz+X9rnMp63sQvuB+MLa1OlOalSjUvrC
|
||||
|
@ -1,26 +1,26 @@
|
||||
/*
|
||||
Graph
|
||||
|
||||
|
||||
A simple example of communication from the Arduino board to the computer:
|
||||
the value of analog input 0 is sent out the serial port. We call this "serial"
|
||||
communication because the connection appears to both the Arduino and the
|
||||
computer as a serial port, even though it may actually use
|
||||
a USB cable. Bytes are sent one after another (serially) from the Arduino
|
||||
to the computer.
|
||||
|
||||
|
||||
You can use the Arduino serial monitor to view the sent data, or it can
|
||||
be read by Processing, PD, Max/MSP, or any other program capable of reading
|
||||
data from a serial port. The Processing code below graphs the data received
|
||||
be read by Processing, PD, Max/MSP, or any other program capable of reading
|
||||
data from a serial port. The Processing code below graphs the data received
|
||||
so you can see the value of the analog input changing over time.
|
||||
|
||||
|
||||
The circuit:
|
||||
Any analog input sensor is attached to analog in pin 0.
|
||||
|
||||
|
||||
created 2006
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe and Scott Fitzgerald
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Graph
|
||||
@ -34,34 +34,34 @@ void setup() {
|
||||
void loop() {
|
||||
// send the value of analog input 0:
|
||||
Serial.println(analogRead(A0));
|
||||
// wait a bit for the analog-to-digital converter
|
||||
// wait a bit for the analog-to-digital converter
|
||||
// to stabilize after the last reading:
|
||||
delay(2);
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
|
||||
|
||||
// Graphing sketch
|
||||
|
||||
|
||||
|
||||
|
||||
// This program takes ASCII-encoded strings
|
||||
// from the serial port at 9600 baud and graphs them. It expects values in the
|
||||
// range 0 to 1023, followed by a newline, or newline and carriage return
|
||||
|
||||
|
||||
// Created 20 Apr 2005
|
||||
// Updated 18 Jan 2008
|
||||
// by Tom Igoe
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
|
||||
Serial myPort; // The serial port
|
||||
int xPos = 1; // horizontal position of the graph
|
||||
|
||||
|
||||
void setup () {
|
||||
// set the window size:
|
||||
size(400, 300);
|
||||
|
||||
size(400, 300);
|
||||
|
||||
// List all the available serial ports
|
||||
println(Serial.list());
|
||||
// I know that the first port in the serial list on my mac
|
||||
@ -76,34 +76,34 @@ void loop() {
|
||||
void draw () {
|
||||
// everything happens in the serialEvent()
|
||||
}
|
||||
|
||||
|
||||
void serialEvent (Serial myPort) {
|
||||
// get the ASCII string:
|
||||
String inString = myPort.readStringUntil('\n');
|
||||
|
||||
|
||||
if (inString != null) {
|
||||
// trim off any whitespace:
|
||||
inString = trim(inString);
|
||||
// convert to an int and map to the screen height:
|
||||
float inByte = float(inString);
|
||||
float inByte = float(inString);
|
||||
inByte = map(inByte, 0, 1023, 0, height);
|
||||
|
||||
|
||||
// draw the line:
|
||||
stroke(127,34,255);
|
||||
line(xPos, height, xPos, height - inByte);
|
||||
|
||||
|
||||
// at the edge of the screen, go back to the beginning:
|
||||
if (xPos >= width) {
|
||||
xPos = 0;
|
||||
background(0);
|
||||
}
|
||||
background(0);
|
||||
}
|
||||
else {
|
||||
// increment the horizontal position:
|
||||
xPos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
/* Max/MSP v5 patch for this example
|
||||
@ -145,5 +145,5 @@ RnMj5vGl1Fs16drnk7Tf1XOLgv1n0d2iEsCxR.eQsNOZ4FGF7whofgfI3kES
|
||||
1kCeOX5L2rifbdu0A9ae2X.V33B1Z+.Bj1FrP5iFrCYCG5EUWSG.hhunHJd.
|
||||
HJ5hhnng3h9HPj4lud02.1bxGw.
|
||||
-----------end_max5_patcher-----------
|
||||
|
||||
|
||||
*/
|
||||
|
@ -1,11 +1,11 @@
|
||||
/*
|
||||
MIDI note player
|
||||
|
||||
|
||||
This sketch shows how to use the serial transmit pin (pin 1) to send MIDI note data.
|
||||
If this circuit is connected to a MIDI synth, it will play
|
||||
If this circuit is connected to a MIDI synth, it will play
|
||||
the notes F#-0 (0x1E) to F#-5 (0x5A) in sequence.
|
||||
|
||||
|
||||
|
||||
The circuit:
|
||||
* digital in 1 connected to MIDI jack pin 5
|
||||
* MIDI jack pin 2 connected to ground
|
||||
@ -14,12 +14,12 @@
|
||||
|
||||
created 13 Jun 2006
|
||||
modified 13 Aug 2012
|
||||
by Tom Igoe
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Midi
|
||||
|
||||
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
@ -34,7 +34,7 @@ void loop() {
|
||||
noteOn(0x90, note, 0x45);
|
||||
delay(100);
|
||||
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
|
||||
noteOn(0x90, note, 0x00);
|
||||
noteOn(0x90, note, 0x00);
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
Mega multple serial test
|
||||
|
||||
Receives from the main serial port, sends to the others.
|
||||
|
||||
Receives from the main serial port, sends to the others.
|
||||
Receives from serial port 1, sends to the main serial (Serial 0).
|
||||
|
||||
|
||||
This example works only on the Arduino Mega
|
||||
|
||||
The circuit:
|
||||
|
||||
The circuit:
|
||||
* Any serial device attached to Serial port 1
|
||||
* Serial monitor open on Serial port 0:
|
||||
|
||||
|
||||
created 30 Dec. 2008
|
||||
modified 20 May 2012
|
||||
by Tom Igoe & Jed Roach
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
@ -29,12 +29,12 @@ void loop() {
|
||||
// read from port 1, send to port 0:
|
||||
if (Serial1.available()) {
|
||||
int inByte = Serial1.read();
|
||||
Serial.write(inByte);
|
||||
Serial.write(inByte);
|
||||
}
|
||||
|
||||
|
||||
// read from port 0, send to port 1:
|
||||
if (Serial.available()) {
|
||||
int inByte = Serial.read();
|
||||
Serial1.write(inByte);
|
||||
Serial1.write(inByte);
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,23 @@
|
||||
/*
|
||||
Physical Pixel
|
||||
|
||||
An example of using the Arduino board to receive data from the
|
||||
|
||||
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.
|
||||
|
||||
|
||||
The circuit:
|
||||
* LED connected from digital pin 13 to ground
|
||||
|
||||
|
||||
created 2006
|
||||
by David A. Mellis
|
||||
modified 30 Aug 2011
|
||||
by Tom Igoe and Scott Fitzgerald
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/PhysicalPixel
|
||||
@ -41,7 +41,7 @@ void loop() {
|
||||
// if it's a capital H (ASCII 72), turn on the LED:
|
||||
if (incomingByte == 'H') {
|
||||
digitalWrite(ledPin, HIGH);
|
||||
}
|
||||
}
|
||||
// if it's an L (ASCII 76) turn off the LED:
|
||||
if (incomingByte == 'L') {
|
||||
digitalWrite(ledPin, LOW);
|
||||
@ -50,76 +50,76 @@ void loop() {
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
|
||||
// mouseover serial
|
||||
|
||||
// Demonstrates how to send data to the Arduino I/O board, in order to
|
||||
// turn ON a light if the mouse is over a square and turn it off
|
||||
// if the mouse is not.
|
||||
|
||||
|
||||
// mouseover serial
|
||||
|
||||
// Demonstrates how to send data to the Arduino I/O board, in order to
|
||||
// turn ON a light if the mouse is over a square and turn it off
|
||||
// if the mouse is not.
|
||||
|
||||
// created 2003-4
|
||||
// based on examples by Casey Reas and Hernando Barragan
|
||||
// modified 30 Aug 2011
|
||||
// by Tom Igoe
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
float boxX;
|
||||
float boxY;
|
||||
int boxSize = 20;
|
||||
boolean mouseOverBox = false;
|
||||
|
||||
Serial port;
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial port;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
boxX = width/2.0;
|
||||
boxY = height/2.0;
|
||||
rectMode(RADIUS);
|
||||
|
||||
// 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);
|
||||
|
||||
rectMode(RADIUS);
|
||||
|
||||
// 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);
|
||||
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(0);
|
||||
|
||||
// Test if the cursor is over the box
|
||||
if (mouseX > boxX-boxSize && mouseX < boxX+boxSize &&
|
||||
|
||||
// Test if the cursor is over the box
|
||||
if (mouseX > boxX-boxSize && mouseX < boxX+boxSize &&
|
||||
mouseY > boxY-boxSize && mouseY < boxY+boxSize) {
|
||||
mouseOverBox = true;
|
||||
mouseOverBox = true;
|
||||
// draw a line around the box and change its color:
|
||||
stroke(255);
|
||||
stroke(255);
|
||||
fill(153);
|
||||
// send an 'H' to indicate mouse is over square:
|
||||
port.write('H');
|
||||
}
|
||||
port.write('H');
|
||||
}
|
||||
else {
|
||||
// return the box to it's inactive state:
|
||||
stroke(153);
|
||||
fill(153);
|
||||
// send an 'L' to turn the LED off:
|
||||
port.write('L');
|
||||
// send an 'L' to turn the LED off:
|
||||
port.write('L');
|
||||
mouseOverBox = false;
|
||||
}
|
||||
|
||||
|
||||
// Draw the box
|
||||
rect(boxX, boxY, boxSize, boxSize);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
@ -165,6 +165,6 @@ uVr3PO8wWwEoTW8lsfraX7ZqzZDDXCRqNkztHsGCYpIDDAOqxDpMVUMKcOrp
|
||||
hN97JSnSfLUXGUoj6ujWXd6Pk1SAC+Pkogm.tZ.1lX1qL.pe6PE11DPeMMZ2
|
||||
.P0K+3peBt3NskC
|
||||
-----------end_max5_patcher-----------
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
@ -1,19 +1,19 @@
|
||||
/*
|
||||
Reading a serial ASCII-encoded string.
|
||||
|
||||
|
||||
This sketch demonstrates the Serial parseInt() function.
|
||||
It looks for an ASCII string of comma-separated values.
|
||||
It parses them into ints, and uses those to fade an RGB LED.
|
||||
|
||||
|
||||
Circuit: Common-anode RGB LED wired like so:
|
||||
* Red cathode: digital pin 3
|
||||
* Green cathode: digital pin 5
|
||||
* blue cathode: digital pin 6
|
||||
* anode: +5V
|
||||
|
||||
|
||||
created 13 Apr 2012
|
||||
by Tom Igoe
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
@ -26,9 +26,9 @@ void setup() {
|
||||
// initialize serial:
|
||||
Serial.begin(9600);
|
||||
// make the pins outputs:
|
||||
pinMode(redPin, OUTPUT);
|
||||
pinMode(greenPin, OUTPUT);
|
||||
pinMode(bluePin, OUTPUT);
|
||||
pinMode(redPin, OUTPUT);
|
||||
pinMode(greenPin, OUTPUT);
|
||||
pinMode(bluePin, OUTPUT);
|
||||
|
||||
}
|
||||
|
||||
@ -37,11 +37,11 @@ void loop() {
|
||||
while (Serial.available() > 0) {
|
||||
|
||||
// look for the next valid integer in the incoming serial stream:
|
||||
int red = Serial.parseInt();
|
||||
int red = Serial.parseInt();
|
||||
// do it again:
|
||||
int green = Serial.parseInt();
|
||||
int green = Serial.parseInt();
|
||||
// do it again:
|
||||
int blue = Serial.parseInt();
|
||||
int blue = Serial.parseInt();
|
||||
|
||||
// look for the newline. That's the end of your
|
||||
// sentence:
|
||||
@ -52,7 +52,7 @@ void loop() {
|
||||
green = 255 - constrain(green, 0, 255);
|
||||
blue = 255 - constrain(blue, 0, 255);
|
||||
|
||||
// fade the red, green, and blue legs of the LED:
|
||||
// fade the red, green, and blue legs of the LED:
|
||||
analogWrite(redPin, red);
|
||||
analogWrite(greenPin, green);
|
||||
analogWrite(bluePin, blue);
|
||||
|
@ -1,18 +1,18 @@
|
||||
/*
|
||||
Serial Call and Response
|
||||
Language: Wiring/Arduino
|
||||
|
||||
|
||||
This program sends an ASCII A (byte of value 65) on startup
|
||||
and repeats that until it gets some data in.
|
||||
Then it waits for a byte in the serial port, and
|
||||
Then it waits for a byte in the serial port, and
|
||||
sends three sensor values whenever it gets a byte in.
|
||||
|
||||
|
||||
Thanks to Greg Shakar and Scott Fitzgerald for the improvements
|
||||
|
||||
|
||||
The circuit:
|
||||
* potentiometers attached to analog inputs 0 and 1
|
||||
* potentiometers attached to analog inputs 0 and 1
|
||||
* pushbutton attached to digital I/O 2
|
||||
|
||||
|
||||
Created 26 Sept. 2005
|
||||
by Tom Igoe
|
||||
modified 24 April 2012
|
||||
@ -38,7 +38,7 @@ void setup()
|
||||
}
|
||||
|
||||
pinMode(2, INPUT); // digital sensor is on digital pin 2
|
||||
establishContact(); // send a byte to establish contact until receiver responds
|
||||
establishContact(); // send a byte to establish contact until receiver responds
|
||||
}
|
||||
|
||||
void loop()
|
||||
@ -48,17 +48,17 @@ void loop()
|
||||
// get incoming byte:
|
||||
inByte = Serial.read();
|
||||
// read first analog input, divide by 4 to make the range 0-255:
|
||||
firstSensor = analogRead(A0)/4;
|
||||
firstSensor = analogRead(A0) / 4;
|
||||
// delay 10ms to let the ADC recover:
|
||||
delay(10);
|
||||
// read second analog input, divide by 4 to make the range 0-255:
|
||||
secondSensor = analogRead(1)/4;
|
||||
secondSensor = analogRead(1) / 4;
|
||||
// read switch, map it to 0 or 255L
|
||||
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
|
||||
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
|
||||
// send sensor values:
|
||||
Serial.write(firstSensor);
|
||||
Serial.write(secondSensor);
|
||||
Serial.write(thirdSensor);
|
||||
Serial.write(thirdSensor);
|
||||
}
|
||||
}
|
||||
|
||||
@ -115,15 +115,15 @@ void serialEvent(Serial myPort) {
|
||||
int inByte = myPort.read();
|
||||
// if this is the first byte received, and it's an A,
|
||||
// clear the serial buffer and note that you've
|
||||
// had first contact from the microcontroller.
|
||||
// had first contact from the microcontroller.
|
||||
// Otherwise, add the incoming byte to the array:
|
||||
if (firstContact == false) {
|
||||
if (inByte == 'A') {
|
||||
if (inByte == 'A') {
|
||||
myPort.clear(); // clear the serial port buffer
|
||||
firstContact = true; // you've had first contact from the microcontroller
|
||||
myPort.write('A'); // ask for more
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Add the latest byte from the serial port to array:
|
||||
serialInArray[serialCount] = inByte;
|
||||
|
@ -1,31 +1,31 @@
|
||||
/*
|
||||
Serial Call and Response in ASCII
|
||||
Language: Wiring/Arduino
|
||||
|
||||
|
||||
This program sends an ASCII A (byte of value 65) on startup
|
||||
and repeats that until it gets some data in.
|
||||
Then it waits for a byte in the serial port, and
|
||||
sends three ASCII-encoded, comma-separated sensor values,
|
||||
truncated by a linefeed and carriage return,
|
||||
Then it waits for a byte in the serial port, and
|
||||
sends three ASCII-encoded, comma-separated sensor values,
|
||||
truncated by a linefeed and carriage return,
|
||||
whenever it gets a byte in.
|
||||
|
||||
|
||||
Thanks to Greg Shakar and Scott Fitzgerald for the improvements
|
||||
|
||||
|
||||
The circuit:
|
||||
* potentiometers attached to analog inputs 0 and 1
|
||||
* potentiometers attached to analog inputs 0 and 1
|
||||
* pushbutton attached to digital I/O 2
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Created 26 Sept. 2005
|
||||
by Tom Igoe
|
||||
modified 24 Apr 2012
|
||||
by Tom Igoe and Scott Fitzgerald
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/SerialCallResponseASCII
|
||||
|
||||
|
||||
*/
|
||||
|
||||
int firstSensor = 0; // first analog sensor
|
||||
@ -41,9 +41,9 @@ void setup()
|
||||
; // wait for serial port to connect. Needed for Leonardo only
|
||||
}
|
||||
|
||||
|
||||
|
||||
pinMode(2, INPUT); // digital sensor is on digital pin 2
|
||||
establishContact(); // send a byte to establish contact until receiver responds
|
||||
establishContact(); // send a byte to establish contact until receiver responds
|
||||
}
|
||||
|
||||
void loop()
|
||||
@ -57,13 +57,13 @@ void loop()
|
||||
// read second analog input:
|
||||
secondSensor = analogRead(A1);
|
||||
// read switch, map it to 0 or 255L
|
||||
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
|
||||
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
|
||||
// send sensor values:
|
||||
Serial.print(firstSensor);
|
||||
Serial.print(",");
|
||||
Serial.print(secondSensor);
|
||||
Serial.print(",");
|
||||
Serial.println(thirdSensor);
|
||||
Serial.println(thirdSensor);
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,7 +101,7 @@ void setup() {
|
||||
|
||||
// read bytes into a buffer until you get a linefeed (ASCII 10):
|
||||
myPort.bufferUntil('\n');
|
||||
|
||||
|
||||
// draw with smooth edges:
|
||||
smooth();
|
||||
}
|
||||
@ -114,22 +114,22 @@ void draw() {
|
||||
}
|
||||
|
||||
// serialEvent method is run automatically by the Processing applet
|
||||
// whenever the buffer reaches the byte value set in the bufferUntil()
|
||||
// whenever the buffer reaches the byte value set in the bufferUntil()
|
||||
// method in the setup():
|
||||
|
||||
void serialEvent(Serial myPort) {
|
||||
void serialEvent(Serial myPort) {
|
||||
// read the serial buffer:
|
||||
String myString = myPort.readStringUntil('\n');
|
||||
// if you got any bytes other than the linefeed:
|
||||
myString = trim(myString);
|
||||
|
||||
|
||||
// split the string at the commas
|
||||
// and convert the sections into integers:
|
||||
int sensors[] = int(split(myString, ','));
|
||||
|
||||
// print out the values you got:
|
||||
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
|
||||
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
|
||||
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
|
||||
}
|
||||
// add a linefeed after all the sensor values are printed:
|
||||
println();
|
||||
|
@ -1,20 +1,20 @@
|
||||
/*
|
||||
Serial Event example
|
||||
|
||||
|
||||
When new serial data arrives, this sketch adds it to a String.
|
||||
When a newline is received, the loop prints the string and
|
||||
When a newline is received, the loop prints the string and
|
||||
clears it.
|
||||
|
||||
A good test for this is to try it with a GPS receiver
|
||||
that sends out NMEA 0183 sentences.
|
||||
|
||||
|
||||
A good test for this is to try it with a GPS receiver
|
||||
that sends out NMEA 0183 sentences.
|
||||
|
||||
Created 9 May 2011
|
||||
by Tom Igoe
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/SerialEvent
|
||||
|
||||
|
||||
*/
|
||||
|
||||
String inputString = ""; // a string to hold incoming data
|
||||
@ -30,7 +30,7 @@ void setup() {
|
||||
void loop() {
|
||||
// print the string when a newline arrives:
|
||||
if (stringComplete) {
|
||||
Serial.println(inputString);
|
||||
Serial.println(inputString);
|
||||
// clear the string:
|
||||
inputString = "";
|
||||
stringComplete = false;
|
||||
@ -46,14 +46,14 @@ void loop() {
|
||||
void serialEvent() {
|
||||
while (Serial.available()) {
|
||||
// get the new byte:
|
||||
char inChar = (char)Serial.read();
|
||||
char inChar = (char)Serial.read();
|
||||
// add it to the inputString:
|
||||
inputString += inChar;
|
||||
// if the incoming character is a newline, set a flag
|
||||
// so the main loop can do something about it:
|
||||
if (inChar == '\n') {
|
||||
stringComplete = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,17 +2,17 @@
|
||||
This example reads three analog sensors (potentiometers are easiest)
|
||||
and sends their values serially. The Processing and Max/MSP programs at the bottom
|
||||
take those three values and use them to change the background color of the screen.
|
||||
|
||||
|
||||
The circuit:
|
||||
* potentiometers attached to analog inputs 0, 1, and 2
|
||||
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/VirtualColorMixer
|
||||
|
||||
|
||||
created 2 Dec 2006
|
||||
by David A. Mellis
|
||||
modified 30 Aug 2011
|
||||
by Tom Igoe and Scott Fitzgerald
|
||||
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
@ -35,20 +35,20 @@ void loop()
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
|
||||
float redValue = 0; // red value
|
||||
float greenValue = 0; // green value
|
||||
float blueValue = 0; // blue value
|
||||
|
||||
|
||||
Serial myPort;
|
||||
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
|
||||
|
||||
// List all the available serial ports
|
||||
println(Serial.list());
|
||||
// I know that the first port in the serial list on my mac
|
||||
@ -58,20 +58,20 @@ void loop()
|
||||
// don't generate a serialEvent() unless you get a newline character:
|
||||
myPort.bufferUntil('\n');
|
||||
}
|
||||
|
||||
|
||||
void draw() {
|
||||
// set the background color with the color values:
|
||||
background(redValue, greenValue, blueValue);
|
||||
}
|
||||
|
||||
void serialEvent(Serial myPort) {
|
||||
|
||||
void serialEvent(Serial myPort) {
|
||||
// get the ASCII string:
|
||||
String inString = myPort.readStringUntil('\n');
|
||||
|
||||
|
||||
if (inString != null) {
|
||||
// trim off any whitespace:
|
||||
inString = trim(inString);
|
||||
// split the string on the commas and convert the
|
||||
// split the string on the commas and convert the
|
||||
// resulting substrings into an integer array:
|
||||
float[] colors = float(split(inString, ","));
|
||||
// if the array has at least three elements, you know
|
||||
@ -126,5 +126,5 @@ ASi6Zyw8.RQi65J8ZsNx3ho93OhGWENtWpowepae4YhCFeLErOLENtXJrOSc
|
||||
iadi39rf4hwc8xdhHz3gn3dBI7iDRlFe8huAfIZhq
|
||||
-----------end_max5_patcher-----------
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
Reference in New Issue
Block a user