Serial_Assingement_Bluetooth_3s3_Original
Dependencies: mbed
main.cpp@0:804d8b0fe9f3, 2015-11-16 (annotated)
- Committer:
- NorNick
- Date:
- Mon Nov 16 16:56:34 2015 +0000
- Revision:
- 0:804d8b0fe9f3
Finish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NorNick | 0:804d8b0fe9f3 | 1 | /* |
NorNick | 0:804d8b0fe9f3 | 2 | #include "mbed.h" |
NorNick | 0:804d8b0fe9f3 | 3 | |
NorNick | 0:804d8b0fe9f3 | 4 | //------------------------------------ |
NorNick | 0:804d8b0fe9f3 | 5 | // Hyperterminal configuration |
NorNick | 0:804d8b0fe9f3 | 6 | // 9600 bauds, 8-bit data, no parity |
NorNick | 0:804d8b0fe9f3 | 7 | //------------------------------------ |
NorNick | 0:804d8b0fe9f3 | 8 | |
NorNick | 0:804d8b0fe9f3 | 9 | //Serial pc(SERIAL_TX, SERIAL_RX); |
NorNick | 0:804d8b0fe9f3 | 10 | |
NorNick | 0:804d8b0fe9f3 | 11 | Serial device(D8, D2); |
NorNick | 0:804d8b0fe9f3 | 12 | |
NorNick | 0:804d8b0fe9f3 | 13 | AnalogIn analog_value(A1); |
NorNick | 0:804d8b0fe9f3 | 14 | |
NorNick | 0:804d8b0fe9f3 | 15 | DigitalIn Sw(D3); |
NorNick | 0:804d8b0fe9f3 | 16 | |
NorNick | 0:804d8b0fe9f3 | 17 | BusOut ledSet1(D11, D10, D9, D4); //อย่าลืมกลับเลขด้วยเพราะมันต้องใช้ 7-Segment |
NorNick | 0:804d8b0fe9f3 | 18 | BusOut ledSet2(D15, D14, D13, D12); //อย่าลืมกลับเลขด้วยเพราะมันต้องใช้ 7-Segment |
NorNick | 0:804d8b0fe9f3 | 19 | |
NorNick | 0:804d8b0fe9f3 | 20 | |
NorNick | 0:804d8b0fe9f3 | 21 | |
NorNick | 0:804d8b0fe9f3 | 22 | int main() |
NorNick | 0:804d8b0fe9f3 | 23 | { |
NorNick | 0:804d8b0fe9f3 | 24 | |
NorNick | 0:804d8b0fe9f3 | 25 | uint8_t state_menu=0; |
NorNick | 0:804d8b0fe9f3 | 26 | //uint8_t state_show=0; |
NorNick | 0:804d8b0fe9f3 | 27 | //uint8_t state_exit =0; |
NorNick | 0:804d8b0fe9f3 | 28 | uint8_t data; |
NorNick | 0:804d8b0fe9f3 | 29 | char value[3]; |
NorNick | 0:804d8b0fe9f3 | 30 | |
NorNick | 0:804d8b0fe9f3 | 31 | ledSet1 = ledSet2 = 0; |
NorNick | 0:804d8b0fe9f3 | 32 | device.printf("Hello Nucleo !\n"); |
NorNick | 0:804d8b0fe9f3 | 33 | while(1) { |
NorNick | 0:804d8b0fe9f3 | 34 | |
NorNick | 0:804d8b0fe9f3 | 35 | if(state_menu == 0) { |
NorNick | 0:804d8b0fe9f3 | 36 | device.printf("Input Value Between 00-99 OR Select a To Read Analog Voltage. \n"); |
NorNick | 0:804d8b0fe9f3 | 37 | state_menu = 1; |
NorNick | 0:804d8b0fe9f3 | 38 | } |
NorNick | 0:804d8b0fe9f3 | 39 | if(device.readable()) { |
NorNick | 0:804d8b0fe9f3 | 40 | value[0] = data = device.getc(); |
NorNick | 0:804d8b0fe9f3 | 41 | device.printf("\n"); |
NorNick | 0:804d8b0fe9f3 | 42 | state_menu=0; |
NorNick | 0:804d8b0fe9f3 | 43 | |
NorNick | 0:804d8b0fe9f3 | 44 | if(data == '0' || data == '1' || data == '2' || data == '3' || data == '4'|| data == '5' || data == '6' || data == '7'|| data == '8'|| data == '9' ){ |
NorNick | 0:804d8b0fe9f3 | 45 | //value[0] = data; |
NorNick | 0:804d8b0fe9f3 | 46 | value[1] = device.getc(); |
NorNick | 0:804d8b0fe9f3 | 47 | if(value[1] == '0' || value[1] == '1' || value[1] == '2' || value[1] == '3' || value[1] == '4'|| value[1] == '5' || value[1] == '6' || value[1] == '7'|| value[1] == '8'|| value[1] == '9' ){ |
NorNick | 0:804d8b0fe9f3 | 48 | value[2]='\x0'; // add un 0 to end the c string |
NorNick | 0:804d8b0fe9f3 | 49 | device.printf("The Number is :: %s !\n",value); |
NorNick | 0:804d8b0fe9f3 | 50 | device.printf(" %d is desub.\n",atoi(value)/10); |
NorNick | 0:804d8b0fe9f3 | 51 | device.printf(" %d is Conveart.\n",atoi(value)%10); |
NorNick | 0:804d8b0fe9f3 | 52 | ledSet1 = atoi(value)/10; |
NorNick | 0:804d8b0fe9f3 | 53 | ledSet2 = atoi(value)%10; |
NorNick | 0:804d8b0fe9f3 | 54 | } |
NorNick | 0:804d8b0fe9f3 | 55 | else |
NorNick | 0:804d8b0fe9f3 | 56 | device.printf("plz select Input No 00-99 or a\n\n\n"); |
NorNick | 0:804d8b0fe9f3 | 57 | |
NorNick | 0:804d8b0fe9f3 | 58 | } |
NorNick | 0:804d8b0fe9f3 | 59 | else if(data == 'a'){ |
NorNick | 0:804d8b0fe9f3 | 60 | ledSet1 = ledSet2 = 0; |
NorNick | 0:804d8b0fe9f3 | 61 | state_menu=0; |
NorNick | 0:804d8b0fe9f3 | 62 | float value = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
NorNick | 0:804d8b0fe9f3 | 63 | value = (value * 3300)/1000; // Change the value to be in the 0 to 3300 range |
NorNick | 0:804d8b0fe9f3 | 64 | device.printf("Status Voltage :: %.2f volte. \n",value); |
NorNick | 0:804d8b0fe9f3 | 65 | } |
NorNick | 0:804d8b0fe9f3 | 66 | else |
NorNick | 0:804d8b0fe9f3 | 67 | device.printf("plz select Input No 00-99 or a\n\n\n"); |
NorNick | 0:804d8b0fe9f3 | 68 | |
NorNick | 0:804d8b0fe9f3 | 69 | } |
NorNick | 0:804d8b0fe9f3 | 70 | } |
NorNick | 0:804d8b0fe9f3 | 71 | } |
NorNick | 0:804d8b0fe9f3 | 72 | */ |
NorNick | 0:804d8b0fe9f3 | 73 | |
NorNick | 0:804d8b0fe9f3 | 74 | #include "mbed.h" |
NorNick | 0:804d8b0fe9f3 | 75 | |
NorNick | 0:804d8b0fe9f3 | 76 | //------------------------------------ |
NorNick | 0:804d8b0fe9f3 | 77 | // Hyperterminal configuration |
NorNick | 0:804d8b0fe9f3 | 78 | // 9600 bauds, 8-bit data, no parity |
NorNick | 0:804d8b0fe9f3 | 79 | //------------------------------------ |
NorNick | 0:804d8b0fe9f3 | 80 | |
NorNick | 0:804d8b0fe9f3 | 81 | //Serial device(SERIAL_TX, SERIAL_RX); |
NorNick | 0:804d8b0fe9f3 | 82 | |
NorNick | 0:804d8b0fe9f3 | 83 | Serial device(D8, D2); |
NorNick | 0:804d8b0fe9f3 | 84 | |
NorNick | 0:804d8b0fe9f3 | 85 | AnalogIn analog_value(A1); |
NorNick | 0:804d8b0fe9f3 | 86 | |
NorNick | 0:804d8b0fe9f3 | 87 | BusOut ledSet1(D11, D10, D9, D4); //อย่าลืมกลับเลขด้วยเพราะมันต้องใช้ 7-Segment |
NorNick | 0:804d8b0fe9f3 | 88 | BusOut ledSet2(D15, D14, D13, D12); //อย่าลืมกลับเลขด้วยเพราะมันต้องใช้ 7-Segment |
NorNick | 0:804d8b0fe9f3 | 89 | |
NorNick | 0:804d8b0fe9f3 | 90 | Timer t; |
NorNick | 0:804d8b0fe9f3 | 91 | |
NorNick | 0:804d8b0fe9f3 | 92 | int main() |
NorNick | 0:804d8b0fe9f3 | 93 | { |
NorNick | 0:804d8b0fe9f3 | 94 | |
NorNick | 0:804d8b0fe9f3 | 95 | uint8_t state_menu=0; |
NorNick | 0:804d8b0fe9f3 | 96 | char data[3]={"\x0"}; |
NorNick | 0:804d8b0fe9f3 | 97 | int i=0; |
NorNick | 0:804d8b0fe9f3 | 98 | |
NorNick | 0:804d8b0fe9f3 | 99 | ledSet1 = ledSet2 = 0; |
NorNick | 0:804d8b0fe9f3 | 100 | while(1) { |
NorNick | 0:804d8b0fe9f3 | 101 | t.reset(); |
NorNick | 0:804d8b0fe9f3 | 102 | t.start(); |
NorNick | 0:804d8b0fe9f3 | 103 | i=0; |
NorNick | 0:804d8b0fe9f3 | 104 | if(state_menu == 0) { |
NorNick | 0:804d8b0fe9f3 | 105 | device.printf("Input Value Between 00-99 OR Select a To Read Analog Voltage. \n"); |
NorNick | 0:804d8b0fe9f3 | 106 | state_menu = 1; |
NorNick | 0:804d8b0fe9f3 | 107 | } |
NorNick | 0:804d8b0fe9f3 | 108 | while(i<2){ |
NorNick | 0:804d8b0fe9f3 | 109 | if(t.read_ms() < 1200){ |
NorNick | 0:804d8b0fe9f3 | 110 | if (device.readable()) { |
NorNick | 0:804d8b0fe9f3 | 111 | data[i] = device.getc(); |
NorNick | 0:804d8b0fe9f3 | 112 | i++; |
NorNick | 0:804d8b0fe9f3 | 113 | } |
NorNick | 0:804d8b0fe9f3 | 114 | } |
NorNick | 0:804d8b0fe9f3 | 115 | else |
NorNick | 0:804d8b0fe9f3 | 116 | break; |
NorNick | 0:804d8b0fe9f3 | 117 | } |
NorNick | 0:804d8b0fe9f3 | 118 | data[++i]='\0'; // add un 0 to end the c string |
NorNick | 0:804d8b0fe9f3 | 119 | if(strlen(data)==0){ |
NorNick | 0:804d8b0fe9f3 | 120 | float value = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
NorNick | 0:804d8b0fe9f3 | 121 | value = (value * 3.3); // Change the value to be in the 0 to 3300 range |
NorNick | 0:804d8b0fe9f3 | 122 | device.printf("Status Voltage :: %.2f volte. \n",value); |
NorNick | 0:804d8b0fe9f3 | 123 | } |
NorNick | 0:804d8b0fe9f3 | 124 | else if(( data[0] == '0' || data[0] == '1' || data[0] == '2' || data[0] == '3' || data[0] == '4'|| data[0] == '5' || data[0] == '6' || data[0] == '7'|| data[0] == '8'|| data[0] == '9' ) |
NorNick | 0:804d8b0fe9f3 | 125 | && (data[1] == '0' || data[1] == '1' || data[1] == '2' || data[1] == '3' || data[1] == '4'|| data[1] == '5' || data[1] == '6' || data[1] == '7'|| data[1] == '8'|| data[1] == '9' || data[1] == '\x0' )){ |
NorNick | 0:804d8b0fe9f3 | 126 | device.printf("The Number is :: %d !\n",atoi(data)); |
NorNick | 0:804d8b0fe9f3 | 127 | ledSet1 = atoi(data)/10; |
NorNick | 0:804d8b0fe9f3 | 128 | ledSet2 = atoi(data)%10; |
NorNick | 0:804d8b0fe9f3 | 129 | } |
NorNick | 0:804d8b0fe9f3 | 130 | else{ |
NorNick | 0:804d8b0fe9f3 | 131 | ledSet1 = ledSet2 = 0; |
NorNick | 0:804d8b0fe9f3 | 132 | device.printf("Cannot is condition.\n\n\n"); |
NorNick | 0:804d8b0fe9f3 | 133 | } |
NorNick | 0:804d8b0fe9f3 | 134 | state_menu=0; |
NorNick | 0:804d8b0fe9f3 | 135 | strcpy(data,"\x0"); |
NorNick | 0:804d8b0fe9f3 | 136 | } |
NorNick | 0:804d8b0fe9f3 | 137 | } |
NorNick | 0:804d8b0fe9f3 | 138 |