1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-17 22:23:10 +03:00

Adding Tom's new examples.

This commit is contained in:
David A. Mellis
2009-07-11 00:34:59 +00:00
parent 8d73929f5d
commit 29c339013a
55 changed files with 7166 additions and 0 deletions

View File

@ -0,0 +1,55 @@
/*
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
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);
}
}

View File

@ -0,0 +1,45 @@
/*
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
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 = 0; 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);
}
}

View File

@ -0,0 +1,53 @@
/*
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
by Tom Igoe
http://arduino.cc/en/Tutorial/
*/
// These constants won't change:
const int analogPin = 0; // 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(LED, 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);
}

View File

@ -0,0 +1,86 @@
/*
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 25 Jun 2009
by Tom Igoe
http://arduino.cc/en/Tutorial/WhileLoop
*/
// These constants won't change:
const int sensorPin = 2; // 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;
}
}

View File

@ -0,0 +1,86 @@
/*
Conditionals - while statement
This example demonstrates the use of while() statements.
It reads the state of a potentiometer (an analog input) and blinks an LED
while the LED remains above a certain threshold level. It prints the analog value
only if it's below the threshold.
This example uses principles explained in the BlinkWithoutDelay example as well.
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
by Tom Igoe
*/
#define ledPin 13 // the pin for the LED
#define analogPin 0 // the analog pin that the potentiometer is attached to
#include "WProgram.h"
void setup();
void loop();
int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
int ledState = LOW; // the state of the LED
int lastBlinkTime = 0; // last time the LED changed
int blinkDelay = 500; // how long to hold between changes of the LED
int analogValue; // variable to hold the value 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:
analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
while (analogValue > threshold) {
// if enough time has passed since the last change of the LED,
// then change it. Note you're using the technique from BlinkWithoutDelay
// here so that the while loop doesn't delay the rest of the program:
if (millis() - lastBlinkTime > blinkDelay) {
// if the ledState is high, this makes it low, and vice versa:
ledState = !ledState;
digitalWrite(ledPin, ledState);
// save the last time the LED changed in a variable:
lastBlinkTime = millis();
}
// while you're in the while loop, you have to read the
// input again:
analogValue = analogRead(analogPin);
}
// if you're below the threshold, print the analog value:
Serial.println(analogValue, DEC);
// turn the LED off:
digitalWrite(ledPin, LOW);
}
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}

View File

@ -0,0 +1,155 @@
:100000000C9462000C94DC020C94F9020C948A00AB
:100010000C948A000C948A000C948A000C948A0038
:100020000C948A000C948A000C948A000C948A0028
:100030000C948A000C948A000C948A000C948A0018
:100040000C9416030C948A000C94F6000C948A000D
:100050000C948A000C948A000C948A000C948A00F8
:100060000C948A000C948A000000000024002700F1
:100070002A0000000000250028002B0000000000DE
:1000800023002600290004040404040404040202DA
:100090000202020203030303030301020408102007
:1000A0004080010204081020010204081020000012
:1000B0000007000201000003040600000000000029
:1000C0000000AF0111241FBECFEFD4E0DEBFCDBFD3
:1000D00011E0A0E0B1E0EAE8F9E002C005900D927D
:1000E000AC30B107D9F711E0ACE0B1E001C01D922E
:1000F000A83BB107E1F710E0C4ECD0E004C02297C0
:10010000FE010E94BF04C23CD107C9F70E94EF0064
:100110000C94C3040C94000032C00E94860320910A
:100120000E0130910F01442737FD4095542F621B7B
:10013000730B840B950B209102013091030144272E
:1001400037FD4095542F2617370748075907B8F44D
:1001500060E070E080910C0190910D01892B11F409
:1001600061E070E070930D0160930C018DE00E94DE
:100170000C040E94860370930F0160930E0180E0CF
:100180000E94CE03909311018093100120911001E1
:100190003091110180910001909101018217930724
:1001A0000CF4BBCFA901662757FD6095762F2AE096
:1001B00030E086E991E00E94D10260E08DE00E948B
:1001C0000C04089561E08DE00E94EC0340E855E2E4
:1001D00060E070E086E991E00E94330108950E949A
:1001E00094030E94E2000E948C00FDCF1F920F92A8
:1001F0000FB60F9211242F933F934F935F936F93FA
:100200007F938F939F93AF93BF93EF93FF9340910F
:10021000C600E0919201F0919301CF01019660E850
:1002200070E00E9459049C01809194019091950185
:100230002817390739F0EE5EFE4F40833093930163
:1002400020939201FF91EF91BF91AF919F918F9178
:100250007F916F915F914F913F912F910F900FBEC2
:100260000F901F901895AF92BF92CF92DF92EF92AE
:10027000FF920F931F93CF93DF936C017A018B0151
:10028000DC011496AD90BC901597CB01BA0122E029
:1002900030E040E050E00E948E04205C3D4B404F37
:1002A0005F4FCA01B901A80197010E948E04C901DC
:1002B000DA010197A109B109292F3A2F4B2F5527B0
:1002C00047FD5A950196A11DB11DE5012883E60160
:1002D000EE81FF8181508083EA85FB85208141E0AA
:1002E00050E0CA010E8402C0880F991F0A94E2F7F9
:1002F000282B2083EA85FB852081CA010F8402C058
:10030000880F991F0A94E2F7282B2083EA85FB8542
:100310008081088802C0440F551F0A94E2F7842B9D
:100320008083DF91CF911F910F91FF90EF90DF902D
:10033000CF90BF90AF900895FC01A085B185218931
:100340008C9190E0022E02C0959587950A94E2F771
:1003500080FFF6CF0484F585E02D6083089589E061
:1003600091E0909397018093960182E191E09093C0
:1003700099018093980185EC90E090939B01809384
:100380009A0184EC90E090939D0180939C0180EC15
:1003900090E090939F0180939E0181EC90E0909378
:1003A000A1018093A00186EC90E09093A30180933B
:1003B000A20184E08093A40183E08093A50187E0FB
:1003C0008093A60185E08093A70108950F931F9362
:1003D0008C01DC01ED91FC910190F081E02D6DE04C
:1003E0000995D801ED91FC910190F081E02D6AE032
:1003F000C80109951F910F9108952F923F924F9236
:100400005F926F927F928F929F92AF92BF92CF92A4
:10041000DF92EF92FF920F931F93DF93CF93CDB7AD
:10042000DEB7A0970FB6F894DEBF0FBECDBF1C019C
:100430006A017B01411551056105710549F4DC0133
:10044000ED91FC910190F081E02D60E3099554C09D
:10045000882499245401422E55246624772401E0EF
:1004600010E00C0F1D1F080D191DC701B601A301D7
:1004700092010E946C04F80160830894811C911C15
:10048000A11CB11CC701B601A30192010E946C041A
:10049000C901DA016C017D01C114D104E104F10448
:1004A000F1F681E0E82EF12CEC0EFD1EE80CF91CB3
:1004B0003E010894611C711CD501C4010197A1097A
:1004C000B1096C01C818D90814C0F601EE0DFF1D62
:1004D00060816A3010F4605D01C0695CD101ED910A
:1004E000FC910190F081E02DC10109950894E1088B
:1004F000F1086E147F0449F7A0960FB6F894DEBF9A
:100500000FBECDBFCF91DF911F910F91FF90EF9064
:10051000DF90CF90BF90AF909F908F907F906F9023
:100520005F904F903F902F900895EF92FF920F931E
:100530001F93CF93DF93EC017A018B0177FF0FC0FC
:10054000E881F9810190F081E02D6DE20995109527
:100550000095F094E094E11CF11C011D111D2AE0AE
:10056000B801A701CE010E94FD01DF91CF911F913B
:100570000F91FF90EF900895DC012115310541F4B2
:10058000ED91FC910190F081E02D642F0995089583
:100590002A30310519F40E94950208950E94FD0148
:1005A00008950F931F938C010E94BC02C8010E9402
:1005B000E6011F910F9108951F920F920FB60F92AF
:1005C00011248F939F93EF93FF938091A8019091B3
:1005D000A901892B29F0E091A801F091A9010995C1
:1005E000FF91EF919F918F910F900FBE0F901F90F1
:1005F00018951F920F920FB60F9211248F939F930D
:10060000EF93FF938091AA019091AB01892B29F080
:10061000E091AA01F091AB010995FF91EF919F91B3
:100620008F910F900FBE0F901F9018951F920F92F1
:100630000FB60F9211242F938F939F93AF93BF9375
:100640008091AC019091AD01A091AE01B091AF014C
:100650000196A11DB11D8093AC019093AD01A093B3
:10066000AE01B093AF018091B0019091B101A09122
:10067000B201B091B3018050904CAF4FBF4F809307
:10068000B0019093B101A093B201B093B30127C020
:100690008091B0019091B101A091B201B091B301EC
:1006A00080589E43A040B0408093B0019093B10128
:1006B000A093B201B093B3018091B4019091B501C0
:1006C000A091B601B091B7010196A11DB11D809313
:1006D000B4019093B501A093B601B093B701809196
:1006E000B0019091B101A091B201B091B3018158D4
:1006F0009E43A040B04060F6BF91AF919F918F9113
:100700002F910F900FBE0F901F9018958FB7F894F0
:100710002091B4013091B5014091B6015091B701DB
:100720008FBFB901CA010895789484B5826084BDF1
:1007300084B5816084BD85B5826085BD85B58160E5
:1007400085BDEEE6F0E0808181608083E1E8F0E045
:10075000808182608083808181608083E0E8F0E036
:10076000808181608083E1EBF0E080818460808320
:10077000E0EBF0E0808181608083EAE7F0E0808157
:1007800084608083808182608083808181608083B7
:100790008081806880831092C10008958770909155
:1007A00004019295990F990F907C982B90937C005F
:1007B00080917A00806480937A0080917A0086FD2F
:1007C000FCCF2091780040917900942F80E030E0B8
:1007D000282B392BC9010895282F30E0C9018656EE
:1007E0009F4FFC0194912A573F4FF9018491882330
:1007F00091F0E82FF0E0EE0FFF1FE859FF4FA591B1
:10080000B491662329F48C91909589238C93089553
:100810008C91892B8C930895482F50E0CA01825502
:100820009F4FFC012491CA0186569F4FFC01949171
:100830004A575F4FFA0134913323D1F1222331F12A
:10084000233021F4809180008F7705C0243031F46B
:10085000809180008F7D8093800018C0213019F432
:1008600084B58F7704C0223021F484B58F7D84BD98
:100870000DC0263021F48091B0008F7705C027305D
:1008800029F48091B0008F7D8093B000E32FF0E0D9
:10089000EE0FFF1FEE58FF4FA591B491662329F488
:1008A0008C91909589238C9308958C91892B8C93AE
:1008B000089597FB092E07260AD077FD04D049D06A
:1008C00006D000201AF4709561957F4F0895F6F7D1
:1008D000909581959F4F0895A1E21A2EAA1BBB1BEC
:1008E000FD010DC0AA1FBB1FEE1FFF1FA217B307FC
:1008F000E407F50720F0A21BB30BE40BF50B661F12
:10090000771F881F991F1A9469F7609570958095D5
:1009100090959B01AC01BD01CF01089597FB092E75
:1009200005260ED057FD04D0D7DF0AD0001C38F4BE
:1009300050954095309521953F4F4F4F5F4F08950B
:10094000F6F790958095709561957F4F8F4F9F4FEB
:100950000895AA1BBB1B51E107C0AA1FBB1FA61706
:10096000B70710F0A61BB70B881F991F5A95A9F758
:1009700080959095BC01CD010895EE0FFF1F059065
:0A098000F491E02D0994F894FFCFE4
:0C098A009001F40101000000009C01003D
:00000001FF

View File

@ -0,0 +1,70 @@
/*
Conditionals - while statement
This example demonstrates the use of while() statements.
It reads the state of a potentiometer (an analog input) and blinks an LED
while the LED remains above a certain threshold level. It prints the analog value
only if it's below the threshold.
This example uses principles explained in the BlinkWithoutDelay example as well.
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
by Tom Igoe
*/
#define ledPin 13 // the pin for the LED
#define analogPin 0 // the analog pin that the potentiometer is attached to
int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
int ledState = LOW; // the state of the LED
int lastBlinkTime = 0; // last time the LED changed
int blinkDelay = 500; // how long to hold between changes of the LED
int analogValue; // variable to hold the value 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:
analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
while (analogValue > threshold) {
// if enough time has passed since the last change of the LED,
// then change it. Note you're using the technique from BlinkWithoutDelay
// here so that the while loop doesn't delay the rest of the program:
if (millis() - lastBlinkTime > blinkDelay) {
// if the ledState is high, this makes it low, and vice versa:
ledState = !ledState;
digitalWrite(ledPin, ledState);
// save the last time the LED changed in a variable:
lastBlinkTime = millis();
}
// while you're in the while loop, you have to read the
// input again:
analogValue = analogRead(analogPin);
}
// if you're below the threshold, print the analog value:
Serial.println(analogValue, DEC);
// turn the LED off:
digitalWrite(ledPin, LOW);
}

Binary file not shown.

View File

@ -0,0 +1,45 @@
#include "WProgram.h"
void setup();
void loop();
void setup() {
Serial.begin(9600);
}
void loop() {
int distance = analogRead(0);
int range = map(distance, 0, 600, 0, 3);
switch (range) {
case 0:
Serial.println("dark");
break;
case 1:
Serial.println("dim");
break;
case 2:
Serial.println("medium");
break;
case 3:
Serial.println("bright");
break;
}
}
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}

View File

@ -0,0 +1 @@
:00000001FF

View File

@ -0,0 +1,119 @@
:100000000C9435000C945D000C945D000C945D0024
:100010000C945D000C945D000C945D000C945D00EC
:100020000C945D000C945D000C945D000C945D00DC
:100030000C945D000C945D000C945D000C945D00CC
:100040000C94CD000C945D000C946D010C945D003B
:100050000C945D000C945D000C945D000C945D00AC
:100060000C945D000C945D00260211241FBECFEF9E
:10007000D4E0DEBFCDBF11E0A0E0B1E0E4E2F7E004
:1000800002C005900D92A232B107D9F711E0A2E2A9
:10009000B1E001C01D92A13CB107E1F710E0CAE652
:1000A000D0E004C02297FE010E948C03C836D1071D
:1000B000C9F70E94C6000C9490030C940000AF9204
:1000C000BF92CF92DF92EF92FF920F931F9380E047
:1000D0000E944F0100D000D0AA2797FDA095BA2F0B
:1000E00023E030E040E050E0EDB7FEB721833283FB
:1000F00043835483BC01CD0120E030E040E050E078
:10010000E8E5EE2EE2E0FE2E012D112DAA24BB24FF
:1001100065010E94BE020F900F900F900F9061300A
:10012000710591F0623071051CF4672B41F01CC021
:100130006230710571F063307105B1F40FC08FEA60
:1001400091E060E071E00EC08FEA91E065E071E05F
:1001500009C08FEA91E069E071E004C08FEA91E0A4
:1001600060E171E00E94B3021F910F91FF90EF9048
:10017000DF90CF90BF90AF9008958FEA91E040E874
:1001800055E260E070E00E94AA0108950E94150106
:100190000E94BD000E945F00FDCF1F920F920FB61C
:1001A0000F9211242F933F938F939F93AF93BF93FD
:1001B0008091260190912701A0912801B0912901F9
:1001C00030912A010196A11DB11D232F2D5F2D37DE
:1001D00020F02D570196A11DB11D20932A01809377
:1001E000260190932701A0932801B09329018091C3
:1001F000220190912301A0912401B0912501019643
:10020000A11DB11D8093220190932301A09324018D
:10021000B0932501BF91AF919F918F913F912F9105
:100220000F900FBE0F901F901895789484B5826040
:1002300084BD84B5816084BD85B5826085BD85B58A
:10024000816085BDEEE6F0E0808181608083E1E839
:10025000F0E0808182608083808181608083E0E83B
:10026000F0E0808181608083E1EBF0E08081846058
:100270008083E0EBF0E0808181608083EAE7F0E05A
:1002800080818460808380818260808380818160BE
:1002900080838081806880831092C1000895877078
:1002A000909117019295990F990F907C982B9093AC
:1002B0007C0080917A00806480937A0080917A003B
:1002C00086FDFCCF2091780040917900942F80E04A
:1002D00030E0282B392BC90108951F920F920FB6D9
:1002E0000F9211242F933F934F935F936F937F93BC
:1002F0008F939F93AF93BF93EF93FF934091C6006B
:10030000E091AB01F091AC01CF01019660E870E0A3
:100310000E9426039C018091AD019091AE012817A7
:10032000390739F0E55DFE4F40833093AC012093EF
:10033000AB01FF91EF91BF91AF919F918F917F9111
:100340006F915F914F913F912F910F900FBE0F9042
:100350001F901895AF92BF92CF92DF92EF92FF92CB
:100360000F931F93CF93DF936C017A018B01DC0114
:100370001496AD90BC901597CB01BA0122E030E005
:1003800040E050E00E945B03205C3D4B404F5F4FDC
:10039000CA01B901A80197010E945B03C901DA01F2
:1003A0000197A109B109292F3A2F4B2F552747FD56
:1003B0005A950196A11DB11DE5012883E601EE8144
:1003C000FF8181508083EA85FB85208141E050E0F8
:1003D000CA010E8402C0880F991F0A94E2F7282BE5
:1003E0002083EA85FB852081CA010F8402C0880F23
:1003F000991F0A94E2F7282B2083EA85FB858081E8
:10040000088802C0440F551F0A94E2F7842B8083AA
:10041000DF91CF911F910F91FF90EF90DF90CF90E0
:10042000BF90AF900895FC01A085B18521898C9182
:1004300090E0022E02C0959587950A94E2F780FF1E
:10044000F6CF0484F585E02D608308958CE191E07A
:100450009093B0018093AF018BE291E09093B20151
:100460008093B10185EC90E09093B4018093B30147
:1004700084EC90E09093B6018093B50180EC90E01D
:100480009093B8018093B70181EC90E09093BA010A
:100490008093B90186EC90E09093BC018093BB01FE
:1004A00084E08093BD0183E08093BE0187E0809368
:1004B000BF0185E08093C00108950F931F93CF93F0
:1004C000DF938C01EB0109C02196D801ED91FC91DD
:1004D0000190F081E02DC801099568816623A1F79C
:1004E000DF91CF911F910F910895EF92FF920F939B
:1004F0001F93CF93DF938C017B01EA010CC0D701DE
:100500006D917D01D801ED91FC910190F081E02D7C
:10051000C80109952197209791F7DF91CF911F91FD
:100520000F91FF90EF900895DC01ED91FC91028016
:10053000F381E02D099508950F931F938C01DC0141
:10054000ED91FC910190F081E02D6DE00995D801CD
:10055000ED91FC910190F081E02DC8016AE00995D0
:100560001F910F9108950F931F938C010E94940285
:10057000C8010E949C021F910F9108952F923F92F3
:100580004F925F926F927F928F929F92AF92BF92A3
:10059000CF92DF92EF92FF920F931F93DF93CF934F
:1005A000CDB7DEB73B014C0119012A016D897E8967
:1005B0008F89988D6A197B098C099D09621A730AC3
:1005C000840A950AA40193010E940703E218F30824
:1005D00004091509A80197010E945B032A0D3B1D20
:1005E0004C1D5D1DB901CA01CF91DF911F910F9183
:1005F000FF90EF90DF90CF90BF90AF909F908F9043
:100600007F906F905F904F903F902F900895629FE2
:10061000D001739FF001829FE00DF11D649FE00DFA
:10062000F11D929FF00D839FF00D749FF00D659F5B
:10063000F00D9927729FB00DE11DF91F639FB00D5A
:10064000E11DF91FBD01CF011124089597FB092E6B
:1006500007260AD077FD04D049D006D000201AF42E
:10066000709561957F4F0895F6F7909581959F4F0E
:100670000895A1E21A2EAA1BBB1BFD010DC0AA1FE3
:10068000BB1FEE1FFF1FA217B307E407F50720F0FB
:10069000A21BB30BE40BF50B661F771F881F991F76
:1006A0001A9469F760957095809590959B01AC01BF
:1006B000BD01CF01089597FB092E05260ED057FDE9
:1006C00004D0D7DF0AD0001C38F4509540953095FF
:1006D00021953F4F4F4F5F4F0895F6F790958095C6
:1006E000709561957F4F8F4F9F4F0895AA1BBB1B3D
:1006F00051E107C0AA1FBB1FA617B70710F0A61B22
:10070000B70B881F991F5A95A9F780959095BC0142
:10071000CD010895EE0FFF1F0590F491E02D09948F
:04072000F894FFCF7B
:100724006461726B0064696D006D656469756D0068
:1007340062726967687400010000000013025D02C0
:0207440075023C
:00000001FF

View File

@ -0,0 +1,59 @@
/*
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
by Tom Igoe
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(0);
// 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;
}
}

View File

@ -0,0 +1,64 @@
/*
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
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);
}
}
}
}