Move built-in examples to dedicated repository
A dedicated repository for the examples will make it easier for them to be built in to arbitrary development tools. It will make it easier to use them for compilation testing of boards platforms. It provides a dedicated location for issue reports and pull requests that are specific to the examples. It continues the work done by moving the AVR and SAM boards platforms and built-in libraries towards making the arduino/Arduino repository solely a place to host the GUI code of the Arduino IDE.
1
.gitignore
vendored
@ -48,6 +48,7 @@ build/linux/*.tar.bz2
|
|||||||
build/linux/*.zip
|
build/linux/*.zip
|
||||||
build/linux/libastylej*
|
build/linux/libastylej*
|
||||||
build/linux/liblistSerials*
|
build/linux/liblistSerials*
|
||||||
|
build/shared/arduino-examples*
|
||||||
build/shared/reference*.zip
|
build/shared/reference*.zip
|
||||||
build/shared/Edison*.zip
|
build/shared/Edison*.zip
|
||||||
build/shared/Galileo*.zip
|
build/shared/Galileo*.zip
|
||||||
|
@ -22,6 +22,7 @@ Thanks for your interest in contributing to this free open source project! Ardui
|
|||||||
| [Language Reference](https://www.arduino.cc/reference) | [Reference repositories](https://github.com/arduino?q=reference-) |
|
| [Language Reference](https://www.arduino.cc/reference) | [Reference repositories](https://github.com/arduino?q=reference-) |
|
||||||
| Arduino Forum | [arduino/forum-issues](https://github.com/arduino/forum-issues/issues) |
|
| Arduino Forum | [arduino/forum-issues](https://github.com/arduino/forum-issues/issues) |
|
||||||
| Arduino libraries | [arduino-libraries](https://github.com/arduino-libraries) |
|
| Arduino libraries | [arduino-libraries](https://github.com/arduino-libraries) |
|
||||||
|
| Built-in examples | [arduino/arduino-examples](https://github.com/arduino/arduino-examples/issues) |
|
||||||
| arduino-builder | [arduino/arduino-builder](https://github.com/arduino/arduino-builder/issues) |
|
| arduino-builder | [arduino/arduino-builder](https://github.com/arduino/arduino-builder/issues) |
|
||||||
| [Arduino Web Editor](https://create.arduino.cc/editor) | [**Create > Editor** section of the Arduino Forum](http://forum.arduino.cc/index.php?board=101.0) |
|
| [Arduino Web Editor](https://create.arduino.cc/editor) | [**Create > Editor** section of the Arduino Forum](http://forum.arduino.cc/index.php?board=101.0) |
|
||||||
| Arduino AVR Boards (Uno, Mega, Leonardo, etc.) | [arduino/ArduinoCore-avr](https://github.com/arduino/ArduinoCore-avr/issues) |
|
| Arduino AVR Boards (Uno, Mega, Leonardo, etc.) | [arduino/ArduinoCore-avr](https://github.com/arduino/ArduinoCore-avr/issues) |
|
||||||
@ -60,7 +61,6 @@ Pull requests are an easy and effective way to submit a proposal for a change to
|
|||||||
- Search [existing pull requests](https://github.com/arduino/Arduino/pulls?q=) to see if one has already been submitted for this change. Search the [issues](https://github.com/arduino/Arduino/issues?q=is%3Aissue) to see if there has been a discussion on this topic and whether your pull request can close any issues.
|
- Search [existing pull requests](https://github.com/arduino/Arduino/pulls?q=) to see if one has already been submitted for this change. Search the [issues](https://github.com/arduino/Arduino/issues?q=is%3Aissue) to see if there has been a discussion on this topic and whether your pull request can close any issues.
|
||||||
- Code formatting should be consistent with the style used in the existing code.
|
- Code formatting should be consistent with the style used in the existing code.
|
||||||
- Don't leave commented out code. A record of this code is already preserved in the commit history.
|
- Don't leave commented out code. A record of this code is already preserved in the commit history.
|
||||||
- Note that the Arduino core libraries support many boards and processors. When fixing or adding functionality for one of them, it's easy to break something on the others. Please test your changes on as many processors as possible. Even if you don't have a particular board, try compiling your patch for it to make sure that you haven't introduced any errors.
|
|
||||||
- All commits must be atomic. This means that the commit completely accomplishes a single task. Each commit should result in fully functional code. Multiple tasks should not be combined in a single commit, but a single task should not be split over multiple commits (e.g. one commit per file modified is not a good practice). For more information see http://www.freshconsulting.com/atomic-commits.
|
- All commits must be atomic. This means that the commit completely accomplishes a single task. Each commit should result in fully functional code. Multiple tasks should not be combined in a single commit, but a single task should not be split over multiple commits (e.g. one commit per file modified is not a good practice). For more information see http://www.freshconsulting.com/atomic-commits.
|
||||||
- Each pull request should address a single bug fix or enhancement. This may consist of multiple commits. If you have multiple, unrelated fixes or enhancements to contribute, submit them as separate pull requests.
|
- Each pull request should address a single bug fix or enhancement. This may consist of multiple commits. If you have multiple, unrelated fixes or enhancements to contribute, submit them as separate pull requests.
|
||||||
- Commit messages:
|
- Commit messages:
|
||||||
|
@ -105,7 +105,7 @@ public class CommandLineTest extends AbstractWithPreferencesTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCommandLineBuildWithRelativePath() throws Exception {
|
public void testCommandLineBuildWithRelativePath() throws Exception {
|
||||||
File wd = new File(buildPath, "build/shared/examples/01.Basics/Blink/");
|
File wd = new File(buildPath, "app/testdata/sketches/Blink/");
|
||||||
runArduino(true, true, wd, new String[] {
|
runArduino(true, true, wd, new String[] {
|
||||||
"--board", "arduino:avr:uno",
|
"--board", "arduino:avr:uno",
|
||||||
"--verify", "Blink.ino",
|
"--verify", "Blink.ino",
|
||||||
|
1
build/arduino-examples-1.9.0.zip.sha
Normal file
@ -0,0 +1 @@
|
|||||||
|
22e2a43d7a4eea5f99998a397b0094f505806642
|
@ -105,6 +105,8 @@
|
|||||||
<property name="AVRDUDE-VERSION" value="6.3.0-arduino17" />
|
<property name="AVRDUDE-VERSION" value="6.3.0-arduino17" />
|
||||||
<property name="AVRCORE-VERSION" value="1.8.3" />
|
<property name="AVRCORE-VERSION" value="1.8.3" />
|
||||||
<property name="arduinoOTA-VERSION" value="1.3.0" />
|
<property name="arduinoOTA-VERSION" value="1.3.0" />
|
||||||
|
<!-- https://github.com/arduino/built-in-examples -->
|
||||||
|
<property name="BUILT-IN-EXAMPLES-VERSION" value="1.9.0" />
|
||||||
|
|
||||||
<!-- Libraries required for running arduino -->
|
<!-- Libraries required for running arduino -->
|
||||||
<fileset dir=".." id="runtime.jars">
|
<fileset dir=".." id="runtime.jars">
|
||||||
@ -209,10 +211,7 @@
|
|||||||
|
|
||||||
<antcall target="assemble-hardware" />
|
<antcall target="assemble-hardware" />
|
||||||
|
|
||||||
<!-- copy shared examples folder -->
|
<antcall target="assemble-examples" />
|
||||||
<copy todir="${target.path}/examples">
|
|
||||||
<fileset dir="shared/examples" />
|
|
||||||
</copy>
|
|
||||||
|
|
||||||
<mkdir dir="${target.path}/reference"/>
|
<mkdir dir="${target.path}/reference"/>
|
||||||
|
|
||||||
@ -232,6 +231,27 @@
|
|||||||
</antcall>
|
</antcall>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
|
<target name="assemble-examples">
|
||||||
|
<!-- Add the built-in example sketches -->
|
||||||
|
<property name="BUILT-IN-EXAMPLES-REPOSITORY-OWNER" value="arduino" />
|
||||||
|
<property name="BUILT-IN-EXAMPLES-REPOSITORY-NAME" value="arduino-examples" />
|
||||||
|
<property name="BUILT-IN-EXAMPLES-STAGING-PATH" value="shared" />
|
||||||
|
<property name="BUILT-IN-EXAMPLES-FINAL-PATH" value="${target.path}/examples" />
|
||||||
|
|
||||||
|
<!-- Download built-in examples from repository -->
|
||||||
|
<antcall target="unzip">
|
||||||
|
<param name="archive_file" value="${BUILT-IN-EXAMPLES-REPOSITORY-NAME}-${BUILT-IN-EXAMPLES-VERSION}.zip" />
|
||||||
|
<param name="archive_url" value="https://github.com/${BUILT-IN-EXAMPLES-REPOSITORY-OWNER}/${BUILT-IN-EXAMPLES-REPOSITORY-NAME}/archive/${BUILT-IN-EXAMPLES-VERSION}.zip" />
|
||||||
|
<param name="final_folder" value="${BUILT-IN-EXAMPLES-FINAL-PATH}" />
|
||||||
|
<param name="dest_folder" value="${BUILT-IN-EXAMPLES-STAGING-PATH}" />
|
||||||
|
</antcall>
|
||||||
|
|
||||||
|
<!-- Copy examples from subfolder of the downloaded repository -->
|
||||||
|
<copy todir="${BUILT-IN-EXAMPLES-FINAL-PATH}">
|
||||||
|
<fileset dir="${BUILT-IN-EXAMPLES-STAGING-PATH}/${BUILT-IN-EXAMPLES-REPOSITORY-NAME}-${BUILT-IN-EXAMPLES-VERSION}/examples" />
|
||||||
|
</copy>
|
||||||
|
</target>
|
||||||
|
|
||||||
<target name="assemble-docs" unless="no_docs">
|
<target name="assemble-docs" unless="no_docs">
|
||||||
<!-- Unzip documentation -->
|
<!-- Unzip documentation -->
|
||||||
<antcall target="unzip">
|
<antcall target="unzip">
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
AnalogReadSerial
|
|
||||||
|
|
||||||
Reads an analog input on pin 0, prints the result to the Serial Monitor.
|
|
||||||
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
|
|
||||||
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/AnalogReadSerial
|
|
||||||
*/
|
|
||||||
|
|
||||||
// the setup routine runs once when you press reset:
|
|
||||||
void setup() {
|
|
||||||
// initialize serial communication at 9600 bits per second:
|
|
||||||
Serial.begin(9600);
|
|
||||||
}
|
|
||||||
|
|
||||||
// the loop routine runs over and over again forever:
|
|
||||||
void loop() {
|
|
||||||
// read the input on analog pin 0:
|
|
||||||
int sensorValue = analogRead(A0);
|
|
||||||
// print out the value you read:
|
|
||||||
Serial.println(sensorValue);
|
|
||||||
delay(1); // delay in between reads for stability
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Read a potentiometer, print its state out to the Arduino Serial Monitor.
|
|
Before Width: | Height: | Size: 27 KiB |
Before Width: | Height: | Size: 32 KiB |
@ -1,9 +0,0 @@
|
|||||||
void setup() {
|
|
||||||
// put your setup code here, to run once:
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// put your main code here, to run repeatedly:
|
|
||||||
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
The bare minimum of code needed to start an Arduino sketch.
|
|
@ -1 +0,0 @@
|
|||||||
Turn an LED on and off.
|
|
@ -1,29 +0,0 @@
|
|||||||
/*
|
|
||||||
DigitalReadSerial
|
|
||||||
|
|
||||||
Reads a digital input on pin 2, prints the result to the Serial Monitor
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/DigitalReadSerial
|
|
||||||
*/
|
|
||||||
|
|
||||||
// digital pin 2 has a pushbutton attached to it. Give it a name:
|
|
||||||
int pushButton = 2;
|
|
||||||
|
|
||||||
// the setup routine runs once when you press reset:
|
|
||||||
void setup() {
|
|
||||||
// initialize serial communication at 9600 bits per second:
|
|
||||||
Serial.begin(9600);
|
|
||||||
// make the pushbutton's pin an input:
|
|
||||||
pinMode(pushButton, INPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// the loop routine runs over and over again forever:
|
|
||||||
void loop() {
|
|
||||||
// read the input pin:
|
|
||||||
int buttonState = digitalRead(pushButton);
|
|
||||||
// print out the state of the button:
|
|
||||||
Serial.println(buttonState);
|
|
||||||
delay(1); // delay in between reads for stability
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Read a switch, print the state out to the Arduino Serial Monitor.
|
|
Before Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 34 KiB |
@ -1,40 +0,0 @@
|
|||||||
/*
|
|
||||||
Fade
|
|
||||||
|
|
||||||
This example shows how to fade an LED on pin 9 using the analogWrite()
|
|
||||||
function.
|
|
||||||
|
|
||||||
The analogWrite() function uses PWM, so if you want to change the pin you're
|
|
||||||
using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
|
|
||||||
are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Fade
|
|
||||||
*/
|
|
||||||
|
|
||||||
int led = 9; // the PWM pin the LED is attached to
|
|
||||||
int brightness = 0; // how bright the LED is
|
|
||||||
int fadeAmount = 5; // how many points to fade the LED by
|
|
||||||
|
|
||||||
// the setup routine runs once when you press reset:
|
|
||||||
void setup() {
|
|
||||||
// declare pin 9 to be an output:
|
|
||||||
pinMode(led, OUTPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// the loop routine runs over and over again forever:
|
|
||||||
void loop() {
|
|
||||||
// set the brightness of pin 9:
|
|
||||||
analogWrite(led, brightness);
|
|
||||||
|
|
||||||
// change the brightness for next time through the loop:
|
|
||||||
brightness = brightness + fadeAmount;
|
|
||||||
|
|
||||||
// reverse the direction of the fading at the ends of the fade:
|
|
||||||
if (brightness <= 0 || brightness >= 255) {
|
|
||||||
fadeAmount = -fadeAmount;
|
|
||||||
}
|
|
||||||
// wait for 30 milliseconds to see the dimming effect
|
|
||||||
delay(30);
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Demonstrates the use of analogWrite() to fade an LED.
|
|
Before Width: | Height: | Size: 37 KiB |
Before Width: | Height: | Size: 31 KiB |
@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
ReadAnalogVoltage
|
|
||||||
|
|
||||||
Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
|
|
||||||
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
|
|
||||||
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage
|
|
||||||
*/
|
|
||||||
|
|
||||||
// the setup routine runs once when you press reset:
|
|
||||||
void setup() {
|
|
||||||
// initialize serial communication at 9600 bits per second:
|
|
||||||
Serial.begin(9600);
|
|
||||||
}
|
|
||||||
|
|
||||||
// the loop routine runs over and over again forever:
|
|
||||||
void loop() {
|
|
||||||
// read the input on analog pin 0:
|
|
||||||
int sensorValue = analogRead(A0);
|
|
||||||
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
|
|
||||||
float voltage = sensorValue * (5.0 / 1023.0);
|
|
||||||
// print out the value you read:
|
|
||||||
Serial.println(voltage);
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Reads an analog input and prints the voltage to the Serial Monitor.
|
|
Before Width: | Height: | Size: 27 KiB |
Before Width: | Height: | Size: 32 KiB |
@ -1,71 +0,0 @@
|
|||||||
/*
|
|
||||||
Blink without Delay
|
|
||||||
|
|
||||||
Turns on and off a light emitting diode (LED) connected to a digital pin,
|
|
||||||
without using the delay() function. This means that other code can run at the
|
|
||||||
same time without being interrupted by the LED code.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- Use the onboard LED.
|
|
||||||
- Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
|
|
||||||
and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
|
|
||||||
is set to the correct LED pin independent of which board is used.
|
|
||||||
If you want to know what pin the on-board LED is connected to on your
|
|
||||||
Arduino model, check the Technical Specs of your board at:
|
|
||||||
https://www.arduino.cc/en/Main/Products
|
|
||||||
|
|
||||||
created 2005
|
|
||||||
by David A. Mellis
|
|
||||||
modified 8 Feb 2010
|
|
||||||
by Paul Stoffregen
|
|
||||||
modified 11 Nov 2013
|
|
||||||
by Scott Fitzgerald
|
|
||||||
modified 9 Jan 2017
|
|
||||||
by Arturo Guadalupi
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
|
|
||||||
*/
|
|
||||||
|
|
||||||
// constants won't change. Used here to set a pin number:
|
|
||||||
const int ledPin = LED_BUILTIN;// the number of the LED pin
|
|
||||||
|
|
||||||
// Variables will change:
|
|
||||||
int ledState = LOW; // ledState used to set the LED
|
|
||||||
|
|
||||||
// Generally, you should use "unsigned long" for variables that hold time
|
|
||||||
// The value will quickly become too large for an int to store
|
|
||||||
unsigned long previousMillis = 0; // will store last time LED was updated
|
|
||||||
|
|
||||||
// constants won't change:
|
|
||||||
const long interval = 1000; // interval at which to blink (milliseconds)
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// set the digital pin as output:
|
|
||||||
pinMode(ledPin, OUTPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// here is where you'd put code that needs to be running all the time.
|
|
||||||
|
|
||||||
// check to see if it's time to blink the LED; that is, if the difference
|
|
||||||
// between the current time and last time you blinked the LED is bigger than
|
|
||||||
// the interval at which you want to blink the LED.
|
|
||||||
unsigned long currentMillis = millis();
|
|
||||||
|
|
||||||
if (currentMillis - previousMillis >= interval) {
|
|
||||||
// save the last time you blinked the LED
|
|
||||||
previousMillis = currentMillis;
|
|
||||||
|
|
||||||
// if the LED is off turn it on and vice-versa:
|
|
||||||
if (ledState == LOW) {
|
|
||||||
ledState = HIGH;
|
|
||||||
} else {
|
|
||||||
ledState = LOW;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the LED with the ledState of the variable:
|
|
||||||
digitalWrite(ledPin, ledState);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Blinking an LED without using the delay() function.
|
|
Before Width: | Height: | Size: 51 KiB |
Before Width: | Height: | Size: 31 KiB |
@ -1,51 +0,0 @@
|
|||||||
/*
|
|
||||||
Button
|
|
||||||
|
|
||||||
Turns on and off a light emitting diode(LED) connected to digital pin 13,
|
|
||||||
when pressing a pushbutton attached to pin 2.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- LED attached from pin 13 to ground
|
|
||||||
- pushbutton attached to pin 2 from +5V
|
|
||||||
- 10K resistor attached to pin 2 from ground
|
|
||||||
|
|
||||||
- Note: on most Arduinos there is already an LED on the board
|
|
||||||
attached to pin 13.
|
|
||||||
|
|
||||||
created 2005
|
|
||||||
by DojoDave <http://www.0j0.org>
|
|
||||||
modified 30 Aug 2011
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Button
|
|
||||||
*/
|
|
||||||
|
|
||||||
// constants won't change. They're used here to set pin numbers:
|
|
||||||
const int buttonPin = 2; // the number of the pushbutton pin
|
|
||||||
const int ledPin = 13; // the number of the LED pin
|
|
||||||
|
|
||||||
// variables will change:
|
|
||||||
int buttonState = 0; // variable for reading the pushbutton status
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// initialize the LED pin as an output:
|
|
||||||
pinMode(ledPin, OUTPUT);
|
|
||||||
// initialize the pushbutton pin as an input:
|
|
||||||
pinMode(buttonPin, INPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// read the state of the pushbutton value:
|
|
||||||
buttonState = digitalRead(buttonPin);
|
|
||||||
|
|
||||||
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
|
|
||||||
if (buttonState == HIGH) {
|
|
||||||
// turn LED on:
|
|
||||||
digitalWrite(ledPin, HIGH);
|
|
||||||
} else {
|
|
||||||
// turn LED off:
|
|
||||||
digitalWrite(ledPin, LOW);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Use a pushbutton to control an LED.
|
|
Before Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 26 KiB |
@ -1,86 +0,0 @@
|
|||||||
/*
|
|
||||||
Debounce
|
|
||||||
|
|
||||||
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
|
|
||||||
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
|
|
||||||
minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- LED attached from pin 13 to ground
|
|
||||||
- pushbutton attached from pin 2 to +5V
|
|
||||||
- 10 kilohm resistor attached from pin 2 to ground
|
|
||||||
|
|
||||||
- Note: On most Arduino boards, there is already an LED on the board connected
|
|
||||||
to pin 13, so you don't need any extra components for this example.
|
|
||||||
|
|
||||||
created 21 Nov 2006
|
|
||||||
by David A. Mellis
|
|
||||||
modified 30 Aug 2011
|
|
||||||
by Limor Fried
|
|
||||||
modified 28 Dec 2012
|
|
||||||
by Mike Walters
|
|
||||||
modified 30 Aug 2016
|
|
||||||
by Arturo Guadalupi
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Debounce
|
|
||||||
*/
|
|
||||||
|
|
||||||
// constants won't change. They're used here to set pin numbers:
|
|
||||||
const int buttonPin = 2; // the number of the pushbutton pin
|
|
||||||
const int ledPin = 13; // the number of the LED pin
|
|
||||||
|
|
||||||
// Variables will change:
|
|
||||||
int ledState = HIGH; // the current state of the output pin
|
|
||||||
int buttonState; // the current reading from the input pin
|
|
||||||
int lastButtonState = LOW; // the previous reading from the input pin
|
|
||||||
|
|
||||||
// the following variables are unsigned longs because the time, measured in
|
|
||||||
// milliseconds, will quickly become a bigger number than can be stored in an int.
|
|
||||||
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
|
|
||||||
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
pinMode(buttonPin, INPUT);
|
|
||||||
pinMode(ledPin, OUTPUT);
|
|
||||||
|
|
||||||
// set initial LED state
|
|
||||||
digitalWrite(ledPin, ledState);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// read the state of the switch into a local variable:
|
|
||||||
int reading = digitalRead(buttonPin);
|
|
||||||
|
|
||||||
// check to see if you just pressed the button
|
|
||||||
// (i.e. the input went from LOW to HIGH), and you've waited long enough
|
|
||||||
// since the last press to ignore any noise:
|
|
||||||
|
|
||||||
// If the switch changed, due to noise or pressing:
|
|
||||||
if (reading != lastButtonState) {
|
|
||||||
// reset the debouncing timer
|
|
||||||
lastDebounceTime = millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((millis() - lastDebounceTime) > debounceDelay) {
|
|
||||||
// whatever the reading is at, it's been there for longer than the debounce
|
|
||||||
// delay, so take it as the actual current state:
|
|
||||||
|
|
||||||
// if the button state has changed:
|
|
||||||
if (reading != buttonState) {
|
|
||||||
buttonState = reading;
|
|
||||||
|
|
||||||
// only toggle the LED if the new button state is HIGH
|
|
||||||
if (buttonState == HIGH) {
|
|
||||||
ledState = !ledState;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the LED:
|
|
||||||
digitalWrite(ledPin, ledState);
|
|
||||||
|
|
||||||
// save the reading. Next time through the loop, it'll be the lastButtonState:
|
|
||||||
lastButtonState = reading;
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Read a pushbutton, filtering noise.
|
|
Before Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 34 KiB |
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
Input Pull-up Serial
|
|
||||||
|
|
||||||
This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
|
|
||||||
input on pin 2 and prints the results to the Serial Monitor.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- momentary switch attached from pin 2 to ground
|
|
||||||
- built-in LED on pin 13
|
|
||||||
|
|
||||||
Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
|
|
||||||
20K-ohm resistor is pulled to 5V. This configuration causes the input to read
|
|
||||||
HIGH when the switch is open, and LOW when it is closed.
|
|
||||||
|
|
||||||
created 14 Mar 2012
|
|
||||||
by Scott Fitzgerald
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/InputPullupSerial
|
|
||||||
*/
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
//start serial connection
|
|
||||||
Serial.begin(9600);
|
|
||||||
//configure pin 2 as an input and enable the internal pull-up resistor
|
|
||||||
pinMode(2, INPUT_PULLUP);
|
|
||||||
pinMode(13, OUTPUT);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
//read the pushbutton value into a variable
|
|
||||||
int sensorVal = digitalRead(2);
|
|
||||||
//print out the value of the pushbutton
|
|
||||||
Serial.println(sensorVal);
|
|
||||||
|
|
||||||
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
|
|
||||||
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
|
|
||||||
// button's pressed, and off when it's not:
|
|
||||||
if (sensorVal == HIGH) {
|
|
||||||
digitalWrite(13, LOW);
|
|
||||||
} else {
|
|
||||||
digitalWrite(13, HIGH);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Demonstrates the use of INPUT_PULLUP with pinMode().
|
|
Before Width: | Height: | Size: 205 KiB |
Before Width: | Height: | Size: 60 KiB |
@ -1,79 +0,0 @@
|
|||||||
/*
|
|
||||||
State change detection (edge detection)
|
|
||||||
|
|
||||||
Often, you don't need to know the state of a digital input all the time, but
|
|
||||||
you just need to know when the input changes from one state to another.
|
|
||||||
For example, you want to know when a button goes from OFF to ON. This is called
|
|
||||||
state change detection, or edge detection.
|
|
||||||
|
|
||||||
This example shows how to detect when a button or button changes from off to on
|
|
||||||
and on to off.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- pushbutton attached to pin 2 from +5V
|
|
||||||
- 10 kilohm resistor attached to pin 2 from ground
|
|
||||||
- LED attached from pin 13 to ground (or use the built-in LED on most
|
|
||||||
Arduino boards)
|
|
||||||
|
|
||||||
created 27 Sep 2005
|
|
||||||
modified 30 Aug 2011
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/ButtonStateChange
|
|
||||||
*/
|
|
||||||
|
|
||||||
// this constant won't change:
|
|
||||||
const int buttonPin = 2; // the pin that the pushbutton is attached to
|
|
||||||
const int ledPin = 13; // the pin that the LED is attached to
|
|
||||||
|
|
||||||
// Variables will change:
|
|
||||||
int buttonPushCounter = 0; // counter for the number of button presses
|
|
||||||
int buttonState = 0; // current state of the button
|
|
||||||
int lastButtonState = 0; // previous state of the button
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// initialize the button pin as a input:
|
|
||||||
pinMode(buttonPin, INPUT);
|
|
||||||
// initialize the LED as an output:
|
|
||||||
pinMode(ledPin, OUTPUT);
|
|
||||||
// initialize serial communication:
|
|
||||||
Serial.begin(9600);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// read the pushbutton input pin:
|
|
||||||
buttonState = digitalRead(buttonPin);
|
|
||||||
|
|
||||||
// compare the buttonState to its previous state
|
|
||||||
if (buttonState != lastButtonState) {
|
|
||||||
// if the state has changed, increment the counter
|
|
||||||
if (buttonState == HIGH) {
|
|
||||||
// if the current state is HIGH then the button went from off to on:
|
|
||||||
buttonPushCounter++;
|
|
||||||
Serial.println("on");
|
|
||||||
Serial.print("number of button pushes: ");
|
|
||||||
Serial.println(buttonPushCounter);
|
|
||||||
} else {
|
|
||||||
// if the current state is LOW then the button went from on to off:
|
|
||||||
Serial.println("off");
|
|
||||||
}
|
|
||||||
// Delay a little bit to avoid bouncing
|
|
||||||
delay(50);
|
|
||||||
}
|
|
||||||
// save the current state as the last state, for next time through the loop
|
|
||||||
lastButtonState = buttonState;
|
|
||||||
|
|
||||||
|
|
||||||
// turns on the LED every four button pushes by checking the modulo of the
|
|
||||||
// button push counter. the modulo function gives you the remainder of the
|
|
||||||
// division of two numbers:
|
|
||||||
if (buttonPushCounter % 4 == 0) {
|
|
||||||
digitalWrite(ledPin, HIGH);
|
|
||||||
} else {
|
|
||||||
digitalWrite(ledPin, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Counting the number of button pushes.
|
|
Before Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 83 KiB |
@ -1,95 +0,0 @@
|
|||||||
/*************************************************
|
|
||||||
* Public Constants
|
|
||||||
*************************************************/
|
|
||||||
|
|
||||||
#define NOTE_B0 31
|
|
||||||
#define NOTE_C1 33
|
|
||||||
#define NOTE_CS1 35
|
|
||||||
#define NOTE_D1 37
|
|
||||||
#define NOTE_DS1 39
|
|
||||||
#define NOTE_E1 41
|
|
||||||
#define NOTE_F1 44
|
|
||||||
#define NOTE_FS1 46
|
|
||||||
#define NOTE_G1 49
|
|
||||||
#define NOTE_GS1 52
|
|
||||||
#define NOTE_A1 55
|
|
||||||
#define NOTE_AS1 58
|
|
||||||
#define NOTE_B1 62
|
|
||||||
#define NOTE_C2 65
|
|
||||||
#define NOTE_CS2 69
|
|
||||||
#define NOTE_D2 73
|
|
||||||
#define NOTE_DS2 78
|
|
||||||
#define NOTE_E2 82
|
|
||||||
#define NOTE_F2 87
|
|
||||||
#define NOTE_FS2 93
|
|
||||||
#define NOTE_G2 98
|
|
||||||
#define NOTE_GS2 104
|
|
||||||
#define NOTE_A2 110
|
|
||||||
#define NOTE_AS2 117
|
|
||||||
#define NOTE_B2 123
|
|
||||||
#define NOTE_C3 131
|
|
||||||
#define NOTE_CS3 139
|
|
||||||
#define NOTE_D3 147
|
|
||||||
#define NOTE_DS3 156
|
|
||||||
#define NOTE_E3 165
|
|
||||||
#define NOTE_F3 175
|
|
||||||
#define NOTE_FS3 185
|
|
||||||
#define NOTE_G3 196
|
|
||||||
#define NOTE_GS3 208
|
|
||||||
#define NOTE_A3 220
|
|
||||||
#define NOTE_AS3 233
|
|
||||||
#define NOTE_B3 247
|
|
||||||
#define NOTE_C4 262
|
|
||||||
#define NOTE_CS4 277
|
|
||||||
#define NOTE_D4 294
|
|
||||||
#define NOTE_DS4 311
|
|
||||||
#define NOTE_E4 330
|
|
||||||
#define NOTE_F4 349
|
|
||||||
#define NOTE_FS4 370
|
|
||||||
#define NOTE_G4 392
|
|
||||||
#define NOTE_GS4 415
|
|
||||||
#define NOTE_A4 440
|
|
||||||
#define NOTE_AS4 466
|
|
||||||
#define NOTE_B4 494
|
|
||||||
#define NOTE_C5 523
|
|
||||||
#define NOTE_CS5 554
|
|
||||||
#define NOTE_D5 587
|
|
||||||
#define NOTE_DS5 622
|
|
||||||
#define NOTE_E5 659
|
|
||||||
#define NOTE_F5 698
|
|
||||||
#define NOTE_FS5 740
|
|
||||||
#define NOTE_G5 784
|
|
||||||
#define NOTE_GS5 831
|
|
||||||
#define NOTE_A5 880
|
|
||||||
#define NOTE_AS5 932
|
|
||||||
#define NOTE_B5 988
|
|
||||||
#define NOTE_C6 1047
|
|
||||||
#define NOTE_CS6 1109
|
|
||||||
#define NOTE_D6 1175
|
|
||||||
#define NOTE_DS6 1245
|
|
||||||
#define NOTE_E6 1319
|
|
||||||
#define NOTE_F6 1397
|
|
||||||
#define NOTE_FS6 1480
|
|
||||||
#define NOTE_G6 1568
|
|
||||||
#define NOTE_GS6 1661
|
|
||||||
#define NOTE_A6 1760
|
|
||||||
#define NOTE_AS6 1865
|
|
||||||
#define NOTE_B6 1976
|
|
||||||
#define NOTE_C7 2093
|
|
||||||
#define NOTE_CS7 2217
|
|
||||||
#define NOTE_D7 2349
|
|
||||||
#define NOTE_DS7 2489
|
|
||||||
#define NOTE_E7 2637
|
|
||||||
#define NOTE_F7 2794
|
|
||||||
#define NOTE_FS7 2960
|
|
||||||
#define NOTE_G7 3136
|
|
||||||
#define NOTE_GS7 3322
|
|
||||||
#define NOTE_A7 3520
|
|
||||||
#define NOTE_AS7 3729
|
|
||||||
#define NOTE_B7 3951
|
|
||||||
#define NOTE_C8 4186
|
|
||||||
#define NOTE_CS8 4435
|
|
||||||
#define NOTE_D8 4699
|
|
||||||
#define NOTE_DS8 4978
|
|
||||||
|
|
||||||
|
|
Before Width: | Height: | Size: 50 KiB |
@ -1,44 +0,0 @@
|
|||||||
/*
|
|
||||||
Keyboard
|
|
||||||
|
|
||||||
Plays a pitch that changes based on a changing analog input
|
|
||||||
|
|
||||||
circuit:
|
|
||||||
- three force-sensing resistors from +5V to analog in 0 through 5
|
|
||||||
- three 10 kilohm resistors from analog in 0 through 5 to ground
|
|
||||||
- 8 ohm speaker on digital pin 8
|
|
||||||
|
|
||||||
created 21 Jan 2010
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Tone3
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "pitches.h"
|
|
||||||
|
|
||||||
const int threshold = 10; // minimum reading of the sensors that generates a note
|
|
||||||
|
|
||||||
// notes to play, corresponding to the 3 sensors:
|
|
||||||
int notes[] = {
|
|
||||||
NOTE_A4, NOTE_B4, NOTE_C3
|
|
||||||
};
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
|
|
||||||
// get a sensor reading:
|
|
||||||
int sensorReading = analogRead(thisSensor);
|
|
||||||
|
|
||||||
// if the sensor is pressed hard enough:
|
|
||||||
if (sensorReading > threshold) {
|
|
||||||
// play the note corresponding to this sensor:
|
|
||||||
tone(8, notes[thisSensor], 20);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
A three-key musical keyboard using force sensors and a piezo speaker.
|
|
Before Width: | Height: | Size: 37 KiB |
@ -1,95 +0,0 @@
|
|||||||
/*************************************************
|
|
||||||
* Public Constants
|
|
||||||
*************************************************/
|
|
||||||
|
|
||||||
#define NOTE_B0 31
|
|
||||||
#define NOTE_C1 33
|
|
||||||
#define NOTE_CS1 35
|
|
||||||
#define NOTE_D1 37
|
|
||||||
#define NOTE_DS1 39
|
|
||||||
#define NOTE_E1 41
|
|
||||||
#define NOTE_F1 44
|
|
||||||
#define NOTE_FS1 46
|
|
||||||
#define NOTE_G1 49
|
|
||||||
#define NOTE_GS1 52
|
|
||||||
#define NOTE_A1 55
|
|
||||||
#define NOTE_AS1 58
|
|
||||||
#define NOTE_B1 62
|
|
||||||
#define NOTE_C2 65
|
|
||||||
#define NOTE_CS2 69
|
|
||||||
#define NOTE_D2 73
|
|
||||||
#define NOTE_DS2 78
|
|
||||||
#define NOTE_E2 82
|
|
||||||
#define NOTE_F2 87
|
|
||||||
#define NOTE_FS2 93
|
|
||||||
#define NOTE_G2 98
|
|
||||||
#define NOTE_GS2 104
|
|
||||||
#define NOTE_A2 110
|
|
||||||
#define NOTE_AS2 117
|
|
||||||
#define NOTE_B2 123
|
|
||||||
#define NOTE_C3 131
|
|
||||||
#define NOTE_CS3 139
|
|
||||||
#define NOTE_D3 147
|
|
||||||
#define NOTE_DS3 156
|
|
||||||
#define NOTE_E3 165
|
|
||||||
#define NOTE_F3 175
|
|
||||||
#define NOTE_FS3 185
|
|
||||||
#define NOTE_G3 196
|
|
||||||
#define NOTE_GS3 208
|
|
||||||
#define NOTE_A3 220
|
|
||||||
#define NOTE_AS3 233
|
|
||||||
#define NOTE_B3 247
|
|
||||||
#define NOTE_C4 262
|
|
||||||
#define NOTE_CS4 277
|
|
||||||
#define NOTE_D4 294
|
|
||||||
#define NOTE_DS4 311
|
|
||||||
#define NOTE_E4 330
|
|
||||||
#define NOTE_F4 349
|
|
||||||
#define NOTE_FS4 370
|
|
||||||
#define NOTE_G4 392
|
|
||||||
#define NOTE_GS4 415
|
|
||||||
#define NOTE_A4 440
|
|
||||||
#define NOTE_AS4 466
|
|
||||||
#define NOTE_B4 494
|
|
||||||
#define NOTE_C5 523
|
|
||||||
#define NOTE_CS5 554
|
|
||||||
#define NOTE_D5 587
|
|
||||||
#define NOTE_DS5 622
|
|
||||||
#define NOTE_E5 659
|
|
||||||
#define NOTE_F5 698
|
|
||||||
#define NOTE_FS5 740
|
|
||||||
#define NOTE_G5 784
|
|
||||||
#define NOTE_GS5 831
|
|
||||||
#define NOTE_A5 880
|
|
||||||
#define NOTE_AS5 932
|
|
||||||
#define NOTE_B5 988
|
|
||||||
#define NOTE_C6 1047
|
|
||||||
#define NOTE_CS6 1109
|
|
||||||
#define NOTE_D6 1175
|
|
||||||
#define NOTE_DS6 1245
|
|
||||||
#define NOTE_E6 1319
|
|
||||||
#define NOTE_F6 1397
|
|
||||||
#define NOTE_FS6 1480
|
|
||||||
#define NOTE_G6 1568
|
|
||||||
#define NOTE_GS6 1661
|
|
||||||
#define NOTE_A6 1760
|
|
||||||
#define NOTE_AS6 1865
|
|
||||||
#define NOTE_B6 1976
|
|
||||||
#define NOTE_C7 2093
|
|
||||||
#define NOTE_CS7 2217
|
|
||||||
#define NOTE_D7 2349
|
|
||||||
#define NOTE_DS7 2489
|
|
||||||
#define NOTE_E7 2637
|
|
||||||
#define NOTE_F7 2794
|
|
||||||
#define NOTE_FS7 2960
|
|
||||||
#define NOTE_G7 3136
|
|
||||||
#define NOTE_GS7 3322
|
|
||||||
#define NOTE_A7 3520
|
|
||||||
#define NOTE_AS7 3729
|
|
||||||
#define NOTE_B7 3951
|
|
||||||
#define NOTE_C8 4186
|
|
||||||
#define NOTE_CS8 4435
|
|
||||||
#define NOTE_D8 4699
|
|
||||||
#define NOTE_DS8 4978
|
|
||||||
|
|
||||||
|
|
Before Width: | Height: | Size: 35 KiB |
@ -1,50 +0,0 @@
|
|||||||
/*
|
|
||||||
Melody
|
|
||||||
|
|
||||||
Plays a melody
|
|
||||||
|
|
||||||
circuit:
|
|
||||||
- 8 ohm speaker on digital pin 8
|
|
||||||
|
|
||||||
created 21 Jan 2010
|
|
||||||
modified 30 Aug 2011
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Tone
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "pitches.h"
|
|
||||||
|
|
||||||
// notes in the melody:
|
|
||||||
int melody[] = {
|
|
||||||
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
|
|
||||||
};
|
|
||||||
|
|
||||||
// note durations: 4 = quarter note, 8 = eighth note, etc.:
|
|
||||||
int noteDurations[] = {
|
|
||||||
4, 8, 8, 4, 4, 4, 4, 4
|
|
||||||
};
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// iterate over the notes of the melody:
|
|
||||||
for (int thisNote = 0; thisNote < 8; thisNote++) {
|
|
||||||
|
|
||||||
// to calculate the note duration, take one second divided by the note type.
|
|
||||||
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
|
|
||||||
int noteDuration = 1000 / noteDurations[thisNote];
|
|
||||||
tone(8, melody[thisNote], noteDuration);
|
|
||||||
|
|
||||||
// to distinguish the notes, set a minimum time between them.
|
|
||||||
// the note's duration + 30% seems to work well:
|
|
||||||
int pauseBetweenNotes = noteDuration * 1.30;
|
|
||||||
delay(pauseBetweenNotes);
|
|
||||||
// stop the tone playing:
|
|
||||||
noTone(8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// no need to repeat the melody.
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Play a melody with a Piezo speaker.
|
|
Before Width: | Height: | Size: 58 KiB |
Before Width: | Height: | Size: 41 KiB |
@ -1,40 +0,0 @@
|
|||||||
/*
|
|
||||||
Multiple tone player
|
|
||||||
|
|
||||||
Plays multiple tones on multiple pins in sequence
|
|
||||||
|
|
||||||
circuit:
|
|
||||||
- three 8 ohm speakers on digital pins 6, 7, and 8
|
|
||||||
|
|
||||||
created 8 Mar 2010
|
|
||||||
by Tom Igoe
|
|
||||||
based on a snippet from Greg Borenstein
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Tone4
|
|
||||||
*/
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// turn off tone function for pin 8:
|
|
||||||
noTone(8);
|
|
||||||
// play a note on pin 6 for 200 ms:
|
|
||||||
tone(6, 440, 200);
|
|
||||||
delay(200);
|
|
||||||
|
|
||||||
// turn off tone function for pin 6:
|
|
||||||
noTone(6);
|
|
||||||
// play a note on pin 7 for 500 ms:
|
|
||||||
tone(7, 494, 500);
|
|
||||||
delay(500);
|
|
||||||
|
|
||||||
// turn off tone function for pin 7:
|
|
||||||
noTone(7);
|
|
||||||
// play a note on pin 8 for 300 ms:
|
|
||||||
tone(8, 523, 300);
|
|
||||||
delay(300);
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Play tones on multiple speakers sequentially using the tone() command.
|
|
Before Width: | Height: | Size: 72 KiB |
Before Width: | Height: | Size: 39 KiB |
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
Pitch follower
|
|
||||||
|
|
||||||
Plays a pitch that changes based on a changing analog input
|
|
||||||
|
|
||||||
circuit:
|
|
||||||
- 8 ohm speaker on digital pin 9
|
|
||||||
- photoresistor on analog 0 to 5V
|
|
||||||
- 4.7 kilohm resistor on analog 0 to ground
|
|
||||||
|
|
||||||
created 21 Jan 2010
|
|
||||||
modified 31 May 2012
|
|
||||||
by Tom Igoe, with suggestion from Michael Flynn
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Tone2
|
|
||||||
*/
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// initialize serial communications (for debugging only):
|
|
||||||
Serial.begin(9600);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// read the sensor:
|
|
||||||
int sensorReading = analogRead(A0);
|
|
||||||
// print the sensor reading so you know its range
|
|
||||||
Serial.println(sensorReading);
|
|
||||||
// map the analog input range (in this case, 400 - 1000 from the photoresistor)
|
|
||||||
// to the output pitch range (120 - 1500Hz)
|
|
||||||
// change the minimum and maximum input numbers below depending on the range
|
|
||||||
// your sensor's giving:
|
|
||||||
int thisPitch = map(sensorReading, 400, 1000, 120, 1500);
|
|
||||||
|
|
||||||
// play the pitch:
|
|
||||||
tone(9, thisPitch, 10);
|
|
||||||
delay(1); // delay in between reads for stability
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Play a pitch on a piezo speaker depending on an analog input.
|
|
@ -1,52 +0,0 @@
|
|||||||
/*
|
|
||||||
Analog input, analog output, serial output
|
|
||||||
|
|
||||||
Reads an analog input pin, maps the result to a range from 0 to 255 and uses
|
|
||||||
the result to set the pulse width modulation (PWM) of an output pin.
|
|
||||||
Also prints the results to the Serial Monitor.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- potentiometer connected to analog pin 0.
|
|
||||||
Center pin of the potentiometer goes to the analog pin.
|
|
||||||
side pins of the potentiometer go to +5V and ground
|
|
||||||
- LED connected from digital pin 9 to ground
|
|
||||||
|
|
||||||
created 29 Dec. 2008
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/AnalogInOutSerial
|
|
||||||
*/
|
|
||||||
|
|
||||||
// These constants won't change. They're used to give names to the pins used:
|
|
||||||
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
|
|
||||||
const int analogOutPin = 9; // Analog output pin that the LED is attached to
|
|
||||||
|
|
||||||
int sensorValue = 0; // value read from the pot
|
|
||||||
int outputValue = 0; // value output to the PWM (analog out)
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// initialize serial communications at 9600 bps:
|
|
||||||
Serial.begin(9600);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// read the analog in value:
|
|
||||||
sensorValue = analogRead(analogInPin);
|
|
||||||
// map it to the range of the analog out:
|
|
||||||
outputValue = map(sensorValue, 0, 1023, 0, 255);
|
|
||||||
// change the analog out value:
|
|
||||||
analogWrite(analogOutPin, outputValue);
|
|
||||||
|
|
||||||
// print the results to the Serial Monitor:
|
|
||||||
Serial.print("sensor = ");
|
|
||||||
Serial.print(sensorValue);
|
|
||||||
Serial.print("\t output = ");
|
|
||||||
Serial.println(outputValue);
|
|
||||||
|
|
||||||
// wait 2 milliseconds before the next loop for the analog-to-digital
|
|
||||||
// converter to settle after the last reading:
|
|
||||||
delay(2);
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Read an analog input pin, map the result, and then use that data to dim or brighten an LED.
|
|
Before Width: | Height: | Size: 32 KiB |
Before Width: | Height: | Size: 38 KiB |
@ -1,50 +0,0 @@
|
|||||||
/*
|
|
||||||
Analog Input
|
|
||||||
|
|
||||||
Demonstrates analog input by reading an analog sensor on analog pin 0 and
|
|
||||||
turning on and off a light emitting diode(LED) connected to digital pin 13.
|
|
||||||
The amount of time the LED will be on and off depends on the value obtained
|
|
||||||
by analogRead().
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- potentiometer
|
|
||||||
center pin of the potentiometer to the analog input 0
|
|
||||||
one side pin (either one) to ground
|
|
||||||
the other side pin to +5V
|
|
||||||
- LED
|
|
||||||
anode (long leg) attached to digital output 13
|
|
||||||
cathode (short leg) attached to ground
|
|
||||||
|
|
||||||
- Note: because most Arduinos have a built-in LED attached to pin 13 on the
|
|
||||||
board, the LED is optional.
|
|
||||||
|
|
||||||
created by David Cuartielles
|
|
||||||
modified 30 Aug 2011
|
|
||||||
By Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/AnalogInput
|
|
||||||
*/
|
|
||||||
|
|
||||||
int sensorPin = A0; // select the input pin for the potentiometer
|
|
||||||
int ledPin = 13; // 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);
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Use a potentiometer to control the blinking of an LED.
|
|
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 32 KiB |
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
Mega analogWrite() test
|
|
||||||
|
|
||||||
This sketch fades LEDs up and down one at a time on digital pins 2 through 13.
|
|
||||||
This sketch was written for the Arduino Mega, and will not work on other boards.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- LEDs attached from pins 2 through 13 to ground.
|
|
||||||
|
|
||||||
created 8 Feb 2009
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/AnalogWriteMega
|
|
||||||
*/
|
|
||||||
|
|
||||||
// These constants won't change. They're used to give names to the pins used:
|
|
||||||
const int lowestPin = 2;
|
|
||||||
const int highestPin = 13;
|
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// set pins 2 through 13 as outputs:
|
|
||||||
for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
|
|
||||||
pinMode(thisPin, OUTPUT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// iterate over the pins:
|
|
||||||
for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
|
|
||||||
// fade the LED on thisPin from off to brightest:
|
|
||||||
for (int brightness = 0; brightness < 255; brightness++) {
|
|
||||||
analogWrite(thisPin, brightness);
|
|
||||||
delay(2);
|
|
||||||
}
|
|
||||||
// fade the LED on thisPin from brightest to off:
|
|
||||||
for (int brightness = 255; brightness >= 0; brightness--) {
|
|
||||||
analogWrite(thisPin, brightness);
|
|
||||||
delay(2);
|
|
||||||
}
|
|
||||||
// pause between LEDs:
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Fade 12 LEDs on and off, one by one, using an Arduino Mega board.
|
|
Before Width: | Height: | Size: 85 KiB |
Before Width: | Height: | Size: 101 KiB |
@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
Calibration
|
|
||||||
|
|
||||||
Demonstrates one technique for calibrating sensor input. The sensor readings
|
|
||||||
during the first five seconds of the sketch execution define the minimum and
|
|
||||||
maximum of expected values attached to the sensor pin.
|
|
||||||
|
|
||||||
The sensor minimum and maximum initial values may seem backwards. Initially,
|
|
||||||
you set the minimum high and listen for anything lower, saving it as the new
|
|
||||||
minimum. Likewise, you set the maximum low and listen for anything higher as
|
|
||||||
the new maximum.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- analog sensor (potentiometer will do) attached to analog input 0
|
|
||||||
- LED attached from digital pin 9 to ground
|
|
||||||
|
|
||||||
created 29 Oct 2008
|
|
||||||
by David A Mellis
|
|
||||||
modified 30 Aug 2011
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Calibration
|
|
||||||
*/
|
|
||||||
|
|
||||||
// These constants won't change:
|
|
||||||
const int sensorPin = A0; // pin that the sensor is attached to
|
|
||||||
const int ledPin = 9; // pin that the LED is attached to
|
|
||||||
|
|
||||||
// variables:
|
|
||||||
int sensorValue = 0; // the sensor value
|
|
||||||
int sensorMin = 1023; // minimum sensor value
|
|
||||||
int sensorMax = 0; // maximum sensor value
|
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// turn on LED to signal the start of the calibration period:
|
|
||||||
pinMode(13, OUTPUT);
|
|
||||||
digitalWrite(13, HIGH);
|
|
||||||
|
|
||||||
// calibrate during the first five seconds
|
|
||||||
while (millis() < 5000) {
|
|
||||||
sensorValue = analogRead(sensorPin);
|
|
||||||
|
|
||||||
// record the maximum sensor value
|
|
||||||
if (sensorValue > sensorMax) {
|
|
||||||
sensorMax = sensorValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// record the minimum sensor value
|
|
||||||
if (sensorValue < sensorMin) {
|
|
||||||
sensorMin = sensorValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// signal the end of the calibration period
|
|
||||||
digitalWrite(13, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// read the sensor:
|
|
||||||
sensorValue = analogRead(sensorPin);
|
|
||||||
|
|
||||||
// apply the calibration to the sensor reading
|
|
||||||
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
|
|
||||||
|
|
||||||
// in case the sensor value is outside the range seen during calibration
|
|
||||||
sensorValue = constrain(sensorValue, 0, 255);
|
|
||||||
|
|
||||||
// fade the LED using the calibrated value:
|
|
||||||
analogWrite(ledPin, sensorValue);
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Define a maximum and minimum for expected analog sensor values.
|
|
Before Width: | Height: | Size: 97 KiB |
Before Width: | Height: | Size: 41 KiB |
@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
Fading
|
|
||||||
|
|
||||||
This example shows how to fade an LED using the analogWrite() function.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- LED attached from digital pin 9 to ground.
|
|
||||||
|
|
||||||
created 1 Nov 2008
|
|
||||||
by David A. Mellis
|
|
||||||
modified 30 Aug 2011
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Fading
|
|
||||||
*/
|
|
||||||
|
|
||||||
int ledPin = 9; // LED connected to digital pin 9
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// nothing happens in setup
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// fade in from min to max in increments of 5 points:
|
|
||||||
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
|
|
||||||
// sets the value (range from 0 to 255):
|
|
||||||
analogWrite(ledPin, fadeValue);
|
|
||||||
// wait for 30 milliseconds to see the dimming effect
|
|
||||||
delay(30);
|
|
||||||
}
|
|
||||||
|
|
||||||
// fade out from max to min in increments of 5 points:
|
|
||||||
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
|
|
||||||
// sets the value (range from 0 to 255):
|
|
||||||
analogWrite(ledPin, fadeValue);
|
|
||||||
// wait for 30 milliseconds to see the dimming effect
|
|
||||||
delay(30);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Use an analog output (PWM pin) to fade an LED.
|
|
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 33 KiB |
@ -1,64 +0,0 @@
|
|||||||
/*
|
|
||||||
Smoothing
|
|
||||||
|
|
||||||
Reads repeatedly from an analog input, calculating a running average and
|
|
||||||
printing it to the computer. Keeps ten readings in an array and continually
|
|
||||||
averages them.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- analog sensor (potentiometer will do) attached to analog input 0
|
|
||||||
|
|
||||||
created 22 Apr 2007
|
|
||||||
by David A. Mellis <dam@mellis.org>
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Smoothing
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Define the number of samples to keep track of. The higher the number, the
|
|
||||||
// more the readings will be smoothed, but the slower the output will respond to
|
|
||||||
// the input. Using a constant rather than a normal variable lets us use this
|
|
||||||
// value to determine the size of the readings array.
|
|
||||||
const int numReadings = 10;
|
|
||||||
|
|
||||||
int readings[numReadings]; // the readings from the analog input
|
|
||||||
int readIndex = 0; // the index of the current reading
|
|
||||||
int total = 0; // the running total
|
|
||||||
int average = 0; // the average
|
|
||||||
|
|
||||||
int inputPin = A0;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// initialize serial communication with computer:
|
|
||||||
Serial.begin(9600);
|
|
||||||
// initialize all the readings to 0:
|
|
||||||
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
|
|
||||||
readings[thisReading] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// subtract the last reading:
|
|
||||||
total = total - readings[readIndex];
|
|
||||||
// read from the sensor:
|
|
||||||
readings[readIndex] = analogRead(inputPin);
|
|
||||||
// add the reading to the total:
|
|
||||||
total = total + readings[readIndex];
|
|
||||||
// advance to the next position in the array:
|
|
||||||
readIndex = readIndex + 1;
|
|
||||||
|
|
||||||
// if we're at the end of the array...
|
|
||||||
if (readIndex >= numReadings) {
|
|
||||||
// ...wrap around to the beginning:
|
|
||||||
readIndex = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// calculate the average:
|
|
||||||
average = total / numReadings;
|
|
||||||
// send it to the computer as ASCII digits
|
|
||||||
Serial.println(average);
|
|
||||||
delay(1); // delay in between reads for stability
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Smooth multiple readings of an analog input.
|
|
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 32 KiB |
@ -1,77 +0,0 @@
|
|||||||
/*
|
|
||||||
ASCII table
|
|
||||||
|
|
||||||
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 <http://www.zambetti.com>
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/ASCIITable
|
|
||||||
*/
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
//Initialize serial and wait for port to open:
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for native USB port only
|
|
||||||
}
|
|
||||||
|
|
||||||
// prints title with ending line break
|
|
||||||
Serial.println("ASCII Table ~ Character Map");
|
|
||||||
}
|
|
||||||
|
|
||||||
// first visible ASCIIcharacter '!' is number 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 = '!';
|
|
||||||
|
|
||||||
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: ");
|
|
||||||
// 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);
|
|
||||||
// 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(", hex: ");
|
|
||||||
// prints value as string in hexadecimal (base 16):
|
|
||||||
Serial.print(thisByte, HEX);
|
|
||||||
|
|
||||||
Serial.print(", oct: ");
|
|
||||||
// prints value as string in octal (base 8);
|
|
||||||
Serial.print(thisByte, OCT);
|
|
||||||
|
|
||||||
Serial.print(", bin: ");
|
|
||||||
// prints value as string in binary (base 2) also prints ending line break:
|
|
||||||
Serial.println(thisByte, BIN);
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// go on to the next character
|
|
||||||
thisByte++;
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Demonstrates Arduino's advanced serial output functions.
|
|
@ -1,114 +0,0 @@
|
|||||||
/*
|
|
||||||
Dimmer
|
|
||||||
|
|
||||||
Demonstrates 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
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// initialize the serial communication:
|
|
||||||
Serial.begin(9600);
|
|
||||||
// initialize the ledPin as an output:
|
|
||||||
pinMode(ledPin, OUTPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
byte brightness;
|
|
||||||
|
|
||||||
// check if data has been sent from the computer:
|
|
||||||
if (Serial.available()) {
|
|
||||||
// read the most recent byte (which will be from 0 to 255):
|
|
||||||
brightness = Serial.read();
|
|
||||||
// set the brightness of the LED:
|
|
||||||
analogWrite(ledPin, brightness);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Processing code for this example
|
|
||||||
|
|
||||||
// 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:");
|
|
||||||
// if using Processing 2.1 or later, use Serial.printArray()
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* Max/MSP v5 patch for this example
|
|
||||||
|
|
||||||
----------begin_max5_patcher----------
|
|
||||||
1008.3ocuXszaiaCD9r8uhA5rqAeHIa0aAMaAVf1S6hdoYQAsDiL6JQZHQ2M
|
|
||||||
YWr+2KeX4vjnjXKKkKhhiGQ9MeyCNz+X9rnMp63sQvuB+MLa1OlOalSjUvrC
|
|
||||||
ymEUytKuh05TKJWUWyk5nE9eSyuS6jesvHu4F4MxOuUzB6X57sPKWVzBLXiP
|
|
||||||
xZtGj6q2vafaaT0.BzJfjj.p8ZPukazsQvpfcpFs8mXR3plh8BoBxURIOWyK
|
|
||||||
rxspZ0YI.eTCEh5Vqp+wGtFXZMKe6CZc3yWZwTdCmYW.BBkdiby8v0r+ST.W
|
|
||||||
sD9SdUkn8FYspPbqvnBNFtZWiUyLmleJWo0vuKzeuj2vpJLaWA7YiE7wREui
|
|
||||||
FpDFDp1KcbAFcP5sJoVxp4NB5Jq40ougIDxJt1wo3GDZHiNocKhiIExx+owv
|
|
||||||
AdOEAksDs.RRrOoww1Arc.9RvN2J9tamwjkcqknvAE0l+8WnjHqreNet8whK
|
|
||||||
z6mukIK4d+Xknv3jstvJs8EirMMhxsZIusET25jXbX8xczIl5xPVxhPcTGFu
|
|
||||||
xNDu9rXtUCg37g9Q8Yc+EuofIYmg8QdkPCrOnXsaHwYs3rWx9PGsO+pqueG2
|
|
||||||
uNQBqWFh1X7qQG+3.VHcHrfO1nyR2TlqpTM9MDsLKNCQVz6KO.+Sfc5j1Ykj
|
|
||||||
jzkn2jwNDRP7LVb3d9LtoWBAOnvB92Le6yRmZ4UF7YpQhiFi7A5Ka8zXhKdA
|
|
||||||
4r9TRGG7V4COiSbAJKdXrWNhhF0hNUh7uBa4Mba0l7JUK+omjDMwkSn95Izr
|
|
||||||
TOwkdp7W.oPRmNRQsiKeu4j3CkfVgt.NYPEYqMGvvJ48vIlPiyzrIuZskWIS
|
|
||||||
xGJPcmPiWOfLodybH3wjPbMYwlbFIMNHPHFOtLBNaLSa9sGk1TxMzCX5KTa6
|
|
||||||
WIH2ocxSdngM0QPqFRxyPHFsprrhGc9Gy9xoBjz0NWdR2yW9DUa2F85jG2v9
|
|
||||||
FgTO4Q8qiC7fzzQNpmNpsY3BrYPVJBMJQ1uVmoItRhw9NrVGO3NMNzYZ+zS7
|
|
||||||
3WTvTOnUydG5kHMKLqAOjTe7fN2bGSxOZDkMrBrGQ9J1gONBEy0k4gVo8qHc
|
|
||||||
cxmfxVihWz6a3yqY9NazzUYkua9UnynadOtogW.JfsVGRVNEbWF8I+eHtcwJ
|
|
||||||
+wLXqZeSdWLo+FQF6731Tva0BISKTx.cLwmgJsUTTvkg1YsnXmxDge.CDR7x
|
|
||||||
D6YmX6fMznaF7kdczmJXwm.XSOOrdoHhNA7GMiZYLZZR.+4lconMaJP6JOZ8
|
|
||||||
ftCs1YWHZI3o.sIXezX5ihMSuXzZtk3ai1mXRSczoCS32hAydeyXNEu5SHyS
|
|
||||||
xqZqbd3ZLdera1iPqYxOm++v7SUSz
|
|
||||||
-----------end_max5_patcher-----------
|
|
||||||
|
|
||||||
*/
|
|
@ -1 +0,0 @@
|
|||||||
Move the mouse to change the brightness of an LED.
|
|
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 31 KiB |
@ -1,152 +0,0 @@
|
|||||||
/*
|
|
||||||
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 so you
|
|
||||||
can see the value of the analog input changing over time.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- any analog input sensor 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// initialize the serial communication:
|
|
||||||
Serial.begin(9600);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// send the value of analog input 0:
|
|
||||||
Serial.println(analogRead(A0));
|
|
||||||
// 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 24 Nov 2015
|
|
||||||
// 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
|
|
||||||
float inByte = 0;
|
|
||||||
|
|
||||||
void setup () {
|
|
||||||
// set the window size:
|
|
||||||
size(400, 300);
|
|
||||||
|
|
||||||
// List all the available serial ports
|
|
||||||
// if using Processing 2.1 or later, use Serial.printArray()
|
|
||||||
println(Serial.list());
|
|
||||||
|
|
||||||
// I know that the first port in the serial list on my Mac is always my
|
|
||||||
// Arduino, so I open Serial.list()[0].
|
|
||||||
// Open whatever port is the one you're using.
|
|
||||||
myPort = new Serial(this, Serial.list()[0], 9600);
|
|
||||||
|
|
||||||
// don't generate a serialEvent() unless you get a newline character:
|
|
||||||
myPort.bufferUntil('\n');
|
|
||||||
|
|
||||||
// set initial background:
|
|
||||||
background(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void draw () {
|
|
||||||
// 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);
|
|
||||||
} else {
|
|
||||||
// increment the horizontal position:
|
|
||||||
xPos++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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:
|
|
||||||
inByte = float(inString);
|
|
||||||
println(inByte);
|
|
||||||
inByte = map(inByte, 0, 1023, 0, height);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* Max/MSP v5 patch for this example
|
|
||||||
|
|
||||||
----------begin_max5_patcher----------
|
|
||||||
1591.3oc0YszbaaCD9r7uBL5RalQUAO3CvdyS5zVenWZxs5NcfHgjPCIfJIT
|
|
||||||
RTxj+6AOHkoTDooroUs0AQPR73a+1cwtK3WtZxzEpOwqlB9YveAlL4KWMYh6
|
|
||||||
Q1GLo99ISKXeJMmU451zTUQAWpmNy+NM+SZ2y+sR1l02JuU9t0hJvFlNcMPy
|
|
||||||
dOuBv.U5Rgb0LPpRpYBooM3529latArTUVvzZdFPtsXAuDrrTU.f.sBffXxL
|
|
||||||
vGE50lIHkUVJXq3fRtdaoDvjYfbgjujaFJSCzq4.tLaN.bi1tJefWpqbO0uz
|
|
||||||
1IjIABoluxrJ1guxh2JfPO2B5zRNyBCLDFcqbwNvuv9fHCb8bvevyyEU2JKT
|
|
||||||
YhkBSWPAfq2TZ6YhqmuMUo0feUn+rYpY4YtY+cFw3lUJdCMYAapZqzwUHX8S
|
|
||||||
crjAd+SIOU6UBAwIygy.Q1+HAA1KH6EveWOFQlitUK92ehfal9kFhUxJ3tWc
|
|
||||||
sgpxadigWExbt1o7Ps5dk3yttivyg20W0VcSmg1G90qtx92rAZbH4ez.ruy1
|
|
||||||
nhmaDPidE07J+5n2sg6E6oKXxUSmc20o6E3SPRDbrkXnPGUYE.i5nCNB9TxQ
|
|
||||||
jG.G0kCTZtH88f07Rt0ZMMWUw8VvbKVAaTk6GyoraPdZff7rQTejBN54lgyv
|
|
||||||
HE0Ft7AvIvvgvIwO23jBdUkYOuSvIFSiNcjFhiSsUBwsUCh1AgfNSBAeNDBZ
|
|
||||||
DIDqY.f8.YjfjV1HAn9XDTxyNFYatVTkKx3kcK9GraZpI5jv7GOx+Z37Xh82
|
|
||||||
LSKHIDmDXaESoXRngIZQDKVkpxUkMCyXCQhcCK1z.G457gi3TzMz4RFD515F
|
|
||||||
G3bIQQwcP3SOF0zlkGhiCBQ1kOHHFFlXaEBQIQnCwv9QF1LxPZ.A4jR5cyQs
|
|
||||||
vbvHMJsLll01We+rE2LazX6zYmCraRrsPFwKg1ANBZFY.IAihr8Ox.aH0oAL
|
|
||||||
hB8nQVw0FSJiZeunOykbT6t3r.NP8.iL+bnwNiXuVMNJH9H9YCm89CFXPBER
|
|
||||||
bz422p8.O4dg6kRxdyjDqRwMIHTbT3QFLskxJ8tbmQK4tm0XGeZWF7wKKtYY
|
|
||||||
aTAF.XPNFaaQBinQMJ4QLF0aNHF0JtYuHSxoUZfZY6.UU2ejJTb8lQw8Fo5k
|
|
||||||
Rv6e2PI+fOM71o2ecY1VgTYdCSxxUqLokuYq9jYJi6lxPgD2NIPePLB0mwbG
|
|
||||||
YA9Rgxdiu1k5xiLlSU6JVnx6wzg3sYHwTesB8Z5D7RiGZpXyvDNJY.DQX3.H
|
|
||||||
hvmcUN4bP1yCkhpTle2P37jtBsKrLWcMScEmltOPv22ZfAqQAdKr9HzATQwZ
|
|
||||||
q18PrUGt6Tst2XMCRUfGuhXs6ccn23YloomMqcTiC5iMGPsHsHRWhWFlaenV
|
|
||||||
XcqwgCQiGGJzptyS2ZMODBz6fGza0bzmXBj7+DA94bvpR01MffAlueO7HwcI
|
|
||||||
pWCwmzJdvi9ILgflLAFmyXB6O7ML0YbD26lenmcGxjVsZUN+A6pUK7AtTrPg
|
|
||||||
M+eRYG0qD9j4I7eEbco8Xh6WcO.or9XDC6UCiewbXHkh6xm5LiPEkzpJDRTu
|
|
||||||
mEB44Fgz4NCtJvX.SM1vo2SlTCZGAe7GZu6ahdRyzFOhYZ+mbVVSYptBw.K1
|
|
||||||
tboIkatIA7c1cTKD1u.honLYV04VkluHsXe0szv9pQCE9Ro3jaVB1o15pz2X
|
|
||||||
zYoBvO5KXCAe0LCYJybE8ZODf4fV8t9qW0zYxq.YJfTosj1bv0xc.SaC0+AV
|
|
||||||
9V9L.KKyV3SyTcRtmzi6rO.O16USvts4B5xe9EymDvebK0eMfW6+NIsNlE2m
|
|
||||||
eqRyJ0utRq13+RjmqYKN1e.4d61jjdsauXe3.2p6jgi9hsNIv97CoyJ01xzl
|
|
||||||
c3ZhUCtSHx3UZgjoEJYqNY+hYs5zZQVFW19L3JDYaTlMLqAAt1G2yXlnFg9a
|
|
||||||
53L1FJVcv.cOX0dh7mCVGCLce7GFcQwDdH5Ta3nyAS0pQbHxegr+tGIZORgM
|
|
||||||
RnMj5vGl1Fs16drnk7Tf1XOLgv1n0d2iEsCxR.eQsNOZ4FGF7whofgfI3kES
|
|
||||||
1kCeOX5L2rifbdu0A9ae2X.V33B1Z+.Bj1FrP5iFrCYCG5EUWSG.hhunHJd.
|
|
||||||
HJ5hhnng3h9HPj4lud02.1bxGw.
|
|
||||||
-----------end_max5_patcher-----------
|
|
||||||
|
|
||||||
*/
|
|
@ -1 +0,0 @@
|
|||||||
Send data to the computer and graph it in Processing.
|
|
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 32 KiB |
@ -1 +0,0 @@
|
|||||||
Send MIDI note messages serially.
|
|
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
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 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
|
|
||||||
- MIDI jack pin 4 connected to +5V through 220 ohm resistor
|
|
||||||
- Attach a MIDI cable to the jack, then to a MIDI synth, and play music.
|
|
||||||
|
|
||||||
created 13 Jun 2006
|
|
||||||
modified 13 Aug 2012
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/Midi
|
|
||||||
*/
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Set MIDI baud rate:
|
|
||||||
Serial.begin(31250);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// play notes from F#-0 (0x1E) to F#-5 (0x5A):
|
|
||||||
for (int note = 0x1E; note < 0x5A; note ++) {
|
|
||||||
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
|
|
||||||
noteOn(0x90, note, 0x45);
|
|
||||||
delay(100);
|
|
||||||
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
|
|
||||||
noteOn(0x90, note, 0x00);
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that
|
|
||||||
// data values are less than 127:
|
|
||||||
void noteOn(int cmd, int pitch, int velocity) {
|
|
||||||
Serial.write(cmd);
|
|
||||||
Serial.write(pitch);
|
|
||||||
Serial.write(velocity);
|
|
||||||
}
|
|
Before Width: | Height: | Size: 41 KiB |