HI All,
I'm trying to use a netduino plus 2 to act as a very simple monitor for a momentary switch. Basically all I'm trying to do is when the switch changes states, post to a URL being monitored by an HTTPListener in an app on a pc on my network.
To test this, although I realize it's not the ideal test bench, I have simply put a pulldown resistor between GND and D1 and then used a short length of bell wire to short from 3.3v to D1, just in order to test. Now, I realize this is not an ideal situation and I have some more supplies on the way in order to test in a better bench environment. However, I am seeing behavior that seems bizarre, even given the bounce I would expect from using a wire as a switch and I feel like I am just doing something very basic very wrong. I have attached a picture of my netduino physical setup - I currently am using a 1k 5% pulldown resistor, I started out with a 10k. My code is below. What I am seeing is one of several things, somewhat randomly:
- If I do nothing but turn the LED on or off in the interrupt code based on the status of D1, everything is wonderful - every time I touch the wire to the D1 leg of the resistor the light turns on, and every time I take it away the light turns off.
- If I create a URL and post to it on each interrupt as in the code below:
- Occasionally, it will work fairly well. I may get 15 or 20 hits to the HTTPListener each time I touch or take away the wire, which I would expect without debouncing, but it works.
- Sometimes, I touch the wire once and I get continuous interrupts, with the light constantly flashing and the HTTPListener getting hit over and over again regardless of whether I then take the wire away or touch it
- Sometimes, the HTTPListener gets the appropriate number of hits when I touch and take away the wire, but the light stays on constantly
- When I get a constant stream of interrupts, they all seem to have the same time. I'm not totally positive of this but I saw it at least once
I've been programming for quite a few years and judging by the random behavior I'm getting, I feel like I must be fundamentally misunderstanding the interrupt process, or missing an initialization, or that I am making some sort of grave hardware mistake. Again, I understand I should have a breadboard and switch but from what I've read of the "experiment with a Netduino" posts out there, what I'm doing doesn't seem that fundamentally flawed that I should be having this many issues. I'm hoping someone out there can tell me what it is I'm doing wrong.
Thanks very much!
Rob
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Net;
namespace FirstApp
{
public class Program
{
private static DateTime LastInterrupt; //Part of attempt at filter - see below
//Originally defined these in main, didn't make a difference when I moved them
private static OutputPort onboardLED = new OutputPort(Pins.ONBOARD_LED, false);
//Tried glitch filter true, didn't help
private static InputPort D1 = new InterruptPort(Pins.GPIO_PIN_D1, false , Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
private static int reqnum = 0;
private static String s;
private static HttpWebRequest test;
private static uint lastvalue = 0;
public static void Main()
{
D1.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
LastInterrupt = DateTime.Now.AddDays(-1); //Attempt at debounce - see below
// Wait forever
Thread.Sleep(Timeout.Infinite);
}
private static void button_OnInterrupt(uint port, uint data, DateTime time)
{
//My attempt at debouncing to see if that helps - it didn't seem to. I tried 50, 100, 500, etc
//if (LastInterrupt.AddMilliseconds(50 ) > time)
// return;
//LastInterrupt = time;
onboardLED.Write(data == 1);
reqnum += 1;
String s = (data == 1).ToString();
//My attempt at a simple filter - didn't help
//if (data == lastvalue)
// return;
//lastvalue = data;
HttpWebRequest test = (HttpWebRequest)HttpWebRequest.Create(String.Concat("http://192.168.1.80/this?",reqnum.ToString(), "=", s));
//Thought I was getting random network errors at one point - however try/catch does not seem to make a difference
//try
//{
test.GetResponse();
//}
//catch (Exception ex)
//{
// Debug.Print(ex.Message);
//}
}
}
}
IMG_2073.JPG 149.64KB 2 downloads