Added new switch case example

This commit is contained in:
Tom Igoe 2009-07-01 17:30:39 +00:00
parent c8ae5d9d20
commit 674f4b1c97
6 changed files with 224 additions and 0 deletions

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

Binary file not shown.

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/Loop
*/
// 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;
}
}