mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-01 12:24:14 +01:00
New examples for basic control structures
This commit is contained in:
parent
ee810e8aa7
commit
ca651419c5
51
build/shared/dist/examples/Control/ForLoopIteration/ForLoopIteration.pde
vendored
Normal file
51
build/shared/dist/examples/Control/ForLoopIteration/ForLoopIteration.pde
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
For Loop Iteration
|
||||
|
||||
Demonstrates the use of a for() loop and arrays.
|
||||
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 17 Jan 2009
|
||||
by Tom Igoe
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Loop
|
||||
*/
|
||||
|
||||
int timer = 100; // The higher the number, the slower the timing.
|
||||
int ledPins[] = {
|
||||
2, 3, 4, 5, 6, 7 }; // 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 < thisPin; thisPin++) {
|
||||
pinMode(ledPins[thisPin], OUTPUT);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// loop from the lowest pin to the highest:
|
||||
for (int thisPin = 0; i < 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 (thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
|
||||
// turn the pin on:
|
||||
digitalWrite(ledPins[thisPin], HIGH);
|
||||
delay(timer);
|
||||
// turn the pin off:
|
||||
digitalWrite(ledPins[thisPin], LOW);
|
||||
}
|
||||
}
|
53
build/shared/dist/examples/Control/IfStatementConditional/IfStatementConditional.pde
vendored
Normal file
53
build/shared/dist/examples/Control/IfStatementConditional/IfStatementConditional.pde
vendored
Normal 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);
|
||||
|
||||
}
|
||||
|
37
build/shared/dist/examples/Control/Loop/Loop.pde
vendored
Normal file
37
build/shared/dist/examples/Control/Loop/Loop.pde
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Loop
|
||||
* by David A. Mellis
|
||||
*
|
||||
* Lights multiple LEDs in sequence, then in reverse. Demonstrates
|
||||
* the use of a for() loop and arrays.
|
||||
*
|
||||
* http://www.arduino.cc/en/Tutorial/Loop
|
||||
*/
|
||||
|
||||
int timer = 100; // The higher the number, the slower the timing.
|
||||
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
|
||||
int num_pins = 6; // the number of pins (i.e. the length of the array)
|
||||
|
||||
void setup()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num_pins - 1
|
||||
pinMode(pins[i], OUTPUT); // set each pin as an output
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < num_pins; i++) { // loop through each pin...
|
||||
digitalWrite(pins[i], HIGH); // turning it on,
|
||||
delay(timer); // pausing,
|
||||
digitalWrite(pins[i], LOW); // and turning it off.
|
||||
}
|
||||
for (i = num_pins - 1; i >= 0; i--) {
|
||||
digitalWrite(pins[i], HIGH);
|
||||
delay(timer);
|
||||
digitalWrite(pins[i], LOW);
|
||||
}
|
||||
}
|
75
build/shared/dist/examples/Control/WhileStatementConditional/WhileStatementConditional.pde
vendored
Normal file
75
build/shared/dist/examples/Control/WhileStatementConditional/WhileStatementConditional.pde
vendored
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
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
|
||||
|
||||
*/
|
||||
|
||||
// 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
|
||||
const int blinkDelay = 500; // how long to hold between changes of the LED
|
||||
|
||||
// these variables will change:
|
||||
int ledState = LOW; // the state of the LED
|
||||
int lastBlinkTime = 0; // last time the LED changed
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
:00000001FF
|
BIN
build/shared/dist/examples/Control/WhileStatementConditional/applet/WhileStatementConditional.elf
vendored
Executable file
BIN
build/shared/dist/examples/Control/WhileStatementConditional/applet/WhileStatementConditional.elf
vendored
Executable file
Binary file not shown.
155
build/shared/dist/examples/Control/WhileStatementConditional/applet/WhileStatementConditional.hex
vendored
Normal file
155
build/shared/dist/examples/Control/WhileStatementConditional/applet/WhileStatementConditional.hex
vendored
Normal 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
|
@ -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);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user