There are many examples on how to get your on-board LED to flash, things really get interesting when you can accept input from the pins. This is a Hello World on reading off the digital pins.
The plan is a simple one.
- Connect the D0 to D1.
- Have D0 turn off and on.
- Read off D1.
- See is the value read from D1 matches the value of D0
Digital Pins 0 and 1 connected |
In order to print out these values, we'll just use the Debug.Print() method, so you'll see it in the Output window of the IDE. The code used looks like this:
namespace DigitalIOHelloWorld
{
public class Program
{
public static void Main()
{
OutputPort pinD0 = new OutputPort(Pins.GPIO_PIN_D0, false);
InputPort pinD1 = new InputPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.Disabled);
while (true)
{
Thread.Sleep(1000);
Debug.Print("D0 = " + pinD0.Read());
Debug.Print("Turning D0 on");
pinD0.Write(true);
Debug.Print("Reading D1 = " + pinD1.Read());
Thread.Sleep(1000);
Debug.Print("D0 = " + pinD0.Read());
Debug.Print("Turning D0 off");
pinD0.Write(false);
Debug.Print("Reading D1 = " + pinD1.Read());
}
}
}
}
{
public class Program
{
public static void Main()
{
OutputPort pinD0 = new OutputPort(Pins.GPIO_PIN_D0, false);
InputPort pinD1 = new InputPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.Disabled);
while (true)
{
Thread.Sleep(1000);
Debug.Print("D0 = " + pinD0.Read());
Debug.Print("Turning D0 on");
pinD0.Write(true);
Debug.Print("Reading D1 = " + pinD1.Read());
Thread.Sleep(1000);
Debug.Print("D0 = " + pinD0.Read());
Debug.Print("Turning D0 off");
pinD0.Write(false);
Debug.Print("Reading D1 = " + pinD1.Read());
}
}
}
}
Deploying this and running it will produce this output in the IDE:
D0 = False
Turning D0 on
Reading D1 = True
D0 = True
Turning D0 off
Reading D1 = False
D0 = False
Turning D0 on
Reading D1 = True
D0 = True
Turning D0 on
Reading D1 = True
D0 = True
Turning D0 off
Reading D1 = False
D0 = False
Turning D0 on
Reading D1 = True
D0 = True
...
As you can see, the read value on D1 matched the state of D0.
Hope this helps.
Hi,
ReplyDeleteThanks for your Netduino stuff! I see you looked at https://bitbucket.org/kenderes/freeosek-on-netduino
Did you ever get freeosek going on the Netduino?
Cheers,
P
(pdeneys@gmail.com)