I have two switches (buttons). Red is connected to physical pin 38 (BCM 20) and green to physical pin 40 (BCM 21). I call wiringPiSetupGPIO()
it returns 0. When I use
pullUpDnControl(redSwitch, PUD_UP); // redSwitch = 20
pullUpDnControl(greenSwitch,PUD_UP); // greenSwitch = 21;
and then print
digitalRead(redSwitch), digitalRead(greenSwitch)
red switch is always 0. The greenSwitch prints, 0 when it is pressed, 1 otherwise.
I could have sworn I had this working on a Raspberry Pi at home. It might be a problem with the ribbon, but does anyone have another idea as to why this might be misbehaving?
As per a request, the code is below. I am running jumpers directly out of a ribbon and then closing them for testing purposes, but my output is always:
red: 0, green: 1
unless I connect pin 21 to ground, when I get:
red: 0, green: 0
red should be high (1) to start, but it is not.
Note that if I switch to pin 7, redSwitch works as advertised ... what's so special about pin 20?
const int redLED = 14;
const int yellowLED = 15;
const int greenLED = 18;
const int blueLED = 24; // 23 didn't work
const int redSwitch = 20; // this doesn't work
const int greenSwitch = 21; // this does
const int dht22 = 16;
const int buzzer = 25;
const int loopDelay = 125; // to avoid reporting multiple switch pushes
const int note = 880;
void setup();
void loop();
void switchDetection();
void flashAll();
void setup(){
int status = wiringPiSetupGpio();
printf("Status is %d\n",status);
pullUpDnControl(redSwitch, PUD_UP);
pullUpDnControl(greenSwitch, PUD_UP);
status = softToneCreate(buzzer);
printf("Soft tone create status: %d\n",status);
}
void loop(){
while(1){
printf("red: %d, green: %d\n",digitalRead(redSwitch),digitalRead(greenSwitch));
}
}