Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

RobFromLI's Content

There have been 5 items by RobFromLI (Search limited from 08-May 23)


By content type

See this member's

Sort by                Order  

#61347 Simple switch monitor - having trouble

Posted by RobFromLI on 24 January 2015 - 01:07 AM in Netduino Plus 2 (and Netduino Plus 1)

In my debouncing stuff that i've done I had to increase the time from 50ms to around 200ms.

I also had to not use the time parameter passed in to the event handler, but instead use DateTime.UtcNow as well as wrapping the event handler in a lock block. Here's a quick example (untested) to replace the entire button_OnInterrupt check.

private static bool _currentState = false;
private static void button_OnInterrupt(uint port, uint data, DateTime time)
{
 lock(this)
 {
  var now = DateTime.UtcNow;
  if (LastInterrupt.AddMilliseconds(200) > now)
  {
   LastInterrupt = now;
   return;
  }
  _currentState = !currentState; //I don't trust data because of bouncing
  onboardLED.Write(_currentState);
  reqnum++;
  HttpWebRequest.Create("http://192.168.1.80/this?" + reqnum + "=" + _currentState).GetResponse().Dispose();
  LastInterrupt = now; //set this here because it's possible the web request takes longer than 200ms which would break the debounce. Calls to the handler are queued up. You might want to consider moving the state changes into a queue and have a seperate thread process the queue, though that's more complicated.
 }
}

FYI, UtcNow is faster than .Now

 

THANK YOU!! Your input was key!  Here's the final routine, seems rock solid now.

 

        private static void button_OnInterrupt(uint port, uint data, DateTime time)
        {

            lock (D1)
            {

                var now = DateTime.UtcNow;
                Boolean newValue = false;
                newValue = D1.Read();

                if (LastInterrupt.AddMilliseconds(100) > now && lastvalue == newValue)
                    return;

                // Activate the LED when button is pressed (data == 1)
                lastvalue = newValue;
                onboardLED.Write(lastvalue);
                reqnum += 1;
                
                String s = newValue.ToString();
                String t = String.Concat("http://192.168.1.80/this?D1=");
                //if (data == lastvalue)
                //    return;


                HttpWebRequest test = (HttpWebRequest)HttpWebRequest.Create(String.Concat(t, s));

                //try
                //{
                test.GetResponse();

                test.Dispose();

                //}
                //catch (Exception ex)
                //{
                //    Debug.Print(ex.Message);
                //}
                LastInterrupt = now;

            }
        }

 




#61333 Simple switch monitor - having trouble

Posted by RobFromLI on 21 January 2015 - 07:15 PM in Netduino Plus 2 (and Netduino Plus 1)

In my debouncing stuff that i've done I had to increase the time from 50ms to around 200ms.

I also had to not use the time parameter passed in to the event handler, but instead use DateTime.UtcNow as well as wrapping the event handler in a lock block. Here's a quick example (untested) to replace the entire button_OnInterrupt check.

private static bool _currentState = false;
private static void button_OnInterrupt(uint port, uint data, DateTime time)
{
 lock(this)
 {
  var now = DateTime.UtcNow;
  if (LastInterrupt.AddMilliseconds(200) > now)
  {
   LastInterrupt = now;
   return;
  }
  _currentState = !currentState; //I don't trust data because of bouncing
  onboardLED.Write(_currentState);
  reqnum++;
  HttpWebRequest.Create("http://192.168.1.80/this?" + reqnum + "=" + _currentState).GetResponse().Dispose();
  LastInterrupt = now; //set this here because it's possible the web request takes longer than 200ms which would break the debounce. Calls to the handler are queued up. You might want to consider moving the state changes into a queue and have a seperate thread process the queue, though that's more complicated.
 }
}

FYI, UtcNow is faster than .Now

 

Thanks, I will definitely try this.  I was wondering if I was running into re-entrancy issues because that would explain the randomness, and it makes sense to try the current UTC when the interrupt time param doesn't make sense.  Thanks for the help!




#61321 Simple switch monitor - having trouble

Posted by RobFromLI on 20 January 2015 - 01:31 AM in Netduino Plus 2 (and Netduino Plus 1)

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




#60788 Simple switch detection circuit - don't want to fry board

Posted by RobFromLI on 24 November 2014 - 02:33 AM in Netduino Plus 2 (and Netduino Plus 1)

Hi Rob,

 

You're perfectly on point that your circuit it will work just fine. The Netduio's digital pins are 5V tolerant even though they operate at 3.3V. That is to say, they output 3.3V signals, but will accept 5V or 3.3V signals as input. That said, it certainly wouldn't hurt to go with 3.3V, it will even save a miniscule amount of power (though really, not enough be worthwhile is all but the most power sensitive applications). As a word of caution: the Netduino's analog pins, when not operating as digital pins, are NOT 5V tolerant, and using a 5V signal could damage them. To be safe, I try to only use 3.3V signals with the analog pins.

 

As a point of nomenclature, a resister tied to ground is a pull-down (down to 0) and a resistor tied to VCC is a pull-up.

 

Good luck!

 

 

Thanks very much!  I will proceed with my project and I'll try to report back when I've succeeded.  Thanks for the help!




#60775 Simple switch detection circuit - don't want to fry board

Posted by RobFromLI on 21 November 2014 - 04:55 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi all,

 

I am a very experienced programmer who has a little bit of electronics knowledge.  Basically I've fooled around in the past with some simple switches, lights and motors and maybe a resistor or two.

I'm looking to hook my netduino 2 plus to a normally open circuit and trigger it to bang a web service on my pc when the circuit closes.  I have the code roughed out using the on board button and now I want to code/test just using a short wire or switch before I hook it up to the larger circuit. 

I've done a good bit of searching and found this http://arduino.cc/en...utton_schem.pngwhich is part of this tutorial

http://www.arduino.c...Tutorial/Button

 

I just want to make sure before I try this (assuming I hook everything up properly and protect against static and all that) - if I hook a 10k resistor between digital input 1 and ground as a pull "down"(? or up? but I think down?) and then have a switch that connects the pin to 5v, I won't fry anything, is that right?

 

Do I have to have the pin set to the proper mode when I connect the wire in order to avoid frying something?  In other words if I have a bug and the pin's set to output but I put 5v on it, will it fry then?

 

Can I uses 3.3v, is that any safer / better?

 

TIA

 

Rob





home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.