Allan V
Posts: 1
Joined: Fri Dec 20, 2019 9:45 pm

Reading a GPIO Switch in C language

Fri Dec 20, 2019 9:54 pm

I have this code for incrementing a counter each time a GPIO pin is HIGH. I am interested in each time GPIO pin 7 is HIGH:

Code: Select all

if (7)
  {
      counter += 1;
  }
else
  {
      counter = counter;
  }
This is written in C, and works for my application.

User avatar
Paeryn
Posts: 2968
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Reading a GPIO Switch in C language

Sat Dec 21, 2019 12:00 am

Allan V wrote:
Fri Dec 20, 2019 9:54 pm
I have this code for incrementing a counter each time a GPIO pin is HIGH. I am interested in each time GPIO pin 7 is HIGH:

if (7)
{
counter += 1;
}
else
{
counter = counter;
}

This is written in C, and works for my application.
As with the OP, this code will always increment counter as 7 (being as it is not zero) will always evaluate to true, you have to actually test the value of reading a GPIO otherwise you are just testing the mathematical property that the number 7 is not the number 0. And well done on necroing a 4 year old thread.
<edited due to split from original thread>
Last edited by Paeryn on Sat Dec 21, 2019 2:26 pm, edited 1 time in total.
She who travels light — forgot something.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13107
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Reading a GPIO Switch in C language

Sat Dec 21, 2019 1:55 pm

split off, and moved to C/C++

cmrincon
Posts: 44
Joined: Thu May 24, 2018 7:39 pm

Re: Reading a GPIO Switch in C language

Tue Dec 24, 2019 7:01 pm

Use wiring pi library

You can look for examples here: http://wiringpi.com/examples/

fbe
Posts: 642
Joined: Thu Aug 17, 2017 9:08 pm

Re: Reading a GPIO Switch in C language

Tue Dec 24, 2019 10:18 pm

Avoid the wiringPi library. It is deprecated http://wiringpi.com/wiringpi-deprecated/ :(

dsyleixa123
Posts: 839
Joined: Mon Jun 11, 2018 11:22 am

Re: Reading a GPIO Switch in C language

Wed Dec 25, 2019 10:19 am

wiringPi is still available, and due to it's user-friendly Arduino-like functions it's still undefeated....

(hopefuly somebody will take the efforts to maintaining it further on!)

Heater
Posts: 15978
Joined: Tue Jul 17, 2012 3:02 pm

Re: Reading a GPIO Switch in C language

Wed Dec 25, 2019 2:07 pm

I don't think wiringpi is deprecated at all. Despite what Gordon says.

It's alive and well here:
https://github.com/search?q=wiringpi
https://github.com/WiringPi/WiringPi

There is even discussion on discord with 500 participants.

But if one just want to read and write GPIO from C one does need wiring pi at all. Just go to the registers directly: https://elinux.org/RPi_GPIO_Code_Sample ... ter_access
Memory in C++ is a leaky abstraction .

Return to “C/C++”