Download - Coding Sensors Guide (ROBOTC®) TETRIX® Getting Started Guide ...

Transcript
Page 1: Coding Sensors Guide (ROBOTC®) TETRIX® Getting Started Guide ...

321

Coding Sensors Guide (ROBOTC®)

Allsensors(exceptthebuilt-inrotationsensorinthemotor)connecttoPorts1,2,3,or4.TheseportsarelocatedonthebottomoftheNXTbrickandpassinformationthroughtheNXTconnectorwires.EnsurethatthewiresconnectingeachsensortotheNXTbrickarepluggedintothecorrectportandthatthishasbeencorrectlysetupintheMotors and Sensors SetupunderRobotintheROBOTC®topmenu.

1.OpentheMotors and Sensors SetupandselecttheSensorstabatthetop.

2.Choosetheportandthetypeofsensorthathasbeenpluggedintothatport.

3.Givethesensoraname.Itisbesttochooseasimplenamethataccuratelyrepresentsthesensorbecausethatisthenamethatwillbeusedthroughoutthecode.

Touch Sensor:ChooseTouchoption.

Light Sensor: ChooseLight ActiveifthereflectedlightvalueisdesiredorLight Inactiveiftheambientlightvalueisdesired.WiththeLightActivesettingtherobotwillproduceitsownlight,andwiththeLightInactivesettingitwillnot.

Sound Sensor: ChoosetheSound DBoptionifthetruedecibelvalueisdesiredortheSound DBAoptionifthedecibelvalueasahumanwouldinterpretitismoreappropriate.

Ultrasonic Sensor: ChoosetheSONARoption.Thevalueofthissensorisreturnedincentimeters.

Note:Thetouchsensorwillprovideadigitalsignal,whiletheotherswillprovideananalogsignal.ThismeansthatthetouchsensorwillonlybeabletosendtwodifferentpiecesofinformationtotheNXTbrick(TrueorFalse,orinthiscase,pressedornotpressed),whiletheothersensorswillsendavaluebetween0and100.ThisisimportanttokeepinmindwhencreatingprogramsthatuseinformationfromthesensorsontheNXTBrick.

TETRIX®GettingStartedGuideAdditionalGuides

Page 2: Coding Sensors Guide (ROBOTC®) TETRIX® Getting Started Guide ...

322

Example 1: Touch Sensor

Thefollowingprogramusesdatafromthetouchsensor.Theprogramwilldriveforwarduntilthetouchsensorispressed.Oncethetouchsensorispressed,therobotwillstopandtheprogramwillend.

taskmain()

{

while(SensorValue(touch)==0)

{

motor[motorD]=70;

motor[motorE]=70;

}

motor[motorD]=0;

motor[motorE]=0;

}

TheWhileLoopallowstherobottocontinuetorununtilthecaseinsidethebracketsisnolongertrue.

Note:Thenumbers0and1taketheplaceofthewords“False”and“True”inthecode,respectively.

TherobotisinaFalsestatewhenthetouchsensor(inthiscase,named“touch”)isnotpressed.ItwillcontinuetorunthecodeinsidetheloopuntiltheconditioninthebracketsisTrue.Inotherwords,thetouchsensormustbe“True”(orpressed)inordertoexittheloop.Therefore,oncethetouchsensorhasbeenpressed,thecodeoutsideoftheWhileLoopwillrun;themotorswillstopandtheprogramwillend.

Example 2: Ultrasonic Sensor

Thefollowingprogramusesdatafromtheultrasonicsensor.Theprogramwilldriveforwarduntilthevaluereads50orless.Oncetheultrasonicsensorreadsavaluethatisinthisrange,therobotwillstopandtheprogramwillend.

taskmain()

{

while(SensorValue(ultrasonic)>50)

{

motor[motorD]=70;

motor[motorE]=70;

}

motor[motorD]=0;

motor[motorE]=0;

}

TheWhileLoopallowstherobottocontinuetorununtilthecaseinsidethebracketsisnolongertrue.Therobot’sultrasonicsensor(inthiscase,named“ultrasonic”)willreturnavaluebetween0and100.ItwillcontinuetocheckthevaluereturnedbythesensorandwillrunthecodeinsidetheWhileLoopuntilthevalueisnolongerabove50.Whenthevaluereads50orless,theWhileLoopwillterminateandthecodeoutsideoftheWhileLoopwillexecute.Therefore,oncethevaluereturnedbytheultrasonicsensorreads50orless,thecodeoutsideoftheWhileLoopwillrun;themotorswillstopandtheprogramwillend.

Coding Sensors Guide (ROBOTC®)

AdditionalGuidesTETRIX®GettingStartedGuide