Академический Документы
Профессиональный Документы
Культура Документы
Reference
for statements The for statement is used to repeat a bloc3 of statements enclosed in curly braces. -n increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data4pins.
for (int i=0; i <= 10; i++){ // Put Code Here }
if 4 else statements if4else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together. 5or e6ample, an analog input could be tested and one action ta3en if the input was less than 500, and another action ta3en if the input was 1000 or greater. The code would loo3 li3e this&
if (pinFiveInput < 500){ // do Thin ! }e"#e if (pinFiveInput $= 1000){ // do Thin % }e"#e{ // do Thin C }
"se the following e6ample code to 7et tarted with the 8ending !achine
/& 'e(oun)e *+)h ti,e the input pin oe# fro, -./ to HI0H (e1 1 (e)+u#e of + pu#h2(utton pre##)3 the output pin i# to "ed fro, -./ to HI0H or HI0H to -./1 There4# + ,ini,u, de"+5 (et6een to "e# to de(oun)e the )ir)uit (i1e1 to i nore noi#e)1 The )ir)uit7 & -*' +tt+)hed fro, pin 18 to round & pu#h(utton +tt+)hed fro, pin 9 to +5: & 10; re#i#tor +tt+)hed fro, pin 9 to round &/ // )on#t+nt# 6on4t )h+n e1 The54re u#ed here to #et pin nu,(er#7 )on#t int (uttonPin = 9; // the nu,(er of the pu#h(utton pin )on#t int "edPin = 18; // the nu,(er of the -*' pin // :+ri+("e# 6i"" )h+n e7 int "ed<t+te = HI0H; int (utton<t+te; int "+#t%utton<t+te = -./; // the )urrent #t+te of the output pin // the )urrent re+din fro, the input pin // the previou# re+din fro, the input pin
// the fo""o6in v+ri+("e# +re "on 4# (e)+u#e the ti,e3 ,e+#ured in ,i"i#e)ond#3 // 6i"" =ui)>"5 (e)o,e + (i er nu,(er th+n )+n (e #tored in +n int1 "on "+#t'e(oun)eTi,e = 0; // the "+#t ti,e the output pin 6+# to "ed "on de(oun)e'e"+5 = 50; // the de(oun)e ti,e; in)re+#e if the output f"i)>er# void #etup() { pin?ode((uttonPin3 I@PAT); pin?ode("edPin3 .ATPAT); } void "oop() { // re+d the #t+te of the #6it)h into + "o)+" v+ri+("e7 int re+din = di it+"Be+d((uttonPin); // )he)> to #ee if 5ou Cu#t pre##ed the (utton // (i1e1 the input 6ent fro, -./ to HI0H)3 +nd 5ou4ve 6+ited // "on enou h #in)e the "+#t pre## to i nore +n5 noi#e7 // If the #6it)h )h+n ed3 due to noi#e or pre##in 7 if (re+din D= "+#t%utton<t+te) { // re#et the de(oun)in ti,er "+#t'e(oun)eTi,e = ,i""i#(); } if ((,i""i#() // 6h+tever // th+n the (utton<t+te } 2 "+#t'e(oun)eTi,e) $ de(oun)e'e"+5) { the re+din i# +t3 it4# (een there for "on er de(oun)e de"+53 #o t+>e it +# the +)tu+" )urrent #t+te7 = re+din ;
// #et the -*' u#in the #t+te of the (utton7 di it+"/rite("edPin3 (utton<t+te); // #+ve the re+din 1 @eEt ti,e throu h the "oop3 // it4"" (e the "+#t%utton<t+te7 "+#t%utton<t+te = re+din ;