2023/12/08 Jack OmniXRI整理製作
開發板資訊:Getting Started with Seeed Studio XIAO nRF52840 Sense
https://wiki.seeedstudio.com/XIAO_BLE/
擴展板資訊:Expansion Board Base for XIAO
https://wiki.seeedstudio.com/Seeeduino-XIAO-Expansion-Board/
以 USB Type C纜線連接電腦和開發板,檢查「裝置管理員」下「連接埠(COM和LPT)」是否有多一個「USB序列裝置(COMxx)」。xx即為埠號,會隨電腦即插入USB位置每次都會隨機配置。若沒有產生序列裝置,則可快速按開發板「Reset」鍵二次,令其進入模式。
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
const int buttonPin = D1; // 設定擴展板使用者按鍵腳位
const int buzzerPin = PIN_A3; // 設定擴展板蜂鳴器腳位
int buttonState = 0; // 設定按鍵狀況初始值
void setup() { // 初始化設定
pinMode(buzzerPin , OUTPUT); // 指定擴展板蜂鳴器接腳為輸出
pinMode(buttonPin, INPUT_PULLUP); // 指定擴展板按鍵為具上拉輸入
}
void loop() { // 無限迴圈
buttonState = digitalRead(buttonPin); // 讀取按鍵狀態
if (buttonState == LOW) { // 若按鍵按下
digitalWrite(buzzerPin, HIGH); // 蜂鳴器ON
delayMicroseconds(500); // 等待時間500us
digitalWrite(buzzerPin, LOW); // 蜂鳴器OFF
delayMicroseconds(500); // 等待時間500us
}
}
結合LED閃爍及使用者按鍵程式範例,當按下按鍵時,令蜂鳴器產生1Khz頻率(500us ON + 500us OFF)。
const int buttonPin = D1; // 設定擴展板使用者按鍵腳位
const int buzzerPin = PIN_A3; // 設定擴展板蜂鳴器腳位
int buttonState = 0; // 設定按鍵狀況初始值
void setup() { // 初始化設定
pinMode(buzzerPin , OUTPUT); // 指定擴展板蜂鳴器接腳為輸出
pinMode(buttonPin, INPUT_PULLUP); // 指定擴展板按鍵為具上拉輸入
}
void loop() { // 無限迴圈
buttonState = digitalRead(buttonPin); // 讀取按鍵狀態
if (buttonState == LOW) { // 若按鍵按下
digitalWrite(buzzerPin, HIGH); // 蜂鳴器ON
delayMicroseconds(500); // 等待時間500us
digitalWrite(buzzerPin, LOW); // 蜂鳴器OFF
delayMicroseconds(500); // 等待時間500us
}
}
使用PWM方式,當按下按鍵後從蜂鳴器產1KHz頻率。
const int buttonPin = D1; // 設定擴展板使用者按鍵腳位
int buttonState = 0; // 設定按鍵狀況初始值
void setup() { // 初始化設定
pinMode(buttonPin, INPUT_PULLUP); // 指定擴展板按鍵為具上拉輸入
}
void loop() { // 無限迴圈
buttonState = digitalRead(buttonPin); // 讀取按鍵狀態
if (buttonState == LOW) { // 若按鍵按下
tone(PIN_A3, 1000, 100); // A3接腳以PWM方式發出1000Hz持續100ms
delay(100); // 延遲100ms
}
}
生日快樂歌曲,notes[]定義音高,beats[]定義節奏(音長),names[]定義音高名稱C代表DO依此類推。
int speakerPin = A3;
int length = 28; // the number of notes
char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
int beats[] = { 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8, 8, 16, 1,
2, 2, 8, 8, 8, 8, 16, 1, 2, 2, 8, 8, 8, 16 };
int tempo = 150;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',
'c', 'd', 'e', 'f', 'g', 'a', 'b',
'x', 'y'
};
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,
956, 834, 765, 593, 468, 346, 224,
655 , 715
};
int SPEE = 5;
// play the tone corresponding to the note name
for (int i = 0; i < 16; i++) {
if (names[i] == note) {
int newduration = duration / SPEE;
playTone(tones[i], newduration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo);
}
}
#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>
// 設定OLED I2C接腳
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/ PIN_WIRE_SCL,
/* data=*/ PIN_WIRE_SDA, /* reset=*/ U8X8_PIN_NONE);
void setup(void) { // 初始化設定
u8x8.begin(); // 初始化u8x8函式
u8x8.setFlipMode(0); // 設定螢幕顯示翻轉方式,1為翻轉180度
}
void loop(void) { // 無限迴圈
u8x8.setFont(u8x8_font_chroma48medium8_r); // 設定顯示字形
u8x8.setCursor(0, 0); // 設定起始座標(X, Y)
u8x8.print("Hello World!"); // 設定顯示字串
}
Seeed官方文件:The 6-Axis IMU Usage on Seeed Studio XIAO nRF52840 Sense (LSM6DS3)
https://wiki.seeedstudio.com/XIAO-BLE-Sense-IMU-Usage/
#include <LSM6DS3.h>
#include <Wire.h>
LSM6DS3 myIMU(I2C_MODE, 0x6A); // 建立LSM6DS3實例並設定I2C裝置位址
float aX, aY, aZ, gX, gY, gZ; // 儲存三軸加速度及陀螺儀數值變數
const float accelerationThreshold = 2.5; // 加速度門檻值
const int numSamples = 119; // 樣本數量
int samplesRead = numSamples; // 讀取樣本數量
void setup() { // 初始化設定
Serial.begin(9600); // 設定串例通信速度(bps)
while (!Serial); // 等待串列埠初始化完成
if (myIMU.begin() != 0) { // 初始化運動感測器
Serial.println("Device error");
} else {
Serial.println("aX,aY,aZ,gX,gY,gZ");
}
}
void loop() { // 無限迴圈
// 等待取得足夠數量樣本
while (samplesRead == numSamples) {
// 讀取加速度計值
aX = myIMU.readFloatAccelX();
aY = myIMU.readFloatAccelY();
aZ = myIMU.readFloatAccelZ();
// 求得加速度值總合
float aSum = fabs(aX) + fabs(aY) + fabs(aZ);
// 檢查是否超過門檻值,若是重置已讀樣本數量
if (aSum >= accelerationThreshold) {
samplesRead = 0;
break;
}
}
// 檢查自上次檢測到顯著運動以來是否已讀取所有必需的樣本
while (samplesRead < numSamples) {
samplesRead++; // 已讀取樣本數加1
// print the data in CSV format
Serial.print(myIMU.readFloatAccelX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelZ(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroZ(), 3);
Serial.println();
// 若已讀樣本數已足夠則列印換行
if (samplesRead == numSamples) {
Serial.println();
}
}
}
XIAO
Edge AI