Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 11 months ago.
mode(pullup)
I have ten switches which I want send their state throught serial communication. The idea is to put the a value when the switch value is high and display them together through addition process. The problem is when I put 4 switches nothing comes out on serial. Please help me. Thank you in advance.
DigitalIn button1(D7); DigitalIn button2(D6); DigitalIn button3(D2); DigitalIn button4(D3); int a,b,c,d,e,f,g,h,i,j,k; int a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1; int sum; int main() { button1.mode(PullUp); button2.mode(PullUp); button3.mode(PullUp); button4.mode(PullUp); while(1) { if(!button1) //Detect Switch Press { a=1; //Light LED wait(0.03); //Debounce while(!button1); //Wait for Key release } else{ a=0;} if(!button2) //Detect Switch Press { b=10; //Light LED wait(0.03); //Debounce while(!button2); //Wait for Key release } else{ b=0;} if(!button3) //Detect Switch Press { c=100; //Light LED wait(0.03); //Debounce while(!button3); //Wait for Key release } else{ c=0;} if(!button4) //Detect Switch Press { d=1000; //Light LED wait(0.03); //Debounce while(!button4); //Wait for Key release } else{ d=0;} sum = a + b + c + d; printf("%d\r\n",sum); wait(0.01); } }
1 Answer
6 years, 11 months ago.
For sanity checking, can you try this:
int main() { button1.mode(PullUp); button2.mode(PullUp); button3.mode(PullUp); button4.mode(PullUp); while (1) { printf("%d %d %d %d\r\n", button1.read(), button2.read(), button3.read(), button4.read()); wait_ms(500); } }
And see if the buttons have the right values, and if it keeps printing? If so, your wiring is correct, and your program probably hangs at one of the while() loops you've put in.
Thank you sir, you are right about the program hangs at one of the while() loops. I made a simpler program to show the state of the 10 switches/buttons based on your answer.
10 switches state print
#include "mbed.h" #include "math.h" InterruptIn button1(D7), button2(D6), button3(D4), button4(D3), button5(D5), button6(D10),button7(D11), button8(D12), button9(D13), button10(D14); int a,b,c,d,e,f,g,h,i,j,k; int sum; int main() { button1.mode(PullUp); button2.mode(PullUp); button3.mode(PullUp); button4.mode(PullUp); button5.mode(PullUp); button6.mode(PullUp); button7.mode(PullUp); button8.mode(PullUp); button9.mode(PullUp); button10.mode(PullUp); while (1) { if(!button1 || !button2 || !button3 || !button4 || !button5 || !button6 || !button7 || !button8 || !button9 || !button10){ printf("%d %d %d %d %d %d %d %d %d %d\r\n", button1.read(), button2.read(), button3.read(), button4.read(), button5.read(), button6.read(),button7.read(), button8.read(), button9.read(), button10.read()); wait(0.01); //Debounce while(!button1 || !button2 || !button3 || !button4 || !button5 || !button6); //Wait for Key release } else{printf("1 1 1 1 1 1 1 1 1 1\r\n");} wait(0.01); } }