Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
00001 #include "mbed.h" 00002 Serial pc(USBTX, USBRX); // tx, rx 00003 00004 DigitalIn switch1(D13); // input switch 00005 DigitalOut led(D12); // light on. 00006 00007 AnalogIn p1(A0); 00008 AnalogIn p2(A1); 00009 AnalogIn p3(A2); 00010 AnalogIn p4(A3); 00011 AnalogIn p5(A4); 00012 00013 int sw1 = 0; // input variable 00014 00015 int p1s=0; // individual input with switches 00016 int p2s=0; 00017 int p3s=0; 00018 int p4s=0; 00019 int p5s=0; 00020 00021 float ps[] = {0, 0, 0, 0, 0}; 00022 int psl[] = {0,0,0,0,0}; 00023 00024 int binary = 0; // binary input 00025 int ascii_c = 0; // capital 00026 int ascii_l = 0; // lower letter 00027 bool CAPS =0;// Caps Lock history variable. 00028 00029 bool lastButton = 0; 00030 bool currentButton = 0; 00031 bool current =0; 00032 00033 bool debounce(bool last) // function for debounce 00034 { 00035 sw1 = switch1.read(); // reads current switch status 00036 current = sw1; 00037 if (last != current) 00038 { 00039 wait(0.01); //eliminating bouncing with delayx 00040 current = sw1; 00041 } 00042 return current; 00043 } 00044 00045 int encode(int reading) 00046 { 00047 ps[0] = p1.read(); 00048 ps[1] = p2.read(); 00049 ps[2] = p3.read(); 00050 ps[3] = p4.read(); 00051 ps[4] = p5.read(); 00052 for(int i =0;i<5;i++) 00053 { 00054 if(ps[i]>0.005) 00055 { 00056 psl[i] =1; 00057 } 00058 else 00059 { 00060 psl[i] =0; 00061 } 00062 } 00063 binary = psl[4]*16 + psl[3]*8 + psl[2]*4+psl[1]*2+psl[0]*1; 00064 return binary; 00065 } 00066 00067 int main(){ 00068 00069 while(1) 00070 { 00071 led =1; // led on! 00072 00073 currentButton = debounce(lastButton); 00074 if (lastButton ==0 && currentButton ==1) 00075 { 00076 encode(binary); 00077 if(binary<=25)// 0-25 : A-Z. 00078 { 00079 switch(CAPS) 00080 { 00081 case 0 : // Capital 00082 ascii_c = binary + 65; 00083 pc.printf("%c",ascii_c); 00084 break ; 00085 00086 case 1 : //lower case 00087 ascii_c = binary +97; 00088 pc.printf("%c",ascii_c); 00089 break; 00090 00091 } 00092 } 00093 //else if for CAPS 00094 else 00095 { 00096 switch(binary) 00097 { 00098 case 26 : 00099 ascii_c = binary + 20; // prints out '.' 00100 pc.printf("%c",ascii_c); 00101 break; 00102 00103 case 27 : 00104 ascii_c = binary + 36; // prints out '?' 00105 pc.printf("%c",ascii_c); 00106 break; 00107 00108 case 28 : 00109 ascii_c = binary + 4; // prints out 'space' 00110 pc.printf("%c",ascii_c); 00111 break; 00112 00113 case 29 : 00114 ascii_c = binary -21; // backspace 00115 pc.printf("%c",ascii_c); 00116 break; 00117 00118 case 30 : 00119 ascii_c = binary - 17; // prints out 'enter' 00120 pc.printf("\n%c",ascii_c); 00121 break; 00122 00123 case 31 : //CAPS Lock 00124 CAPS =! CAPS; 00125 break; 00126 00127 } 00128 } 00129 00130 } 00131 lastButton = currentButton; 00132 00133 } 00134 }
Generated on Tue Jul 12 2022 19:06:34 by
1.7.2