ccurran689
Posts: 13
Joined: Sat Sep 26, 2015 9:55 am

Reading a GPIO Switch in C language

Tue Sep 29, 2015 11:25 am

I know I must be doing something stupid here but I cant seem to find a solution, I am trying to have a simple switch in my circuit. I have made a simple switch programme to see where my issue is with no avail;

#include <stdio.h>
#include <wiringPi.h>

int main (void)
{
wiringPisetup () ;
pinMode (4, INPUT) ;
if (4==1) {
printf ("button pressed");
}
return 0;
}

I have one wire connected to one side of button going to gpio 23 ( WiringPi pin 4) and the other going to ground, nothing is happening.

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Switch in C language

Tue Sep 29, 2015 11:33 am

ccurran689 wrote:

Code: Select all

if (4==1) {
What are you trying to do here - test if 4 equals 1? Well, it won't ever (except in some really weird maths;) ).
Shouldn't you have something to read the pin value in there?
Something like

Code: Select all

if ( didgitalRead(4) == 1 ) {

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Switch in C language

Tue Sep 29, 2015 12:06 pm

Note that because there is a "switch" statement in the C language, your thread title is very ambiguous. !
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Switch in C language

Tue Sep 29, 2015 12:49 pm

PeterO wrote:Note that because there is a "switch" statement in the C language, your thread title is very ambiguous. !
PeterO
Did you report the post? There is an option to specify that the title is ambiguous or misleading.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Switch in C language

Tue Sep 29, 2015 12:54 pm

Anyway, back to the problem at hand.

OP, do you have a pullup attached to your button? If not, you should turn on the GPIO pullup in your code:
https://projects.drogon.net/raspberry-p ... functions/

Also, double-check the pin number against the physical pin.

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Switch in C language

Tue Sep 29, 2015 12:55 pm

ame wrote:
PeterO wrote:Note that because there is a "switch" statement in the C language, your thread title is very ambiguous. !
PeterO
Did you report the post? There is an option to specify that the title is ambiguous or misleading.
Since rpdom had provided the answer I didn't think it was worth reporting but was worth pointing out it was ambiguous.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
GerardWassink
Posts: 103
Joined: Sun Aug 02, 2015 5:57 pm
Location: Ulrum (Gr), Netherlands
Contact: Website

Re: Switch in C language

Tue Sep 29, 2015 1:01 pm

ame wrote:Anyway, back to the problem at hand.

OP, do you have a pullup attached to your button? If not, you should turn on the GPIO pullup in your code:
https://projects.drogon.net/raspberry-p ... functions/

Also, double-check the pin number against the physical pin.
Neither of these actions will make (4 == 1)... :D

And in any case, the value of the pin should first be read, otherwise you can test all you want and it will never be what you expect.

And remember:

"Computers do what you tell them, not necessarily what you expect..."
---------ooooO----- \\\\\|/// -----Oooo--------
Hacker on ELF-II, ZX80/1, Commodore 64, 8080, x86,
IBM 370 family mainframes
Machine code! Assembly! C good second.
Running Pi's with Hercules and S/370 OS's

-------------oooO-----------Oooo-------------

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Switch in C language

Tue Sep 29, 2015 1:03 pm

GerardWassink wrote:
ame wrote:Anyway, back to the problem at hand.

OP, do you have a pullup attached to your button? If not, you should turn on the GPIO pullup in your code:
https://projects.drogon.net/raspberry-p ... functions/

Also, double-check the pin number against the physical pin.
Neither of these actions will make (4 == 1)... :D

And in any case, the value of the pin should first be read, otherwise you can test all you want and it will never be what you expect.

And remember:

"Computers do what you tell them, not necessarily what you expect..."
Yeah. My bad. Oh well.

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Switch in C language

Tue Sep 29, 2015 1:24 pm

GerardWassink wrote:
ame wrote:Anyway, back to the problem at hand.

OP, do you have a pullup attached to your button? If not, you should turn on the GPIO pullup in your code:
https://projects.drogon.net/raspberry-p ... functions/

Also, double-check the pin number against the physical pin.
Neither of these actions will make (4 == 1)... :D
However both things are useful advice and need to be correct.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ccurran689
Posts: 13
Joined: Sat Sep 26, 2015 9:55 am

Re: Reading a GPIO Switch in C language

Tue Sep 29, 2015 6:45 pm

Thanks very much got it all sorted, I had tried that before but must of mucked it up somehow. Doing an engineering project and have been left a bit blind for the code sorry about the silly questions!

Return to “C/C++”