Netduino using Visual Studio - I can't get started

Thread Starter

Man_in_UK

Joined May 13, 2008
180
Hi,
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.
 

mcgyvr

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

Thread Starter

Man_in_UK

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

C:
public static void Main()
{
       OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        while (true)
        {
           led.Write(true);
           Thread.Sleep(250);
            led.Write(false);
            Thread.Sleep(250);
         }
}
This makes the LED flash as expected.
But when I delete the above and change the code to ......................
C:
        public static void Main()
        {
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            InputPort button = new InputPort(Pins.ONBOARD_BTN, false, Port.ResistorMode.Disabled);

            bool buttonState = false;

            while (true)
            {
                buttonState = button.Read();
                led.Write(buttonState);
            }


        }
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:

mcgyvr

Joined Oct 15, 2009
5,394
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
 

Thread Starter

Man_in_UK

Joined May 13, 2008
180
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:

Thread Starter

Man_in_UK

Joined May 13, 2008
180
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)
{
led.Write(true):
}
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
 

shteii01

Joined Feb 19, 2010
4,644
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)
{
led.Write(true):
}
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
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.
 

djsfantasi

Joined Apr 11, 2010
9,156
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).
 

Thread Starter

Man_in_UK

Joined May 13, 2008
180
- if statement can be by itself
- if statement can be followed by else, the if else combo.
.
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
 

Thread Starter

Man_in_UK

Joined May 13, 2008
180
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:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace Blinky
{
    public class testSecond    {
        public static void Main()
        {
            // write your code here
            OutputPort led1 = new OutputPort(Pins.GPIO_PIN_D1, false);
            OutputPort led2 = new OutputPort(Pins.GPIO_PIN_D2, false);

            int port = 80;

            Thread.Sleep(50000);


            var interf = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];
            interf.EnableStaticIP("192.168.0.125", "255.255.255.0", "192.168.0.1");
            interf.EnableStaticDns(new string[] { "192.168.0.1", "192.168.0.1" });


            Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint listenerEndPoint = new IPEndPoint(IPAddress.Any, port);

            listenerSocket.Bind(listenerEndPoint);
            listenerSocket.Listen(1);

            while (true)
            {
                Socket clientSocket = listenerSocket.Accept();

                bool dataReady = clientSocket.Poll(5000000, SelectMode.SelectRead);

                if (dataReady && clientSocket.Available > 0)
                {
                    byte[] buffer = new byte[clientSocket.Available];
                    int bytesRead = clientSocket.Receive(buffer);

                    string request = new string(System.Text.Encoding.UTF8.GetChars(buffer));
                   
                 if (request.IndexOf("SWAP") >= 0)
                          
            {
                led1.Write(true);
                led2.Write(false);
                Thread.Sleep(250);
                led1.Write(false);
                led2.Write(true);
                Thread.Sleep(250);
            }


                    if (request.IndexOf("OFF") >= 0)
                    {
                        led1.Write(false);
                        led2.Write(false);
                    }
       
                    string statusText = "STATE IS " + (led1.Read() ? "ON" : "OFF") + ".";

                    string response =
                        "HTTP/1.1 200 OK\r\n" +
                        "Content-Type: text/html; charset=utf-8\r\n\r\n" +
                        "<html><head><title>Setscore</title></head>" +
                        "<body>" + statusText + "</body></html>";


                    clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(response));
                }
                clientSocket.Close();
            }
        }
    }
}
 
Top