24 Micro:bit with Arduino

(2017/12/18)

參考資料:https://learn.adafruit.com/use-micro-bit-with-arduino?view=all

這是一篇翻譯文,將上面參考文章再依樣畫葫蘆,重新做一次來好好學習這文章的知識

一、環境建置

  • Add NRF5x Board Support

    • In Arduino, go to Preferences and add https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json

    • 這也可參考之前的文章

二、點亮一顆燈的程式測試

  • 簡易程式如下:

      1. const int COL1 = 3; // Column #1 control

      2. const int LED = 26; // 'row 1' led

      3. void setup() {

      4. Serial.begin(9600);

      5. Serial.println("microbit is ready!");

      6. // because the LEDs are multiplexed, we must ground the opposite side of the LED

      7. pinMode(COL1, OUTPUT);

      8. digitalWrite(COL1, LOW);

      9. pinMode(LED, OUTPUT);

      10. }

      11. void loop(){

      12. Serial.println("blink!");

      13. digitalWrite(LED, HIGH);

      14. delay(500);

      15. digitalWrite(LED, LOW);

      16. delay(500);

      17. }

  • 結果:

    • 最左上角的那顆燈閃爍

      • 如果要點亮其他燈,有如何處理?

三、在序列埠監視視窗呈現按A、B鍵

  • 簡易程式如下:

    1. const int buttonA = 5; // the number of the pushbutton pin

    2. const int buttonB = 11; // the number of the pushbutton pin

    3. void setup() {

    4. Serial.begin(9600);

    5. Serial.println("microbit is ready!");

    6. pinMode(buttonA, INPUT);

    7. pinMode(buttonB, INPUT);

    8. }

    9. void loop(){

    10. if (! digitalRead(buttonA)) {

    11. Serial.println("Button A pressed");

    12. }

    13. if (! digitalRead(buttonB)) {

    14. Serial.println("Button B pressed");

    15. }

    16. delay(10);

    17. }

  • 結果:

    • 從程式知道,當A鍵被按下時 digitalRead(buttonA)=0;當B鍵被按下時 digitalRead(buttonB)=0

四、其他腳位(Other GPIO)

  • 腳位圖

    • 有一些腳位是與LED顯示器共用,如果要使用這些腳位就不要用顯示器。

      • Pin #0 - large pad - analog in

      • Pin #1 - large pad - analog in

      • Pin #2 - large pad - analog in

      • Pin #3 - analog in, also used for LED matrix

      • Pin #4 - analog in, also used for LED matrix

      • Pin #5 - also used for Button A

      • Pin #6 - also used for LED matrix

      • Pin #7 - also used for LED matrix

      • Pin #8

      • Pin #9 - also used for LED matrix

      • Pin #10 - analog in, also used for LED matrix

      • Pin #11 - also used for button B

      • Pin #12

      • Pin #13 - also available as SPI clock

      • Pin #14 - also available as SPI MISO

      • Pin #15 - also available as SPI MOSI

      • Pin #16

      • Pin #19 - also available as I2C clock

      • Pin #20 - also available as I2C data

    • So really, if you're using the buttons and LEDs(按鍵#5(A)、#11(B);LEDS(3、4、6、7、9、10) ),其他可用有#0, #1, #2, #8, #12, #13, #14, #15, #16, #19 and #20. Which is still a good amount!

五、加速度計和磁力計

  • 磁力計

    • 磁力計晶片 MAG3110

    • 下載Sparkfun's library磁力計函數庫(解壓後,放到arduino/libraries下)

    • 簡易程式如下:

      1. /* *********************************************

      2. * SparkFun_MAG3110_Basic

      3. * Triple Axis Magnetometer Breakout - MAG3110

      4. * Hook Up Guide Example

      5. *

      6. * Utilizing Sparkfun's MAG3110 Library

      7. * A basic sketch that reads x y and z readings

      8. * from the MAG3110 sensor

      9. *

      10. * George B. on behalf of SparkFun Electronics

      11. * Created: Sep 22, 2016

      12. * Updated: n/a

      13. *

      14. * Development Environment Specifics:

      15. * Arduino 1.6.7

      16. *

      17. * Hardware Specifications:

      18. * SparkFun MAG3110

      19. * Bi-directional Logic Level Converter

      20. * Arduino Micro

      21. *

      22. * This code is beerware; if you see me (or any other SparkFun employee) at the

      23. * local, and you've found our code helpful, please buy us a round!

      24. * Distributed as-is; no warranty is given.

      25. * *********************************************/

      26. #include <SparkFun_MAG3110.h>

      27. MAG3110 mag = MAG3110(); //Instantiate MAG3110

      28. void setup() {

      29. Serial.begin(9600);

      30. mag.initialize(); //Initializes the mag sensor

      31. mag.start(); //Puts the sensor in active mode

      32. }

      33. void loop() {

      34. int x, y, z;

      35. //Only read data when it's ready

      36. if(mag.dataReady()) {

      37. //Read the data

      38. mag.readMag(&x, &y, &z);

      39. Serial.print("X: ");

      40. Serial.print(x);

      41. Serial.print(", Y: ");

      42. Serial.print(y);

      43. Serial.print(", Z: ");

      44. Serial.println(z);

      45. Serial.println("--------");

      46. }

      47. }

    • 結果:

      • 請注意,磁力計沒有校準,所以你會得到不同的數字在XYZ上,但是當你旋轉和旋轉mirobit的數字應該上下移動一點! (這就是為什麼磁力儀必須校準)

  • 加速度計

    • 有3軸加速度計

    • 下載akafugu MMA8653函數庫(解壓後,放到arduino/libraries下)

    • 簡易程式如下:

      1. /*

      2. * MMA845XQ test code

      3. * (C) 2012 Akafugu Corporation

      4. *

      5. * This program is free software; you can redistribute it and/or modify it under the

      6. * terms of the GNU General Public License as published by the Free Software

      7. * Foundation; either version 2 of the License, or (at your option) any later

      8. * version.

      9. *

      10. * This program is distributed in the hope that it will be useful, but WITHOUT ANY

      11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A

      12. * PARTICULAR PURPOSE. See the GNU General Public License for more details.

      13. *

      14. */

      15. #include "Wire.h"

      16. #include "MMA8653.h"

      17. MMA8653 accel;

      18. void setup() {

      19. Serial.begin(9600);

      20. Serial.println("microbit accel test");

      21. accel.begin(false, 2); // 8-bit mode, 2g range

      22. }

      23. void loop() {

      24. accel.update();

      25. Serial.print(accel.getX()); Serial.print(" , ");

      26. Serial.print(accel.getY()); Serial.print(", ");

      27. Serial.println(accel.getZ());

      28. delay(100);

      29. }

    • 結果:

      • 這個函數庫是非常舊的和不完整的,所以在這個時候你只能使用它在8位模式。將上面改為如下

    1. void loop() {

    2. accel.update();

    3. Serial.print((float)accel.getX() * 0.0156); Serial.print(" , ");

    4. Serial.print((float)accel.getY() * 0.0156); Serial.print(", ");

    5. Serial.println((float)accel.getZ() * 0.0156);

    6. delay(100);

    7. }