arduino-0022
This commit is contained in:
parent
4f99742f03
commit
a9ad0e80a0
803 changed files with 69785 additions and 33024 deletions
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
AnalogReadSerial
|
||||
Reads an analog input on pin 0, prints the result to the serial monitor
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int sensorValue = analogRead(A0);
|
||||
Serial.println(sensorValue, DEC);
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
}
|
19
arduino-0022-linux-x64/examples/1.Basics/Blink/Blink.pde
Normal file
19
arduino-0022-linux-x64/examples/1.Basics/Blink/Blink.pde
Normal file
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
Blink
|
||||
Turns on an LED on for one second, then off for one second, repeatedly.
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
// initialize the digital pin as an output.
|
||||
// Pin 13 has an LED connected on most Arduino boards:
|
||||
pinMode(13, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
digitalWrite(13, HIGH); // set the LED on
|
||||
delay(1000); // wait for a second
|
||||
digitalWrite(13, LOW); // set the LED off
|
||||
delay(1000); // wait for a second
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
DigitalReadSerial
|
||||
Reads a digital input on pin 2, prints the result to the serial monitor
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
pinMode(2, INPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int sensorValue = digitalRead(2);
|
||||
Serial.println(sensorValue, DEC);
|
||||
}
|
||||
|
||||
|
||||
|
31
arduino-0022-linux-x64/examples/1.Basics/Fade/Fade.pde
Normal file
31
arduino-0022-linux-x64/examples/1.Basics/Fade/Fade.pde
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
Fade
|
||||
|
||||
This example shows how to fade an LED on pin 9
|
||||
using the analogWrite() function.
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
int brightness = 0; // how bright the LED is
|
||||
int fadeAmount = 5; // how many points to fade the LED by
|
||||
|
||||
void setup() {
|
||||
// declare pin 9 to be an output:
|
||||
pinMode(9, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// set the brightness of pin 9:
|
||||
analogWrite(9, 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);
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
/* 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:
|
||||
* LED attached from pin 13 to ground.
|
||||
* Note: on most Arduinos, there is already an LED on the board
|
||||
that's attached to pin 13, so no hardware is needed for this example.
|
||||
|
||||
|
||||
created 2005
|
||||
by David A. Mellis
|
||||
modified 8 Feb 2010
|
||||
by Paul Stoffregen
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
|
||||
*/
|
||||
|
||||
// constants won't change. Used here to
|
||||
// set pin numbers:
|
||||
const int ledPin = 13; // the number of the LED pin
|
||||
|
||||
// Variables will change:
|
||||
int ledState = LOW; // ledState used to set the LED
|
||||
long previousMillis = 0; // will store last time LED was updated
|
||||
|
||||
// the follow variables is a long because the time, measured in miliseconds,
|
||||
// will quickly become a bigger number than can be stored in an int.
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
56
arduino-0022-linux-x64/examples/2.Digital/Button/Button.pde
Normal file
56
arduino-0022-linux-x64/examples/2.Digital/Button/Button.pde
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
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 28 Oct 2010
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
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
|
||||
* 10K 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 November 2006
|
||||
by David A. Mellis
|
||||
modified 3 Jul 2009
|
||||
by Limor Fried
|
||||
|
||||
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 long's because the time, measured in miliseconds,
|
||||
// will quickly become a bigger number than can be stored in an int.
|
||||
long lastDebounceTime = 0; // the last time the output pin was toggled
|
||||
long debounceDelay = 50; // the debounce time; increase if the output flickers
|
||||
|
||||
void setup() {
|
||||
pinMode(buttonPin, INPUT);
|
||||
pinMode(ledPin, OUTPUT);
|
||||
}
|
||||
|
||||
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:
|
||||
buttonState = reading;
|
||||
}
|
||||
|
||||
// set the LED using the state of the button:
|
||||
digitalWrite(ledPin, buttonState);
|
||||
|
||||
// save the reading. Next time through the loop,
|
||||
// it'll be the lastButtonState:
|
||||
lastButtonState = reading;
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
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
|
||||
* 10K 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 14 Oct 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://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
|
||||
// wend from off to on:
|
||||
buttonPushCounter++;
|
||||
Serial.println("on");
|
||||
Serial.print("number of button pushes: ");
|
||||
Serial.println(buttonPushCounter, DEC);
|
||||
}
|
||||
else {
|
||||
// if the current state is LOW then the button
|
||||
// wend from on to off:
|
||||
Serial.println("off");
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
/*************************************************
|
||||
* 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
|
||||
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
keyboard
|
||||
|
||||
Plays a pitch that changes based on a changing analog input
|
||||
|
||||
circuit:
|
||||
* 3 force-sensing resistors from +5V to analog in 0 through 5
|
||||
* 3 10K resistors from analog in 0 through 5 to ground
|
||||
* 8-ohm speaker on digital pin 8
|
||||
|
||||
created 21 Jan 2010
|
||||
Modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://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);
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
/*************************************************
|
||||
* 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
|
||||
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
Melody
|
||||
|
||||
Plays a melody
|
||||
|
||||
circuit:
|
||||
* 8-ohm speaker on digital pin 8
|
||||
|
||||
created 21 Jan 2010
|
||||
modified 14 Oct 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://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.
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
/*************************************************
|
||||
* 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
|
||||
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
Multiple tone player
|
||||
|
||||
Plays multiple tones on multiple pins in sequence
|
||||
|
||||
circuit:
|
||||
* 3 8-ohm speaker on digital pins 6, 7, and 11
|
||||
|
||||
created 8 March 2010
|
||||
by Tom Igoe
|
||||
based on a snippet from Greg Borenstein
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://arduino.cc/en/Tutorial/Tone4
|
||||
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// turn off tone function for pin 11:
|
||||
noTone(11);
|
||||
// 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 11 for 500 ms:
|
||||
tone(11, 523, 300);
|
||||
delay(300);
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
Pitch follower
|
||||
|
||||
Plays a pitch that changes based on a changing analog input
|
||||
|
||||
circuit:
|
||||
* 8-ohm speaker on digital pin 8
|
||||
* photoresistor on analog 0 to 5V
|
||||
* 4.7K resistor on analog 0 to ground
|
||||
|
||||
created 21 Jan 2010
|
||||
Modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://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 pitch to the range of the analog input.
|
||||
// change the minimum and maximum input numbers below
|
||||
// depending on the range your sensor's giving:
|
||||
int thisPitch = map(sensorReading, 400, 1000, 100, 1000);
|
||||
|
||||
// play the pitch:
|
||||
tone(8, thisPitch, 10);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
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 pulsewidth 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 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
// 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 10 milliseconds before the next loop
|
||||
// for the analog-to-digital converter to settle
|
||||
// after the last reading:
|
||||
delay(10);
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
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 attached to analog input 0
|
||||
* center pin of the potentiometer to the analog pin
|
||||
* one side pin (either one) to ground
|
||||
* the other side pin to +5V
|
||||
* LED anode (long leg) attached to digital output 13
|
||||
* LED 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 4 Sep 2010
|
||||
By Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://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);
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
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 previous 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.
|
||||
|
||||
*/
|
||||
// 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 brithstest to off:
|
||||
for (int brightness = 255; brightness >= 0; brightness--) {
|
||||
analogWrite(thisPin, brightness);
|
||||
delay(2);
|
||||
}
|
||||
// pause between LEDs:
|
||||
delay(100);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
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 4 Sep 2010
|
||||
By Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/Calibration
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
// 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);
|
||||
}
|
45
arduino-0022-linux-x64/examples/3.Analog/Fading/Fading.pde
Normal file
45
arduino-0022-linux-x64/examples/3.Analog/Fading/Fading.pde
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
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 17 June 2009
|
||||
By Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/Fading
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
|
||||
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 April 2007
|
||||
By David A. Mellis <dam@mellis.org>
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Smoothing
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// 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
|
||||
// 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 index = 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[index];
|
||||
// read from the sensor:
|
||||
readings[index] = analogRead(inputPin);
|
||||
// add the reading to the total:
|
||||
total= total + readings[index];
|
||||
// advance to the next position in the array:
|
||||
index = index + 1;
|
||||
|
||||
// if we're at the end of the array...
|
||||
if (index >= numReadings)
|
||||
// ...wrap around to the beginning:
|
||||
index = 0;
|
||||
|
||||
// calculate the average:
|
||||
average = total / numReadings;
|
||||
// send it to the computer (as ASCII digits)
|
||||
Serial.println(average, DEC);
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
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
|
||||
modified 18 Jan 2009
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
<http://www.zambetti.com>
|
||||
|
||||
*/
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
// 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.print(thisByte, BYTE);
|
||||
|
||||
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++;
|
||||
}
|
|
@ -0,0 +1,364 @@
|
|||
/*
|
||||
Dimmer
|
||||
|
||||
Demonstrates the sending data from the computer to the Arduino board,
|
||||
in this case to control the brightness of an LED. The data is sent
|
||||
in individual bytes, each of which ranges from 0 to 255. Arduino
|
||||
reads these bytes and uses them to set the brightness of the LED.
|
||||
|
||||
The circuit:
|
||||
LED attached from digital pin 9 to ground.
|
||||
Serial connection to Processing, Max/MSP, or another serial application
|
||||
|
||||
created 2006
|
||||
by David A. Mellis
|
||||
modified 14 Apr 2009
|
||||
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:");
|
||||
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
|
||||
|
||||
{
|
||||
"boxes" : [ {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Dimmer\n\nThis patch sends a binary number from 0 to 255 out the serial port to an Arduino connected to the port. It dims an LED attached to the Arduino.\n\ncreated 2006\nby David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
|
||||
"linecount" : 10,
|
||||
"patching_rect" : [ 209.0, 55.0, 344.0, 144.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-32",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "change the slider to alter the brightness of the LED",
|
||||
"linecount" : 3,
|
||||
"patching_rect" : [ 90.0, 235.0, 117.0, 48.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-7",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "number",
|
||||
"patching_rect" : [ 215.0, 385.0, 50.0, 19.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 10.0,
|
||||
"outlettype" : [ "int", "bang" ],
|
||||
"id" : "obj-6",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "slider",
|
||||
"patching_rect" : [ 215.0, 235.0, 20.0, 140.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "" ],
|
||||
"bgcolor" : [ 0.94902, 0.94902, 0.94902, 0.0 ],
|
||||
"id" : "obj-1",
|
||||
"size" : 256.0,
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "select 0 1",
|
||||
"patching_rect" : [ 342.0, 305.0, 62.0, 20.0 ],
|
||||
"numoutlets" : 3,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "bang", "bang", "" ],
|
||||
"id" : "obj-30",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "click here to close the serial port",
|
||||
"patching_rect" : [ 390.0, 396.0, 206.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-26",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "click here to open the serial port",
|
||||
"patching_rect" : [ 415.0, 370.0, 206.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-27",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "close",
|
||||
"patching_rect" : [ 342.0, 396.0, 39.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-21",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "port a",
|
||||
"patching_rect" : [ 364.0, 370.0, 41.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-19",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click here to get a list of serial ports",
|
||||
"patching_rect" : [ 435.0, 344.0, 207.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-2",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "toggle",
|
||||
"patching_rect" : [ 342.0, 268.0, 15.0, 15.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-11",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "print",
|
||||
"patching_rect" : [ 384.0, 344.0, 36.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-13",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "serial a 9600",
|
||||
"patching_rect" : [ 259.0, 420.0, 84.0, 20.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int", "" ],
|
||||
"id" : "obj-14",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click to start",
|
||||
"patching_rect" : [ 369.0, 268.0, 117.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-17",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "panel",
|
||||
"patching_rect" : [ 215.0, 235.0, 21.0, 139.0 ],
|
||||
"numoutlets" : 0,
|
||||
"mode" : 1,
|
||||
"grad1" : [ 1.0, 1.0, 1.0, 1.0 ],
|
||||
"id" : "obj-8",
|
||||
"grad2" : [ 0.509804, 0.509804, 0.509804, 1.0 ],
|
||||
"numinlets" : 1,
|
||||
"angle" : 270.0
|
||||
}
|
||||
|
||||
}
|
||||
],
|
||||
"lines" : [ {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-11", 0 ],
|
||||
"destination" : [ "obj-30", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 351.0, 296.0, 351.5, 296.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-30", 1 ],
|
||||
"destination" : [ "obj-19", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-30", 0 ],
|
||||
"destination" : [ "obj-21", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-21", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 351.5, 416.5, 268.5, 416.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-19", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 373.5, 393.5, 268.5, 393.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-13", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 393.5, 365.5, 268.5, 365.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-1", 0 ],
|
||||
"destination" : [ "obj-6", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-6", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 224.5, 411.5, 268.5, 411.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
579
arduino-0022-linux-x64/examples/4.Communication/Graph/Graph.pde
Normal file
579
arduino-0022-linux-x64/examples/4.Communication/Graph/Graph.pde
Normal file
|
@ -0,0 +1,579 @@
|
|||
/*
|
||||
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 is attached to analog in pin 0.
|
||||
|
||||
created 2006
|
||||
by David A. Mellis
|
||||
modified 14 Apr 2009
|
||||
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(10);
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
|
||||
// Graphing sketch
|
||||
|
||||
|
||||
// This program takes ASCII-encoded strings
|
||||
// from the serial port at 9600 baud and graphs them. It expects values in the
|
||||
// range 0 to 1023, followed by a newline, or newline and carriage return
|
||||
|
||||
// Created 20 Apr 2005
|
||||
// Updated 18 Jan 2008
|
||||
// by Tom Igoe
|
||||
// This example code is in the public domain.
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
Serial myPort; // The serial port
|
||||
int xPos = 1; // horizontal position of the graph
|
||||
|
||||
void setup () {
|
||||
// set the window size:
|
||||
size(400, 300);
|
||||
|
||||
// List all the available serial ports
|
||||
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 inital background:
|
||||
background(0);
|
||||
}
|
||||
void draw () {
|
||||
// everything happens in the serialEvent()
|
||||
}
|
||||
|
||||
void serialEvent (Serial myPort) {
|
||||
// get the ASCII string:
|
||||
String inString = myPort.readStringUntil('\n');
|
||||
|
||||
if (inString != null) {
|
||||
// trim off any whitespace:
|
||||
inString = trim(inString);
|
||||
// convert to an int and map to the screen height:
|
||||
float inByte = float(inString);
|
||||
inByte = map(inByte, 0, 1023, 0, height);
|
||||
|
||||
// draw the line:
|
||||
stroke(127,34,255);
|
||||
line(xPos, height, xPos, height - inByte);
|
||||
|
||||
// at the edge of the screen, go back to the beginning:
|
||||
if (xPos >= width) {
|
||||
xPos = 0;
|
||||
background(0);
|
||||
}
|
||||
else {
|
||||
// increment the horizontal position:
|
||||
xPos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
/* Max/MSP v5 patch for this example
|
||||
{
|
||||
"boxes" : [ {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Graph\n\nThis patch takes a string, containing ASCII formatted number from 0 to 1023, with a carriage return and linefeed at the end. It converts the string to an integer and graphs it.\n\ncreated 2006\nby David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
|
||||
"linecount" : 10,
|
||||
"patching_rect" : [ 479.0, 6.0, 344.0, 144.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-32",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "select 0 1",
|
||||
"patching_rect" : [ 327.0, 80.0, 62.0, 20.0 ],
|
||||
"numoutlets" : 3,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "bang", "bang", "" ],
|
||||
"id" : "obj-30",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "click here to close the serial port",
|
||||
"patching_rect" : [ 412.0, 231.0, 206.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-26",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "click here to open the serial port",
|
||||
"patching_rect" : [ 412.0, 205.0, 206.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-27",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "close",
|
||||
"patching_rect" : [ 327.0, 231.0, 39.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-21",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "port a",
|
||||
"patching_rect" : [ 349.0, 205.0, 41.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-19",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "multislider",
|
||||
"candicane7" : [ 0.878431, 0.243137, 0.145098, 1.0 ],
|
||||
"patching_rect" : [ 302.0, 450.0, 246.0, 167.0 ],
|
||||
"contdata" : 1,
|
||||
"numoutlets" : 2,
|
||||
"peakcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ],
|
||||
"slidercolor" : [ 0.066667, 0.058824, 0.776471, 1.0 ],
|
||||
"candicane8" : [ 0.027451, 0.447059, 0.501961, 1.0 ],
|
||||
"outlettype" : [ "", "" ],
|
||||
"setminmax" : [ 0.0, 1023.0 ],
|
||||
"settype" : 0,
|
||||
"candicane6" : [ 0.733333, 0.035294, 0.788235, 1.0 ],
|
||||
"setstyle" : 3,
|
||||
"bgcolor" : [ 0.231373, 0.713726, 1.0, 1.0 ],
|
||||
"id" : "obj-1",
|
||||
"candicane4" : [ 0.439216, 0.619608, 0.070588, 1.0 ],
|
||||
"candicane5" : [ 0.584314, 0.827451, 0.431373, 1.0 ],
|
||||
"candicane2" : [ 0.145098, 0.203922, 0.356863, 1.0 ],
|
||||
"candicane3" : [ 0.290196, 0.411765, 0.713726, 1.0 ],
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click here to get a list of serial ports",
|
||||
"patching_rect" : [ 412.0, 179.0, 207.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-2",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Here's the number from Arduino's analog input",
|
||||
"linecount" : 2,
|
||||
"patching_rect" : [ 153.0, 409.0, 138.0, 34.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-3",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Convert ASCII to symbol",
|
||||
"patching_rect" : [ 379.0, 378.0, 147.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-4",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Convert integer to ASCII",
|
||||
"patching_rect" : [ 379.0, 355.0, 147.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-5",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "number",
|
||||
"patching_rect" : [ 302.0, 414.0, 37.0, 20.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int", "bang" ],
|
||||
"bgcolor" : [ 0.866667, 0.866667, 0.866667, 1.0 ],
|
||||
"id" : "obj-6",
|
||||
"triscale" : 0.9,
|
||||
"fontname" : "Arial",
|
||||
"htextcolor" : [ 0.870588, 0.870588, 0.870588, 1.0 ],
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "fromsymbol",
|
||||
"patching_rect" : [ 302.0, 378.0, 74.0, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-7",
|
||||
"fontname" : "Arial",
|
||||
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "itoa",
|
||||
"patching_rect" : [ 302.0, 355.0, 46.0, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-8",
|
||||
"fontname" : "Arial",
|
||||
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
|
||||
"numinlets" : 3
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "zl group 4",
|
||||
"patching_rect" : [ 302.0, 332.0, 64.0, 20.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "", "" ],
|
||||
"id" : "obj-9",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "select 10 13",
|
||||
"patching_rect" : [ 244.0, 281.0, 77.0, 20.0 ],
|
||||
"numoutlets" : 3,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "bang", "bang", "" ],
|
||||
"id" : "obj-10",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "toggle",
|
||||
"patching_rect" : [ 244.0, 43.0, 15.0, 15.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-11",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "qmetro 10",
|
||||
"patching_rect" : [ 244.0, 80.0, 65.0, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "bang" ],
|
||||
"id" : "obj-12",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "print",
|
||||
"patching_rect" : [ 369.0, 179.0, 36.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-13",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "serial a 9600",
|
||||
"patching_rect" : [ 244.0, 255.0, 84.0, 20.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int", "" ],
|
||||
"id" : "obj-14",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Read serial input buffer every 10 milliseconds",
|
||||
"linecount" : 2,
|
||||
"patching_rect" : [ 53.0, 72.0, 185.0, 34.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-15",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "If you get newline (ASCII 10), send the list. If you get return (ASCII 13) do nothing. Any other value, add to the list",
|
||||
"linecount" : 3,
|
||||
"patching_rect" : [ 332.0, 269.0, 320.0, 48.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-16",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click to open/close serial port and start/stop patch",
|
||||
"linecount" : 2,
|
||||
"patching_rect" : [ 271.0, 32.0, 199.0, 34.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-17",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
],
|
||||
"lines" : [ {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-6", 0 ],
|
||||
"destination" : [ "obj-1", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-7", 0 ],
|
||||
"destination" : [ "obj-6", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-8", 0 ],
|
||||
"destination" : [ "obj-7", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-9", 0 ],
|
||||
"destination" : [ "obj-8", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-10", 0 ],
|
||||
"destination" : [ "obj-9", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 253.5, 308.0, 311.5, 308.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-10", 2 ],
|
||||
"destination" : [ "obj-9", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 311.5, 320.0, 311.5, 320.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-14", 0 ],
|
||||
"destination" : [ "obj-10", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-12", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-11", 0 ],
|
||||
"destination" : [ "obj-12", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-13", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 378.5, 200.5, 253.5, 200.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-19", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 358.5, 228.5, 253.5, 228.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-21", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 336.5, 251.5, 253.5, 251.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-30", 0 ],
|
||||
"destination" : [ "obj-21", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-30", 1 ],
|
||||
"destination" : [ "obj-19", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-11", 0 ],
|
||||
"destination" : [ "obj-30", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 253.0, 71.0, 336.5, 71.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
*/
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
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 2 Jul 2009
|
||||
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 (intnote = 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.print(cmd, BYTE);
|
||||
Serial.print(pitch, BYTE);
|
||||
Serial.print(velocity, BYTE);
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
Mega multple serial test
|
||||
|
||||
Receives from the main serial port, sends to the others.
|
||||
Receives from serial port 1, sends to the main serial (Serial 0).
|
||||
|
||||
This example works only on the Arduino Mega
|
||||
|
||||
The circuit:
|
||||
* Any serial device attached to Serial port 1
|
||||
* Serial monitor open on Serial port 0:
|
||||
|
||||
created 30 Dec. 2008
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
void setup() {
|
||||
// initialize both serial ports:
|
||||
Serial.begin(9600);
|
||||
Serial1.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// read from port 1, send to port 0:
|
||||
if (Serial1.available()) {
|
||||
int inByte = Serial1.read();
|
||||
Serial.print(inByte, BYTE);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,711 @@
|
|||
/*
|
||||
Physical Pixel
|
||||
|
||||
An example of using the Arduino board to receive data from the
|
||||
computer. In this case, the Arduino boards turns on an LED when
|
||||
it receives the character 'H', and turns off the LED when it
|
||||
receives the character 'L'.
|
||||
|
||||
The data can be sent from the Arduino serial monitor, or another
|
||||
program like Processing (see code below), Flash (via a serial-net
|
||||
proxy), PD, or Max/MSP.
|
||||
|
||||
The circuit:
|
||||
* LED connected from digital pin 13 to ground
|
||||
|
||||
created 2006
|
||||
by David A. Mellis
|
||||
modified 14 Apr 2009
|
||||
by Tom Igoe and Scott Fitzgerald
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/PhysicalPixel
|
||||
*/
|
||||
|
||||
const int ledPin = 13; // the pin that the LED is attached to
|
||||
int incomingByte; // a variable to read incoming serial data into
|
||||
|
||||
void setup() {
|
||||
// initialize serial communication:
|
||||
Serial.begin(9600);
|
||||
// initialize the LED pin as an output:
|
||||
pinMode(ledPin, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// see if there's incoming serial data:
|
||||
if (Serial.available() > 0) {
|
||||
// read the oldest byte in the serial buffer:
|
||||
incomingByte = Serial.read();
|
||||
// if it's a capital H (ASCII 72), turn on the LED:
|
||||
if (incomingByte == 'H') {
|
||||
digitalWrite(ledPin, HIGH);
|
||||
}
|
||||
// if it's an L (ASCII 76) turn off the LED:
|
||||
if (incomingByte == 'L') {
|
||||
digitalWrite(ledPin, LOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
|
||||
// mouseover serial
|
||||
|
||||
// Demonstrates how to send data to the Arduino I/O board, in order to
|
||||
// turn ON a light if the mouse is over a square and turn it off
|
||||
// if the mouse is not.
|
||||
|
||||
// created 2003-4
|
||||
// based on examples by Casey Reas and Hernando Barragan
|
||||
// modified 18 Jan 2009
|
||||
// by Tom Igoe
|
||||
// This example code is in the public domain.
|
||||
|
||||
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
float boxX;
|
||||
float boxY;
|
||||
int boxSize = 20;
|
||||
boolean mouseOverBox = false;
|
||||
|
||||
Serial port;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
boxX = width/2.0;
|
||||
boxY = height/2.0;
|
||||
rectMode(RADIUS);
|
||||
|
||||
// List all the available serial ports in the output pane.
|
||||
// You will need to choose the port that the Arduino board is
|
||||
// connected to from this list. The first port in the list is
|
||||
// port #0 and the third port in the list is port #2.
|
||||
println(Serial.list());
|
||||
|
||||
// Open the port that the Arduino board is connected to (in this case #0)
|
||||
// Make sure to open the port at the same speed Arduino is using (9600bps)
|
||||
port = new Serial(this, Serial.list()[0], 9600);
|
||||
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(0);
|
||||
|
||||
// Test if the cursor is over the box
|
||||
if (mouseX > boxX-boxSize && mouseX < boxX+boxSize &&
|
||||
mouseY > boxY-boxSize && mouseY < boxY+boxSize) {
|
||||
mouseOverBox = true;
|
||||
// draw a line around the box and change its color:
|
||||
stroke(255);
|
||||
fill(153);
|
||||
// send an 'H' to indicate mouse is over square:
|
||||
port.write('H');
|
||||
}
|
||||
else {
|
||||
// return the box to it's inactive state:
|
||||
stroke(153);
|
||||
fill(153);
|
||||
// send an 'L' to turn the LED off:
|
||||
port.write('L');
|
||||
mouseOverBox = false;
|
||||
}
|
||||
|
||||
// Draw the box
|
||||
rect(boxX, boxY, boxSize, boxSize);
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
{
|
||||
"boxes" : [ {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Physical Pixel\n\nThis patch sends an ASCII H or an ASCII L out the serial port to turn on an LED attached to an Arduino board. It can also send alternating H and L characters once every second to make the LED blink.\n\ncreated 2006\nby David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
|
||||
"linecount" : 11,
|
||||
"patching_rect" : [ 14.0, 35.0, 354.0, 158.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-1",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click to blink every second",
|
||||
"patching_rect" : [ 99.0, 251.0, 161.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-38",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "toggle",
|
||||
"patching_rect" : [ 74.0, 251.0, 21.0, 21.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-39",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "p blink",
|
||||
"patching_rect" : [ 74.0, 286.0, 45.0, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-37",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2,
|
||||
"patcher" : {
|
||||
"fileversion" : 1,
|
||||
"rect" : [ 54.0, 94.0, 640.0, 480.0 ],
|
||||
"bglocked" : 0,
|
||||
"defrect" : [ 54.0, 94.0, 640.0, 480.0 ],
|
||||
"openrect" : [ 0.0, 0.0, 0.0, 0.0 ],
|
||||
"openinpresentation" : 0,
|
||||
"default_fontsize" : 10.0,
|
||||
"default_fontface" : 0,
|
||||
"default_fontname" : "Verdana",
|
||||
"gridonopen" : 0,
|
||||
"gridsize" : [ 25.0, 25.0 ],
|
||||
"gridsnaponopen" : 0,
|
||||
"toolbarvisible" : 1,
|
||||
"boxanimatetime" : 200,
|
||||
"imprint" : 0,
|
||||
"boxes" : [ {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "* 1000",
|
||||
"patching_rect" : [ 200.0, 150.0, 46.0, 19.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 10.0,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-12",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "inlet",
|
||||
"patching_rect" : [ 200.0, 75.0, 25.0, 25.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-11",
|
||||
"numinlets" : 0,
|
||||
"comment" : ""
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "toggle",
|
||||
"patching_rect" : [ 125.0, 250.0, 20.0, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-10",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "metro 1000",
|
||||
"patching_rect" : [ 115.0, 190.0, 69.0, 19.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 10.0,
|
||||
"outlettype" : [ "bang" ],
|
||||
"id" : "obj-3",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "outlet",
|
||||
"patching_rect" : [ 125.0, 400.0, 25.0, 25.0 ],
|
||||
"numoutlets" : 0,
|
||||
"id" : "obj-2",
|
||||
"numinlets" : 1,
|
||||
"comment" : ""
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "inlet",
|
||||
"patching_rect" : [ 100.0, 25.0, 25.0, 25.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-1",
|
||||
"numinlets" : 0,
|
||||
"comment" : ""
|
||||
}
|
||||
|
||||
}
|
||||
],
|
||||
"lines" : [ {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-12", 0 ],
|
||||
"destination" : [ "obj-3", 1 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-11", 0 ],
|
||||
"destination" : [ "obj-12", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-1", 0 ],
|
||||
"destination" : [ "obj-3", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-10", 0 ],
|
||||
"destination" : [ "obj-2", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-3", 0 ],
|
||||
"destination" : [ "obj-10", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
]
|
||||
}
|
||||
,
|
||||
"saved_object_attributes" : {
|
||||
"fontface" : 0,
|
||||
"fontsize" : 10.0,
|
||||
"default_fontface" : 0,
|
||||
"default_fontname" : "Verdana",
|
||||
"default_fontsize" : 10.0,
|
||||
"fontname" : "Verdana",
|
||||
"globalpatchername" : ""
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "convert to int",
|
||||
"patching_rect" : [ 154.0, 386.0, 104.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-36",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "send L if 0, H if 1",
|
||||
"patching_rect" : [ 154.0, 361.0, 104.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-35",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "is it on or off?",
|
||||
"patching_rect" : [ 179.0, 336.0, 95.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-34",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "atoi",
|
||||
"patching_rect" : [ 279.0, 386.0, 46.0, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "list" ],
|
||||
"id" : "obj-33",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 3
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "H",
|
||||
"patching_rect" : [ 329.0, 361.0, 32.5, 17.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 10.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-32",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "L",
|
||||
"patching_rect" : [ 279.0, 361.0, 32.5, 17.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 10.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-31",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "select 0 1",
|
||||
"patching_rect" : [ 279.0, 336.0, 62.0, 20.0 ],
|
||||
"numoutlets" : 3,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "bang", "bang", "" ],
|
||||
"id" : "obj-25",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click to turn the LED on and off",
|
||||
"linecount" : 2,
|
||||
"patching_rect" : [ 130.0, 205.0, 143.0, 34.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-24",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "toggle",
|
||||
"patching_rect" : [ 279.0, 211.0, 24.0, 24.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-23",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "select 0 1",
|
||||
"patching_rect" : [ 381.0, 331.0, 62.0, 20.0 ],
|
||||
"numoutlets" : 3,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "bang", "bang", "" ],
|
||||
"id" : "obj-30",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "click here to close the serial port",
|
||||
"patching_rect" : [ 429.0, 422.0, 206.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-26",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "click here to open the serial port",
|
||||
"patching_rect" : [ 454.0, 396.0, 206.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-27",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "close",
|
||||
"patching_rect" : [ 381.0, 422.0, 39.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-21",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "port a",
|
||||
"patching_rect" : [ 403.0, 396.0, 41.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-19",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click here to get a list of serial ports",
|
||||
"patching_rect" : [ 474.0, 370.0, 207.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-2",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "toggle",
|
||||
"patching_rect" : [ 381.0, 181.0, 21.0, 21.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-11",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "print",
|
||||
"patching_rect" : [ 423.0, 370.0, 36.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-13",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "serial a 9600",
|
||||
"patching_rect" : [ 279.0, 461.0, 84.0, 20.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int", "" ],
|
||||
"id" : "obj-14",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click to start",
|
||||
"patching_rect" : [ 408.0, 181.0, 117.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-17",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
],
|
||||
"lines" : [ {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-39", 0 ],
|
||||
"destination" : [ "obj-37", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-37", 0 ],
|
||||
"destination" : [ "obj-25", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 83.5, 320.5, 288.5, 320.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-33", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-32", 0 ],
|
||||
"destination" : [ "obj-33", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 338.5, 381.5, 288.5, 381.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-31", 0 ],
|
||||
"destination" : [ "obj-33", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-25", 0 ],
|
||||
"destination" : [ "obj-31", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-25", 1 ],
|
||||
"destination" : [ "obj-32", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 310.0, 358.0, 338.5, 358.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-23", 0 ],
|
||||
"destination" : [ "obj-25", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-13", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 432.5, 389.0, 367.0, 389.0, 367.0, 411.0, 288.5, 411.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-19", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 412.5, 417.0, 288.5, 417.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-21", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 390.5, 450.0, 288.5, 450.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-30", 0 ],
|
||||
"destination" : [ "obj-21", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-30", 1 ],
|
||||
"destination" : [ "obj-19", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-11", 0 ],
|
||||
"destination" : [ "obj-30", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 390.5, 322.0, 390.5, 322.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
*/
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,699 @@
|
|||
/*
|
||||
This example reads three analog sensors (potentiometers are easiest)
|
||||
and sends their values serially. The Processing and Max/MSP programs at the bottom
|
||||
take those three values and use them to change the background color of the screen.
|
||||
|
||||
The circuit:
|
||||
* potentiometers attached to analog inputs 0, 1, and 2
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/VirtualColorMixer
|
||||
|
||||
created 2 Dec 2006
|
||||
by David A. Mellis
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe and Scott Fitzgerald
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
const int redPin = A0; // sensor to control red color
|
||||
const int greenPin = A1; // sensor to control green color
|
||||
const int bluePin = A2; // sensor to control blue color
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.print(analogRead(redPin));
|
||||
Serial.print(",");
|
||||
Serial.print(analogRead(greenPin));
|
||||
Serial.print(",");
|
||||
Serial.println(analogRead(bluePin));
|
||||
}
|
||||
|
||||
/* Processing code for this example
|
||||
|
||||
// This example code is in the public domain.
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
float redValue = 0; // red value
|
||||
float greenValue = 0; // green value
|
||||
float blueValue = 0; // blue value
|
||||
|
||||
Serial myPort;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
|
||||
// List all the available serial ports
|
||||
println(Serial.list());
|
||||
// I know that the first port in the serial list on my mac
|
||||
// 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');
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// set the background color with the color values:
|
||||
background(redValue, greenValue, blueValue);
|
||||
}
|
||||
|
||||
void serialEvent(Serial myPort) {
|
||||
// get the ASCII string:
|
||||
String inString = myPort.readStringUntil('\n');
|
||||
|
||||
if (inString != null) {
|
||||
// trim off any whitespace:
|
||||
inString = trim(inString);
|
||||
// split the string on the commas and convert the
|
||||
// resulting substrings into an integer array:
|
||||
float[] colors = float(split(inString, ","));
|
||||
// if the array has at least three elements, you know
|
||||
// you got the whole thing. Put the numbers in the
|
||||
// color variables:
|
||||
if (colors.length >=3) {
|
||||
// map them to the range 0-255:
|
||||
redValue = map(colors[0], 0, 1023, 0, 255);
|
||||
greenValue = map(colors[1], 0, 1023, 0, 255);
|
||||
blueValue = map(colors[2], 0, 1023, 0, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/* Max/MSP patch for this example
|
||||
{
|
||||
"boxes" : [ {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "/ 4",
|
||||
"patching_rect" : [ 448.0, 502.0, 32.5, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-25",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "/ 4",
|
||||
"patching_rect" : [ 398.0, 502.0, 32.5, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-24",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "/ 4",
|
||||
"patching_rect" : [ 348.0, 502.0, 32.5, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-23",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Virtual color mixer\n\nThis patch takes a string, containing three comma-separated ASCII formatted numbers from 0 to 1023, with a carriage return and linefeed at the end. It converts the string to three integers and uses them to set the background color.\n\n created 2 Dec 2006\n by David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
|
||||
"linecount" : 11,
|
||||
"patching_rect" : [ 524.0, 51.0, 398.0, 158.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-32",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "select 0 1",
|
||||
"patching_rect" : [ 372.0, 125.0, 62.0, 20.0 ],
|
||||
"numoutlets" : 3,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "bang", "bang", "" ],
|
||||
"id" : "obj-30",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "click here to close the serial port",
|
||||
"patching_rect" : [ 457.0, 276.0, 206.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-26",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "click here to open the serial port",
|
||||
"patching_rect" : [ 457.0, 250.0, 206.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-27",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "close",
|
||||
"patching_rect" : [ 372.0, 276.0, 39.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-21",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "port a",
|
||||
"patching_rect" : [ 394.0, 250.0, 41.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-19",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click here to get a list of serial ports",
|
||||
"patching_rect" : [ 457.0, 224.0, 207.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-2",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Convert ASCII to symbol",
|
||||
"patching_rect" : [ 424.0, 423.0, 147.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-4",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Convert integer to ASCII",
|
||||
"patching_rect" : [ 424.0, 400.0, 147.0, 20.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-5",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "fromsymbol",
|
||||
"patching_rect" : [ 347.0, 423.0, 74.0, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-7",
|
||||
"fontname" : "Arial",
|
||||
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "itoa",
|
||||
"patching_rect" : [ 347.0, 400.0, 46.0, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-8",
|
||||
"fontname" : "Arial",
|
||||
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
|
||||
"numinlets" : 3
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "zl group",
|
||||
"patching_rect" : [ 347.0, 377.0, 53.0, 20.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "", "" ],
|
||||
"id" : "obj-9",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "select 10 13",
|
||||
"patching_rect" : [ 289.0, 326.0, 77.0, 20.0 ],
|
||||
"numoutlets" : 3,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "bang", "bang", "" ],
|
||||
"id" : "obj-10",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "toggle",
|
||||
"patching_rect" : [ 289.0, 88.0, 15.0, 15.0 ],
|
||||
"numoutlets" : 1,
|
||||
"outlettype" : [ "int" ],
|
||||
"id" : "obj-11",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "qmetro 10",
|
||||
"patching_rect" : [ 289.0, 125.0, 65.0, 20.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "bang" ],
|
||||
"id" : "obj-12",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "message",
|
||||
"text" : "print",
|
||||
"patching_rect" : [ 414.0, 224.0, 36.0, 18.0 ],
|
||||
"numoutlets" : 1,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "" ],
|
||||
"id" : "obj-13",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 2
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "serial a 9600",
|
||||
"patching_rect" : [ 289.0, 300.0, 84.0, 20.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 12.0,
|
||||
"outlettype" : [ "int", "" ],
|
||||
"id" : "obj-14",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Read serial input buffer every 10 milliseconds",
|
||||
"linecount" : 2,
|
||||
"patching_rect" : [ 98.0, 117.0, 185.0, 34.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-15",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "If you get newline (ASCII 10), send the list. If you get return (ASCII 13) do nothing. Any other value, add to the list",
|
||||
"linecount" : 3,
|
||||
"patching_rect" : [ 377.0, 314.0, 320.0, 48.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-16",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Click to open/close serial port and start/stop patch",
|
||||
"linecount" : 2,
|
||||
"patching_rect" : [ 316.0, 77.0, 199.0, 34.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-17",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "bgcolor 0 0 0",
|
||||
"patching_rect" : [ 348.0, 585.0, 169.0, 19.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 10.0,
|
||||
"id" : "obj-6",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 4
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "newobj",
|
||||
"text" : "unpack 0 0 0 0 0",
|
||||
"patching_rect" : [ 347.0, 470.0, 119.0, 19.0 ],
|
||||
"numoutlets" : 5,
|
||||
"fontsize" : 10.0,
|
||||
"outlettype" : [ "int", "int", "int", "int", "int" ],
|
||||
"id" : "obj-20",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "number",
|
||||
"patching_rect" : [ 448.0, 535.0, 50.0, 19.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 10.0,
|
||||
"outlettype" : [ "int", "bang" ],
|
||||
"id" : "obj-18",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "number",
|
||||
"patching_rect" : [ 398.0, 535.0, 50.0, 19.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 10.0,
|
||||
"outlettype" : [ "int", "bang" ],
|
||||
"id" : "obj-1",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "number",
|
||||
"patching_rect" : [ 348.0, 535.0, 50.0, 19.0 ],
|
||||
"numoutlets" : 2,
|
||||
"fontsize" : 10.0,
|
||||
"outlettype" : [ "int", "bang" ],
|
||||
"id" : "obj-22",
|
||||
"fontname" : "Verdana",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"box" : {
|
||||
"maxclass" : "comment",
|
||||
"text" : "Here's the numbers from Arduino's analog input",
|
||||
"linecount" : 3,
|
||||
"patching_rect" : [ 198.0, 484.0, 138.0, 48.0 ],
|
||||
"numoutlets" : 0,
|
||||
"fontsize" : 12.0,
|
||||
"id" : "obj-3",
|
||||
"fontname" : "Arial",
|
||||
"numinlets" : 1
|
||||
}
|
||||
|
||||
}
|
||||
],
|
||||
"lines" : [ {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-18", 0 ],
|
||||
"destination" : [ "obj-6", 2 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-1", 0 ],
|
||||
"destination" : [ "obj-6", 1 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-22", 0 ],
|
||||
"destination" : [ "obj-6", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-25", 0 ],
|
||||
"destination" : [ "obj-18", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-20", 4 ],
|
||||
"destination" : [ "obj-25", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-20", 2 ],
|
||||
"destination" : [ "obj-24", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-24", 0 ],
|
||||
"destination" : [ "obj-1", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-23", 0 ],
|
||||
"destination" : [ "obj-22", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-20", 0 ],
|
||||
"destination" : [ "obj-23", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-8", 0 ],
|
||||
"destination" : [ "obj-7", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-14", 0 ],
|
||||
"destination" : [ "obj-10", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-12", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-11", 0 ],
|
||||
"destination" : [ "obj-12", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-13", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 423.5, 245.5, 298.5, 245.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-19", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 403.5, 273.5, 298.5, 273.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-21", 0 ],
|
||||
"destination" : [ "obj-14", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 381.5, 296.5, 298.5, 296.5 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-30", 0 ],
|
||||
"destination" : [ "obj-21", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-30", 1 ],
|
||||
"destination" : [ "obj-19", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-11", 0 ],
|
||||
"destination" : [ "obj-30", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 298.0, 116.0, 381.5, 116.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-7", 0 ],
|
||||
"destination" : [ "obj-20", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-9", 0 ],
|
||||
"destination" : [ "obj-8", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-10", 0 ],
|
||||
"destination" : [ "obj-9", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 298.5, 353.0, 356.5, 353.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
, {
|
||||
"patchline" : {
|
||||
"source" : [ "obj-10", 2 ],
|
||||
"destination" : [ "obj-9", 0 ],
|
||||
"hidden" : 0,
|
||||
"midpoints" : [ 356.5, 365.0, 356.5, 365.0 ]
|
||||
}
|
||||
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
*/
|
57
arduino-0022-linux-x64/examples/5.Control/Arrays/Arrays.pde
Normal file
57
arduino-0022-linux-x64/examples/5.Control/Arrays/Arrays.pde
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
Arrays
|
||||
|
||||
Demonstrates the use of an array to hold pin numbers
|
||||
in order to iterate over the pins in a sequence.
|
||||
Lights multiple LEDs in sequence, then in reverse.
|
||||
|
||||
Unlike the For Loop tutorial, where the pins have to be
|
||||
contiguous, here the pins can be in any random order.
|
||||
|
||||
The circuit:
|
||||
* LEDs from pins 2 through 7 to ground
|
||||
|
||||
created 2006
|
||||
by David A. Mellis
|
||||
modified 5 Jul 2009
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Array
|
||||
*/
|
||||
|
||||
int timer = 100; // The higher the number, the slower the timing.
|
||||
int ledPins[] = {
|
||||
2, 7, 4, 6, 5, 3 }; // an array of pin numbers to which LEDs are attached
|
||||
int pinCount = 6; // the number of pins (i.e. the length of the array)
|
||||
|
||||
void setup() {
|
||||
int thisPin;
|
||||
// the array elements are numbered from 0 to (pinCount - 1).
|
||||
// use a for loop to initialize each pin as an output:
|
||||
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
|
||||
pinMode(ledPins[thisPin], OUTPUT);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// loop from the lowest pin to the highest:
|
||||
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
|
||||
// turn the pin on:
|
||||
digitalWrite(ledPins[thisPin], HIGH);
|
||||
delay(timer);
|
||||
// turn the pin off:
|
||||
digitalWrite(ledPins[thisPin], LOW);
|
||||
|
||||
}
|
||||
|
||||
// loop from the highest pin to the lowest:
|
||||
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
|
||||
// turn the pin on:
|
||||
digitalWrite(ledPins[thisPin], HIGH);
|
||||
delay(timer);
|
||||
// turn the pin off:
|
||||
digitalWrite(ledPins[thisPin], LOW);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
For Loop Iteration
|
||||
|
||||
Demonstrates the use of a for() loop.
|
||||
Lights multiple LEDs in sequence, then in reverse.
|
||||
|
||||
The circuit:
|
||||
* LEDs from pins 2 through 7 to ground
|
||||
|
||||
created 2006
|
||||
by David A. Mellis
|
||||
modified 5 Jul 2009
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/ForLoop
|
||||
*/
|
||||
|
||||
int timer = 100; // The higher the number, the slower the timing.
|
||||
|
||||
void setup() {
|
||||
// use a for loop to initialize each pin as an output:
|
||||
for (int thisPin = 2; thisPin < 8; thisPin++) {
|
||||
pinMode(thisPin, OUTPUT);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// loop from the lowest pin to the highest:
|
||||
for (int thisPin = 2; thisPin < 8; thisPin++) {
|
||||
// turn the pin on:
|
||||
digitalWrite(thisPin, HIGH);
|
||||
delay(timer);
|
||||
// turn the pin off:
|
||||
digitalWrite(thisPin, LOW);
|
||||
}
|
||||
|
||||
// loop from the highest pin to the lowest:
|
||||
for (int thisPin = 7; thisPin >= 2; thisPin--) {
|
||||
// turn the pin on:
|
||||
digitalWrite(thisPin, HIGH);
|
||||
delay(timer);
|
||||
// turn the pin off:
|
||||
digitalWrite(thisPin, LOW);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
Conditionals - If statement
|
||||
|
||||
This example demonstrates the use of if() statements.
|
||||
It reads the state of a potentiometer (an analog input) and turns on an LED
|
||||
only if the LED goes above a certain threshold level. It prints the analog value
|
||||
regardless of the level.
|
||||
|
||||
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 13 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 17 Jan 2009
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://arduino.cc/en/Tutorial/IfStatement
|
||||
|
||||
*/
|
||||
|
||||
// These constants won't change:
|
||||
const int analogPin = A0; // pin that the sensor is attached to
|
||||
const int ledPin = 13; // pin that the LED is attached to
|
||||
const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
|
||||
|
||||
void setup() {
|
||||
// initialize the LED pin as an output:
|
||||
pinMode(ledPin, OUTPUT);
|
||||
// initialize serial communications:
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// read the value of the potentiometer:
|
||||
int analogValue = analogRead(analogPin);
|
||||
|
||||
// if the analog value is high enough, turn on the LED:
|
||||
if (analogValue > threshold) {
|
||||
digitalWrite(ledPin, HIGH);
|
||||
}
|
||||
else {
|
||||
digitalWrite(ledPin,LOW);
|
||||
}
|
||||
|
||||
// print the analog value:
|
||||
Serial.println(analogValue, DEC);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
Conditionals - while statement
|
||||
|
||||
This example demonstrates the use of while() statements.
|
||||
|
||||
While the pushbutton is pressed, the sketch runs the calibration routine.
|
||||
The sensor readings during the while loop define the minimum and maximum
|
||||
of expected values from the photo resistor.
|
||||
|
||||
This is a variation on the calibrate example.
|
||||
|
||||
The circuit:
|
||||
* photo resistor connected from +5V to analog in pin 0
|
||||
* 10K resistor connected from ground to analog in pin 0
|
||||
* LED connected from digital pin 9 to ground through 220 ohm resistor
|
||||
* pushbutton attached from pin 2 to +5V
|
||||
* 10K resistor attached from pin 2 to ground
|
||||
|
||||
created 17 Jan 2009
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://arduino.cc/en/Tutorial/WhileLoop
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// These constants won't change:
|
||||
const int sensorPin = A2; // pin that the sensor is attached to
|
||||
const int ledPin = 9; // pin that the LED is attached to
|
||||
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
|
||||
const int buttonPin = 2; // pin that the button is attached to
|
||||
|
||||
|
||||
// These variables will change:
|
||||
int sensorMin = 1023; // minimum sensor value
|
||||
int sensorMax = 0; // maximum sensor value
|
||||
int sensorValue = 0; // the sensor value
|
||||
|
||||
|
||||
void setup() {
|
||||
// set the LED pins as outputs and the switch pin as input:
|
||||
pinMode(indicatorLedPin, OUTPUT);
|
||||
pinMode (ledPin, OUTPUT);
|
||||
pinMode (buttonPin, INPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// while the button is pressed, take calibration readings:
|
||||
while (digitalRead(buttonPin) == HIGH) {
|
||||
calibrate();
|
||||
}
|
||||
// signal the end of the calibration period
|
||||
digitalWrite(indicatorLedPin, LOW);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
void calibrate() {
|
||||
// turn on the indicator LED to indicate that calibration is happening:
|
||||
digitalWrite(indicatorLedPin, HIGH);
|
||||
// read the sensor:
|
||||
sensorValue = analogRead(sensorPin);
|
||||
|
||||
// record the maximum sensor value
|
||||
if (sensorValue > sensorMax) {
|
||||
sensorMax = sensorValue;
|
||||
}
|
||||
|
||||
// record the minimum sensor value
|
||||
if (sensorValue < sensorMin) {
|
||||
sensorMin = sensorValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
Switch statement
|
||||
|
||||
Demonstrates the use of a switch statement. The switch
|
||||
statement allows you to choose from among a set of discrete values
|
||||
of a variable. It's like a series of if statements.
|
||||
|
||||
To see this sketch in action, but the board and sensor in a well-lit
|
||||
room, open the serial monitor, and and move your hand gradually
|
||||
down over the sensor.
|
||||
|
||||
The circuit:
|
||||
* photoresistor from analog in 0 to +5V
|
||||
* 10K resistor from analog in 0 to ground
|
||||
|
||||
created 1 Jul 2009
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/SwitchCase
|
||||
*/
|
||||
|
||||
// these constants won't change:
|
||||
const int sensorMin = 0; // sensor minimum, discovered through experiment
|
||||
const int sensorMax = 600; // sensor maximum, discovered through experiment
|
||||
|
||||
void setup() {
|
||||
// initialize serial communication:
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// read the sensor:
|
||||
int sensorReading = analogRead(A0);
|
||||
// map the sensor range to a range of four options:
|
||||
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
|
||||
|
||||
// do something different depending on the
|
||||
// range value:
|
||||
switch (range) {
|
||||
case 0: // your hand is on the sensor
|
||||
Serial.println("dark");
|
||||
break;
|
||||
case 1: // your hand is close to the sensor
|
||||
Serial.println("dim");
|
||||
break;
|
||||
case 2: // your hand is a few inches from the sensor
|
||||
Serial.println("medium");
|
||||
break;
|
||||
case 3: // your hand is nowhere near the sensor
|
||||
Serial.println("bright");
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
Switch statement with serial input
|
||||
|
||||
Demonstrates the use of a switch statement. The switch
|
||||
statement allows you to choose from among a set of discrete values
|
||||
of a variable. It's like a series of if statements.
|
||||
|
||||
To see this sketch in action, open the Serial monitor and send any character.
|
||||
The characters a, b, c, d, and e, will turn on LEDs. Any other character will turn
|
||||
the LEDs off.
|
||||
|
||||
The circuit:
|
||||
* 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
|
||||
|
||||
created 1 Jul 2009
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/SwitchCase2
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
// initialize serial communication:
|
||||
Serial.begin(9600);
|
||||
// initialize the LED pins:
|
||||
for (int thisPin = 2; thisPin < 7; thisPin++) {
|
||||
pinMode(thisPin, OUTPUT);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// read the sensor:
|
||||
if (Serial.available() > 0) {
|
||||
int inByte = Serial.read();
|
||||
// do something different depending on the character received.
|
||||
// The switch statement expects single number values for each case;
|
||||
// in this exmaple, though, you're using single quotes to tell
|
||||
// the controller to get the ASCII value for the character. For
|
||||
// example 'a' = 97, 'b' = 98, and so forth:
|
||||
|
||||
switch (inByte) {
|
||||
case 'a':
|
||||
digitalWrite(2, HIGH);
|
||||
break;
|
||||
case 'b':
|
||||
digitalWrite(3, HIGH);
|
||||
break;
|
||||
case 'c':
|
||||
digitalWrite(4, HIGH);
|
||||
break;
|
||||
case 'd':
|
||||
digitalWrite(5, HIGH);
|
||||
break;
|
||||
case 'e':
|
||||
digitalWrite(6, HIGH);
|
||||
break;
|
||||
default:
|
||||
// turn all the LEDs off:
|
||||
for (int thisPin = 2; thisPin < 7; thisPin++) {
|
||||
digitalWrite(thisPin, LOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
|
||||
/*
|
||||
ADXL3xx
|
||||
|
||||
Reads an Analog Devices ADXL3xx accelerometer and communicates the
|
||||
acceleration to the computer. The pins used are designed to be easily
|
||||
compatible with the breakout boards from Sparkfun, available from:
|
||||
http://www.sparkfun.com/commerce/categories.php?c=80
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/ADXL3xx
|
||||
|
||||
The circuit:
|
||||
analog 0: accelerometer self test
|
||||
analog 1: z-axis
|
||||
analog 2: y-axis
|
||||
analog 3: x-axis
|
||||
analog 4: ground
|
||||
analog 5: vcc
|
||||
|
||||
created 2 Jul 2008
|
||||
by David A. Mellis
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
// these constants describe the pins. They won't change:
|
||||
const int groundpin = 18; // analog input pin 4 -- ground
|
||||
const int powerpin = 19; // analog input pin 5 -- voltage
|
||||
const int xpin = A3; // x-axis of the accelerometer
|
||||
const int ypin = A2; // y-axis
|
||||
const int zpin = A1; // z-axis (only on 3-axis models)
|
||||
|
||||
void setup()
|
||||
{
|
||||
// initialize the serial communications:
|
||||
Serial.begin(9600);
|
||||
|
||||
// Provide ground and power by using the analog inputs as normal
|
||||
// digital pins. This makes it possible to directly connect the
|
||||
// breakout board to the Arduino. If you use the normal 5V and
|
||||
// GND pins on the Arduino, you can remove these lines.
|
||||
pinMode(groundpin, OUTPUT);
|
||||
pinMode(powerpin, OUTPUT);
|
||||
digitalWrite(groundpin, LOW);
|
||||
digitalWrite(powerpin, HIGH);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// print the sensor values:
|
||||
Serial.print(analogRead(xpin));
|
||||
// print a tab between values:
|
||||
Serial.print("\t");
|
||||
Serial.print(analogRead(ypin));
|
||||
// print a tab between values:
|
||||
Serial.print("\t");
|
||||
Serial.print(analogRead(zpin));
|
||||
Serial.println();
|
||||
// delay before next reading:
|
||||
delay(100);
|
||||
}
|
55
arduino-0022-linux-x64/examples/6.Sensors/Knock/Knock.pde
Normal file
55
arduino-0022-linux-x64/examples/6.Sensors/Knock/Knock.pde
Normal file
|
@ -0,0 +1,55 @@
|
|||
/* Knock Sensor
|
||||
|
||||
This sketch reads a piezo element to detect a knocking sound.
|
||||
It reads an analog pin and compares the result to a set threshold.
|
||||
If the result is greater than the threshold, it writes
|
||||
"knock" to the serial port, and toggles the LED on pin 13.
|
||||
|
||||
The circuit:
|
||||
* + connection of the piezo attached to analog in 0
|
||||
* - connection of the piezo attached to ground
|
||||
* 1-megohm resistor attached from analog in 0 to ground
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Knock
|
||||
|
||||
created 25 Mar 2007
|
||||
by David Cuartielles <http://www.0j0.org>
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// these constants won't change:
|
||||
const int ledPin = 13; // led connected to digital pin 13
|
||||
const int knockSensor = A0; // the piezo is connected to analog pin 0
|
||||
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
|
||||
|
||||
|
||||
// these variables will change:
|
||||
int sensorReading = 0; // variable to store the value read from the sensor pin
|
||||
int ledState = LOW; // variable used to store the last LED status, to toggle the light
|
||||
|
||||
void setup() {
|
||||
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
|
||||
Serial.begin(9600); // use the serial port
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// read the sensor and store it in the variable sensorReading:
|
||||
sensorReading = analogRead(knockSensor);
|
||||
|
||||
// if the sensor reading is greater than the threshold:
|
||||
if (sensorReading >= threshold) {
|
||||
// toggle the status of the ledPin:
|
||||
ledState = !ledState;
|
||||
// update the LED pin itself:
|
||||
digitalWrite(ledPin, ledState);
|
||||
// send the string "Knock!" back to the computer, followed by newline
|
||||
Serial.println("Knock!");
|
||||
}
|
||||
delay(100); // delay to avoid overloading the serial port buffer
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
Memsic2125
|
||||
|
||||
Read the Memsic 2125 two-axis accelerometer. Converts the
|
||||
pulses output by the 2125 into milli-g's (1/1000 of earth's
|
||||
gravity) and prints them over the serial connection to the
|
||||
computer.
|
||||
|
||||
The circuit:
|
||||
* X output of accelerometer to digital pin 2
|
||||
* Y output of accelerometer to digital pin 3
|
||||
* +V of accelerometer to +5V
|
||||
* GND of accelerometer to ground
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Memsic2125
|
||||
|
||||
created 6 Nov 2008
|
||||
by David A. Mellis
|
||||
modified 30 Jun 2009
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
// these constants won't change:
|
||||
const int xPin = 2; // X output of the accelerometer
|
||||
const int yPin = 3; // Y output of the accelerometer
|
||||
|
||||
void setup() {
|
||||
// initialize serial communications:
|
||||
Serial.begin(9600);
|
||||
// initialize the pins connected to the accelerometer
|
||||
// as inputs:
|
||||
pinMode(xPin, INPUT);
|
||||
pinMode(yPin, INPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// variables to read the pulse widths:
|
||||
int pulseX, pulseY;
|
||||
// variables to contain the resulting accelerations
|
||||
int accelerationX, accelerationY;
|
||||
|
||||
// read pulse from x- and y-axes:
|
||||
pulseX = pulseIn(xPin,HIGH);
|
||||
pulseY = pulseIn(yPin,HIGH);
|
||||
|
||||
// convert the pulse width into acceleration
|
||||
// accelerationX and accelerationY are in milli-g's:
|
||||
// earth's gravity is 1000 milli-g's, or 1g.
|
||||
accelerationX = ((pulseX / 10) - 500) * 8;
|
||||
accelerationY = ((pulseY / 10) - 500) * 8;
|
||||
|
||||
// print the acceleration
|
||||
Serial.print(accelerationX);
|
||||
// print a tab character:
|
||||
Serial.print("\t");
|
||||
Serial.print(accelerationY);
|
||||
Serial.println();
|
||||
|
||||
delay(100);
|
||||
}
|
84
arduino-0022-linux-x64/examples/6.Sensors/Ping/Ping.pde
Normal file
84
arduino-0022-linux-x64/examples/6.Sensors/Ping/Ping.pde
Normal file
|
@ -0,0 +1,84 @@
|
|||
/* Ping))) Sensor
|
||||
|
||||
This sketch reads a PING))) ultrasonic rangefinder and returns the
|
||||
distance to the closest object in range. To do this, it sends a pulse
|
||||
to the sensor to initiate a reading, then listens for a pulse
|
||||
to return. The length of the returning pulse is proportional to
|
||||
the distance of the object from the sensor.
|
||||
|
||||
The circuit:
|
||||
* +V connection of the PING))) attached to +5V
|
||||
* GND connection of the PING))) attached to ground
|
||||
* SIG connection of the PING))) attached to digital pin 7
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Ping
|
||||
|
||||
created 3 Nov 2008
|
||||
by David A. Mellis
|
||||
modified 30 Jun 2009
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
*/
|
||||
|
||||
// this constant won't change. It's the pin number
|
||||
// of the sensor's output:
|
||||
const int pingPin = 7;
|
||||
|
||||
void setup() {
|
||||
// initialize serial communication:
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// establish variables for duration of the ping,
|
||||
// and the distance result in inches and centimeters:
|
||||
long duration, inches, cm;
|
||||
|
||||
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
|
||||
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
|
||||
pinMode(pingPin, OUTPUT);
|
||||
digitalWrite(pingPin, LOW);
|
||||
delayMicroseconds(2);
|
||||
digitalWrite(pingPin, HIGH);
|
||||
delayMicroseconds(5);
|
||||
digitalWrite(pingPin, LOW);
|
||||
|
||||
// The same pin is used to read the signal from the PING))): a HIGH
|
||||
// pulse whose duration is the time (in microseconds) from the sending
|
||||
// of the ping to the reception of its echo off of an object.
|
||||
pinMode(pingPin, INPUT);
|
||||
duration = pulseIn(pingPin, HIGH);
|
||||
|
||||
// convert the time into a distance
|
||||
inches = microsecondsToInches(duration);
|
||||
cm = microsecondsToCentimeters(duration);
|
||||
|
||||
Serial.print(inches);
|
||||
Serial.print("in, ");
|
||||
Serial.print(cm);
|
||||
Serial.print("cm");
|
||||
Serial.println();
|
||||
|
||||
delay(100);
|
||||
}
|
||||
|
||||
long microsecondsToInches(long microseconds)
|
||||
{
|
||||
// According to Parallax's datasheet for the PING))), there are
|
||||
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
|
||||
// second). This gives the distance travelled by the ping, outbound
|
||||
// and return, so we divide by 2 to get the distance of the obstacle.
|
||||
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
|
||||
return microseconds / 74 / 2;
|
||||
}
|
||||
|
||||
long microsecondsToCentimeters(long microseconds)
|
||||
{
|
||||
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
|
||||
// The ping travels out and back, so to find the distance of the
|
||||
// object we take half of the distance travelled.
|
||||
return microseconds / 29 / 2;
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
Row-Column Scanning an 8x8 LED matrix with X-Y input
|
||||
|
||||
This example controls an 8x8 LED matrix using two analog inputs
|
||||
|
||||
created 27 May 2009
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example works for the Lumex LDM-24488NI Matrix. See
|
||||
http://sigma.octopart.com/140413/datasheet/Lumex-LDM-24488NI.pdf
|
||||
for the pin connections
|
||||
|
||||
For other LED cathode column matrixes, you should only need to change
|
||||
the pin numbers in the row[] and column[] arrays
|
||||
|
||||
rows are the anodes
|
||||
cols are the cathodes
|
||||
---------
|
||||
|
||||
Pin numbers:
|
||||
Matrix:
|
||||
* Digital pins 2 through 13,
|
||||
* analog pins 2 through 5 used as digital 16 through 19
|
||||
Potentiometers:
|
||||
* center pins are attached to analog pins 0 and 1, respectively
|
||||
* side pins attached to +5V and ground, respectively.
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/RowColumnScanning
|
||||
|
||||
see also http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more
|
||||
*/
|
||||
|
||||
|
||||
// 2-dimensional array of row pin numbers:
|
||||
const int row[8] = {
|
||||
2,7,19,5,13,18,12,16 };
|
||||
|
||||
// 2-dimensional array of column pin numbers:
|
||||
const int col[8] = {
|
||||
6,11,10,3,17,4,8,9 };
|
||||
|
||||
// 2-dimensional array of pixels:
|
||||
int pixels[8][8];
|
||||
|
||||
// cursor position:
|
||||
int x = 5;
|
||||
int y = 5;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
// initialize the I/O pins as outputs:
|
||||
|
||||
// iterate over the pins:
|
||||
for (int thisPin = 0; thisPin < 8; thisPin++) {
|
||||
// initialize the output pins:
|
||||
pinMode(col[thisPin], OUTPUT);
|
||||
pinMode(row[thisPin], OUTPUT);
|
||||
// take the col pins (i.e. the cathodes) high to ensure that
|
||||
// the LEDS are off:
|
||||
digitalWrite(col[thisPin], HIGH);
|
||||
}
|
||||
|
||||
// initialize the pixel matrix:
|
||||
for (int x = 0; x < 8; x++) {
|
||||
for (int y = 0; y < 8; y++) {
|
||||
pixels[x][y] = HIGH;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// read input:
|
||||
readSensors();
|
||||
|
||||
// draw the screen:
|
||||
refreshScreen();
|
||||
}
|
||||
|
||||
void readSensors() {
|
||||
// turn off the last position:
|
||||
pixels[x][y] = HIGH;
|
||||
// read the sensors for X and Y values:
|
||||
x = 7 - map(analogRead(A0), 0, 1023, 0, 7);
|
||||
y = map(analogRead(A1), 0, 1023, 0, 7);
|
||||
// set the new pixel position low so that the LED will turn on
|
||||
// in the next screen refresh:
|
||||
pixels[x][y] = LOW;
|
||||
|
||||
}
|
||||
|
||||
void refreshScreen() {
|
||||
// iterate over the rows (anodes):
|
||||
for (int thisRow = 0; thisRow < 8; thisRow++) {
|
||||
// take the row pin (anode) high:
|
||||
digitalWrite(row[thisRow], HIGH);
|
||||
// iterate over the cols (cathodes):
|
||||
for (int thisCol = 0; thisCol < 8; thisCol++) {
|
||||
// get the state of the current pixel;
|
||||
int thisPixel = pixels[thisRow][thisCol];
|
||||
// when the row is HIGH and the col is LOW,
|
||||
// the LED where they meet turns on:
|
||||
digitalWrite(col[thisCol], thisPixel);
|
||||
// turn the pixel off:
|
||||
if (thisPixel == LOW) {
|
||||
digitalWrite(col[thisCol], HIGH);
|
||||
}
|
||||
}
|
||||
// take the row pin low to turn off the whole row:
|
||||
digitalWrite(row[thisRow], LOW);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
LED bar graph
|
||||
|
||||
Turns on a series of LEDs based on the value of an analog sensor.
|
||||
This is a simple way to make a bar graph display. Though this graph
|
||||
uses 10 LEDs, you can use any number by changing the LED count
|
||||
and the pins in the array.
|
||||
|
||||
This method can be used to control any series of digital outputs that
|
||||
depends on an analog input.
|
||||
|
||||
The circuit:
|
||||
* LEDs from pins 2 through 11 to ground
|
||||
|
||||
created 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/BarGraph
|
||||
*/
|
||||
|
||||
|
||||
// these constants won't change:
|
||||
const int analogPin = A0; // the pin that the potentiometer is attached to
|
||||
const int ledCount = 10; // the number of LEDs in the bar graph
|
||||
|
||||
int ledPins[] = {
|
||||
2, 3, 4, 5, 6, 7,8,9,10,11 }; // an array of pin numbers to which LEDs are attached
|
||||
|
||||
|
||||
void setup() {
|
||||
// loop over the pin array and set them all to output:
|
||||
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
|
||||
pinMode(ledPins[thisLed], OUTPUT);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// read the potentiometer:
|
||||
int sensorReading = analogRead(analogPin);
|
||||
// map the result to a range from 0 to the number of LEDs:
|
||||
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
|
||||
|
||||
// loop over the LED array:
|
||||
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
|
||||
// if the array element's index is less than ledLevel,
|
||||
// turn the pin for this element on:
|
||||
if (thisLed < ledLevel) {
|
||||
digitalWrite(ledPins[thisLed], HIGH);
|
||||
}
|
||||
// turn off all pins higher than the ledLevel:
|
||||
else {
|
||||
digitalWrite(ledPins[thisLed], LOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
Character analysis operators
|
||||
|
||||
Examples using the character analysis operators.
|
||||
Send any byte and the sketch will tell you about it.
|
||||
|
||||
created 29 Nov 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
// Open serial communications:
|
||||
Serial.begin(9600);
|
||||
|
||||
// send an intro:
|
||||
Serial.println("send any byte and I'll tell you everything I can about it");
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// get any incoming bytes:
|
||||
if (Serial.available() > 0) {
|
||||
int thisChar = Serial.read();
|
||||
|
||||
// say what was sent:
|
||||
Serial.print("You sent me: \'");
|
||||
Serial.write(thisChar);
|
||||
Serial.print("\' ASCII Value: ");
|
||||
Serial.println(thisChar);
|
||||
|
||||
// analyze what was sent:
|
||||
if(isAlphaNumeric(thisChar)) {
|
||||
Serial.println("it's alphanumeric");
|
||||
}
|
||||
if(isAlpha(thisChar)) {
|
||||
Serial.println("it's alphabetic");
|
||||
}
|
||||
if(isAscii(thisChar)) {
|
||||
Serial.println("it's ASCII");
|
||||
}
|
||||
if(isWhitespace(thisChar)) {
|
||||
Serial.println("it's whitespace");
|
||||
}
|
||||
if(isControl(thisChar)) {
|
||||
Serial.println("it's a control character");
|
||||
}
|
||||
if(isDigit(thisChar)) {
|
||||
Serial.println("it's a numeric digit");
|
||||
}
|
||||
if(isGraph(thisChar)) {
|
||||
Serial.println("it's a printable character that's not whitespace");
|
||||
}
|
||||
if(isLowerCase(thisChar)) {
|
||||
Serial.println("it's lower case");
|
||||
}
|
||||
if(isPrintable(thisChar)) {
|
||||
Serial.println("it's printable");
|
||||
}
|
||||
if(isPunct(thisChar)) {
|
||||
Serial.println("it's punctuation");
|
||||
}
|
||||
if(isSpace(thisChar)) {
|
||||
Serial.println("it's a space character");
|
||||
}
|
||||
if(isUpperCase(thisChar)) {
|
||||
Serial.println("it's upper case");
|
||||
}
|
||||
if (isHexadecimalDigit(thisChar)) {
|
||||
Serial.println("it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)");
|
||||
}
|
||||
|
||||
// add some space and ask for another byte:
|
||||
Serial.println();
|
||||
Serial.println("Give me another byte:");
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
Adding Strings together
|
||||
|
||||
Examples of how to add strings together
|
||||
You can also add several different data types to string, as shown here:
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringAdditionOperator
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
// declare three strings:
|
||||
String stringOne, stringTwo, stringThree;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
stringOne = String("stringThree = ");
|
||||
stringTwo = String("this string");
|
||||
stringThree = String ();
|
||||
Serial.println("\n\nAdding strings together (concatenation):");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// adding a constant integer to a string:
|
||||
stringThree = stringOne + 123;
|
||||
Serial.println(stringThree); // prints "stringThree = 123"
|
||||
|
||||
// adding a constant long interger to a string:
|
||||
stringThree = stringOne + 123456789;
|
||||
Serial.println(stringThree); // prints " You added 123456789"
|
||||
|
||||
// adding a constant character to a string:
|
||||
stringThree = stringOne + 'A';
|
||||
Serial.println(stringThree); // prints "You added A"
|
||||
|
||||
// adding a constant string to a string:
|
||||
stringThree = stringOne + "abc";
|
||||
Serial.println(stringThree); // prints "You added abc"
|
||||
|
||||
stringThree = stringOne + stringTwo;
|
||||
Serial.println(stringThree); // prints "You added this string"
|
||||
|
||||
// adding a variable integer to a string:
|
||||
int sensorValue = analogRead(A0);
|
||||
stringOne = "Sensor value: ";
|
||||
stringThree = stringOne + sensorValue;
|
||||
Serial.println(stringThree); // prints "Sensor Value: 401" or whatever value analogRead(A0) has
|
||||
|
||||
// adding a variable long integer to a string:
|
||||
long currentTime = millis();
|
||||
stringOne="millis() value: ";
|
||||
stringThree = stringOne + millis();
|
||||
Serial.println(stringThree); // prints "The millis: 345345" or whatever value currentTime has
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
Appending to Strings using the += operator and concat()
|
||||
|
||||
Examples of how to append different data types to strings
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringAppendOperator
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
String stringOne, stringTwo;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
stringOne = String("Sensor ");
|
||||
stringTwo = String("value");
|
||||
Serial.println("\n\nAppending to a string:");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println(stringOne); // prints "Sensor "
|
||||
|
||||
// adding a string to a string:
|
||||
stringOne += stringTwo;
|
||||
Serial.println(stringOne); // prints "Sensor value"
|
||||
|
||||
// adding a constant string to a string:
|
||||
stringOne += " for input ";
|
||||
Serial.println(stringOne); // prints "Sensor value for input"
|
||||
|
||||
// adding a constant character to a string:
|
||||
stringOne += 'A';
|
||||
Serial.println(stringOne); // prints "Sensor value for input A"
|
||||
|
||||
// adding a constant integer to a string:
|
||||
stringOne += 0;
|
||||
Serial.println(stringOne); // prints "Sensor value for input A0"
|
||||
|
||||
// adding a constant string to a string:
|
||||
stringOne += ": ";
|
||||
Serial.println(stringOne); // prints "Sensor value for input"
|
||||
|
||||
// adding a variable integer to a string:
|
||||
stringOne += analogRead(A0);
|
||||
Serial.println(stringOne); // prints "Sensor value for input A0: 456" or whatever analogRead(A0) is
|
||||
|
||||
Serial.println("\n\nchanging the Strings' values");
|
||||
stringOne = "A long integer: ";
|
||||
stringTwo = "The millis(): ";
|
||||
|
||||
// adding a constant long integer to a string:
|
||||
stringOne += 123456789;
|
||||
Serial.println(stringOne); // prints "A long integer: 123456789"
|
||||
|
||||
// using concat() to add a long variable to a string:
|
||||
stringTwo.concat(millis());
|
||||
Serial.println(stringTwo); // prints "The millis(): 43534" or whatever the value of the millis() is
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
String Case changes
|
||||
|
||||
Examples of how to change the case of a string
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringCaseChanges
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString case changes:");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// toUpperCase() changes all letters to upper case:
|
||||
String stringOne = "<html><head><body>";
|
||||
Serial.println(stringOne);
|
||||
stringOne = (stringOne.toUpperCase());
|
||||
Serial.println(stringOne);
|
||||
|
||||
// toLowerCase() changes all letters to lower case:
|
||||
String stringTwo = "</BODY></HTML>";
|
||||
Serial.println(stringTwo);
|
||||
stringTwo = stringTwo.toLowerCase();
|
||||
Serial.println(stringTwo);
|
||||
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
String charAt() and setCharAt()
|
||||
|
||||
Examples of how to get and set characters of a String
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringCharacters
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString charAt() and setCharAt():");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// make a string to report a sensor reading:
|
||||
String reportString = "SensorReading: 456";
|
||||
Serial.println(reportString);
|
||||
|
||||
// the reading's most significant digit is at position 15 in the reportString:
|
||||
String mostSignificantDigit = reportString.charAt(15);
|
||||
Serial.println("Most significant digit of the sensor reading is: " + mostSignificantDigit);
|
||||
|
||||
// add blank space:
|
||||
Serial.println();
|
||||
|
||||
// you can alo set the character of a string. Change the : to a = character
|
||||
reportString.setCharAt(13, '=');
|
||||
Serial.println(reportString);
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
/*
|
||||
Comparing Strings
|
||||
|
||||
Examples of how to compare strings using the comparison operators
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringComparisonOperators
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
String stringOne, stringTwo;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
stringOne = String("this");
|
||||
stringTwo = String("that");
|
||||
Serial.println("\n\nComparing Strings:");
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// two strings equal:
|
||||
if (stringOne == "this") {
|
||||
Serial.println("StringOne == \"this\"");
|
||||
}
|
||||
// two strings not equal:
|
||||
if (stringOne != stringTwo) {
|
||||
Serial.println(stringOne + " =! " + stringTwo);
|
||||
}
|
||||
|
||||
// two strings not equal (case sensitivity matters):
|
||||
stringOne = "This";
|
||||
stringTwo = "this";
|
||||
if (stringOne != stringTwo) {
|
||||
Serial.println(stringOne + " =! " + stringTwo);
|
||||
}
|
||||
// you can also use equals() to see if two strings are the same:
|
||||
if (stringOne.equals(stringTwo)) {
|
||||
Serial.println(stringOne + " equals " + stringTwo);
|
||||
}
|
||||
else {
|
||||
Serial.println(stringOne + " does not equal " + stringTwo);
|
||||
}
|
||||
|
||||
// or perhaps you want to ignore case:
|
||||
if (stringOne.equalsIgnoreCase(stringTwo)) {
|
||||
Serial.println(stringOne + " equals (ignoring case) " + stringTwo);
|
||||
}
|
||||
else {
|
||||
Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo);
|
||||
}
|
||||
|
||||
// a numeric string compared to the number it represents:
|
||||
stringOne = "1";
|
||||
int numberOne = 1;
|
||||
if (stringOne == numberOne) {
|
||||
Serial.println(stringOne + " = " + numberOne);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// two numeric strings compared:
|
||||
stringOne = "2";
|
||||
stringTwo = "1";
|
||||
if (stringOne >= stringTwo) {
|
||||
Serial.println(stringOne + " >= " + stringTwo);
|
||||
}
|
||||
|
||||
// comparison operators can be used to compare strings for alphabetic sorting too:
|
||||
stringOne = String("Brown");
|
||||
if (stringOne < "Charles") {
|
||||
Serial.println(stringOne + " < Charles");
|
||||
}
|
||||
|
||||
if (stringOne > "Adams") {
|
||||
Serial.println(stringOne + " > Adams");
|
||||
}
|
||||
|
||||
if (stringOne <= "Browne") {
|
||||
Serial.println(stringOne + " <= Browne");
|
||||
}
|
||||
|
||||
|
||||
if (stringOne >= "Brow") {
|
||||
Serial.println(stringOne + " >= Brow");
|
||||
}
|
||||
|
||||
// the compareTo() operator also allows you to compare strings
|
||||
// it evaluates on the first character that's different.
|
||||
// if the first character of the string you're comparing to
|
||||
// comes first in alphanumeric order, then compareTo() is greater than 0:
|
||||
stringOne = "Cucumber";
|
||||
stringTwo = "Cucuracha";
|
||||
if (stringOne.compareTo(stringTwo) < 0 ) {
|
||||
Serial.println(stringOne + " comes before " + stringTwo);
|
||||
}
|
||||
else {
|
||||
Serial.println(stringOne + " comes after " + stringTwo);
|
||||
}
|
||||
|
||||
delay(10000); // because the next part is a loop:
|
||||
|
||||
// compareTo() is handy when you've got strings with numbers in them too:
|
||||
|
||||
while (true) {
|
||||
stringOne = "Sensor: ";
|
||||
stringTwo= "Sensor: ";
|
||||
|
||||
stringOne += analogRead(A0);
|
||||
stringTwo += analogRead(A5);
|
||||
|
||||
if (stringOne.compareTo(stringTwo) < 0 ) {
|
||||
Serial.println(stringOne + " comes before " + stringTwo);
|
||||
}
|
||||
else {
|
||||
Serial.println(stringOne + " comes after " + stringTwo);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
String constructors
|
||||
|
||||
Examples of how to create strings from other data types
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringConstructors
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// using a constant String:
|
||||
String stringOne = "Hello String";
|
||||
Serial.println(stringOne); // prints "Hello String"
|
||||
|
||||
// converting a constant char into a String:
|
||||
stringOne = String('a');
|
||||
Serial.println(stringOne); // prints "a"
|
||||
|
||||
// converting a constant string into a String object:
|
||||
String stringTwo = String("This is a string");
|
||||
Serial.println(stringTwo); // prints "This is a string"
|
||||
|
||||
// concatenating two strings:
|
||||
stringOne = String(stringTwo + " with more");
|
||||
// prints "This is a string with more":
|
||||
Serial.println(stringOne);
|
||||
|
||||
// using a constant integer:
|
||||
stringOne = String(13);
|
||||
Serial.println(stringOne); // prints "13"
|
||||
|
||||
// using an int and a base:
|
||||
stringOne = String(analogRead(A0), DEC);
|
||||
// prints "453" or whatever the value of analogRead(A0) is
|
||||
Serial.println(stringOne);
|
||||
|
||||
// using an int and a base (hexadecimal):
|
||||
stringOne = String(45, HEX);
|
||||
// prints "2d", which is the hexadecimal version of decimal 45:
|
||||
Serial.println(stringOne);
|
||||
|
||||
// using an int and a base (binary)
|
||||
stringOne = String(255, BIN);
|
||||
// prints "11111111" which is the binary value of 255
|
||||
Serial.println(stringOne);
|
||||
|
||||
// using a long and a base:
|
||||
stringOne = String(millis(), DEC);
|
||||
// prints "123456" or whatever the value of millis() is:
|
||||
Serial.println(stringOne);
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
String indexOf() and lastIndexOf() functions
|
||||
|
||||
Examples of how to evaluate, look for, and replace characters in a String
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringIndexOf
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString indexOf() and lastIndexOf() functions:");
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// indexOf() returns the position (i.e. index) of a particular character
|
||||
// in a string. For example, if you were parsing HTML tags, you could use it:
|
||||
String stringOne = "<HTML><HEAD><BODY>";
|
||||
int firstClosingBracket = stringOne.indexOf('>');
|
||||
Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket);
|
||||
|
||||
stringOne = "<HTML><HEAD><BODY>";
|
||||
int secondOpeningBracket = firstClosingBracket + 1;
|
||||
int secondClosingBracket = stringOne.indexOf('>', secondOpeningBracket );
|
||||
Serial.println("The index of the second > in the string " + stringOne + " is " + secondClosingBracket);
|
||||
|
||||
// you can also use indexOf() to search for Strings:
|
||||
stringOne = "<HTML><HEAD><BODY>";
|
||||
int bodyTag = stringOne.indexOf("<BODY>");
|
||||
Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag);
|
||||
|
||||
stringOne = "<UL><LI>item<LI>item<LI>item</UL>";
|
||||
int firstListItem = stringOne.indexOf("<LI>");
|
||||
int secondListItem = stringOne.indexOf("item", firstListItem + 1 );
|
||||
Serial.println("The index of the second list item in the string " + stringOne + " is " + secondClosingBracket);
|
||||
|
||||
// lastIndexOf() gives you the last occurrence of a character or string:
|
||||
int lastOpeningBracket = stringOne.lastIndexOf('<');
|
||||
Serial.println("The index of the last < in the string " + stringOne + " is " + lastOpeningBracket);
|
||||
|
||||
int lastListItem = stringOne.lastIndexOf("<LI>");
|
||||
Serial.println("The index of the last list item in the string " + stringOne + " is " + lastListItem);
|
||||
|
||||
|
||||
// lastIndexOf() can also search for a string:
|
||||
stringOne = "<p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p>";
|
||||
int lastParagraph = stringOne.lastIndexOf("<p");
|
||||
int secondLastGraf = stringOne.lastIndexOf("<p", lastParagraph - 1);
|
||||
Serial.println("The index of the second last paragraph tag " + stringOne + " is " + secondLastGraf);
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
String length()
|
||||
|
||||
Examples of how to use length() in a String.
|
||||
Open the Serial Monitor and start sending characters to see the results.
|
||||
|
||||
created 1 Aug 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringLengthTrim
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
String txtMsg = ""; // a string for incoming text
|
||||
int lastStringLength = txtMsg.length(); // previous length of the String
|
||||
|
||||
void setup() {
|
||||
// open the serial port:
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// add any incoming characters to the String:
|
||||
while (Serial.available() > 0) {
|
||||
char inChar = Serial.read();
|
||||
txtMsg += inChar;
|
||||
}
|
||||
|
||||
// print the message and a notice if it's changed:
|
||||
if (txtMsg.length() != lastStringLength) {
|
||||
Serial.println(txtMsg);
|
||||
Serial.println(txtMsg.length());
|
||||
// if the String's longer than 140 characters, complain:
|
||||
if (txtMsg.length() < 140) {
|
||||
Serial.println("That's a perfectly acceptable text message");
|
||||
}
|
||||
else {
|
||||
Serial.println("That's too long for a text message.");
|
||||
}
|
||||
// note the length for next time through the loop:
|
||||
lastStringLength = txtMsg.length();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
String length() and trim()
|
||||
|
||||
Examples of how to use length() and trim() in a String
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringLengthTrim
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString length() and trim():");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// here's a String with empty spaces at the end (called white space):
|
||||
String stringOne = "Hello! ";
|
||||
Serial.print(stringOne);
|
||||
Serial.print("<--- end of string. Length: ");
|
||||
Serial.println(stringOne.length());
|
||||
|
||||
// trim the white space off the string:
|
||||
stringOne = stringOne.trim();
|
||||
Serial.print(stringOne);
|
||||
Serial.print("<--- end of trimmed string. Length: ");
|
||||
Serial.println(stringOne.length());
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
String replace()
|
||||
|
||||
Examples of how to replace characters or substrings of a string
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringReplace
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString replace:");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
String stringOne = "<html><head><body>";
|
||||
Serial.println(stringOne);
|
||||
// replace() changes all instances of one substring with another:
|
||||
String stringTwo = stringOne.replace("<", "</");
|
||||
Serial.println(stringTwo);
|
||||
|
||||
// you can also use replace() on single characters:
|
||||
String normalString = "bookkeeper";
|
||||
Serial.println("normal: " + normalString);
|
||||
String leetString = normalString.replace('o', '0');
|
||||
leetString = leetString.replace('e', '3');
|
||||
Serial.println("l33tspeak: " + leetString);
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
String startWith() and endsWith()
|
||||
|
||||
Examples of how to use startsWith() and endsWith() in a String
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringStartsWithEndsWith
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString startsWith() and endsWith():");
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// startsWith() checks to see if a String starts with a particular substring:
|
||||
String stringOne = "HTTP/1.1 200 OK";
|
||||
Serial.println(stringOne);
|
||||
if (stringOne.startsWith("HTTP/1.1")) {
|
||||
Serial.println("Server's using http version 1.1");
|
||||
}
|
||||
|
||||
// you can also look for startsWith() at an offset position in the string:
|
||||
stringOne = "HTTP/1.1 200 OK";
|
||||
if (stringOne.startsWith("200 OK", 9)) {
|
||||
Serial.println("Got an OK from the server");
|
||||
}
|
||||
|
||||
// endsWith() checks to see if a String ends with a particular character:
|
||||
String sensorReading = "sensor = ";
|
||||
sensorReading += analogRead(A0);
|
||||
Serial.print (sensorReading);
|
||||
if (sensorReading.endsWith(0)) {
|
||||
Serial.println(". This reading is divisible by ten");
|
||||
}
|
||||
else {
|
||||
Serial.println(". This reading is not divisible by ten");
|
||||
|
||||
}
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
String substring()
|
||||
|
||||
Examples of how to use substring in a String
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringSubstring
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString substring():");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Set up a String:
|
||||
String stringOne = "Content-Type: text/html";
|
||||
Serial.println(stringOne);
|
||||
|
||||
// substring(index) looks for the substring from the index position to the end:
|
||||
if (stringOne.substring(19) == "html") {
|
||||
Serial.println("It's an html file");
|
||||
}
|
||||
// you can also look for a substring in the middle of a string:
|
||||
if (stringOne.substring(14,18) == "text") {
|
||||
Serial.println("It's a text-based file");
|
||||
}
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
String to Integer conversion
|
||||
|
||||
Reads a serial input string until it sees a newline, then converts
|
||||
the string to a number if the characters are digits.
|
||||
|
||||
The circuit:
|
||||
No external components needed.
|
||||
|
||||
created 29 Nov 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
String inString = ""; // string to hold input
|
||||
|
||||
void setup() {
|
||||
// Initialize serial communications:
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Read serial input:
|
||||
while (Serial.available() > 0) {
|
||||
int inChar = Serial.read();
|
||||
if (isDigit(inChar)) {
|
||||
// convert the incoming byte to a char
|
||||
// and add it to the string:
|
||||
inString += (char)inChar;
|
||||
}
|
||||
// if you get a newline, print the string,
|
||||
// then the string's value:
|
||||
if (inChar == '\n') {
|
||||
Serial.print("Value:");
|
||||
Serial.println(inString.toInt());
|
||||
Serial.print("String: ");
|
||||
Serial.println(inString);
|
||||
// clear the string for new input:
|
||||
inString = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,230 @@
|
|||
/*
|
||||
Serial RGB controller
|
||||
|
||||
Reads a serial input string looking for three comma-separated
|
||||
integers with a newline at the end. Values should be between
|
||||
0 and 255. The sketch uses those values to set the color
|
||||
of an RGB LED attached to pins 9 - 11.
|
||||
|
||||
The circuit:
|
||||
* Common-anode RGB LED cathodes attached to pins 9 - 11
|
||||
* LED anode connected to pin 13
|
||||
|
||||
To turn on any given channel, set the pin LOW.
|
||||
To turn off, set the pin HIGH. The higher the analogWrite level,
|
||||
the lower the brightness.
|
||||
|
||||
created 29 Nov 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
String inString = ""; // string to hold input
|
||||
int currentColor = 0;
|
||||
int red, green, blue = 0;
|
||||
|
||||
void setup() {
|
||||
// Initialize serial communications:
|
||||
Serial.begin(9600);
|
||||
// set LED cathode pins as outputs:
|
||||
pinMode(9, OUTPUT);
|
||||
pinMode(10, OUTPUT);
|
||||
pinMode(11, OUTPUT);
|
||||
// turn on pin 13 to power the LEDs:
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, HIGH);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int inChar;
|
||||
|
||||
// Read serial input:
|
||||
if (Serial.available() > 0) {
|
||||
inChar = Serial.read();
|
||||
}
|
||||
|
||||
if (isDigit(inChar)) {
|
||||
// convert the incoming byte to a char
|
||||
// and add it to the string:
|
||||
inString += (char)inChar;
|
||||
}
|
||||
|
||||
// if you get a comma, convert to a number,
|
||||
// set the appropriate color, and increment
|
||||
// the color counter:
|
||||
if (inChar == ',') {
|
||||
// do something different for each value of currentColor:
|
||||
switch (currentColor) {
|
||||
case 0: // 0 = red
|
||||
red = inString.toInt();
|
||||
// clear the string for new input:
|
||||
inString = "";
|
||||
break;
|
||||
case 1: // 1 = green:
|
||||
green = inString.toInt();
|
||||
// clear the string for new input:
|
||||
inString = "";
|
||||
break;
|
||||
}
|
||||
currentColor++;
|
||||
}
|
||||
// if you get a newline, you know you've got
|
||||
// the last color, i.e. blue:
|
||||
if (inChar == '\n') {
|
||||
blue = inString.toInt();
|
||||
|
||||
// set the levels of the LED.
|
||||
// subtract value from 255 because a higher
|
||||
// analogWrite level means a dimmer LED, since
|
||||
// you're raising the level on the anode:
|
||||
analogWrite(11, 255 - red);
|
||||
analogWrite(9, 255 - green);
|
||||
analogWrite(10, 255 - blue);
|
||||
|
||||
// print the colors:
|
||||
Serial.print("Red: ");
|
||||
Serial.print(red);
|
||||
Serial.print(", Green: ");
|
||||
Serial.print(green);
|
||||
Serial.print(", Blue: ");
|
||||
Serial.println(blue);
|
||||
|
||||
// clear the string for new input:
|
||||
inString = "";
|
||||
// reset the color counter:
|
||||
currentColor = 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Here's a Processing sketch that will draw a color wheel and send a serial
|
||||
string with the color you click on:
|
||||
|
||||
// Subtractive Color Wheel with Serial
|
||||
// Based on a Processing example by Ira Greenberg.
|
||||
// Serial output added by Tom Igoe
|
||||
//
|
||||
// The primaries are red, yellow, and blue. The secondaries are green,
|
||||
// purple, and orange. The tertiaries are yellow-orange, red-orange,
|
||||
// red-purple, blue-purple, blue-green, and yellow-green.
|
||||
//
|
||||
// Create a shade or tint of the subtractive color wheel using
|
||||
// SHADE or TINT parameters.
|
||||
|
||||
// Updated 29 November 2010.
|
||||
|
||||
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
int segs = 12;
|
||||
int steps = 6;
|
||||
float rotAdjust = TWO_PI / segs / 2;
|
||||
float radius;
|
||||
float segWidth;
|
||||
float interval = TWO_PI / segs;
|
||||
|
||||
Serial myPort;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(127);
|
||||
smooth();
|
||||
ellipseMode(RADIUS);
|
||||
noStroke();
|
||||
// make the diameter 90% of the sketch area
|
||||
radius = min(width, height) * 0.45;
|
||||
segWidth = radius / steps;
|
||||
|
||||
// swap which line is commented out to draw the other version
|
||||
// drawTintWheel();
|
||||
drawShadeWheel();
|
||||
// open the first serial port in your computer's list
|
||||
myPort = new Serial(this, Serial.list()[0], 9600);
|
||||
}
|
||||
|
||||
|
||||
void drawShadeWheel() {
|
||||
for (int j = 0; j < steps; j++) {
|
||||
color[] cols = {
|
||||
color(255-(255/steps)*j, 255-(255/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/1.5)-((255/1.5)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2)-((255/2)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, 0, 0),
|
||||
color(255-(255/steps)*j, 0, (255/2)-((255/2)/steps)*j),
|
||||
color(255-(255/steps)*j, 0, 255-(255/steps)*j),
|
||||
color((255/2)-((255/2)/steps)*j, 0, 255-(255/steps)*j),
|
||||
color(0, 0, 255-(255/steps)*j),
|
||||
color(0, 255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j),
|
||||
color(0, 255-(255/steps)*j, 0),
|
||||
color((255/2)-((255/2)/steps)*j, 255-(255/steps)*j, 0)
|
||||
};
|
||||
for (int i = 0; i < segs; i++) {
|
||||
fill(cols[i]);
|
||||
arc(width/2, height/2, radius, radius,
|
||||
interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void drawTintWheel() {
|
||||
for (int j = 0; j < steps; j++) {
|
||||
color[] cols = {
|
||||
color((255/steps)*j, (255/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/1.5)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2.5)/steps)*j, 0),
|
||||
color((255/steps)*j, 0, 0),
|
||||
color((255/steps)*j, 0, ((255/2)/steps)*j),
|
||||
color((255/steps)*j, 0, (255/steps)*j),
|
||||
color(((255/2)/steps)*j, 0, (255/steps)*j),
|
||||
color(0, 0, (255/steps)*j),
|
||||
color(0, (255/steps)*j, ((255/2.5)/steps)*j),
|
||||
color(0, (255/steps)*j, 0),
|
||||
color(((255/2)/steps)*j, (255/steps)*j, 0)
|
||||
};
|
||||
for (int i = 0; i < segs; i++) {
|
||||
fill(cols[i]);
|
||||
arc(width/2, height/2, radius, radius,
|
||||
interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// nothing happens here
|
||||
}
|
||||
|
||||
void mouseReleased() {
|
||||
// get the color of the mouse position's pixel:
|
||||
color targetColor = get(mouseX, mouseY);
|
||||
// get the component values:
|
||||
int r = int(red(targetColor));
|
||||
int g = int(green(targetColor));
|
||||
int b = int(blue(targetColor));
|
||||
// make a comma-separated string:
|
||||
String colorString = r + "," + g + "," + b + "\n";
|
||||
// send it out the serial port:
|
||||
myPort.write(colorString );
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
478
arduino-0022-linux-x64/examples/ArduinoISP/ArduinoISP.pde
Normal file
478
arduino-0022-linux-x64/examples/ArduinoISP/ArduinoISP.pde
Normal file
|
@ -0,0 +1,478 @@
|
|||
// this sketch turns the Arduino into a AVRISP
|
||||
// using the following pins:
|
||||
// 10: slave reset
|
||||
// 11: MOSI
|
||||
// 12: MISO
|
||||
// 13: SCK
|
||||
|
||||
// Put an LED (with resistor) on the following pins:
|
||||
// 9: Heartbeat - shows the programmer is running
|
||||
// 8: Error - Lights up if something goes wrong (use red if that makes sense)
|
||||
// 7: Programming - In communication with the slave
|
||||
//
|
||||
// October 2009 by David A. Mellis
|
||||
// - Added support for the read signature command
|
||||
//
|
||||
// February 2009 by Randall Bohn
|
||||
// - Added support for writing to EEPROM (what took so long?)
|
||||
// Windows users should consider WinAVR's avrdude instead of the
|
||||
// avrdude included with Arduino software.
|
||||
//
|
||||
// January 2008 by Randall Bohn
|
||||
// - Thanks to Amplificar for helping me with the STK500 protocol
|
||||
// - The AVRISP/STK500 (mk I) protocol is used in the arduino bootloader
|
||||
// - The SPI functions herein were developed for the AVR910_ARD programmer
|
||||
// - More information at http://code.google.com/p/mega-isp
|
||||
|
||||
#include "pins_arduino.h" // defines SS,MOSI,MISO,SCK
|
||||
#define RESET SS
|
||||
|
||||
#define LED_HB 9
|
||||
#define LED_ERR 8
|
||||
#define LED_PMODE 7
|
||||
|
||||
#define HWVER 2
|
||||
#define SWMAJ 1
|
||||
#define SWMIN 18
|
||||
|
||||
// STK Definitions
|
||||
#define STK_OK 0x10
|
||||
#define STK_FAILED 0x11
|
||||
#define STK_UNKNOWN 0x12
|
||||
#define STK_INSYNC 0x14
|
||||
#define STK_NOSYNC 0x15
|
||||
#define CRC_EOP 0x20 //ok it is a space...
|
||||
|
||||
void pulse(int pin, int times);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(19200);
|
||||
pinMode(7, OUTPUT);
|
||||
pulse(7, 2);
|
||||
pinMode(8, OUTPUT);
|
||||
pulse(8, 2);
|
||||
pinMode(9, OUTPUT);
|
||||
pulse(9, 2);
|
||||
}
|
||||
|
||||
int error=0;
|
||||
int pmode=0;
|
||||
// address for reading and writing, set by 'U' command
|
||||
int here;
|
||||
uint8_t buff[256]; // global block storage
|
||||
|
||||
#define beget16(addr) (*addr * 256 + *(addr+1) )
|
||||
typedef struct param {
|
||||
uint8_t devicecode;
|
||||
uint8_t revision;
|
||||
uint8_t progtype;
|
||||
uint8_t parmode;
|
||||
uint8_t polling;
|
||||
uint8_t selftimed;
|
||||
uint8_t lockbytes;
|
||||
uint8_t fusebytes;
|
||||
int flashpoll;
|
||||
int eeprompoll;
|
||||
int pagesize;
|
||||
int eepromsize;
|
||||
int flashsize;
|
||||
}
|
||||
parameter;
|
||||
|
||||
parameter param;
|
||||
|
||||
// this provides a heartbeat on pin 9, so you can tell the software is running.
|
||||
uint8_t hbval=128;
|
||||
int8_t hbdelta=8;
|
||||
void heartbeat() {
|
||||
if (hbval > 192) hbdelta = -hbdelta;
|
||||
if (hbval < 32) hbdelta = -hbdelta;
|
||||
hbval += hbdelta;
|
||||
analogWrite(LED_HB, hbval);
|
||||
delay(40);
|
||||
}
|
||||
|
||||
|
||||
void loop(void) {
|
||||
// is pmode active?
|
||||
if (pmode) digitalWrite(LED_PMODE, HIGH);
|
||||
else digitalWrite(LED_PMODE, LOW);
|
||||
// is there an error?
|
||||
if (error) digitalWrite(LED_ERR, HIGH);
|
||||
else digitalWrite(LED_ERR, LOW);
|
||||
|
||||
// light the heartbeat LED
|
||||
heartbeat();
|
||||
if (Serial.available()) {
|
||||
avrisp();
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t getch() {
|
||||
while(!Serial.available());
|
||||
return Serial.read();
|
||||
}
|
||||
void readbytes(int n) {
|
||||
for (int x = 0; x < n; x++) {
|
||||
buff[x] = Serial.read();
|
||||
}
|
||||
}
|
||||
|
||||
#define PTIME 30
|
||||
void pulse(int pin, int times) {
|
||||
do {
|
||||
digitalWrite(pin, HIGH);
|
||||
delay(PTIME);
|
||||
digitalWrite(pin, LOW);
|
||||
delay(PTIME);
|
||||
}
|
||||
while (times--);
|
||||
}
|
||||
|
||||
void spi_init() {
|
||||
uint8_t x;
|
||||
SPCR = 0x53;
|
||||
x=SPSR;
|
||||
x=SPDR;
|
||||
}
|
||||
|
||||
void spi_wait() {
|
||||
do {
|
||||
}
|
||||
while (!(SPSR & (1 << SPIF)));
|
||||
}
|
||||
|
||||
uint8_t spi_send(uint8_t b) {
|
||||
uint8_t reply;
|
||||
SPDR=b;
|
||||
spi_wait();
|
||||
reply = SPDR;
|
||||
return reply;
|
||||
}
|
||||
|
||||
uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
|
||||
uint8_t n;
|
||||
spi_send(a);
|
||||
n=spi_send(b);
|
||||
//if (n != a) error = -1;
|
||||
n=spi_send(c);
|
||||
return spi_send(d);
|
||||
}
|
||||
|
||||
void empty_reply() {
|
||||
if (CRC_EOP == getch()) {
|
||||
Serial.print((char)STK_INSYNC);
|
||||
Serial.print((char)STK_OK);
|
||||
}
|
||||
else {
|
||||
Serial.print((char)STK_NOSYNC);
|
||||
}
|
||||
}
|
||||
|
||||
void breply(uint8_t b) {
|
||||
if (CRC_EOP == getch()) {
|
||||
Serial.print((char)STK_INSYNC);
|
||||
Serial.print((char)b);
|
||||
Serial.print((char)STK_OK);
|
||||
}
|
||||
else {
|
||||
Serial.print((char)STK_NOSYNC);
|
||||
}
|
||||
}
|
||||
|
||||
void get_version(uint8_t c) {
|
||||
switch(c) {
|
||||
case 0x80:
|
||||
breply(HWVER);
|
||||
break;
|
||||
case 0x81:
|
||||
breply(SWMAJ);
|
||||
break;
|
||||
case 0x82:
|
||||
breply(SWMIN);
|
||||
break;
|
||||
case 0x93:
|
||||
breply('S'); // serial programmer
|
||||
break;
|
||||
default:
|
||||
breply(0);
|
||||
}
|
||||
}
|
||||
|
||||
void set_parameters() {
|
||||
// call this after reading paramter packet into buff[]
|
||||
param.devicecode = buff[0];
|
||||
param.revision = buff[1];
|
||||
param.progtype = buff[2];
|
||||
param.parmode = buff[3];
|
||||
param.polling = buff[4];
|
||||
param.selftimed = buff[5];
|
||||
param.lockbytes = buff[6];
|
||||
param.fusebytes = buff[7];
|
||||
param.flashpoll = buff[8];
|
||||
// ignore buff[9] (= buff[8])
|
||||
//getch(); // discard second value
|
||||
|
||||
// WARNING: not sure about the byte order of the following
|
||||
// following are 16 bits (big endian)
|
||||
param.eeprompoll = beget16(&buff[10]);
|
||||
param.pagesize = beget16(&buff[12]);
|
||||
param.eepromsize = beget16(&buff[14]);
|
||||
|
||||
// 32 bits flashsize (big endian)
|
||||
param.flashsize = buff[16] * 0x01000000
|
||||
+ buff[17] * 0x00010000
|
||||
+ buff[18] * 0x00000100
|
||||
+ buff[19];
|
||||
|
||||
}
|
||||
|
||||
void start_pmode() {
|
||||
spi_init();
|
||||
// following delays may not work on all targets...
|
||||
pinMode(RESET, OUTPUT);
|
||||
digitalWrite(RESET, HIGH);
|
||||
pinMode(SCK, OUTPUT);
|
||||
digitalWrite(SCK, LOW);
|
||||
delay(50);
|
||||
digitalWrite(RESET, LOW);
|
||||
delay(50);
|
||||
pinMode(MISO, INPUT);
|
||||
pinMode(MOSI, OUTPUT);
|
||||
spi_transaction(0xAC, 0x53, 0x00, 0x00);
|
||||
pmode = 1;
|
||||
}
|
||||
|
||||
void end_pmode() {
|
||||
pinMode(MISO, INPUT);
|
||||
pinMode(MOSI, INPUT);
|
||||
pinMode(SCK, INPUT);
|
||||
pinMode(RESET, INPUT);
|
||||
pmode = 0;
|
||||
}
|
||||
|
||||
void universal() {
|
||||
int w;
|
||||
uint8_t ch;
|
||||
|
||||
for (w = 0; w < 4; w++) {
|
||||
buff[w] = getch();
|
||||
}
|
||||
ch = spi_transaction(buff[0], buff[1], buff[2], buff[3]);
|
||||
breply(ch);
|
||||
}
|
||||
|
||||
void flash(uint8_t hilo, int addr, uint8_t data) {
|
||||
spi_transaction(0x40+8*hilo,
|
||||
addr>>8 & 0xFF,
|
||||
addr & 0xFF,
|
||||
data);
|
||||
}
|
||||
void commit(int addr) {
|
||||
spi_transaction(0x4C, (addr >> 8) & 0xFF, addr & 0xFF, 0);
|
||||
}
|
||||
|
||||
//#define _current_page(x) (here & 0xFFFFE0)
|
||||
int current_page(int addr) {
|
||||
if (param.pagesize == 32) return here & 0xFFFFFFF0;
|
||||
if (param.pagesize == 64) return here & 0xFFFFFFE0;
|
||||
if (param.pagesize == 128) return here & 0xFFFFFFC0;
|
||||
if (param.pagesize == 256) return here & 0xFFFFFF80;
|
||||
return here;
|
||||
}
|
||||
uint8_t write_flash(int length) {
|
||||
if (param.pagesize < 1) return STK_FAILED;
|
||||
//if (param.pagesize != 64) return STK_FAILED;
|
||||
int page = current_page(here);
|
||||
int x = 0;
|
||||
while (x < length) {
|
||||
if (page != current_page(here)) {
|
||||
commit(page);
|
||||
page = current_page(here);
|
||||
}
|
||||
flash(LOW, here, buff[x++]);
|
||||
flash(HIGH, here, buff[x++]);
|
||||
here++;
|
||||
}
|
||||
|
||||
commit(page);
|
||||
|
||||
return STK_OK;
|
||||
}
|
||||
|
||||
uint8_t write_eeprom(int length) {
|
||||
// here is a word address, so we use here*2
|
||||
// this writes byte-by-byte,
|
||||
// page writing may be faster (4 bytes at a time)
|
||||
for (int x = 0; x < length; x++) {
|
||||
spi_transaction(0xC0, 0x00, here*2+x, buff[x]);
|
||||
delay(45);
|
||||
}
|
||||
return STK_OK;
|
||||
}
|
||||
|
||||
void program_page() {
|
||||
char result = (char) STK_FAILED;
|
||||
int length = 256 * getch() + getch();
|
||||
if (length > 256) {
|
||||
Serial.print((char) STK_FAILED);
|
||||
return;
|
||||
}
|
||||
char memtype = getch();
|
||||
for (int x = 0; x < length; x++) {
|
||||
buff[x] = getch();
|
||||
}
|
||||
if (CRC_EOP == getch()) {
|
||||
Serial.print((char) STK_INSYNC);
|
||||
if (memtype == 'F') result = (char)write_flash(length);
|
||||
if (memtype == 'E') result = (char)write_eeprom(length);
|
||||
Serial.print(result);
|
||||
}
|
||||
else {
|
||||
Serial.print((char) STK_NOSYNC);
|
||||
}
|
||||
}
|
||||
uint8_t flash_read(uint8_t hilo, int addr) {
|
||||
return spi_transaction(0x20 + hilo * 8,
|
||||
(addr >> 8) & 0xFF,
|
||||
addr & 0xFF,
|
||||
0);
|
||||
}
|
||||
|
||||
char flash_read_page(int length) {
|
||||
for (int x = 0; x < length; x+=2) {
|
||||
uint8_t low = flash_read(LOW, here);
|
||||
Serial.print((char) low);
|
||||
uint8_t high = flash_read(HIGH, here);
|
||||
Serial.print((char) high);
|
||||
here++;
|
||||
}
|
||||
return STK_OK;
|
||||
}
|
||||
|
||||
char eeprom_read_page(int length) {
|
||||
// here again we have a word address
|
||||
for (int x = 0; x < length; x++) {
|
||||
uint8_t ee = spi_transaction(0xA0, 0x00, here*2+x, 0xFF);
|
||||
Serial.print((char) ee);
|
||||
}
|
||||
return STK_OK;
|
||||
}
|
||||
|
||||
void read_page() {
|
||||
char result = (char)STK_FAILED;
|
||||
int length = 256 * getch() + getch();
|
||||
char memtype = getch();
|
||||
if (CRC_EOP != getch()) {
|
||||
Serial.print((char) STK_NOSYNC);
|
||||
return;
|
||||
}
|
||||
Serial.print((char) STK_INSYNC);
|
||||
if (memtype == 'F') result = flash_read_page(length);
|
||||
if (memtype == 'E') result = eeprom_read_page(length);
|
||||
Serial.print(result);
|
||||
return;
|
||||
}
|
||||
|
||||
void read_signature() {
|
||||
if (CRC_EOP != getch()) {
|
||||
Serial.print((char) STK_NOSYNC);
|
||||
return;
|
||||
}
|
||||
Serial.print((char) STK_INSYNC);
|
||||
uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00);
|
||||
Serial.print((char) high);
|
||||
uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00);
|
||||
Serial.print((char) middle);
|
||||
uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00);
|
||||
Serial.print((char) low);
|
||||
Serial.print((char) STK_OK);
|
||||
}
|
||||
//////////////////////////////////////////
|
||||
//////////////////////////////////////////
|
||||
|
||||
|
||||
////////////////////////////////////
|
||||
////////////////////////////////////
|
||||
int avrisp() {
|
||||
uint8_t data, low, high;
|
||||
uint8_t ch = getch();
|
||||
switch (ch) {
|
||||
case '0': // signon
|
||||
empty_reply();
|
||||
break;
|
||||
case '1':
|
||||
if (getch() == CRC_EOP) {
|
||||
Serial.print((char) STK_INSYNC);
|
||||
Serial.print("AVR ISP");
|
||||
Serial.print((char) STK_OK);
|
||||
}
|
||||
break;
|
||||
case 'A':
|
||||
get_version(getch());
|
||||
break;
|
||||
case 'B':
|
||||
readbytes(20);
|
||||
set_parameters();
|
||||
empty_reply();
|
||||
break;
|
||||
case 'E': // extended parameters - ignore for now
|
||||
readbytes(5);
|
||||
empty_reply();
|
||||
break;
|
||||
|
||||
case 'P':
|
||||
start_pmode();
|
||||
empty_reply();
|
||||
break;
|
||||
case 'U':
|
||||
here = getch() + 256 * getch();
|
||||
empty_reply();
|
||||
break;
|
||||
|
||||
case 0x60: //STK_PROG_FLASH
|
||||
low = getch();
|
||||
high = getch();
|
||||
empty_reply();
|
||||
break;
|
||||
case 0x61: //STK_PROG_DATA
|
||||
data = getch();
|
||||
empty_reply();
|
||||
break;
|
||||
|
||||
case 0x64: //STK_PROG_PAGE
|
||||
program_page();
|
||||
break;
|
||||
|
||||
case 0x74: //STK_READ_PAGE
|
||||
read_page();
|
||||
break;
|
||||
|
||||
case 'V':
|
||||
universal();
|
||||
break;
|
||||
case 'Q':
|
||||
error=0;
|
||||
end_pmode();
|
||||
empty_reply();
|
||||
break;
|
||||
|
||||
case 0x75: //STK_READ_SIGN
|
||||
read_signature();
|
||||
break;
|
||||
|
||||
// expecting a command, not CRC_EOP
|
||||
// this is how we can get back in sync
|
||||
case CRC_EOP:
|
||||
Serial.print((char) STK_NOSYNC);
|
||||
break;
|
||||
|
||||
// anything else we will return STK_UNKNOWN
|
||||
default:
|
||||
if (CRC_EOP == getch())
|
||||
Serial.print((char)STK_UNKNOWN);
|
||||
else
|
||||
Serial.print((char)STK_NOSYNC);
|
||||
}
|
||||
}
|
||||
|
Reference in a new issue