mdperry
Posts: 13
Joined: Mon Dec 31, 2012 10:18 pm

Detecting a button press with Lazarus

Fri Feb 08, 2013 5:28 am

I am working on a simple project to detect a button press in a gui application in Lazarus. I have the wiringPi library and Lazarus wrapper set up and I know they work because I can turn on and off a led programmatically. The button circuit is very simple. I have a wire from GPIO pin 3 (wiringPi pin 8) running to the switch and then another wire on the other side of the switch running to a ground pin. I have tested the continuity with a voltmeter and it works as anticipated. I am trying to detect the button push with the following code in my Lazarus project:

Code: Select all

pinMode (P3, INPUT);

while true do
begin
  if digitalRead(P3) = 0 then
  begin
    ShowMessage('Did you press a button?');
    exit;
 end;
end;
When I run the code digitalRead returns a 1 no matter what the state of the button. I know almost nothing about working with GPIO so I am sure it is something simple but I have been stuck on this for a couple of days.

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Detecting a button press with Lazarus

Fri Feb 08, 2013 9:19 am

mdperry wrote:I am working on a simple project to detect a button press in a gui application in Lazarus. I have the wiringPi library and Lazarus wrapper set up and I know they work because I can turn on and off a led programmatically. The button circuit is very simple. I have a wire from GPIO pin 3 (wiringPi pin 8) running to the switch and then another wire on the other side of the switch running to a ground pin. I have tested the continuity with a voltmeter and it works as anticipated. I am trying to detect the button push with the following code in my Lazarus project:

Code: Select all

pinMode (P3, INPUT);

while true do
begin
  if digitalRead(P3) = 0 then
  begin
    ShowMessage('Did you press a button?');
    exit;
 end;
end;
When I run the code digitalRead returns a 1 no matter what the state of the button. I know almost nothing about working with GPIO so I am sure it is something simple but I have been stuck on this for a couple of days.
I've no idea how Lazarus/FP is initialising wiringPi - does it set it up for you, or do you have to call some sort of setup code?

In my C world, there are 2 setup functions - wiringPiSetup() and wiringPiSetupGpio() - the former uses wiringPi's own numbering scheme, and the latter native BCM_GPIO numbers.

If Lazarus is using the latter mechanism, then pin 3 is the one you want (one of the I2C pins), however that will only work on a Rev 2 Pi - if you're on a Rev 1, then you need to use Pin 1.

If Lazarus is initialised in wiringPi mode, then it's pin 9 regardless of the board rev.

Rev 1:

BCM_GPIO 0 -> wiringPi 8
1 -> 9

Rev 2:

BCM_GPIO 2 -> wiringPi 8
3 -> 9

So it's important to know how wiringPi has been initialised in the first place.

-Gordon
--
Gordons projects: https://projects.drogon.net/

mdperry
Posts: 13
Joined: Mon Dec 31, 2012 10:18 pm

Re: Detecting a button press with Lazarus

Fri Feb 08, 2013 11:13 am

I forgot to add that bit of code which is called when my form is initialized. Here is what I am using:

Code: Select all

if wiringPiSetup <> -1 then
  //do some setup stuff
else
  ShowMessage ('Total Nuclear Meltdown');
I have the 512MB version Pi with 2 USB ports and ethernet. I think that means this is the Rev 2 board.

I suspect I am getting confused with the pin numbering scheme. The Lazarus wrapper for wiringPi uses the following:

Code: Select all

// Px represents to physical pin on the RaspberryPi P1 connector
    P3  = 8;
    P5  = 9;
    P7  = 7;
    P8  = 15;
    P10 = 16;
    P11 = 0;
    P12 = 1;
    P13 = 2;
    P15 = 3;
    P16 = 4;
    P18 = 5;
    P19 = 12;
    P21 = 13;
    P22 = 6;
    P23 = 14;
    P24 = 10;
    P26 = 11;
If I understand you correctly then I should be using P5 instead of P3. Is that correct?

I really appreciate your help.

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Detecting a button press with Lazarus

Fri Feb 08, 2013 11:24 am

mdperry wrote:I forgot to add that bit of code which is called when my form is initialized. Here is what I am using:

If I understand you correctly then I should be using P5 instead of P3. Is that correct?

I really appreciate your help.
It looks like it. I think the "P" numbers there refer to the physical pin numbers on the GPIO connector. Have a look at this diagram: https://projects.drogon.net/raspberry-pi/wiringpi/pins/ and look a the grey bar in the middle - those are the physical pin numbers - which I think the P numbers are refering to.

-Gordon
--
Gordons projects: https://projects.drogon.net/

mdperry
Posts: 13
Joined: Mon Dec 31, 2012 10:18 pm

Re: Detecting a button press with Lazarus

Sat Feb 09, 2013 2:53 am

OK, so I think I have the pin issue sorted out thanks to Gordon's help. If anyone has a simple but clear explanation (or a link) about why this button has to be wired to certain pins only, I am all ears.

Anyway after wiring it up correctly the digitalRead call in my code still returns a 1 when the switch is shorted to ground. I used the gpio utility to read the pin state and it is 0 when shorted and 1 when open so I know the hardware is working. The problem has to be with my code in Lazarus. It is very simple so I have no idea why it might be failing. I downloaded and built the latest version of wiringPi (which looks like it has some significant changes). It built fine but now when I try to call the wiringPiSetup function in my Lazarus project I get a segmentation fault.

mdperry
Posts: 13
Joined: Mon Dec 31, 2012 10:18 pm

Re: Detecting a button press with Lazarus

Sat Feb 09, 2013 3:47 am

I finally got it! I realized I was using a wrapper for a very old version of wiringPi. I ran the wiringPi.h file through the h2pas tool and replaced the wrapper I was using with the generated file. After a few tweaks and some judicious commenting out of lines that would not compile, it works!!!

(yes, I was a liberal arts major)

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Detecting a button press with Lazarus

Sat Feb 09, 2013 10:49 am

mdperry wrote:I finally got it! I realized I was using a wrapper for a very old version of wiringPi. I ran the wiringPi.h file through the h2pas tool and replaced the wrapper I was using with the generated file. After a few tweaks and some judicious commenting out of lines that would not compile, it works!!!

(yes, I was a liberal arts major)
Glad you're going now.

One day I might even install FP+Lazarus...

-Gordon
--
Gordons projects: https://projects.drogon.net/

Nexus12
Posts: 10
Joined: Sat Jan 05, 2013 10:37 pm

Re: Detecting a button press with Lazarus

Sat Feb 09, 2013 2:14 pm

mdperry wrote:I finally got it! I realized I was using a wrapper for a very old version of wiringPi. I ran the wiringPi.h file through the h2pas tool and replaced the wrapper I was using with the generated file. After a few tweaks and some judicious commenting out of lines that would not compile, it works!!!

(yes, I was a liberal arts major)
Are you using the Rasbian distro - I've been trying for a while to get wiringPi to work with Lazarus on rasbian.

Just don't know if it possible

I have got Lazarus to work with FP, to control some outputs, but seems slow.

mdperry
Posts: 13
Joined: Mon Dec 31, 2012 10:18 pm

Re: Detecting a button press with Lazarus

Sun Feb 10, 2013 1:51 am

I am using Raspbian. It definitely took some tweaking to get things working. The one big one is that you have to run the Lazarus with elevated privileges or it will not work. I do that by launching Lazarus from a terminal:

Code: Select all

sudo lazarus-ide

.

Nexus12
Posts: 10
Joined: Sat Jan 05, 2013 10:37 pm

Re: Detecting a button press with Lazarus

Sun Feb 10, 2013 11:07 pm

mdperry wrote:I am using Raspbian. It definitely took some tweaking to get things working. The one big one is that you have to run the Lazarus with elevated privileges or it will not work. I do that by launching Lazarus from a terminal:

Code: Select all

sudo lazarus-ide

.
Thanks I'll give that a try

Ph1lj2
Posts: 35
Joined: Sat Oct 20, 2012 9:21 pm

Re: Detecting a button press with Lazarus

Tue Feb 12, 2013 5:50 pm

mdperry wrote:I finally got it! I realized I was using a wrapper for a very old version of wiringPi. I ran the wiringPi.h file through the h2pas tool and replaced the wrapper I was using with the generated file. After a few tweaks and some judicious commenting out of lines that would not compile, it works!!!

(yes, I was a liberal arts major)
Hi

Where did you find the h2pas tool, I'm having the same issue, I can get output to respond but see no response when reading an input on P3. Would it be possible to send me the updated wrapper ?

I'm using v2

hope you can help

mdperry
Posts: 13
Joined: Mon Dec 31, 2012 10:18 pm

Re: Detecting a button press with Lazarus

Wed Feb 13, 2013 4:25 am

I zipped up a copy of my project and put it at http://www.kymark.com/laz-gpio-test.tar.gz. I was not able to attach it to this post for some reason. In any case, I hope it helps and let me know if you have any questions. I am by no means an expert but I have spent a lot of time trying to make it work (my SD card was corrupted for the second time yesterday so I had to reinstall, recompile, reconfigure, and rewrite everything again). This is the second SD fail in the past few weeks. Frustrating but at least my results are repeatable.

Ph1lj2
Posts: 35
Joined: Sat Oct 20, 2012 9:21 pm

Re: Detecting a button press with Lazarus

Wed Feb 13, 2013 1:26 pm

mdperry wrote:I zipped up a copy of my project and put it at http://www.kymark.com/laz-gpio-test.tar.gz. I was not able to attach it to this post for some reason. In any case, I hope it helps and let me know if you have any questions. I am by no means an expert but I have spent a lot of time trying to make it work (my SD card was corrupted for the second time yesterday so I had to reinstall, recompile, reconfigure, and rewrite everything again). This is the second SD fail in the past few weeks. Frustrating but at least my results are repeatable.
Thanks, I'll give it a try. Re SD-card corruption - I know the feeling, seems to be a regular thing for me, no matter what SD card I use

Return to “Other programming languages”