Main Site Documentation

Cannot get interrupt from Cobra II LDR0 and LDR1 buttons


#1

With this code using an InterruptPort in place, to react to the LDR0 button press, the interrupt handler function never gets called, and worse, pressing the LDR0 button stops communication on the Ethernet ENC20 module. But when I use an InputPort instead of an InterruptPort and read the port, I do see the false value, and there is no impact on the Ethernet communications.


 public partial class Program
 {
        /// <summary>
        ///This method is run when the mainboard is powered up or reset.    
        /// </summary>
        void ProgramStarted()
      {


   InterruptPort IntButtonLDR0 = new InterruptPort(GHI.Hardware.G120.Pin.P2_10, true,
                                                        Port.ResistorMode.PullUp,
                                                        Port.InterruptMode.InterruptEdgeBoth);
                IntButtonLDR0.OnInterrupt += new NativeEventHandler(IntButton_OnInterruptLDR0);
      }

     static void IntButton_OnInterruptLDR0(uint port, uint state, DateTime time)
        {
            Debug.Print("Button LDR0 Pressed");
        }

}


#2

You should use the gadgeteer version. Please see the button drive source for example.


#3

Or use a non gadgeteer project. I prefer console


#4

Did this yesterday, which works fine. Its just to test the built-in LED and buttons.

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Microsoft.SPOT.Hardware;
using GHI.Hardware.G120;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHINET = GHI.Premium.Net;

namespace GadgeteerApp4
{
    public partial class Program
    {
        bool lightLED;
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            InterruptPort ldr1 = new InterruptPort(Pin.P0_22, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            ldr1.OnInterrupt += ldr1_OnInterrupt;
            InterruptPort ldr2 = new InterruptPort(Pin.P2_10, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            ldr2.OnInterrupt += ldr2_OnInterrupt;

        }
        void ldr1_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            lightLED = !lightLED;
            Mainboard.SetDebugLED(lightLED);
        }
        void ldr2_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            Mainboard.SetDebugLED(true);
            Debug.Print("ldr2 pressed!");
        }
    }
}


#5

Does that mean it’s related to the Interrupt EdgeBoth setting?


#6

No, if you use that I would expect the event to trigger both when you press, and when you release the button…which could be relevant to some people?


#7

I am not sure what is wrong with the original code, so decided to post something that works… ::slight_smile:


#8

I tried it using InterruptEdgeLow set, did not help. The interrupt handler is not being called.


#9

so in your working project change your mode to EdgeBoth like the original post does and tell us the results? That’s the only visible difference I can see?!


#10

Did you try my program, there might be some mess with the new syntax for eventhandlers?

Else, I suggest to try a blank new project where you paste my program into.


#11

I copied and pasted your code into my project that uses the ENC28 Ethernet module,and a Gadgeteer Button module, but it did not help. I then created a new Gadgeteer Visual Studio project with no modules attached, pasted in your button interrupt code, and now it works in the new empty project.


#12

Brilliant, now take it from there, add the modules one by one and check if they work.