[wiki:wade/embedded/arduino 回到 Arduino] {{{ #!c /* * Arduino 由 XBee 接收資料。 * 輸入 1:顯示 ARDUINO。 * 2:控制 buzzer 出現音樂。 * 0:顯示 WELCOME:。 * a-k:產生 DO RE MI... DO 聲音。 * /* * 08 07 06 05 04 03 02 01 * * * 74HC595 PIN 09 10 11 12 13 14 15 16 * SH ST DS * 棕 白 紫 * Arduino PIN 3 2 4 * */ #include "pitches.h" #define node_size 8 #define buzzer_pin 11 // notes in the melody: int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; // note durations: 4 = quarter note, 8 = eighth note, etc.: int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 }; // Pin connected to ST_CP of 74HC595 for scanning int scan_latch_pin = 7; // 白 // Pin connected to SH_CP of 74HC595 for scanning int scan_clock_pin = 8; // 藍 // Pin connected to DS of 74HC595 for scanning int scan_data_pin = 9; // 棕 // display array size #define display_array_size 168 // how many led matrix do you #define led_matrix_number 4 // ascii 5x7 dot font #define data_null 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // null char #define data_ascii_minus 0x08, 0x08, 0x08, 0x08, 0x08, 0x00 // - #define data_ascii_dot 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 // . #define data_ascii_A 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 // A 65 #define data_ascii_B 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 // B #define data_ascii_C 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 // C #define data_ascii_D 0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00 // D #define data_ascii_E 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 // E #define data_ascii_F 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 // F 70 #define data_ascii_G 0x3E, 0x41, 0x41, 0x51, 0x73, 0x00 // G #define data_ascii_H 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00 // H #define data_ascii_I 0x00, 0x41, 0x7F, 0x41, 0x00, 0x00 // I #define data_ascii_J 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00 // J #define data_ascii_K 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00 // K 75 #define data_ascii_L 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00 // L #define data_ascii_M 0x7F, 0x02, 0x1C, 0x02, 0x7F, 0x00 // M #define data_ascii_N 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00 // N #define data_ascii_O 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00 // O #define data_ascii_P 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00 // P 80 #define data_ascii_Q 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00 // Q 81 #define data_ascii_R 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00 // R 82 #define data_ascii_S 0x26, 0x49, 0x49, 0x49, 0x32, 0x00 // S 83 #define data_ascii_T 0x03, 0x01, 0x7F, 0x01, 0x03, 0x00 // T 84 #define data_ascii_U 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00 // U 85 #define data_ascii_V 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00 // V 86 #define data_ascii_W 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00 // W 87 #define data_ascii_X 0x63, 0x14, 0x08, 0x14, 0x63, 0x00 // Y 88 #define data_ascii_Y 0x03, 0x04, 0x78, 0x04, 0x03, 0x00 // X 89 #define data_ascii_Z 0x61, 0x59, 0x49, 0x4D, 0x43, 0x00 // Z 90 #define data_ascii_colon 0x00, 0x00, 0x14, 0x00, 0x00, 0x00 // : 58 // display array byte data_ascii[][display_array_size] = { // ARDUINO: //{ data_null, data_null, data_null, data_null, data_ascii_A, data_ascii_R, data_ascii_D, data_ascii_U, data_ascii_I, data_ascii_N, data_ascii_O, data_null, data_null, data_null, data_null // WELCOME /* { data_null, data_null, data_null, data_null, data_ascii_W, data_ascii_E, data_ascii_L, data_ascii_C, data_ascii_O, data_ascii_M, data_ascii_E, data_ascii_colon, data_null, data_null, data_null, data_null} */ }; int serial_data = 0; // 由 serial 讀入的資料 void setup() { for (byte i = 2; i <=13; i++) pinMode(i, OUTPUT); // let 3-color LED disable digitalWrite(3, HIGH); digitalWrite(5, HIGH); digitalWrite(6, HIGH); Serial.begin(9600); } void loop() { if (Serial.available() > 0) { serial_data = Serial.read(); Serial.print("I receive: "); Serial.println(serial_data); } switch (serial_data) { case 49: // 1 // every 8 lines compose a word, 8 bits compose a line. // In order to avoid the back of LED have no information. // display "ARDUINO:" for (int i = 0; i < (display_array_size - led_matrix_number * 8); i++ ) display_led_from(0, i, 25); break; case 50: // 2 // buzzer // iterate over the notes of the melody: for (int thisNote = 0; thisNote < node_size; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000/noteDurations[thisNote]; tone(buzzer_pin, melody[thisNote],noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); } break; case 51: // 3 digitalWrite(12, HIGH); delay(500); digitalWrite(12, LOW); break; case 52: // 4 digitalWrite(10, HIGH); delay(500); digitalWrite(10, LOW); break; case 53: // 5 digitalWrite(11, HIGH); delay(500); digitalWrite(11, LOW); break; case 54: // 6 digitalWrite(11, HIGH); delay(500); digitalWrite(11, LOW); break; case 55: // 7 digitalWrite(11, HIGH); delay(500); digitalWrite(11, LOW); break; case 56: // 8 digitalWrite(11, HIGH); delay(500); digitalWrite(11, LOW); break; case 57: // 9 digitalWrite(11, HIGH); delay(500); digitalWrite(11, LOW); break; case 48: // 0 // display "WELCOME" for (int i = 1; i < (display_array_size - led_matrix_number * 8); i++ ) display_led_from(1, i, 25); break; case 97: // a // play DO tone(buzzer_pin, NOTE_C3, 50); break; case 115: // s // play RE tone(buzzer_pin, NOTE_D3, 50); break; case 100: // d // play MI tone(buzzer_pin, NOTE_E3, 50); break; case 102: // f // play FA tone(buzzer_pin, NOTE_F3, 50); break; case 103: // g // play SOL tone(buzzer_pin, NOTE_G3, 50); break; case 104: // h // play LA tone(buzzer_pin, NOTE_A3, 50); break; case 106: // j // play SI tone(buzzer_pin, NOTE_B3, 50); break; case 107: // k // play RE tone(buzzer_pin, NOTE_C4, 50); break; } serial_data = 0; delay(200); } void clear_statue() { serial_data = 0; for (int i = 2; i <= 13; i++) digitalWrite(i, LOW); } void display_led_from(int str_number, int index, int continue_time) { //refresh times for (int k = 0; k < continue_time; k++) { // col = 6 for (int j = index; j <= (index + 8); j++) { //ground scan_latch_pin and hold low for as long as you are transmitting digitalWrite(scan_latch_pin, LOW); //transmitting the data of LED Matrix shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, byte((1 << (j-index)) ^ 0xFF)); //shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(1 << (j-index))); shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(data_ascii[str_number][j] )); //transmitting the data of LED Matrix shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, byte((1 << (j-index)) ^ 0xFF)); //shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(1 << (j-index))); shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(data_ascii[str_number][j+8] )); //transmitting the data of LED Matrix shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, byte((1 << (j-index)) ^ 0xFF)); //shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(1 << (j-index))); shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(data_ascii[str_number][j+16] )); //transmitting the data of LED Matrix shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, byte((1 << (j-index)) ^ 0xFF)); //shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int( (1 << (j-index)) ) ); shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(data_ascii[str_number][j+24] )); //return the latch pin high to signal chip that it //no longer needs to listen for information digitalWrite(scan_latch_pin, HIGH); //Serial.print(1 << j-index, HEX); //Serial.print(" = "); //Serial.println(data_ascii[0][j], HEX); //delay(1000); } } } }}}