EE185Lab Basics:ConditionalStatements Conditionalstatementsaretheheartofdecision-makinginprogramming. SuggestedPrerequisites • ProgrammingIntro SetupandPreparation ListofMaterials • • • • • • TIMSP430G2LaunchPad Breadboard M/FJumperWires(4) M/MJumperWires(4) Pushbuttons(2) Resistorsofsmallvalue(~1kΩ)(4) Preparethecircuit Designthefollowingcircuit(Figure1). Figure1:ConditionalLabCircuit Program1:ifstatement Createanduploadthefollowingprogram(Figure2)totheLaunchPad. Figure2:ifstatement Thingstoconsider • • • • • Youcanuseavoltmetertoexaminethevoltageacrossthebuttonwhenitgetspressed. TheSerialMonitorcanbeusedtoexaminethevaluesofpinState,buttonIsPressed, andLEDstate(You’llneedtorevisethecodetoenablethis). ThereisarelationshipbetweenthevaluesofpinStateandthevoltageacrossthebutton. ConsiderwhytheNOToperator!wasincludedinthisprogram. Considerhowfastthisprogramisrepeatingthecodeintheloop()function. Program2:NestedIfstatements Createanduploadthefollowingprogram(Figure3)totheLaunchPad. Figure3:Nestedifstatements Thingstoconsider • • • • Considerthegeneraleffectofnestingifstatements. Youcananalyzetheprogrambyexaminingthe3variablesbutton1IsPressed, button2IsPressed,andLEDstate,usingtheSerialMonitor. Thisprogramcanbeimplementedwithasingleifstatement,usingBooleanoperators. InformationaboutBooleanoperatorscanbefoundatthefollowinglink: o http://energia.nu/reference/ Program3:Ifstatementsinsequence Createanduploadthefollowingprogram(Figure4)totheLaunchPad. Figure4:Ifstatementsinsequence Thingstoconsider • • • • Considerthegeneraleffectofputtingifstatementsinsequencethatdothesamethingbutare activatedbydifferentconditionalstatements. Youcananalyzetheprogrambyexaminingthe3variablesbutton1IsPressed, button2IsPressed,REDstate,andGREENstateusingtheSerialMonitor. ThisprogramcanbeimplementedwithasingleifstatementusingBooleanoperators. InformationaboutBooleanoperatorscanbefoundatthefollowinglink: o http://energia.nu/reference/ Program4:elseifstatements Createanduploadthefollowingprogram(Figure5)totheLaunchPad. Figure5:elseifstatement Thingstoconsider • • • Unlikeifstatementsinsequence,elseifstatementsarelinkedtotheirprecedingifstatement, andexecutionismutuallyexclusive.Youcandeterminewhichconditionalstatementhas precedenceintheeventthattheyarebothtrue. Youcananalyzetheprogrambyexaminingthe3variablesbutton1IsPressed, button2IsPressed,REDstate,andGREENstateusingtheSerialMonitor. Youcantrytoexplainthelogicalflowoftheif…elseifcodestructurewithablockdiagram. Program5:elsestatements Createanduploadthefollowingprogram(Figure6)totheLaunchPad. Figure6:elsestatement Thingstoconsider • • • • • • Similartoelseifstatements,elsestatementsarelinkedtotheprecedingifstatement. Noticethattheelsestatementdoesnotcontainanyconditionalstatement. Youcananalyzetheprogrambyexaminingthe3variablesbuttonIsPressed,REDstate, andGREENstateusingtheSerialMonitor. Youcantrytoexplainthelogicalflowoftheif…elsecodestructurewithablockdiagram. Itisalsocommontoputanelsestatementafteranif…elseifcodestructure. Informationaboutifstatementsandtheirvariationscanbefoundatthefollowinglink: o http://energia.nu/reference/else/ Review Afterthislab,youshouldhaveagoodunderstandingofthefollowingtopics.Ifyou’renotsureabout someofthem,gobackthroughthelabandtrytoafindagoodplacetoexplorethetopic. Youshouldbeabletodothefollowing • • • • Createprogramsthatuseifstatementsanditsvariantstoexecutecodeconditionally. Explainthelogicalflowofanifstatementsanditsvariantswithblockdiagrams. UseNOT,AND,andORBooleanoperatorstomakemoresophisticatedconditionalstatements. UsethevoltmeterandSerialMonitortoanalyzeandtroubleshootacircuitorprogram. Application Tryapplyingtheskillsyoulearnedfromthislab.Youcancomeupwithyourownprojectideaortryone fromtheflowchart.
© Copyright 2026 Paperzz