31.07.2015 Views

A-Teachers-Guide-to-the-Intel-Galileo-Final

A-Teachers-Guide-to-the-Intel-Galileo-Final

A-Teachers-Guide-to-the-Intel-Galileo-Final

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

37 | Beginning with Arduino2. Check also that <strong>the</strong> correct <strong>Intel</strong>® board has been selected.3. Copy and paste <strong>the</strong> code below, noting values set for both infra-red sensor and microservo. (Example will trigger servo within 70cm from object. You can change values <strong>to</strong> suitrequirement.)#include // this provides code for <strong>the</strong> ServoServo myServo; // create servo object <strong>to</strong> control a servoint infraredSensor = 0; // analog pin used <strong>to</strong> connect <strong>the</strong> potentiometerint sensorValue; // variable <strong>to</strong> read <strong>the</strong> value from <strong>the</strong> analog pinvoid setup(){myServo.attach(9); // attaches <strong>the</strong> servo on pin 9 <strong>to</strong> <strong>the</strong> servo objectSerial.println("In setup ");}void loop(){sensorValue = analogRead(infraredSensor); // reads <strong>the</strong> value of <strong>the</strong> infrared sensor(value between 0 and 1023)Serial.println("Motion sensor value is ");Serial.print(sensorValue); // write some debugs <strong>to</strong> <strong>the</strong> Serial consoleif (sensorValue > 100) {myServo.write(179); // write <strong>the</strong> maximum value of <strong>the</strong> Servo, 179}delay(1000); // hold <strong>the</strong> bin lid up for one secondmyServo.write(0); // push <strong>the</strong> bin lid back down}Download and run program.Explanation of <strong>the</strong> CodeThe first line of <strong>the</strong> program includes some prewritten code that is specific <strong>to</strong> servo mo<strong>to</strong>rs on<strong>the</strong> Arduino:#include // this provides code for <strong>the</strong> ServoThe following lines of code initialise variables <strong>to</strong> represent <strong>the</strong> servo mo<strong>to</strong>r, <strong>the</strong> infrared sensorand <strong>the</strong> value returned from <strong>the</strong> sensor:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!