Archive original Arduino development files (part 2)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- examples/01.Basics/AnalogReadSerial/AnalogReadSerial.txt +1 -0
- examples/01.Basics/AnalogReadSerial/layout.png +3 -0
- examples/01.Basics/AnalogReadSerial/schematic.png +3 -0
- examples/01.Basics/BareMinimum/BareMinimum.ino +9 -0
- examples/01.Basics/BareMinimum/BareMinimum.txt +1 -0
- examples/01.Basics/Blink/Blink.ino +37 -0
- examples/01.Basics/Blink/Blink.txt +1 -0
- examples/01.Basics/DigitalReadSerial/DigitalReadSerial.ino +29 -0
- examples/01.Basics/DigitalReadSerial/DigitalReadSerial.txt +1 -0
- examples/01.Basics/DigitalReadSerial/schematic.png +3 -0
- examples/01.Basics/Fade/Fade.ino +40 -0
- examples/01.Basics/Fade/Fade.txt +1 -0
- examples/01.Basics/Fade/layout.png +3 -0
- examples/01.Basics/Fade/schematic.png +3 -0
- examples/01.Basics/ReadAnalogVoltage/ReadAnalogVoltage.ino +27 -0
- examples/01.Basics/ReadAnalogVoltage/ReadAnalogVoltage.txt +1 -0
- examples/02.Digital/BlinkWithoutDelay/BlinkWithoutDelay.ino +71 -0
- examples/02.Digital/BlinkWithoutDelay/BlinkWithoutDelay.txt +1 -0
- examples/02.Digital/BlinkWithoutDelay/layout.png +3 -0
- examples/02.Digital/BlinkWithoutDelay/schematic.png +3 -0
- examples/02.Digital/Button/Button.ino +51 -0
- examples/02.Digital/Button/Button.txt +1 -0
- examples/02.Digital/Button/schematic.png +3 -0
- examples/02.Digital/Debounce/Debounce.ino +86 -0
- examples/02.Digital/Debounce/Debounce.txt +1 -0
- examples/02.Digital/Debounce/button.png +3 -0
- examples/02.Digital/DigitalInputPullup/DigitalInputPullup.ino +46 -0
- examples/02.Digital/DigitalInputPullup/DigitalInputPullup.txt +1 -0
- examples/02.Digital/DigitalInputPullup/layout.png +3 -0
- examples/02.Digital/DigitalInputPullup/schematic.png +3 -0
- examples/02.Digital/StateChangeDetection/StateChangeDetection.ino +79 -0
- examples/02.Digital/StateChangeDetection/StateChangeDetection.txt +1 -0
- examples/02.Digital/toneKeyboard/layout.png +3 -0
- examples/02.Digital/toneKeyboard/pitches.h +95 -0
- examples/02.Digital/toneKeyboard/schematic.png +3 -0
- examples/02.Digital/toneKeyboard/toneKeyboard.ino +44 -0
- examples/02.Digital/toneKeyboard/toneKeyboard.txt +1 -0
- examples/02.Digital/toneMelody/layout.png +3 -0
- examples/02.Digital/toneMelody/schematic.png +3 -0
- examples/02.Digital/toneMelody/toneMelody.ino +50 -0
- examples/02.Digital/toneMelody/toneMelody.txt +1 -0
- examples/02.Digital/toneMultiple/layout.png +3 -0
- examples/02.Digital/toneMultiple/schematic.png +3 -0
- examples/02.Digital/toneMultiple/toneMultiple.ino +40 -0
- examples/02.Digital/toneMultiple/toneMultiple.txt +1 -0
- examples/02.Digital/tonePitchFollower/layout.png +3 -0
- examples/02.Digital/tonePitchFollower/schematic.png +3 -0
- examples/02.Digital/tonePitchFollower/tonePitchFollower.ino +39 -0
- examples/02.Digital/tonePitchFollower/tonePitchFollower.txt +1 -0
- examples/03.Analog/AnalogInOutSerial/AnalogInOutSerial.ino +52 -0
examples/01.Basics/AnalogReadSerial/AnalogReadSerial.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Read a potentiometer, print its state out to the Arduino Serial Monitor.
|
examples/01.Basics/AnalogReadSerial/layout.png
ADDED
|
Git LFS Details
|
examples/01.Basics/AnalogReadSerial/schematic.png
ADDED
|
Git LFS Details
|
examples/01.Basics/BareMinimum/BareMinimum.ino
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
void setup() {
|
| 2 |
+
// put your setup code here, to run once:
|
| 3 |
+
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
void loop() {
|
| 7 |
+
// put your main code here, to run repeatedly:
|
| 8 |
+
|
| 9 |
+
}
|
examples/01.Basics/BareMinimum/BareMinimum.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
The bare minimum of code needed to start an Arduino sketch.
|
examples/01.Basics/Blink/Blink.ino
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Blink
|
| 3 |
+
|
| 4 |
+
Turns an LED on for one second, then off for one second, repeatedly.
|
| 5 |
+
|
| 6 |
+
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
|
| 7 |
+
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
|
| 8 |
+
the correct LED pin independent of which board is used.
|
| 9 |
+
If you want to know what pin the on-board LED is connected to on your Arduino
|
| 10 |
+
model, check the Technical Specs of your board at:
|
| 11 |
+
https://www.arduino.cc/en/Main/Products
|
| 12 |
+
|
| 13 |
+
modified 8 May 2014
|
| 14 |
+
by Scott Fitzgerald
|
| 15 |
+
modified 2 Sep 2016
|
| 16 |
+
by Arturo Guadalupi
|
| 17 |
+
modified 8 Sep 2016
|
| 18 |
+
by Colby Newman
|
| 19 |
+
|
| 20 |
+
This example code is in the public domain.
|
| 21 |
+
|
| 22 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
|
| 23 |
+
*/
|
| 24 |
+
|
| 25 |
+
// the setup function runs once when you press reset or power the board
|
| 26 |
+
void setup() {
|
| 27 |
+
// initialize digital pin LED_BUILTIN as an output.
|
| 28 |
+
pinMode(LED_BUILTIN, OUTPUT);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// the loop function runs over and over again forever
|
| 32 |
+
void loop() {
|
| 33 |
+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
|
| 34 |
+
delay(1000); // wait for a second
|
| 35 |
+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
|
| 36 |
+
delay(1000); // wait for a second
|
| 37 |
+
}
|
examples/01.Basics/Blink/Blink.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Turn an LED on and off.
|
examples/01.Basics/DigitalReadSerial/DigitalReadSerial.ino
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
DigitalReadSerial
|
| 3 |
+
|
| 4 |
+
Reads a digital input on pin 2, prints the result to the Serial Monitor
|
| 5 |
+
|
| 6 |
+
This example code is in the public domain.
|
| 7 |
+
|
| 8 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial
|
| 9 |
+
*/
|
| 10 |
+
|
| 11 |
+
// digital pin 2 has a pushbutton attached to it. Give it a name:
|
| 12 |
+
int pushButton = 2;
|
| 13 |
+
|
| 14 |
+
// the setup routine runs once when you press reset:
|
| 15 |
+
void setup() {
|
| 16 |
+
// initialize serial communication at 9600 bits per second:
|
| 17 |
+
Serial.begin(9600);
|
| 18 |
+
// make the pushbutton's pin an input:
|
| 19 |
+
pinMode(pushButton, INPUT);
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// the loop routine runs over and over again forever:
|
| 23 |
+
void loop() {
|
| 24 |
+
// read the input pin:
|
| 25 |
+
int buttonState = digitalRead(pushButton);
|
| 26 |
+
// print out the state of the button:
|
| 27 |
+
Serial.println(buttonState);
|
| 28 |
+
delay(1); // delay in between reads for stability
|
| 29 |
+
}
|
examples/01.Basics/DigitalReadSerial/DigitalReadSerial.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Read a switch, print the state out to the Arduino Serial Monitor.
|
examples/01.Basics/DigitalReadSerial/schematic.png
ADDED
|
Git LFS Details
|
examples/01.Basics/Fade/Fade.ino
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Fade
|
| 3 |
+
|
| 4 |
+
This example shows how to fade an LED on pin 9 using the analogWrite()
|
| 5 |
+
function.
|
| 6 |
+
|
| 7 |
+
The analogWrite() function uses PWM, so if you want to change the pin you're
|
| 8 |
+
using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
|
| 9 |
+
are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
|
| 10 |
+
|
| 11 |
+
This example code is in the public domain.
|
| 12 |
+
|
| 13 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
|
| 14 |
+
*/
|
| 15 |
+
|
| 16 |
+
int led = 9; // the PWM pin the LED is attached to
|
| 17 |
+
int brightness = 0; // how bright the LED is
|
| 18 |
+
int fadeAmount = 5; // how many points to fade the LED by
|
| 19 |
+
|
| 20 |
+
// the setup routine runs once when you press reset:
|
| 21 |
+
void setup() {
|
| 22 |
+
// declare pin 9 to be an output:
|
| 23 |
+
pinMode(led, OUTPUT);
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// the loop routine runs over and over again forever:
|
| 27 |
+
void loop() {
|
| 28 |
+
// set the brightness of pin 9:
|
| 29 |
+
analogWrite(led, brightness);
|
| 30 |
+
|
| 31 |
+
// change the brightness for next time through the loop:
|
| 32 |
+
brightness = brightness + fadeAmount;
|
| 33 |
+
|
| 34 |
+
// reverse the direction of the fading at the ends of the fade:
|
| 35 |
+
if (brightness <= 0 || brightness >= 255) {
|
| 36 |
+
fadeAmount = -fadeAmount;
|
| 37 |
+
}
|
| 38 |
+
// wait for 30 milliseconds to see the dimming effect
|
| 39 |
+
delay(30);
|
| 40 |
+
}
|
examples/01.Basics/Fade/Fade.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Demonstrates the use of analogWrite() to fade an LED.
|
examples/01.Basics/Fade/layout.png
ADDED
|
Git LFS Details
|
examples/01.Basics/Fade/schematic.png
ADDED
|
Git LFS Details
|
examples/01.Basics/ReadAnalogVoltage/ReadAnalogVoltage.ino
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
ReadAnalogVoltage
|
| 3 |
+
|
| 4 |
+
Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
|
| 5 |
+
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
|
| 6 |
+
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
|
| 7 |
+
|
| 8 |
+
This example code is in the public domain.
|
| 9 |
+
|
| 10 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/ReadAnalogVoltage
|
| 11 |
+
*/
|
| 12 |
+
|
| 13 |
+
// the setup routine runs once when you press reset:
|
| 14 |
+
void setup() {
|
| 15 |
+
// initialize serial communication at 9600 bits per second:
|
| 16 |
+
Serial.begin(9600);
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// the loop routine runs over and over again forever:
|
| 20 |
+
void loop() {
|
| 21 |
+
// read the input on analog pin 0:
|
| 22 |
+
int sensorValue = analogRead(A0);
|
| 23 |
+
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
|
| 24 |
+
float voltage = sensorValue * (5.0 / 1023.0);
|
| 25 |
+
// print out the value you read:
|
| 26 |
+
Serial.println(voltage);
|
| 27 |
+
}
|
examples/01.Basics/ReadAnalogVoltage/ReadAnalogVoltage.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Reads an analog input and prints the voltage to the Serial Monitor.
|
examples/02.Digital/BlinkWithoutDelay/BlinkWithoutDelay.ino
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Blink without Delay
|
| 3 |
+
|
| 4 |
+
Turns on and off a light emitting diode (LED) connected to a digital pin,
|
| 5 |
+
without using the delay() function. This means that other code can run at the
|
| 6 |
+
same time without being interrupted by the LED code.
|
| 7 |
+
|
| 8 |
+
The circuit:
|
| 9 |
+
- Use the onboard LED.
|
| 10 |
+
- Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
|
| 11 |
+
and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
|
| 12 |
+
is set to the correct LED pin independent of which board is used.
|
| 13 |
+
If you want to know what pin the on-board LED is connected to on your
|
| 14 |
+
Arduino model, check the Technical Specs of your board at:
|
| 15 |
+
https://www.arduino.cc/en/Main/Products
|
| 16 |
+
|
| 17 |
+
created 2005
|
| 18 |
+
by David A. Mellis
|
| 19 |
+
modified 8 Feb 2010
|
| 20 |
+
by Paul Stoffregen
|
| 21 |
+
modified 11 Nov 2013
|
| 22 |
+
by Scott Fitzgerald
|
| 23 |
+
modified 9 Jan 2017
|
| 24 |
+
by Arturo Guadalupi
|
| 25 |
+
|
| 26 |
+
This example code is in the public domain.
|
| 27 |
+
|
| 28 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
|
| 29 |
+
*/
|
| 30 |
+
|
| 31 |
+
// constants won't change. Used here to set a pin number:
|
| 32 |
+
const int ledPin = LED_BUILTIN;// the number of the LED pin
|
| 33 |
+
|
| 34 |
+
// Variables will change:
|
| 35 |
+
int ledState = LOW; // ledState used to set the LED
|
| 36 |
+
|
| 37 |
+
// Generally, you should use "unsigned long" for variables that hold time
|
| 38 |
+
// The value will quickly become too large for an int to store
|
| 39 |
+
unsigned long previousMillis = 0; // will store last time LED was updated
|
| 40 |
+
|
| 41 |
+
// constants won't change:
|
| 42 |
+
const long interval = 1000; // interval at which to blink (milliseconds)
|
| 43 |
+
|
| 44 |
+
void setup() {
|
| 45 |
+
// set the digital pin as output:
|
| 46 |
+
pinMode(ledPin, OUTPUT);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
void loop() {
|
| 50 |
+
// here is where you'd put code that needs to be running all the time.
|
| 51 |
+
|
| 52 |
+
// check to see if it's time to blink the LED; that is, if the difference
|
| 53 |
+
// between the current time and last time you blinked the LED is bigger than
|
| 54 |
+
// the interval at which you want to blink the LED.
|
| 55 |
+
unsigned long currentMillis = millis();
|
| 56 |
+
|
| 57 |
+
if (currentMillis - previousMillis >= interval) {
|
| 58 |
+
// save the last time you blinked the LED
|
| 59 |
+
previousMillis = currentMillis;
|
| 60 |
+
|
| 61 |
+
// if the LED is off turn it on and vice-versa:
|
| 62 |
+
if (ledState == LOW) {
|
| 63 |
+
ledState = HIGH;
|
| 64 |
+
} else {
|
| 65 |
+
ledState = LOW;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// set the LED with the ledState of the variable:
|
| 69 |
+
digitalWrite(ledPin, ledState);
|
| 70 |
+
}
|
| 71 |
+
}
|
examples/02.Digital/BlinkWithoutDelay/BlinkWithoutDelay.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Blinking an LED without using the delay() function.
|
examples/02.Digital/BlinkWithoutDelay/layout.png
ADDED
|
Git LFS Details
|
examples/02.Digital/BlinkWithoutDelay/schematic.png
ADDED
|
Git LFS Details
|
examples/02.Digital/Button/Button.ino
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Button
|
| 3 |
+
|
| 4 |
+
Turns on and off a light emitting diode(LED) connected to digital pin 13,
|
| 5 |
+
when pressing a pushbutton attached to pin 2.
|
| 6 |
+
|
| 7 |
+
The circuit:
|
| 8 |
+
- LED attached from pin 13 to ground through 220 ohm resistor
|
| 9 |
+
- pushbutton attached to pin 2 from +5V
|
| 10 |
+
- 10K resistor attached to pin 2 from ground
|
| 11 |
+
|
| 12 |
+
- Note: on most Arduinos there is already an LED on the board
|
| 13 |
+
attached to pin 13.
|
| 14 |
+
|
| 15 |
+
created 2005
|
| 16 |
+
by DojoDave <http://www.0j0.org>
|
| 17 |
+
modified 30 Aug 2011
|
| 18 |
+
by Tom Igoe
|
| 19 |
+
|
| 20 |
+
This example code is in the public domain.
|
| 21 |
+
|
| 22 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
|
| 23 |
+
*/
|
| 24 |
+
|
| 25 |
+
// constants won't change. They're used here to set pin numbers:
|
| 26 |
+
const int buttonPin = 2; // the number of the pushbutton pin
|
| 27 |
+
const int ledPin = 13; // the number of the LED pin
|
| 28 |
+
|
| 29 |
+
// variables will change:
|
| 30 |
+
int buttonState = 0; // variable for reading the pushbutton status
|
| 31 |
+
|
| 32 |
+
void setup() {
|
| 33 |
+
// initialize the LED pin as an output:
|
| 34 |
+
pinMode(ledPin, OUTPUT);
|
| 35 |
+
// initialize the pushbutton pin as an input:
|
| 36 |
+
pinMode(buttonPin, INPUT);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
void loop() {
|
| 40 |
+
// read the state of the pushbutton value:
|
| 41 |
+
buttonState = digitalRead(buttonPin);
|
| 42 |
+
|
| 43 |
+
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
|
| 44 |
+
if (buttonState == HIGH) {
|
| 45 |
+
// turn LED on:
|
| 46 |
+
digitalWrite(ledPin, HIGH);
|
| 47 |
+
} else {
|
| 48 |
+
// turn LED off:
|
| 49 |
+
digitalWrite(ledPin, LOW);
|
| 50 |
+
}
|
| 51 |
+
}
|
examples/02.Digital/Button/Button.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Use a pushbutton to control an LED.
|
examples/02.Digital/Button/schematic.png
ADDED
|
Git LFS Details
|
examples/02.Digital/Debounce/Debounce.ino
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Debounce
|
| 3 |
+
|
| 4 |
+
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
|
| 5 |
+
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
|
| 6 |
+
minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
|
| 7 |
+
|
| 8 |
+
The circuit:
|
| 9 |
+
- LED attached from pin 13 to ground through 220 ohm resistor
|
| 10 |
+
- pushbutton attached from pin 2 to +5V
|
| 11 |
+
- 10 kilohm resistor attached from pin 2 to ground
|
| 12 |
+
|
| 13 |
+
- Note: On most Arduino boards, there is already an LED on the board connected
|
| 14 |
+
to pin 13, so you don't need any extra components for this example.
|
| 15 |
+
|
| 16 |
+
created 21 Nov 2006
|
| 17 |
+
by David A. Mellis
|
| 18 |
+
modified 30 Aug 2011
|
| 19 |
+
by Limor Fried
|
| 20 |
+
modified 28 Dec 2012
|
| 21 |
+
by Mike Walters
|
| 22 |
+
modified 30 Aug 2016
|
| 23 |
+
by Arturo Guadalupi
|
| 24 |
+
|
| 25 |
+
This example code is in the public domain.
|
| 26 |
+
|
| 27 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Debounce
|
| 28 |
+
*/
|
| 29 |
+
|
| 30 |
+
// constants won't change. They're used here to set pin numbers:
|
| 31 |
+
const int buttonPin = 2; // the number of the pushbutton pin
|
| 32 |
+
const int ledPin = 13; // the number of the LED pin
|
| 33 |
+
|
| 34 |
+
// Variables will change:
|
| 35 |
+
int ledState = HIGH; // the current state of the output pin
|
| 36 |
+
int buttonState; // the current reading from the input pin
|
| 37 |
+
int lastButtonState = LOW; // the previous reading from the input pin
|
| 38 |
+
|
| 39 |
+
// the following variables are unsigned longs because the time, measured in
|
| 40 |
+
// milliseconds, will quickly become a bigger number than can be stored in an int.
|
| 41 |
+
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
|
| 42 |
+
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
|
| 43 |
+
|
| 44 |
+
void setup() {
|
| 45 |
+
pinMode(buttonPin, INPUT);
|
| 46 |
+
pinMode(ledPin, OUTPUT);
|
| 47 |
+
|
| 48 |
+
// set initial LED state
|
| 49 |
+
digitalWrite(ledPin, ledState);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
void loop() {
|
| 53 |
+
// read the state of the switch into a local variable:
|
| 54 |
+
int reading = digitalRead(buttonPin);
|
| 55 |
+
|
| 56 |
+
// check to see if you just pressed the button
|
| 57 |
+
// (i.e. the input went from LOW to HIGH), and you've waited long enough
|
| 58 |
+
// since the last press to ignore any noise:
|
| 59 |
+
|
| 60 |
+
// If the switch changed, due to noise or pressing:
|
| 61 |
+
if (reading != lastButtonState) {
|
| 62 |
+
// reset the debouncing timer
|
| 63 |
+
lastDebounceTime = millis();
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
if ((millis() - lastDebounceTime) > debounceDelay) {
|
| 67 |
+
// whatever the reading is at, it's been there for longer than the debounce
|
| 68 |
+
// delay, so take it as the actual current state:
|
| 69 |
+
|
| 70 |
+
// if the button state has changed:
|
| 71 |
+
if (reading != buttonState) {
|
| 72 |
+
buttonState = reading;
|
| 73 |
+
|
| 74 |
+
// only toggle the LED if the new button state is HIGH
|
| 75 |
+
if (buttonState == HIGH) {
|
| 76 |
+
ledState = !ledState;
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// set the LED:
|
| 82 |
+
digitalWrite(ledPin, ledState);
|
| 83 |
+
|
| 84 |
+
// save the reading. Next time through the loop, it'll be the lastButtonState:
|
| 85 |
+
lastButtonState = reading;
|
| 86 |
+
}
|
examples/02.Digital/Debounce/Debounce.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Read a pushbutton, filtering noise.
|
examples/02.Digital/Debounce/button.png
ADDED
|
Git LFS Details
|
examples/02.Digital/DigitalInputPullup/DigitalInputPullup.ino
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Input Pull-up Serial
|
| 3 |
+
|
| 4 |
+
This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
|
| 5 |
+
input on pin 2 and prints the results to the Serial Monitor.
|
| 6 |
+
|
| 7 |
+
The circuit:
|
| 8 |
+
- momentary switch attached from pin 2 to ground
|
| 9 |
+
- built-in LED on pin 13
|
| 10 |
+
|
| 11 |
+
Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
|
| 12 |
+
20K-ohm resistor is pulled to 5V. This configuration causes the input to read
|
| 13 |
+
HIGH when the switch is open, and LOW when it is closed.
|
| 14 |
+
|
| 15 |
+
created 14 Mar 2012
|
| 16 |
+
by Scott Fitzgerald
|
| 17 |
+
|
| 18 |
+
This example code is in the public domain.
|
| 19 |
+
|
| 20 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/InputPullupSerial
|
| 21 |
+
*/
|
| 22 |
+
|
| 23 |
+
void setup() {
|
| 24 |
+
//start serial connection
|
| 25 |
+
Serial.begin(9600);
|
| 26 |
+
//configure pin 2 as an input and enable the internal pull-up resistor
|
| 27 |
+
pinMode(2, INPUT_PULLUP);
|
| 28 |
+
pinMode(13, OUTPUT);
|
| 29 |
+
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
void loop() {
|
| 33 |
+
//read the pushbutton value into a variable
|
| 34 |
+
int sensorVal = digitalRead(2);
|
| 35 |
+
//print out the value of the pushbutton
|
| 36 |
+
Serial.println(sensorVal);
|
| 37 |
+
|
| 38 |
+
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
|
| 39 |
+
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
|
| 40 |
+
// button's pressed, and off when it's not:
|
| 41 |
+
if (sensorVal == HIGH) {
|
| 42 |
+
digitalWrite(13, LOW);
|
| 43 |
+
} else {
|
| 44 |
+
digitalWrite(13, HIGH);
|
| 45 |
+
}
|
| 46 |
+
}
|
examples/02.Digital/DigitalInputPullup/DigitalInputPullup.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Demonstrates the use of INPUT_PULLUP with pinMode().
|
examples/02.Digital/DigitalInputPullup/layout.png
ADDED
|
Git LFS Details
|
examples/02.Digital/DigitalInputPullup/schematic.png
ADDED
|
Git LFS Details
|
examples/02.Digital/StateChangeDetection/StateChangeDetection.ino
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
State change detection (edge detection)
|
| 3 |
+
|
| 4 |
+
Often, you don't need to know the state of a digital input all the time, but
|
| 5 |
+
you just need to know when the input changes from one state to another.
|
| 6 |
+
For example, you want to know when a button goes from OFF to ON. This is called
|
| 7 |
+
state change detection, or edge detection.
|
| 8 |
+
|
| 9 |
+
This example shows how to detect when a button or button changes from off to on
|
| 10 |
+
and on to off.
|
| 11 |
+
|
| 12 |
+
The circuit:
|
| 13 |
+
- pushbutton attached to pin 2 from +5V
|
| 14 |
+
- 10 kilohm resistor attached to pin 2 from ground
|
| 15 |
+
- LED attached from pin 13 to ground through 220 ohm resistor (or use the
|
| 16 |
+
built-in LED on most Arduino boards)
|
| 17 |
+
|
| 18 |
+
created 27 Sep 2005
|
| 19 |
+
modified 30 Aug 2011
|
| 20 |
+
by Tom Igoe
|
| 21 |
+
|
| 22 |
+
This example code is in the public domain.
|
| 23 |
+
|
| 24 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
|
| 25 |
+
*/
|
| 26 |
+
|
| 27 |
+
// this constant won't change:
|
| 28 |
+
const int buttonPin = 2; // the pin that the pushbutton is attached to
|
| 29 |
+
const int ledPin = 13; // the pin that the LED is attached to
|
| 30 |
+
|
| 31 |
+
// Variables will change:
|
| 32 |
+
int buttonPushCounter = 0; // counter for the number of button presses
|
| 33 |
+
int buttonState = 0; // current state of the button
|
| 34 |
+
int lastButtonState = 0; // previous state of the button
|
| 35 |
+
|
| 36 |
+
void setup() {
|
| 37 |
+
// initialize the button pin as a input:
|
| 38 |
+
pinMode(buttonPin, INPUT);
|
| 39 |
+
// initialize the LED as an output:
|
| 40 |
+
pinMode(ledPin, OUTPUT);
|
| 41 |
+
// initialize serial communication:
|
| 42 |
+
Serial.begin(9600);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
void loop() {
|
| 47 |
+
// read the pushbutton input pin:
|
| 48 |
+
buttonState = digitalRead(buttonPin);
|
| 49 |
+
|
| 50 |
+
// compare the buttonState to its previous state
|
| 51 |
+
if (buttonState != lastButtonState) {
|
| 52 |
+
// if the state has changed, increment the counter
|
| 53 |
+
if (buttonState == HIGH) {
|
| 54 |
+
// if the current state is HIGH then the button went from off to on:
|
| 55 |
+
buttonPushCounter++;
|
| 56 |
+
Serial.println("on");
|
| 57 |
+
Serial.print("number of button pushes: ");
|
| 58 |
+
Serial.println(buttonPushCounter);
|
| 59 |
+
} else {
|
| 60 |
+
// if the current state is LOW then the button went from on to off:
|
| 61 |
+
Serial.println("off");
|
| 62 |
+
}
|
| 63 |
+
// Delay a little bit to avoid bouncing
|
| 64 |
+
delay(50);
|
| 65 |
+
}
|
| 66 |
+
// save the current state as the last state, for next time through the loop
|
| 67 |
+
lastButtonState = buttonState;
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
// turns on the LED every four button pushes by checking the modulo of the
|
| 71 |
+
// button push counter. the modulo function gives you the remainder of the
|
| 72 |
+
// division of two numbers:
|
| 73 |
+
if (buttonPushCounter % 4 == 0) {
|
| 74 |
+
digitalWrite(ledPin, HIGH);
|
| 75 |
+
} else {
|
| 76 |
+
digitalWrite(ledPin, LOW);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
}
|
examples/02.Digital/StateChangeDetection/StateChangeDetection.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Counting the number of button pushes.
|
examples/02.Digital/toneKeyboard/layout.png
ADDED
|
Git LFS Details
|
examples/02.Digital/toneKeyboard/pitches.h
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*************************************************
|
| 2 |
+
Public Constants
|
| 3 |
+
*************************************************/
|
| 4 |
+
|
| 5 |
+
#define NOTE_B0 31
|
| 6 |
+
#define NOTE_C1 33
|
| 7 |
+
#define NOTE_CS1 35
|
| 8 |
+
#define NOTE_D1 37
|
| 9 |
+
#define NOTE_DS1 39
|
| 10 |
+
#define NOTE_E1 41
|
| 11 |
+
#define NOTE_F1 44
|
| 12 |
+
#define NOTE_FS1 46
|
| 13 |
+
#define NOTE_G1 49
|
| 14 |
+
#define NOTE_GS1 52
|
| 15 |
+
#define NOTE_A1 55
|
| 16 |
+
#define NOTE_AS1 58
|
| 17 |
+
#define NOTE_B1 62
|
| 18 |
+
#define NOTE_C2 65
|
| 19 |
+
#define NOTE_CS2 69
|
| 20 |
+
#define NOTE_D2 73
|
| 21 |
+
#define NOTE_DS2 78
|
| 22 |
+
#define NOTE_E2 82
|
| 23 |
+
#define NOTE_F2 87
|
| 24 |
+
#define NOTE_FS2 93
|
| 25 |
+
#define NOTE_G2 98
|
| 26 |
+
#define NOTE_GS2 104
|
| 27 |
+
#define NOTE_A2 110
|
| 28 |
+
#define NOTE_AS2 117
|
| 29 |
+
#define NOTE_B2 123
|
| 30 |
+
#define NOTE_C3 131
|
| 31 |
+
#define NOTE_CS3 139
|
| 32 |
+
#define NOTE_D3 147
|
| 33 |
+
#define NOTE_DS3 156
|
| 34 |
+
#define NOTE_E3 165
|
| 35 |
+
#define NOTE_F3 175
|
| 36 |
+
#define NOTE_FS3 185
|
| 37 |
+
#define NOTE_G3 196
|
| 38 |
+
#define NOTE_GS3 208
|
| 39 |
+
#define NOTE_A3 220
|
| 40 |
+
#define NOTE_AS3 233
|
| 41 |
+
#define NOTE_B3 247
|
| 42 |
+
#define NOTE_C4 262
|
| 43 |
+
#define NOTE_CS4 277
|
| 44 |
+
#define NOTE_D4 294
|
| 45 |
+
#define NOTE_DS4 311
|
| 46 |
+
#define NOTE_E4 330
|
| 47 |
+
#define NOTE_F4 349
|
| 48 |
+
#define NOTE_FS4 370
|
| 49 |
+
#define NOTE_G4 392
|
| 50 |
+
#define NOTE_GS4 415
|
| 51 |
+
#define NOTE_A4 440
|
| 52 |
+
#define NOTE_AS4 466
|
| 53 |
+
#define NOTE_B4 494
|
| 54 |
+
#define NOTE_C5 523
|
| 55 |
+
#define NOTE_CS5 554
|
| 56 |
+
#define NOTE_D5 587
|
| 57 |
+
#define NOTE_DS5 622
|
| 58 |
+
#define NOTE_E5 659
|
| 59 |
+
#define NOTE_F5 698
|
| 60 |
+
#define NOTE_FS5 740
|
| 61 |
+
#define NOTE_G5 784
|
| 62 |
+
#define NOTE_GS5 831
|
| 63 |
+
#define NOTE_A5 880
|
| 64 |
+
#define NOTE_AS5 932
|
| 65 |
+
#define NOTE_B5 988
|
| 66 |
+
#define NOTE_C6 1047
|
| 67 |
+
#define NOTE_CS6 1109
|
| 68 |
+
#define NOTE_D6 1175
|
| 69 |
+
#define NOTE_DS6 1245
|
| 70 |
+
#define NOTE_E6 1319
|
| 71 |
+
#define NOTE_F6 1397
|
| 72 |
+
#define NOTE_FS6 1480
|
| 73 |
+
#define NOTE_G6 1568
|
| 74 |
+
#define NOTE_GS6 1661
|
| 75 |
+
#define NOTE_A6 1760
|
| 76 |
+
#define NOTE_AS6 1865
|
| 77 |
+
#define NOTE_B6 1976
|
| 78 |
+
#define NOTE_C7 2093
|
| 79 |
+
#define NOTE_CS7 2217
|
| 80 |
+
#define NOTE_D7 2349
|
| 81 |
+
#define NOTE_DS7 2489
|
| 82 |
+
#define NOTE_E7 2637
|
| 83 |
+
#define NOTE_F7 2794
|
| 84 |
+
#define NOTE_FS7 2960
|
| 85 |
+
#define NOTE_G7 3136
|
| 86 |
+
#define NOTE_GS7 3322
|
| 87 |
+
#define NOTE_A7 3520
|
| 88 |
+
#define NOTE_AS7 3729
|
| 89 |
+
#define NOTE_B7 3951
|
| 90 |
+
#define NOTE_C8 4186
|
| 91 |
+
#define NOTE_CS8 4435
|
| 92 |
+
#define NOTE_D8 4699
|
| 93 |
+
#define NOTE_DS8 4978
|
| 94 |
+
|
| 95 |
+
|
examples/02.Digital/toneKeyboard/schematic.png
ADDED
|
Git LFS Details
|
examples/02.Digital/toneKeyboard/toneKeyboard.ino
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Keyboard
|
| 3 |
+
|
| 4 |
+
Plays a pitch that changes based on a changing analog input
|
| 5 |
+
|
| 6 |
+
circuit:
|
| 7 |
+
- three force-sensing resistors from +5V to analog in 0 through 5
|
| 8 |
+
- three 10 kilohm resistors from analog in 0 through 5 to ground
|
| 9 |
+
- 8 ohm speaker on digital pin 8
|
| 10 |
+
|
| 11 |
+
created 21 Jan 2010
|
| 12 |
+
modified 9 Apr 2012
|
| 13 |
+
by Tom Igoe
|
| 14 |
+
|
| 15 |
+
This example code is in the public domain.
|
| 16 |
+
|
| 17 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneKeyboard
|
| 18 |
+
*/
|
| 19 |
+
|
| 20 |
+
#include "pitches.h"
|
| 21 |
+
|
| 22 |
+
const int threshold = 10; // minimum reading of the sensors that generates a note
|
| 23 |
+
|
| 24 |
+
// notes to play, corresponding to the 3 sensors:
|
| 25 |
+
int notes[] = {
|
| 26 |
+
NOTE_A4, NOTE_B4, NOTE_C3
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
void setup() {
|
| 30 |
+
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
void loop() {
|
| 34 |
+
for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
|
| 35 |
+
// get a sensor reading:
|
| 36 |
+
int sensorReading = analogRead(thisSensor);
|
| 37 |
+
|
| 38 |
+
// if the sensor is pressed hard enough:
|
| 39 |
+
if (sensorReading > threshold) {
|
| 40 |
+
// play the note corresponding to this sensor:
|
| 41 |
+
tone(8, notes[thisSensor], 20);
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
}
|
examples/02.Digital/toneKeyboard/toneKeyboard.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
A three-key musical keyboard using force sensors and a piezo speaker.
|
examples/02.Digital/toneMelody/layout.png
ADDED
|
Git LFS Details
|
examples/02.Digital/toneMelody/schematic.png
ADDED
|
Git LFS Details
|
examples/02.Digital/toneMelody/toneMelody.ino
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Melody
|
| 3 |
+
|
| 4 |
+
Plays a melody
|
| 5 |
+
|
| 6 |
+
circuit:
|
| 7 |
+
- 8 ohm speaker on digital pin 8
|
| 8 |
+
|
| 9 |
+
created 21 Jan 2010
|
| 10 |
+
modified 30 Aug 2011
|
| 11 |
+
by Tom Igoe
|
| 12 |
+
|
| 13 |
+
This example code is in the public domain.
|
| 14 |
+
|
| 15 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
#include "pitches.h"
|
| 19 |
+
|
| 20 |
+
// notes in the melody:
|
| 21 |
+
int melody[] = {
|
| 22 |
+
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
// note durations: 4 = quarter note, 8 = eighth note, etc.:
|
| 26 |
+
int noteDurations[] = {
|
| 27 |
+
4, 8, 8, 4, 4, 4, 4, 4
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
void setup() {
|
| 31 |
+
// iterate over the notes of the melody:
|
| 32 |
+
for (int thisNote = 0; thisNote < 8; thisNote++) {
|
| 33 |
+
|
| 34 |
+
// to calculate the note duration, take one second divided by the note type.
|
| 35 |
+
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
|
| 36 |
+
int noteDuration = 1000 / noteDurations[thisNote];
|
| 37 |
+
tone(8, melody[thisNote], noteDuration);
|
| 38 |
+
|
| 39 |
+
// to distinguish the notes, set a minimum time between them.
|
| 40 |
+
// the note's duration + 30% seems to work well:
|
| 41 |
+
int pauseBetweenNotes = noteDuration * 1.30;
|
| 42 |
+
delay(pauseBetweenNotes);
|
| 43 |
+
// stop the tone playing:
|
| 44 |
+
noTone(8);
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
void loop() {
|
| 49 |
+
// no need to repeat the melody.
|
| 50 |
+
}
|
examples/02.Digital/toneMelody/toneMelody.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Play a melody with a Piezo speaker.
|
examples/02.Digital/toneMultiple/layout.png
ADDED
|
Git LFS Details
|
examples/02.Digital/toneMultiple/schematic.png
ADDED
|
Git LFS Details
|
examples/02.Digital/toneMultiple/toneMultiple.ino
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Multiple tone player
|
| 3 |
+
|
| 4 |
+
Plays multiple tones on multiple pins in sequence
|
| 5 |
+
|
| 6 |
+
circuit:
|
| 7 |
+
- three 8 ohm speakers on digital pins 6, 7, and 8
|
| 8 |
+
|
| 9 |
+
created 8 Mar 2010
|
| 10 |
+
by Tom Igoe
|
| 11 |
+
based on a snippet from Greg Borenstein
|
| 12 |
+
|
| 13 |
+
This example code is in the public domain.
|
| 14 |
+
|
| 15 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMultiple
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
void setup() {
|
| 19 |
+
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
void loop() {
|
| 23 |
+
// turn off tone function for pin 8:
|
| 24 |
+
noTone(8);
|
| 25 |
+
// play a note on pin 6 for 200 ms:
|
| 26 |
+
tone(6, 440, 200);
|
| 27 |
+
delay(200);
|
| 28 |
+
|
| 29 |
+
// turn off tone function for pin 6:
|
| 30 |
+
noTone(6);
|
| 31 |
+
// play a note on pin 7 for 500 ms:
|
| 32 |
+
tone(7, 494, 500);
|
| 33 |
+
delay(500);
|
| 34 |
+
|
| 35 |
+
// turn off tone function for pin 7:
|
| 36 |
+
noTone(7);
|
| 37 |
+
// play a note on pin 8 for 300 ms:
|
| 38 |
+
tone(8, 523, 300);
|
| 39 |
+
delay(300);
|
| 40 |
+
}
|
examples/02.Digital/toneMultiple/toneMultiple.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Play tones on multiple speakers sequentially using the tone() command.
|
examples/02.Digital/tonePitchFollower/layout.png
ADDED
|
Git LFS Details
|
examples/02.Digital/tonePitchFollower/schematic.png
ADDED
|
Git LFS Details
|
examples/02.Digital/tonePitchFollower/tonePitchFollower.ino
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Pitch follower
|
| 3 |
+
|
| 4 |
+
Plays a pitch that changes based on a changing analog input
|
| 5 |
+
|
| 6 |
+
circuit:
|
| 7 |
+
- 8 ohm speaker on digital pin 9
|
| 8 |
+
- photoresistor on analog 0 to 5V
|
| 9 |
+
- 4.7 kilohm resistor on analog 0 to ground
|
| 10 |
+
|
| 11 |
+
created 21 Jan 2010
|
| 12 |
+
modified 31 May 2012
|
| 13 |
+
by Tom Igoe, with suggestion from Michael Flynn
|
| 14 |
+
|
| 15 |
+
This example code is in the public domain.
|
| 16 |
+
|
| 17 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/tonePitchFollower
|
| 18 |
+
*/
|
| 19 |
+
|
| 20 |
+
void setup() {
|
| 21 |
+
// initialize serial communications (for debugging only):
|
| 22 |
+
Serial.begin(9600);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
void loop() {
|
| 26 |
+
// read the sensor:
|
| 27 |
+
int sensorReading = analogRead(A0);
|
| 28 |
+
// print the sensor reading so you know its range
|
| 29 |
+
Serial.println(sensorReading);
|
| 30 |
+
// map the analog input range (in this case, 400 - 1000 from the photoresistor)
|
| 31 |
+
// to the output pitch range (120 - 1500Hz)
|
| 32 |
+
// change the minimum and maximum input numbers below depending on the range
|
| 33 |
+
// your sensor's giving:
|
| 34 |
+
int thisPitch = map(sensorReading, 400, 1000, 120, 1500);
|
| 35 |
+
|
| 36 |
+
// play the pitch:
|
| 37 |
+
tone(9, thisPitch, 10);
|
| 38 |
+
delay(1); // delay in between reads for stability
|
| 39 |
+
}
|
examples/02.Digital/tonePitchFollower/tonePitchFollower.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Play a pitch on a piezo speaker depending on an analog input.
|
examples/03.Analog/AnalogInOutSerial/AnalogInOutSerial.ino
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
Analog input, analog output, serial output
|
| 3 |
+
|
| 4 |
+
Reads an analog input pin, maps the result to a range from 0 to 255 and uses
|
| 5 |
+
the result to set the pulse width modulation (PWM) of an output pin.
|
| 6 |
+
Also prints the results to the Serial Monitor.
|
| 7 |
+
|
| 8 |
+
The circuit:
|
| 9 |
+
- potentiometer connected to analog pin 0.
|
| 10 |
+
Center pin of the potentiometer goes to the analog pin.
|
| 11 |
+
side pins of the potentiometer go to +5V and ground
|
| 12 |
+
- LED connected from digital pin 9 to ground through 220 ohm resistor
|
| 13 |
+
|
| 14 |
+
created 29 Dec. 2008
|
| 15 |
+
modified 9 Apr 2012
|
| 16 |
+
by Tom Igoe
|
| 17 |
+
|
| 18 |
+
This example code is in the public domain.
|
| 19 |
+
|
| 20 |
+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInOutSerial
|
| 21 |
+
*/
|
| 22 |
+
|
| 23 |
+
// These constants won't change. They're used to give names to the pins used:
|
| 24 |
+
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
|
| 25 |
+
const int analogOutPin = 9; // Analog output pin that the LED is attached to
|
| 26 |
+
|
| 27 |
+
int sensorValue = 0; // value read from the pot
|
| 28 |
+
int outputValue = 0; // value output to the PWM (analog out)
|
| 29 |
+
|
| 30 |
+
void setup() {
|
| 31 |
+
// initialize serial communications at 9600 bps:
|
| 32 |
+
Serial.begin(9600);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
void loop() {
|
| 36 |
+
// read the analog in value:
|
| 37 |
+
sensorValue = analogRead(analogInPin);
|
| 38 |
+
// map it to the range of the analog out:
|
| 39 |
+
outputValue = map(sensorValue, 0, 1023, 0, 255);
|
| 40 |
+
// change the analog out value:
|
| 41 |
+
analogWrite(analogOutPin, outputValue);
|
| 42 |
+
|
| 43 |
+
// print the results to the Serial Monitor:
|
| 44 |
+
Serial.print("sensor = ");
|
| 45 |
+
Serial.print(sensorValue);
|
| 46 |
+
Serial.print("\t output = ");
|
| 47 |
+
Serial.println(outputValue);
|
| 48 |
+
|
| 49 |
+
// wait 2 milliseconds before the next loop for the analog-to-digital
|
| 50 |
+
// converter to settle after the last reading:
|
| 51 |
+
delay(2);
|
| 52 |
+
}
|