Arduino Tutorial 7 Loops

  • Upload
    rockksa

  • View
    235

  • Download
    0

Embed Size (px)

Citation preview

  • 8/12/2019 Arduino Tutorial 7 Loops

    1/4

    Arduino Tutorial 7 - Loops

    Recap : The Arduino sketch has two (2) compulsory sections setup() and loop().

    All commands in the loop() section are repeated over and over until the power is cut.

    In addition to the SECTI! called loop() there is also a C""A!# that loops.

    Project : To learn how to use a $loop $ command% controlled &y a varia&le.

    Definition A loop is a way o' repeatin a command% or a roup o' commands% a num&er o' times%

    until some way o' endin the loop occurs.

    e when a counter% which we increase each time% ets to a particular value.

    r when a condition &ecomes true (or 'alse) e a switch is activated .

    Types There are * types o' $loops+ in Arduino ,

    -or loop

    hile loop

    #o....hile

    The do...while loop works in the same manner as the whileloop% &ut the conditionis tested at the end o' the loop% so the doloop will alwaysrun at least once.

    The For Loop (See http,//math&its.com/"ath0its/CompSci/loopin/'or.htm)

    for ( start_Epression! test_Epression! count_Epression" ## Dia$ra% on pa$e &

    '

    loc) of code!

    *

    The start1Epression is evaluated &e'ore the loop &eins. -or eample i 3 45ou can declare and assin the start1Epression in theforloop (such as int i 3 46).

    This start1Epression is evaluated only once at the &einnin o' the loop.

    The test1Epression will evaluate to T78E (non 9ero) or -A:SE (9ero). 'or eample ; ?.

    0races are not re@uired i' the &ody o' theforloop consists o' only !E statement. Indent the &ody o' the

    loop 'or reada&ility. -or eample 'or ( int 3 < 6 ;

  • 8/12/2019 Arduino Tutorial 7 Loops

    2/4

    ,R L,,P

    #iaram 'rom ,

    tutorialspoint.com/cplusplus/cpp1'or1loop.htm

    hat can you use a loop 'or ,

    -lash a led on and o''% a num&er o' times. :iht a rin or row o' :E#s in turn

    "ove a motor to a set position% in steps

    Frint out all the elements o' an array.

    Check each item in a name array 'or a match.

    ait till an event occurs (switch/key press).

    Set a num&er o' Arduino pins as Input (or

    output).

    While loop

    while ( test condition"

    '

    co%%ands ( .ody of loop"

    *

    The while loop allows prorams to repeat a command or roup

    o' commands% as lon as a certain condition is true.

    -or eample loop until the user enters in'o on the key&oard6

    The test condition must &e in &rackets. The code &lock (also

    called the &ody o' the loop) is enclosed in &races.

    The test condition is evaluated 'irst. I' the condition is T78E%

    the proram eecutes the &ody o' the loop. The proram then

    returns to the test condition and reevaluates.

    I' the condition is still T78E% it eecutes the code aain.

    I' the test condition is -A:SE to &ein with% the proram never

    eecutes the &ody o' the loop.

    Try this while(Serial.availa&le() 334)6 // waitin..........

    CA7E , 5ou "8ST provide a way 'or $condition+ to chane

    I!SI#E the loop% I' you do not% you et an E!#:ESS loop..

    "icroso't 0S# $0lue Screen ' #eath+ is an endless loop.

    Another eample o' an endless loop Try this

    In setup% use pin"ode to set the led and switch pins % A!# diitalrite the switch pin GIBG.

    int state 3 diital7ead(AH)6 // read the state o' a switch/&utton connected to pin AH.

    hile (state 33 true) // I' the switch/&utton is not pressed% the pin stays hih>

    diitalrite(led1pin% :)6 // turn the :E# --

    ?

    diitalrite(led1pin% GIBG)6 // This line will !EE7 &e eecuted. The loop is E!#:ESS.

  • 8/12/2019 Arduino Tutorial 7 Loops

    3/4

    Solution to endless loop

    int state 3 diital7ead(AH)6 // read the state o' a switch/&utton connected to pin AH.

    hile (state 33 true) // I' the switch/&utton is not pressed

    >

    diitalrite(led1pin% :)6 // turn on the :E#

    state 3 diital7ead(AH)6 // read the state o' a switch/&utton connected to pin AH.

    ?

    diitalrite(led1pin% GIBG)6 // This line will &e eecuted% i' the switch/&utton is pressed !.

    To &reak out o' a while loop% use the command break.

    Eample i' ( diital7ead(AH)33 GIBG) &reak6

    Do While loop

    The do loop works in the same manner as the while loop% with the eception that the condition is

    tested at the end o' the loop% so the do loop will alwaysrun at least once.

    do

    >

    // statement &lock6

    ? while (test condition)6

    /ow to eit a loop0

    There are J ways to eit a loop.

    analorite(ledFin% n)6 // write the value o' n to the ledFin

    delay(ms)6

    ? // end o' 'or loop

    ? // end o' main loop

    Gow would you add to this sketch to decrease the &rihtness &ack to 4

    Gow would you rewrite this to use a while loop

  • 8/12/2019 Arduino Tutorial 7 Loops

    4/4

    /M

    Eample o' readin input 'rom the Ney&oard o' your FC

    hen the return key is pressed on your FC key&oard%% the characters are sent &y Serial to the Arduino.

    7ead each character 'rom the Serial input% then add that character to a Strin called content.

    Search the Strin array emStone to 'ind a match 'or the user input strin content.

    This is done &y a loop within a loop called a nested loop.

    M/

    Strin content 3 OO6 // Create a Strin varia&le called content

    char character6 // Create a char varia&le called characterStrin emStonePQ 3 > O#iamondO% OSapphireO% O7u&yO% OEmeraldO% ORirconO?6

    void setup()

    >

    Serial.&ein(L44)6 // &ein serial communication with FC

    ?

    void loop()

    >

    while(content33OO) read1key&oard()6 // keep readin serial port% until you et some input.

    search1ems()6 content 3 OO6 // reset user input varia&le to nul

    ? // end main loop

    void read1key&oard() // put this 'unction in a new TA0 on the I#E.

    >

    while (Serial.availa&le()) // Check i' any serial data has &een received

    > // hen the return key is pressed on the FC key&oard%

    // the characters (in the key&oard &u''er) are sent via Serial.

    character 3 Serial.read()6 // read the serial and store it in our char varia&le content.concat(character)6 // add the character to our strin called content.

    delay(

    i' (content 33 emStonePiQ.su&strin(K % K=8ser1:)) // check i' the user input matches the su&strin

    >

    Serial.print(O -ound a match at O)6 // i' it does match print the name o' the emStone

    Serial.println(emStonePiQ)6

    ?

    ? // end loop loopin throuh all the characters in the emnamePiQ

    ? // end i loop loopin thouh all the ems.

    ? // end search1ems