11 years, 8 months ago.

Switch connection problem

Hello, guys. I have problem on the switch connection and I have no idea why it is wrong. I connected an encoded switch to mbed: 3.3V (VOUT on mbed) to 10KΩ to position 1,2,4 on the switch; position 1,2,4 are connected to pin15,16,17 on mbed and common connected to GND on mbed

I only receive '0' for switch 1 to 7, but when I switch to position 0 and 8 the value is '7'. Could anyone help me? Great thanks! /media/uploads/shef_eee_1/2.jpg /media/uploads/shef_eee_1/1.jpg

My code is

#include "mbed.h"

Serial pc(USBTX, USBRX);

DigitalIn in1(p15);
DigitalIn in2(p16);
DigitalIn in4(p17);

int main() 
{
int inchoice;
inchoice=in4*4+in2*2+in1;

 printf("in Value is %d\n\r", inchoice);
 
    while(1) {
     switch (inchoice)
     {
     case 1:   
        led1 = 1;
        wait(0.2);
        led1 = 0;
        wait(0.2);
        break;
        
     case 2:
        led2 = 1;
        wait(0.2);
        led2 = 0;
        wait(0.2);     
     
     break;
     
     case 3:
        led3 = 1;
        wait(0.2);
        led3 = 0;
        wait(0.2);
     
     break;
     }
     
             }
             
             
}

1 Answer

11 years, 8 months ago.

Try changing the common connection to VOUT (pin 40) on the mbed, this would also let you use BusIn.

#include "mbed.h"

BusIn bus(p15, p16, p17);

int main()
{
    while(1) {
        printf("in Value is %d\n\r", bus.read());
        wait(1);
    }
}

Your circuit is like this

/media/uploads/stevep/c1.jpg

effectively pins 15, 16 & 17 are connected together, this is why you get either 0 or 7.

Accepted Answer