Belgium
Asked — Edited

Reading Switch (Digital Input)

Hi DJ,

I'd like to read the position of a switch. So, when a button is pushed, I get a Boolean that becomes true (or an integer that becomes 1). As soon as the button is released the values changes back to 'false' or 0.

My line of code so far goes like this :

ImpactSensLeft = (EzB_Connect1.EZB.Digital.GetDigitalPort(EZ_B.Digital.DigitalPortEnum.D15))

But this always returns 'true' doesn't really matter if the button gets pushed or whatever. What am I missing here ? Where can I put this code for optimal results (form Load event ?? etc...). I want to read out and interact with the switch all the time while other code is processed. The switch acts as an emergency switch that shuts down the motors of my robot as soon as the switch is pushed. When released, the motors start running again. I'm really looking forward to this.

Best regards,

PhG


ARC Pro

Upgrade to ARC Pro

Join the ARC Pro community and gain access to a wealth of resources and support, ensuring your robot's success.

PRO
Synthiam
#1  

That's good code. That will work just great.

That code will return the logic resut of the digital I/O pin D15. If > 0.1 volts is on the signal pin of D15, it will return true; otherwise false.

Sounds like your switch may be floating and picking up static. The wire is probably acting like a giant antenna.

My advice is to put a 10K resistor between GND and the Digital I/O pin D15. That way it will hold the pin LOW until the switch is shorted. That is called a PULL-DOWN RESISTOR

Belgium
#2  

Yeah I know about the pull-down res. As a matter of fact, I have 2 switches to try different things. And one switch is indeed with a 10K res. I always have my 10K res. close at hand;) Anyway, both switches work fine with the ARC SDK (control, read digital). It just doesn't work when I put the same code in VB. I'm not a VB guru so I wonder where I go wrong. I have use this code within a Click event of a switch on my VB design form, but also in the form Load Event but that doesn't matter. Where can I place the code best so that it reacts immediately when the EZ-B switch is pressed ?

PRO
Synthiam
#3  

Oh, you can't use it on a Form Load event because the connection hasn't been established yet. So it's just returning some value.

You need to make a connection first, then have some event that queries the port. Put that code inside of a button or a timer. If you use a timer, don't query too quick.

Belgium
#4  

Hi DJ,

Weird things going on over here....

First let me explain. I'm testing the EZ-B at 2 computers (not at the same time) :

  • laptop with Windows 7, Visual Studio 2008
  • desktop with Windows XP, Visual Studio 2005

This is my code in a Timer_Tick event :

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    switch = (EzB_Connect1.EZB.Digital.GetDigitalPort(EZ_B.Digital.DigitalPortEnum.D15))

    If switch = True Then
        Label1.Text = "H I T"
    Else
        Label1.Text = "S A F E"
    End If
End Sub

Of course the timer is enabled and the interval is 100ms (I tried all kinds of values). On the desktop I get no errors or messages but it doens't work at all. On the laptop however, I have a error message that the 'Reference Component EZ-B could not be found'.... but it works ! When I push the switch I get immediately result. However, when I release the switch it takes about 3 minutes to change the boolean back to 'False' (with Timer interval at 500ms and sometimes even less). What is happening here ?

Please some help/advice because I'm speding many hours on this and I'm getting desperate.

PhG

PRO
Synthiam
#5  

Zip up your project and upload it here. Use the Attach File option to upload the ZIP. I don't have the time to teach you to program - but i can give you this example file. My advice is to follow some basic programming courses online, or see if there are any offered at your schools.

Here is my tutorial sample: VB-Tutorial02-GetDigital.zip:)

Belgium
#6  

I have a bachelors degree in computers/programming and handle programming reasonably well. My programming skills do have a layer of dust on top, but I'm catching up and resuming quickly. It's the EZ-B that environment that puzzles me. Anyway, I'll prepare a zip folder and upload it by next week.

PRO
Synthiam
#7  

Did you try my tutorial zip that i posted above?

Belgium
#8  

Hi DJ,

Yes I did. I get exactely the same result. So I'm looking into VB right now. I made screenshot(s) and post this with some comments asap.

Thnxs for asking !

Best regards,

PhG