24 Micro:bit with Arduino
(2017/12/18)
參考資料:https://learn.adafruit.com/use-micro-bit-with-arduino?view=all
這是一篇翻譯文,將上面參考文章再依樣畫葫蘆,重新做一次來好好學習這文章的知識
一、環境建置
Install Windows 7 Driver(下載 mBed serial diver)
Install SoftDevice onto MicroBit (下載 Micro:bit BTLE advertising Demo)
不是很了解這個用途,好像是叫出【藍芽】的積木來取代【廣播】積木
上面的程式,如下
Add NRF5x Board Support
In Arduino, go to Preferences and add https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json
這也可參考之前的文章
二、點亮一顆燈的程式測試
簡易程式如下:
const int COL1 = 3; // Column #1 control
const int LED = 26; // 'row 1' led
void setup() {
Serial.begin(9600);
Serial.println("microbit is ready!");
// because the LEDs are multiplexed, we must ground the opposite side of the LED
pinMode(COL1, OUTPUT);
digitalWrite(COL1, LOW);
pinMode(LED, OUTPUT);
}
void loop(){
Serial.println("blink!");
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}
結果:
最左上角的那顆燈閃爍
如果要點亮其他燈,有如何處理?
三、在序列埠監視視窗呈現按A、B鍵
簡易程式如下:
const int buttonA = 5; // the number of the pushbutton pin
const int buttonB = 11; // the number of the pushbutton pin
void setup() {
Serial.begin(9600);
Serial.println("microbit is ready!");
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
}
void loop(){
if (! digitalRead(buttonA)) {
Serial.println("Button A pressed");
}
if (! digitalRead(buttonB)) {
Serial.println("Button B pressed");
}
delay(10);
}
結果:
從程式知道,當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下)
簡易程式如下:
/* *********************************************
* SparkFun_MAG3110_Basic
* Triple Axis Magnetometer Breakout - MAG3110
* Hook Up Guide Example
*
* Utilizing Sparkfun's MAG3110 Library
* A basic sketch that reads x y and z readings
* from the MAG3110 sensor
*
* George B. on behalf of SparkFun Electronics
* Created: Sep 22, 2016
* Updated: n/a
*
* Development Environment Specifics:
* Arduino 1.6.7
*
* Hardware Specifications:
* SparkFun MAG3110
* Bi-directional Logic Level Converter
* Arduino Micro
*
* This code is beerware; if you see me (or any other SparkFun employee) at the
* local, and you've found our code helpful, please buy us a round!
* Distributed as-is; no warranty is given.
* *********************************************/
#include <SparkFun_MAG3110.h>
MAG3110 mag = MAG3110(); //Instantiate MAG3110
void setup() {
Serial.begin(9600);
mag.initialize(); //Initializes the mag sensor
mag.start(); //Puts the sensor in active mode
}
void loop() {
int x, y, z;
//Only read data when it's ready
if(mag.dataReady()) {
//Read the data
mag.readMag(&x, &y, &z);
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Z: ");
Serial.println(z);
Serial.println("--------");
}
}
結果:
請注意,磁力計沒有校準,所以你會得到不同的數字在XYZ上,但是當你旋轉和旋轉mirobit的數字應該上下移動一點! (這就是為什麼磁力儀必須校準)
加速度計
有3軸加速度計
下載akafugu MMA8653函數庫(解壓後,放到arduino/libraries下)
簡易程式如下:
/*
* MMA845XQ test code
* (C) 2012 Akafugu Corporation
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
*/
#include "Wire.h"
#include "MMA8653.h"
MMA8653 accel;
void setup() {
Serial.begin(9600);
Serial.println("microbit accel test");
accel.begin(false, 2); // 8-bit mode, 2g range
}
void loop() {
accel.update();
Serial.print(accel.getX()); Serial.print(" , ");
Serial.print(accel.getY()); Serial.print(", ");
Serial.println(accel.getZ());
delay(100);
}
結果:
這個函數庫是非常舊的和不完整的,所以在這個時候你只能使用它在8位模式。將上面改為如下
void loop() {
accel.update();
Serial.print((float)accel.getX() * 0.0156); Serial.print(" , ");
Serial.print((float)accel.getY() * 0.0156); Serial.print(", ");
Serial.println((float)accel.getZ() * 0.0156);
delay(100);
}