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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A TEACHER’S GUIDE TO THE INTEL® GALILEO | 32// initialise <strong>the</strong> switch digital pin as an inputpinMode(switchPin, INPUT);In <strong>the</strong> loop, <strong>the</strong> main logic occurs.First, <strong>the</strong> red and green result LEDs are set <strong>to</strong> LOW:digitalWrite(redLED,LOW);digitalWrite(greenLED,LOW);Then six LEDs are sequenced <strong>to</strong> flash on (HIGH) and off(LOW). For example, <strong>the</strong> following threelines set <strong>the</strong> LED connected <strong>to</strong> pin 12 <strong>to</strong> flash on and off:digitalWrite(ledPin12,HIGH);delay(delayBetweenLEDs);digitalWrite(ledPin12,LOW);Before writing Pin 7 HIGH, <strong>the</strong> state of <strong>the</strong> push but<strong>to</strong>n is read:switchPinStateBeforeLightingLed7 = digitalRead(switchPin);The next part of <strong>the</strong> code <strong>the</strong>n checks for <strong>the</strong> but<strong>to</strong>n <strong>to</strong> be pressed while <strong>the</strong> green LED is lit. Ituses a for loop <strong>to</strong> check 10000 times for <strong>the</strong> but<strong>to</strong>n being pressed. The following line starts <strong>the</strong>for loop and <strong>the</strong> code inside <strong>the</strong> starting and ending brace { is performed 10000 times.for (int i=0; i < 10000; i++) {If it finds <strong>the</strong> but<strong>to</strong>n is pressed it sets <strong>the</strong> green result LED <strong>to</strong> HIGH and sets <strong>the</strong> red result LED<strong>to</strong> LOW and calls <strong>the</strong> break command <strong>to</strong> jump out of <strong>the</strong> for loop. If <strong>the</strong> but<strong>to</strong>n is not pressed itsets <strong>the</strong> red result LED <strong>to</strong> HIGH.for (int i=0; i < 10000; i++) {if (!switchPinStateBeforeLightingLed7) { // not allowed <strong>to</strong> hold down but<strong>to</strong>n!switchPinState = digitalRead(switchPin);if (switchPinState == HIGH) {digitalWrite(greenLED,HIGH);digitalWrite(redLED,LOW);break;}else {digitalWrite(redLED, HIGH);digitalWrite(greenLED, LOW);}}else {digitalWrite(redLED, HIGH);}}<strong>Final</strong>ly, a delay is called <strong>to</strong> let <strong>the</strong> person see how <strong>the</strong>y went and <strong>the</strong>n <strong>the</strong> Green LED is set <strong>to</strong>LOW:delay(250); // wait <strong>to</strong> let person see how <strong>the</strong>y wentdigitalWrite(ledPin7,LOW);

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

Saved successfully!

Ooh no, something went wrong!