Netduino using Visual Studio - I can't get started

Discussion in 'Embedded Systems and Microcontrollers' started by Man_in_UK, Jan 14, 2016.

  1. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    Are there any helpful users of Netdiuino in here?

    I am trying to follow the beginners guide but I get stuck on project #2, using the button.
    I have run out of ideas and have no expert to ask advise.
  2. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    Well.. why not include the details on where you are getting stuck..
    TONS of helpful users here.. None that read minds :p
  3. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    If I type this and send it to the Netduino, it works perfectly..........................

    Code (C):
    2. public static void Main()
    3. {
    4.        OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
    6.         while (true)
    7.         {
    8.            led.Write(true);
    9.            Thread.Sleep(250);
    10.             led.Write(false);
    11.             Thread.Sleep(250);
    12.          }
    13. }
    This makes the LED flash as expected.
    But when I delete the above and change the code to ......................
    Code (C):
    2.         public static void Main()
    3.         {
    4.             OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
    5.             InputPort button = new InputPort(Pins.ONBOARD_BTN, false, Port.ResistorMode.Disabled);
    7.             bool buttonState = false;
    9.             while (true)
    10.             {
    11.                 buttonState = button.Read();
    12.                 led.Write(buttonState);
    13.             }
    16.         }
    It complies without error so I send it to the Netduino but fails to run. The netduino boots into a state where the LED is off . If I press the button the LED does come on but it looks like the button is acting as a reset button.Pressing the button makes the LED light for about 10 seconds before going off, pressing the button does not make it go off.
    I figured that if VS compiles it with no errors, then it should not be a code error, but I must have something wrong with the way VS is sending it to the netduino ...... but I know nothing.
    I cant even make the switch light the LED!

    MOD Note: Added CODE-tags
    Last edited by a moderator: Jan 15, 2016
  4. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    Your code looks correct..
    Except I think ONBOARD_BTN should ONBOARD_SW1 instead..
    Try that first..
    So from your code when you press the button the LED should light.. As soon as you release the button it should turn off.
    Thats it.. Pressing the button should NOT make it go off as you seemed to state above.. The LED should only be on when the switch is down
  5. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    Thanks for helping.
    If I can crack this, I will have to learn how to post code correctly in here.

    The onboard LED can be confusing as it always comes on when the board is powered, only going off when the program starts up. I have found an easy way to remove this from my confusion.
    I have run the first example replacing the Output Port to D0 and put an external LED on it, it flashes as it should.

    I have also changed the output to D0 on the 'button' example but it does not light up.
    I did change _BTN to _SW1 but it did not cure it.

    Any other ideas?

    The news is good.
    Long story short, reloaded laptop & updated all programs and things have started working .... I am happy again.
    I will soon be looking for some instructions on 'how to post code' correctly in this forum.
    Are they easy to find?
    Last edited: Jan 21, 2016
  6. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    Possibly the most basic question, if I may .............................

    I am playing with a sample code that toggles an LED from a web page by typing <ipaddress./ON or <ipaddress>/OFF
    The code is

    if (request.IndexOf("ON") >= 0)
    else if request bla-bla turn it off again

    I realise that an 'if' statement has to be followed by an 'else if'.... but what if I want to send more than just on & off. I want to be able to send
    light led 1 & light led2 etc.

    I know how to set up more output ports ...but what can I replace the 'if' statement with?

    I apologize in advance for being very new to this
  7. shteii01

    AAC Fanatic!

    Feb 19, 2010
    I am not sure if I understand your problem correctly, but I want to point out:
    - if statement can be by itself
    - if statement can be followed by else, the if else combo.

    In the first case, if by itself. You simply make program check a parameter or check a condition, if(A==1){do something, light up an led for example}. So the program checks the state of A, if A does not meat the condition, if A is not 1, if A is 8 for example, then the program skips the command to light up the led. The command to light up the led is simply bypassed and program moves to the next line of code. My main point is that you are NOT REQUIRED to follow if with else. You can have multiple if statements follow each other for example.
  8. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    This is how to post code.

    First, use the insert button along the top when posting. The icon looks like a page of text with an 'I' in the lower right corner. Then, select 'Code'. It will insert tags into your post. Place the cursor between the brackets, and paste your code.

    Otherwise, you can manually type (without the space) '[ code]', paste your code, and then type '[ /code]' (without the space).
  9. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    This explains it perfectly. I would like to make duplicate 'if' statements. I wrongly assumed I had to use 'else' as I keep seeing examples that only have two functions.

    Thank you
  10. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    Thanks for this help.
    As time goes on I will need to post correctly.
  11. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    Stuck again.

    I can set an output port using a web page request but I would like to know how to start and stop an alternating sequence from a web request. The code below is a poor attempt at:

    Web request (ip address)/OFF - turns off both leds
    Web request (ip address)/SWAP - alternates between led1 & led2 until told to stop.

    How can I get the them to continue to alternate?

    Cheers lots for all help

    Code (Text):
    1. using System;
    2. using System.Net;
    3. using System.Net.Sockets;
    4. using System.Threading;
    5. using Microsoft.SPOT;
    6. using Microsoft.SPOT.Hardware;
    7. using SecretLabs.NETMF.Hardware;
    8. using SecretLabs.NETMF.Hardware.Netduino;
    10. namespace Blinky
    11. {
    12.     public class testSecond    {
    13.         public static void Main()
    14.         {
    15.             // write your code here
    16.             OutputPort led1 = new OutputPort(Pins.GPIO_PIN_D1, false);
    17.             OutputPort led2 = new OutputPort(Pins.GPIO_PIN_D2, false);
    19.             int port = 80;
    21.             Thread.Sleep(50000);
    24.             var interf = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];
    25.             interf.EnableStaticIP("", "", "");
    26.             interf.EnableStaticDns(new string[] { "", "" });
    29.             Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    30.             IPEndPoint listenerEndPoint = new IPEndPoint(IPAddress.Any, port);
    32.             listenerSocket.Bind(listenerEndPoint);
    33.             listenerSocket.Listen(1);
    35.             while (true)
    36.             {
    37.                 Socket clientSocket = listenerSocket.Accept();
    39.                 bool dataReady = clientSocket.Poll(5000000, SelectMode.SelectRead);
    41.                 if (dataReady && clientSocket.Available > 0)
    42.                 {
    43.                     byte[] buffer = new byte[clientSocket.Available];
    44.                     int bytesRead = clientSocket.Receive(buffer);
    46.                     string request = new string(System.Text.Encoding.UTF8.GetChars(buffer));
    48.                  if (request.IndexOf("SWAP") >= 0)
    50.             {
    51.                 led1.Write(true);
    52.                 led2.Write(false);
    53.                 Thread.Sleep(250);
    54.                 led1.Write(false);
    55.                 led2.Write(true);
    56.                 Thread.Sleep(250);
    57.             }
    60.                     if (request.IndexOf("OFF") >= 0)
    61.                     {
    62.                         led1.Write(false);
    63.                         led2.Write(false);
    64.                     }
    66.                     string statusText = "STATE IS " + (led1.Read() ? "ON" : "OFF") + ".";
    68.                     string response =
    69.                         "HTTP/1.1 200 OK\r\n" +
    70.                         "Content-Type: text/html; charset=utf-8\r\n\r\n" +
    71.                         "<html><head><title>Setscore</title></head>" +
    72.                         "<body>" + statusText + "</body></html>";
    75.                     clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(response));
    76.                 }
    77.                 clientSocket.Close();
    78.             }
    79.         }
    80.     }
    81. }