本帖最后由 qwert1213131 于 2024-12-25 21:00 编辑
主要使用Arudino进行任务开发;选用了开发板Arduino® Nano RP2040 Connect作为主要器件,并订购了排针和 一个5键的ADC按键模块;
安装IDE
开发板原理图
任务1
主要使用了ADC按键,通过adc外设来读取不同的按键值,然后打印所按按键的值,并同时让LED彩灯显示随机颜色
#include <WiFiNINA.h>
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = LED_BUILTIN; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int adc_key_val[5] = { 30, 80, 120, 150, 600 };
int NUM_KEYS = 5;
int key = -1;
int oldkey = -1;
void setup() {
Serial.begin(115200);
randomSeed(analogRead(sensorPin));
pinMode(ledPin, OUTPUT);
analogWrite(LEDR, (255));
analogWrite(LEDG, (255));
analogWrite(LEDB, (255));
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// Serial.println(sensorValue);
key = get_key(sensorValue);
digitalWrite(ledPin, LOW);
if (key != oldkey) {
delay(50);
sensorValue = analogRead(sensorPin);
key = get_key(sensorValue);
if (key != oldkey) {
oldkey = key;
if (key >= 0) {
digitalWrite(ledPin, HIGH);
analogWrite(LEDR, random(255));
analogWrite(LEDG, random(255));
analogWrite(LEDB, random(255));
Serial.println("Hello DigiKey & EEWorld!");
switch (key) {
case 0: Serial.println("s1 OK"); break;
case 1: Serial.println("s2 OK"); break;
case 2: Serial.println("s3 OK"); break;
case 3: Serial.println("s4 OK"); break;
case 4: Serial.println("s5 OK"); break;
}
}
}
}
delay(100);
}
int get_key(unsigned int input) {
int k;
for (k = 0; k < NUM_KEYS; k++) {
if (input < adc_key_val[k]) {
return k;
}
}
if (k > NUM_KEYS) k = -1;
return k;
}
任务2
主要使用板载的IMU传感器,将获取的的6轴数据打印到串口终端,并使用自带的绘图仪显示波形
#include <Arduino_LSM6DSOX.h>
float Ax, Ay, Az;
float Gx, Gy, Gz;
void setup() {
Serial.begin(115200);
while(!Serial);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
Serial.println();
Serial.print("Gyroscope sample rate = ");
Serial.print(IMU.gyroscopeSampleRate());
Serial.println("Hz");
Serial.println();
}
void loop() {
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(Ax, Ay, Az);
// Serial.println("Accelerometer data: ");
Serial.print(Ax);
Serial.print('\t');
Serial.print(Ay);
Serial.print('\t');
Serial.print(Az);
Serial.print('\t');
// Serial.println();
}
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(Gx, Gy, Gz);
// Serial.println("Gyroscope data: ");
Serial.print(Gx);
Serial.print('\t');
Serial.print(Gy);
Serial.print('\t');
Serial.println(Gz);
Serial.println();
}
delay(50);
}
任务3
主要通过读取板载pdm麦克风数据,显示在串口上,若声音超过阈值范围则对板载LED进行开关操作
#include <WiFiNINA.h>
#include <PDM.h>
bool LED_SWITCH = false;
// default number of output channels
static const char channels = 1;
// default PCM output frequency
static const int frequency = 20000;
// Buffer to read samples into, each sample is 16-bits
short sampleBuffer[512];
// Number of audio samples read
volatile int samplesRead;
void setup() {
Serial.begin(9600);
pinMode(LEDB, OUTPUT);
while (!Serial);
// Configure the data receive callback
PDM.onReceive(onPDMdata);
// Optionally set the gain
// Defaults to 20 on the BLE Sense and -10 on the Portenta Vision Shields
// PDM.setGain(30);
// Initialize PDM with:
// - one channel (mono mode)
// - a 16 kHz sample rate for the Arduino Nano 33 BLE Sense
// - a 32 kHz or 64 kHz sample rate for the Arduino Portenta Vision Shields
if (!PDM.begin(channels, frequency)) {
Serial.println("Failed to start PDM!");
while (1);
}
}
void loop() {
// Wait for samples to be read
if (samplesRead) {
// Print samples to the serial monitor or plotter
for (int i = 0; i < samplesRead; i++) {
if (channels == 2) {
Serial.print("L:");
Serial.print(sampleBuffer[i]);
Serial.print(" R:");
i++;
}
Serial.println(sampleBuffer[i]);
if (sampleBuffer[i] > 6000 || sampleBuffer[i] <= -6000) {
LED_SWITCH = !LED_SWITCH;
if (LED_SWITCH) {
Serial.println();
digitalWrite(LEDB, HIGH);
Serial.println("ON!");
Serial.println();
delay(1000);
}
else {
Serial.println();
digitalWrite(LEDB, LOW);
Serial.println("OFF!");
Serial.println();
delay(1000);
}
}
}
// Clear the read count
samplesRead = 0;
}
}
/**
Callback function to process the data from the PDM microphone.
NOTE: This callback is executed as part of an ISR.
Therefore using `Serial` to print messages inside this function isn't supported.
* */
void onPDMdata() {
// Query the number of available bytes
int bytesAvailable = PDM.available();
// Read into the sample buffer
PDM.read(sampleBuffer, bytesAvailable);
// 16-bit, 2 bytes per sample
samplesRead = bytesAvailable / 2;
}
非常感谢平台提供的机会,arduino已经包装了很多底层函数,也有很多库可以直接使用,不需要考虑太多底层的运行,对于创建应用很是方便。
可编译下载的代码:download.eeworld.com.cn/detail/qwert1213131/635441
|