Program to blink external LED

Now that everything is connected, type the following program in Cloud9, save it as blinkExternalLED.js and run. You should be able to see the LED blinking each second:

var b = require('bonescript'),
var led = "P8_10";
var state = b.HIGH;
var loopTime = 1000;
var exitTime = 30000;

b.pinMode(led, b.OUTPUT);
b.digitalWrite(led, state);

var loopTimer = setInterval(blink, loopTime);
var exitTimer = setTimeout(exitProgram,exitTime);

function blink()
{
   if(state == b.LOW) state = b.HIGH;
   else state = b.LOW;
   b.digitalWrite(led, state);
}

function exitProgram()
{
   b.digitalWrite(led,b.LOW);
   clearInterval(loopTimer);
}

Explanation

This is the exact same program that we did in the previous chapter except we put LED as string P8_10 here. We declared P8_10 pin direction as output using function pinMode(). When our program does digitalWrite() HIGH on P8_10 pin, the processor puts 3.3V on that pin. The current starts flowing from P8_10 to P8_2 and the LED glows. When the program writes LOW, the processor puts 0V on P8_10. As both ends have no voltage, the current flow stops and our LED goes off. When we do this in a repeating time interval, the external LED blinks. You can replace the LED in the above circuit with a buzzer to get sound notification. You can use a relay switch to ON/OFF AC devices in the home by connecting the relay to the GPIO pin.

Troubleshooting

  1. Do not connect both ends of an LED or resistor on a breadboard such that they become electrically interconnected.
  2. If the LED is not blinking, check if the resistor, LED and jumper pins are correctly fitting in breadboard pin holes. If needed, remove all jumper pins, LED and resistor. Connect everything again correctly. Please make sure that you are using the correct pins for connection – P8_2 and P8_10.
  3. If you use different colors for jumper wires, it becomes easy to understand and debug. Black wire is used to connect with ground as convention. Red wire is used to connect a positive power source. We are using yellow/green wires for the GPIO.
  4. Change the LED and check. Sometimes they blow out.
  5. You can check the voltage across LED pins using a multimeter to find out if there is a broken circuit in between.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.160