Board logo

標題: 新手 Arduino + DFRobot LCD Keypad Shield [打印本頁]

作者: acw@home    時間: 2009-8-26 01:13     標題: 新手 Arduino + DFRobot LCD Keypad Shield

Arduino 團購中買了 DFRobot LCD Keypad Shield, 取貨後即試玩. 但沒有跟原理圖, 只好上網找.

DFRobot website 只說 LCD 用了 digital PIN 4, 5, 6, 7, 8, 9 和 Keypad 用了 analog PIN A0. 經再三找尋, 終於有以下設定:
  1. LCD     Arduino
  2. ---------------
  3. RS            8
  4. Enable        9
  5. D4            4
  6. D5            5
  7. D6            6
  8. D7            7
複製代碼
咁 Shield 上的 5 個 keys 呢? 根據資料, 每個 按下後, A0 讀數不同, 所以先寫程式去讀此數据:
  1. /* LCD Keypad Shield
  2.    Author: acw@home
  3.    Aug 25, 2009
  4.    
  5. LCD PIN assignment:

  6. LCD         Arduino
  7. -------------------
  8. RS          8
  9. ENABLE      9
  10. D4          4
  11. D5          5
  12. D6          6
  13. D7          7

  14. Keypad PIN = A0

  15. */
  16. #include <LiquidCrystal.h>

  17. // analog PIN A0
  18. #define KEY_PIN  0

  19. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

  20. int key;
  21. int last_key = -1; // save the last key pressed

  22. void setup()
  23. {
  24.   lcd.begin(16, 2);
  25.   lcd.print("Press any key");
  26. }

  27. void loop()
  28. {
  29.   key = analogRead(KEY_PIN);
  30.   if (key != last_key) {
  31.     lcd.setCursor(0, 1);
  32.     lcd.print(key);
  33.     lcd.print("        ");
  34.     last_key = key;
  35.   }
  36. }
複製代碼
上載程式到 Arduino 執行, 按下每個 key, 得到以下數据:
  1. Key      Value
  2. --------------
  3. RIGHT        0
  4. UP         145
  5. DOWN       329
  6. LEFT       504
  7. SELECT     739
  8. <NONE>    1023
複製代碼
根據以上數据, 更改程式嘗試:
  1. /* LCD Keypad Shield
  2.    Author: acw@home
  3.    Aug 25, 2009
  4.    
  5. LCD PIN assignment:

  6. LCD         Arduino
  7. -------------------
  8. RS          8
  9. ENABLE      9
  10. D4          4
  11. D5          5
  12. D6          6
  13. D7          7

  14. Keypad PIN = A0

  15. Key pressed       A0 Reading
  16. --------------------------------
  17. RIGHT             0
  18. UP                145
  19. DOWN              329
  20. LEFT              504
  21. SELECT            739
  22. <NONE>            1023

  23. */
  24. #include <LiquidCrystal.h>

  25. enum {
  26.   KEY_RIGHT,
  27.   KEY_UP,
  28.   KEY_DOWN,
  29.   KEY_LEFT,
  30.   KEY_SELECT,
  31.   NUM_KEYS
  32. };

  33. #define LED_PIN  13
  34. #define KEY_PIN  0
  35. #define KEY_NONE 1020

  36. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

  37. int key_val[]= { 140, 320, 500, 730, KEY_NONE };
  38. char *key_name[] = { "right", "up", "down", "left", "select" };
  39. int key;
  40. int last_key; // save the last key pressed

  41. void setup()
  42. {
  43.   lcd.begin(16, 2);
  44.   lcd.print("Press any key");
  45.   last_key = KEY_NONE;
  46. }

  47. int readKey(int pin)
  48. {
  49.   int n, key_in;
  50.   
  51.   n = analogRead(KEY_PIN);
  52.   if (n < KEY_NONE) {
  53.     // a key is pressed
  54.     digitalWrite(LED_PIN, HIGH); // turn on LED
  55.     // show the A0 reading
  56.     lcd.setCursor(0, 0);
  57.     lcd.print("Value: ");
  58.     lcd.print(n);
  59.     lcd.print("                  ");
  60.     // wait for the key being released
  61.     do {
  62.       key_in = n;
  63.       n = analogRead(KEY_PIN);
  64.     } while (n < KEY_NONE);
  65.     digitalWrite(LED_PIN, LOW); // turn off LED
  66.     // decode the key
  67.     for (int i=KEY_RIGHT; i<NUM_KEYS; ++i) {
  68.       if (key_in < key_val[i]) {
  69.         return i;
  70.       }
  71.     }
  72.   }
  73.   return KEY_NONE;
  74. }

  75. void loop()
  76. {
  77.   key = readKey(KEY_PIN);
  78.   if (key != KEY_NONE && key != last_key) {
  79.     lcd.setCursor(0, 1);
  80.     lcd.print("Key  : ");
  81.     lcd.print(key_name[key]);
  82.     lcd.print("        ");
  83.     last_key = key;
  84.   }
  85. }
複製代碼
上載程式到 Arduino 執行, 按下 DOWN key, 得到以下:
DSC03471-s.jpg

圖片附件: DSC03471-s.jpg (2009-8-26 01:13, 136.19 KB) / 下載次數 149
https://www.hkepc.com/forum/attachment.php?aid=933479&k=7e6878c65a13b6700e3686298573ffeb&t=1790479791&sid=G3xBEqqZ8r


作者: avdaemon    時間: 2009-8-26 10:14

知不知道團購還有沒有過剩物品出售?
作者: acw@home    時間: 2009-8-26 11:18

原帖由 avdaemon 於 2009-8-26 10:14 發表
知不知道團購還有沒有過剩物品出售?

你最好直接 PM 團長問,  但現在有團友仍未能取貨
作者: tobyho91    時間: 2009-8-27 16:19

點解我照COPY你D CODE,但隻野冇反應既
作者: acw@home    時間: 2009-8-28 00:31

原帖由 tobyho91 於 2009-8-27 16:19 發表
點解我照COPY你D CODE,但隻野冇反應既

你的 LCD Keypad Shield 是否團購的?

因不同版本, PIN assignment 可能不同.

另你話'隻野冇反應既', 係指冇顯示定keypad冇反應?

我的程式執行中, 若你按下any key, Arduino 上的 red led 會 turn on 的.

[ 本帖最後由 acw@home 於 2009-8-28 00:36 編輯 ]
作者: victorchuk622    時間: 2009-8-28 00:32     標題: 回覆 2# 的帖子

try try pm holder, u may have surprise

[ 本帖最後由 victorchuk622 於 2009-8-28 00:33 編輯 ]
作者: tobyho91    時間: 2009-8-29 01:57

原帖由 acw@home 於 2009-8-28 00:31 發表

你的 LCD Keypad Shield 是否團購的?

因不同版本, PIN assignment 可能不同.

另你話'隻野冇反應既', 係指冇顯示定keypad冇反應?

我的程式執行中, 若你按下any key, Arduino 上的 red led 會 turn on 的.

係呀.團購個堆囉..
我就係UPLOAD左..PRESS ANY KEY都冇反應
作者: acw@home    時間: 2009-8-29 14:14

原帖由 tobyho91 於 2009-8-29 01:57 發表

係呀.團購個堆囉..
我就係UPLOAD左..PRESS ANY KEY都冇反應

1) Are you using Arduino IDE version 0017?

2) Please check whether you have properly connected the LCD Keypad Shield to Arduino.  And the power LED and the backlight of the LCD should be on after the Arduino is connected to PC via USB cable no matter whether there is application or not.

3) Also, in order to check whether your Arduino is working or not, just upload the 'Blink' example and see whether the RED LED (labeled 'L' between 'R7' and 'R5', near digital PIN 13) on the Arduino is flashing.

[ 本帖最後由 acw@home 於 2009-8-29 14:18 編輯 ]
作者: bchan    時間: 2009-9-2 03:31

原帖由 tobyho91 於 2009-8-29 01:57 發表

係呀.團購個堆囉..
我就係UPLOAD左..PRESS ANY KEY都冇反應


i have connected the shield and uploaded the code.
it works
作者: Joe_Black    時間: 2009-9-2 10:34

提示: 作者被禁止或刪除 內容自動屏蔽
作者: bchan    時間: 2009-9-3 12:56

加左lcd shield, 你地點駁其他野?
佢未用哂d pin但又駁唔到其他野。
響中間加其他shield?
作者: acw@home    時間: 2009-9-3 13:29

原帖由 bchan 於 2009-9-3 12:56 發表
加左lcd shield, 你地點駁其他野?
佢未用哂d pin但又駁唔到其他野。
響中間加其他shield?

See dulllou's 'Arduino LCD Keypad shield小改善':
http://global.hkepc.com/forum/viewthread.php?tid=1249168
作者: bchan    時間: 2009-9-3 20:08

原帖由 acw@home 於 2009-9-3 13:29 發表

See dulllou's 'Arduino LCD Keypad shield小改善':
http://global.hkepc.com/forum/viewthread.php?tid=1249168

正
一個咁正ge post點解無人re?





歡迎光臨 電腦領域 HKEPC Hardware (https://www.hkepc.com/forum/) Powered by Discuz! 7.2