Changes between Initial Version and Version 1 of wade/embedded/arduino/example/example_6_XBee_led_matrix_74HC595


Ignore:
Timestamp:
Mar 5, 2010, 2:12:10 PM (14 years ago)
Author:
wade
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • wade/embedded/arduino/example/example_6_XBee_led_matrix_74HC595

    v1 v1  
     1{{{
     2/*
     3 * Arduino 由 XBee 接收資料。
     4 * 輸入 1:顯示 ARDUINO。
     5 *      2:控制 buzzer 出現音樂。
     6 *      0:顯示 WELCOME:。
     7 *    a-k:產生 DO RE MI... DO 聲音。
     8 *
     9/*                           
     10 *              08 07 06 05 04 03 02 01
     11 *                                   *
     12 * 74HC595  PIN 09 10 11 12 13 14 15 16
     13 *                    SH ST    DS
     14 *                    棕 白    紫
     15 * Arduino  PIN       3  2     4
     16 *     
     17 */
     18
     19#include "pitches.h"
     20#define node_size 8
     21#define buzzer_pin 11
     22// notes in the melody:
     23int melody[] = {
     24  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
     25// note durations: 4 = quarter note, 8 = eighth note, etc.:
     26int noteDurations[] = {
     27  4, 8, 8, 4,4,4,4,4 };
     28 
     29// Pin connected to ST_CP of 74HC595 for scanning
     30int scan_latch_pin = 7; // 白
     31// Pin connected to SH_CP of 74HC595 for scanning
     32int scan_clock_pin = 8; // 藍
     33// Pin connected to DS of 74HC595 for scanning
     34int scan_data_pin = 9; // 棕
     35// display array size
     36#define display_array_size 168
     37// how many led matrix do you
     38#define led_matrix_number 4
     39// ascii 5x7 dot font
     40#define data_null 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // null char
     41#define data_ascii_minus 0x08, 0x08, 0x08, 0x08, 0x08, 0x00 // -
     42#define data_ascii_dot 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 // .
     43#define data_ascii_A 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 // A 65
     44#define data_ascii_B 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 // B
     45#define data_ascii_C 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 // C
     46#define data_ascii_D 0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00 // D
     47#define data_ascii_E 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 // E
     48#define data_ascii_F 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 // F 70
     49#define data_ascii_G 0x3E, 0x41, 0x41, 0x51, 0x73, 0x00 // G
     50#define data_ascii_H 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00 // H
     51#define data_ascii_I 0x00, 0x41, 0x7F, 0x41, 0x00, 0x00 // I
     52#define data_ascii_J 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00 // J
     53#define data_ascii_K 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00 // K 75
     54#define data_ascii_L 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00 // L
     55#define data_ascii_M 0x7F, 0x02, 0x1C, 0x02, 0x7F, 0x00 // M
     56#define data_ascii_N 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00 // N
     57#define data_ascii_O 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00 // O
     58#define data_ascii_P 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00 // P 80
     59#define data_ascii_Q 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00 // Q 81
     60#define data_ascii_R 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00 // R 82
     61#define data_ascii_S 0x26, 0x49, 0x49, 0x49, 0x32, 0x00 // S 83
     62#define data_ascii_T 0x03, 0x01, 0x7F, 0x01, 0x03, 0x00 // T 84
     63#define data_ascii_U 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00 // U 85
     64#define data_ascii_V 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00 // V 86
     65#define data_ascii_W 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00 // W 87
     66#define data_ascii_X 0x63, 0x14, 0x08, 0x14, 0x63, 0x00 // Y 88
     67#define data_ascii_Y 0x03, 0x04, 0x78, 0x04, 0x03, 0x00 // X 89
     68#define data_ascii_Z 0x61, 0x59, 0x49, 0x4D, 0x43, 0x00 // Z 90
     69#define data_ascii_colon 0x00, 0x00, 0x14, 0x00, 0x00, 0x00 // : 58
     70
     71//  display array
     72byte data_ascii[][display_array_size] = {
     73                       // ARDUINO:
     74                       //{
     75                       data_null,
     76                       data_null,
     77                       data_null,
     78                       data_null,
     79                       data_ascii_A,
     80                       data_ascii_R,
     81                       data_ascii_D,
     82                       data_ascii_U,
     83                       data_ascii_I,
     84                       data_ascii_N,
     85                       data_ascii_O,
     86                       data_null,
     87                       data_null,
     88                       data_null,
     89                       data_null
     90                       // WELCOME
     91                       /*
     92                       {
     93                       data_null,
     94                       data_null,
     95                       data_null,
     96                       data_null,
     97                       data_ascii_W,
     98                       data_ascii_E,
     99                       data_ascii_L,
     100                       data_ascii_C,
     101                       data_ascii_O,
     102                       data_ascii_M,
     103                       data_ascii_E,
     104                       data_ascii_colon,
     105                       data_null,
     106                       data_null,
     107                       data_null,
     108                       data_null}
     109                       */
     110                       };
     111                     
     112
     113int serial_data = 0;    // 由 serial 讀入的資料
     114 
     115void setup()
     116{
     117  for (byte i = 2; i <=13; i++)
     118    pinMode(i, OUTPUT);
     119   
     120  // let 3-color LED disable
     121  digitalWrite(3, HIGH);
     122  digitalWrite(5, HIGH);
     123  digitalWrite(6, HIGH);
     124  Serial.begin(9600);
     125}
     126
     127void loop()
     128{
     129  if (Serial.available() > 0)
     130  {
     131    serial_data = Serial.read();
     132    Serial.print("I receive: ");
     133    Serial.println(serial_data);
     134  }
     135  switch (serial_data)
     136  {
     137    case 49: // 1
     138      // every 8 lines compose a word, 8 bits compose a line.
     139      // In order to avoid the back of LED have no information.
     140      // display "ARDUINO:"
     141      for (int i = 0; i < (display_array_size - led_matrix_number * 8); i++ )
     142        display_led_from(0, i, 25);
     143      break;
     144    case 50: // 2
     145      // buzzer
     146      // iterate over the notes of the melody:
     147      for (int thisNote = 0; thisNote < node_size; thisNote++) {
     148        // to calculate the note duration, take one second
     149        // divided by the note type.
     150        //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
     151        int noteDuration = 1000/noteDurations[thisNote];
     152        tone(buzzer_pin, melody[thisNote],noteDuration);
     153        // to distinguish the notes, set a minimum time between them.
     154        // the note's duration + 30% seems to work well:
     155        int pauseBetweenNotes = noteDuration * 1.30;
     156        delay(pauseBetweenNotes);
     157      }
     158      break;
     159    case 51: // 3
     160      digitalWrite(12, HIGH);
     161      delay(500);
     162      digitalWrite(12, LOW);
     163      break;
     164    case 52: // 4
     165      digitalWrite(10, HIGH);
     166      delay(500);
     167      digitalWrite(10, LOW);
     168      break;
     169    case 53: // 5
     170      digitalWrite(11, HIGH);
     171      delay(500);
     172      digitalWrite(11, LOW);
     173      break;
     174    case 54: // 6
     175      digitalWrite(11, HIGH);
     176      delay(500);
     177      digitalWrite(11, LOW);
     178      break;
     179    case 55: // 7
     180      digitalWrite(11, HIGH);
     181      delay(500);
     182      digitalWrite(11, LOW);
     183      break;
     184    case 56: // 8
     185      digitalWrite(11, HIGH);
     186      delay(500);
     187      digitalWrite(11, LOW);
     188      break;
     189    case 57: // 9
     190      digitalWrite(11, HIGH);
     191      delay(500);
     192      digitalWrite(11, LOW);
     193      break;
     194    case 48: // 0
     195      // display "WELCOME"
     196      for (int i = 1; i < (display_array_size - led_matrix_number * 8); i++ )
     197        display_led_from(1, i, 25);
     198      break;
     199    case 97:  // a
     200      // play DO
     201      tone(buzzer_pin, NOTE_C3, 50);
     202      break;
     203    case 115:  // s
     204      // play RE
     205      tone(buzzer_pin, NOTE_D3, 50);
     206      break;
     207    case 100:  // d
     208      // play MI
     209      tone(buzzer_pin, NOTE_E3, 50);
     210      break;
     211    case 102:  // f
     212      // play FA
     213      tone(buzzer_pin, NOTE_F3, 50);
     214      break;
     215    case 103:  // g
     216      // play SOL
     217      tone(buzzer_pin, NOTE_G3, 50);
     218      break;
     219    case 104:  // h
     220      // play LA
     221      tone(buzzer_pin, NOTE_A3, 50);
     222      break;
     223    case 106:  // j
     224      // play SI
     225      tone(buzzer_pin, NOTE_B3, 50);
     226      break;
     227   case 107:  // k
     228      // play RE
     229      tone(buzzer_pin, NOTE_C4, 50);
     230      break;
     231  }
     232 
     233  serial_data = 0;
     234  delay(200);
     235}
     236
     237void clear_statue()
     238{
     239  serial_data = 0;
     240  for (int i = 2; i <= 13; i++)
     241    digitalWrite(i, LOW);
     242}
     243
     244void display_led_from(int str_number, int index, int continue_time)
     245{
     246  //refresh times
     247  for (int k = 0; k < continue_time; k++)
     248  {
     249    // col = 6
     250    for (int j = index; j <= (index + 8); j++) {
     251      //ground scan_latch_pin and hold low for as long as you are transmitting
     252      digitalWrite(scan_latch_pin, LOW);
     253     
     254      //transmitting the data of LED Matrix
     255      shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, byte((1 << (j-index)) ^ 0xFF));     
     256      //shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(1 << (j-index)));
     257      shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(data_ascii[str_number][j] ));
     258     
     259      //transmitting the data of LED Matrix
     260      shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, byte((1 << (j-index)) ^ 0xFF));     
     261      //shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(1 << (j-index)));
     262      shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(data_ascii[str_number][j+8] ));
     263     
     264      //transmitting the data of LED Matrix
     265      shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, byte((1 << (j-index)) ^ 0xFF));     
     266      //shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(1 << (j-index)));
     267      shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(data_ascii[str_number][j+16] ));
     268     
     269      //transmitting the data of LED Matrix     
     270      shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, byte((1 << (j-index)) ^ 0xFF));
     271      //shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int( (1 << (j-index)) ) );
     272      shiftOut(scan_data_pin, scan_clock_pin, MSBFIRST, int(data_ascii[str_number][j+24] ));
     273     
     274
     275      //return the latch pin high to signal chip that it
     276      //no longer needs to listen for information
     277      digitalWrite(scan_latch_pin, HIGH);
     278      //Serial.print(1 << j-index, HEX);
     279      //Serial.print(" = ");
     280      //Serial.println(data_ascii[0][j], HEX);
     281      //delay(1000);
     282    }
     283  }
     284}
     285
     286
     287}}}