Main Site Documentation

Digital Input Latching Switch


#1

Hello,
I need some general code help… I am monitoring a digital input on a button. What I want to do is latch on an output (LED, realy, etc.) by pressing activating a digital input high. Then when i activate low, I want the output to stay on. Then when I activate the input high again, I want the output to turn off.
My problem is that I am not faster than the scan time on the processor, so I get inconsistent results. I am running a timer at 50ms that scans the digital input. I think that there is some code to work around this, but I can’t figure it out to get consistent results. Any hints or code tricks that I am missing would be helpful. Code below.

The digital input is called “Button”.

        void Latched_Button_Timer_Tick(GT.Timer Latched_Button_Timer)
        {
            //Code for the latching output
            
            if (Button == true)
            {
                Debug.Print("Previous Button: " + PreviousButton);

                for (int i = 0; i < 1; i++)
                {
                    if (PreviousButton == false /*&& !Latched_Button_Checker.IsRunning*/)
                    {
                        Latched_Out.Write(true);
                        //Latched_Button_Checker.Start();
                        break;
                    }
                  
                    //Thread.Sleep(100);

                    if (PreviousButton == true /* && !Latched_Button_Checker.IsRunning*/)
                    {
                        Latched_Out.Write(false);
                        break;
                        //Latched_Button_Checker.Start();
                    }
                    
                    //Thread.Sleep(100);
                }

            }
            PreviousButton = !PreviousButton;
        }
        void Latched_Button_Checker_Tick(GT.Timer Latched_Button_Checker)
        {
            Latched_Button_Checker.Stop();
        }

Thanks!

Jordan


#2

use an interrupt and make sure you have the glitch filter turned on.


#3

Hi Mike,
Thanks for the quick reply. I did try that. However, I forgot to mention that the reason need to use a regular digital input is that my Pin 3, interrupt pin, is already acting as an analog input.
I have 3 analog inputs and I need to use the 4th input as a digital in connected to a momentary button. That pin will latch on when a press the button and activate data logging. Then I activate that pin low and it will stop date logging.
I do have the glitch filter on already and the resistor as pull down(or up) so it reads false when not high and true when activated high.
I just didn’t know if there is some coding trick to beat the scan time.

Thanks,
Jordan


#4

@ L1256937 - What board are you using? Seems unusual that an interrupt pin is not available.

Could you post a minimal program that displays the issue?

I can think of several possible problems, but I need to see a complete program to determine exactly what is happening. (or not happening)


#5

HI Mike,
Thanks again for your input. I am using a raptor on 4.3. I do have a few sockets left open that I could pull an interrupt port. However, I am running out of room in my enclosure and I only purchased a couple breakout boards so I am trying to maximize the use of my sockets. Basically I have 6 sensors and 2 limit switches. I need to poll those six sensors when I latch one or the other limit switch. So I am trying to put 3 sensors and one limit switch on each socket of 2 and 13 (I think.) Mainly because that is where I have the breadboard X1 modules and for cleanliness and space I want to land all of the inputs on those breadboards.
The following code is just my switch testing routine, so the inputs and outputs are not accurate, but basically what I want to do is take a button and run three different switching modes…momentary, timed, and latched. Momentary and timed work well with the corresponding Digital Output LEDs on the Breadboard X1. However, it is the latching switch that gives me trouble.
I suspect it is because my reaction time is not faster than the scan time of the program.

This is the complete code of my switch mode tester. Thanks for your time.

Jordan

//This program basically demonstrates how you can use digital inputs to monitor a push button switch and use it to set different states on digital outputs.
//Like state machine.
//1) Press a button and turn on an output for 1 second then turn off
//2) press a button and turn on an output and then push again to turn off "latching"
//3) press a button and turn on an output while the button is held and off when released. "Momentary"

namespace GadgeteerApp5
{
    public partial class Program
    {
        public GT.Timer Leave_LED_ON_Timer = new GT.Timer(1500);
        
//timer for digital input monitoring----------
        public GT.Timer timed_button_timer = new GT.Timer(100);
        public GT.Timer Latched_Button_Timer = new GT.Timer(100);
        public GT.Timer Momentary_Button_Timer = new GT.Timer(100);
        public GT.Timer Latched_Button_Checker = new GT.Timer(50);


        private GT.SocketInterfaces.DigitalInput DI_Button;    //pushbutton switch wired to digital input
        private GT.SocketInterfaces.DigitalOutput Timed_Out;  //timed output after a button press
        private GT.SocketInterfaces.DigitalOutput Latched_Out;  //latching output
        private GT.SocketInterfaces.DigitalOutput Momentary_Out;  //momentary output

//socket that the breadboard X1 is attached to
        public GT.Socket Socket2 = GT.Socket.GetSocket(2, false, null, null);

        bool TimedPreviousButtonState = false;
        bool PreviousButton = false;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {

//initialize digital inputs and outputs on the proper socket
            this.DI_Button = GT.SocketInterfaces.DigitalInputFactory.Create(Socket2, GT.Socket.Pin.Six, GT.SocketInterfaces.GlitchFilterMode.On, GT.SocketInterfaces.ResistorMode.Disabled, null);
            this.Timed_Out = GT.SocketInterfaces.DigitalOutputFactory.Create(Socket2, GT.Socket.Pin.Three, false, null);
            this.Latched_Out = GT.SocketInterfaces.DigitalOutputFactory.Create(Socket2, GT.Socket.Pin.Four, false, null);
            this.Momentary_Out = GT.SocketInterfaces.DigitalOutputFactory.Create(Socket2, GT.Socket.Pin.Five, false, null);

            //timer to monitor the digital input momentary button
            timed_button_timer.Tick += timed_button_timer_Tick;
            timed_button_timer.Start();

            Latched_Button_Timer.Tick += Latched_Button_Timer_Tick;
            Latched_Button_Timer.Start();

            Momentary_Button_Timer.Tick += Momentary_Button_Timer_Tick;
            Momentary_Button_Timer.Start();

            Latched_Button_Checker.Tick += Latched_Button_Checker_Tick;


            //Timer for the timed digital output
            Leave_LED_ON_Timer.Tick += Leave_LED_ON_Timer_Tick;            

        }

// this is the section used to query the digital input.  called on with other portions of code.
        public bool Button
        {
            get { return DI_Button.Read(); }
        }



 //This is the timer that monitors the button Input 
        void timed_button_timer_Tick(GT.Timer timed_button_timer)
        {
            //Code to activate the timed digital output.  (currently wired to LED on Breadboard module)
            if (Button == true && !Leave_LED_ON_Timer.IsRunning && TimedPreviousButtonState == false)
            {
                Leave_LED_ON_Timer.Start();
                TimedOutOn = true;
                TimedPreviousButtonState = true;
            }
            //-------------------------------

            //just a test to pulse the Debug LED at the input monitor timer rate when the timed digital output is on.
            if (Leave_LED_ON_Timer.IsRunning)
            {
                PulseDebugLED();
            }
            //----------------------


        }
//--------------------------
 // Turn on the digital output for the timed switch type
        public bool TimedOutOn
        {
            set { Timed_Out.Write(true); }
        }
// Turn off the digital output for the timed switch type
        public bool TimedOutOff
        {
            set { Timed_Out.Write(false); }
        }
// timer to control how long the timed switch type digital output stays on
        void Leave_LED_ON_Timer_Tick(GT.Timer Leave_LED_ON_Timer)
        {
            TimedPreviousButtonState = false;
            Leave_LED_ON_Timer.Stop();
            TimedOutOff = true;
        }
//-----------------------------


        void Momentary_Button_Timer_Tick(GT.Timer Momementary_Button_Timer)
        {
            //Code to turn on/off digital output for latched control type
            if (Button == true)
            {
                MomentaryOutOn = true;
            }

            if (Button == false)
            {
                MomentaryOutOff = true;
            }
            //--------------------------------
        }


        void Latched_Button_Timer_Tick(GT.Timer Latched_Button_Timer)
        {
            //Code for the latching output
            
            if (Button == true)
            {
                Debug.Print("Previous Button: " + PreviousButton);

                for (int i = 0; i < 1; i++)
                {
                    if (PreviousButton == false /*&& !Latched_Button_Checker.IsRunning*/)
                    {
                        Latched_Out.Write(true);
                        //Latched_Button_Checker.Start();
                        break;
                    }
                  
                    //Thread.Sleep(100);

                    if (PreviousButton == true /* && !Latched_Button_Checker.IsRunning*/)
                    {
                        Latched_Out.Write(false);
                        break;
                        //Latched_Button_Checker.Start();
                    }
                    
                    //Thread.Sleep(100);
                }

            }
            PreviousButton = !PreviousButton;
        }
        void Latched_Button_Checker_Tick(GT.Timer Latched_Button_Checker)
        {
            Latched_Button_Checker.Stop();
        }

//sets the digital out puts for the momentary control
        public bool MomentaryOutOn
        {
            set { Momentary_Out.Write(true); }
        }
        public bool MomentaryOutOff
        {
            set { Momentary_Out.Write(false); }
        }
//------------------


    }
}