传感器校正和测试

一。舵机在使用过程中为了防止手动扭动损坏其中的齿轮,一般会使用代码测试并校正到0位。

#include <Servo.h>  
  
Servo myservo;  // 创建一个Servo对象  
  
// 连接到舵机信号线的Arduino引脚  
int servoPin = 9;   
  
void setup() {  
  myservo.attach(servoPin);  // 将myservo对象与servoPin引脚关联  
  
  // 测试舵机全范围移动  
  Serial.begin(9600);  
  Serial.println("开始测试舵机...");  
  for (int pos = 0; pos <= 180; pos += 1) { // 从0度到180度  
    myservo.write(pos);              // 发送舵机位置  
    delay(15);                       // 等待舵机到达位置  
  }  
  for (int pos = 180; pos >= 0; pos -= 1) { // 从180度回到0度  
    myservo.write(pos);  
    delay(15);  
  }  
  
  // 校正舵机到0度位置  
  Serial.println("校正舵机到0度...");  
  myservo.write(90); // 假设90度是舵机的中立位置,但可能需要调整  
  delay(1000); // 等待舵机稳定  
  
  // 如果需要,可以通过实验找到更精确的中立位置值  
  // 例如,你可以尝试将90改为85、95等,看哪个值更接近你的舵机的实际中立位置  
}  
  
void loop() {  
  // 在loop()中不需要做任何事情,因为所有的测试和校正都在setup()中完成了  
  // 如果你需要在之后控制舵机,可以在这里添加代码  
}

二,红外人体感应测试

const int sensorPin = 2;  // 传感器连接的数字引脚  
const int ledPin = 13;    // LED连接的数字引脚  
  
void setup() {  
  pinMode(sensorPin, INPUT);  // 设置传感器引脚为输入模式  
  pinMode(ledPin, OUTPUT);    // 设置LED引脚为输出模式  
  Serial.begin(9600);         // 开启串口通信,方便调试  
}  
  
void loop() {  
  int sensorState = digitalRead(sensorPin);  // 读取传感器状态  
  
  if (sensorState == HIGH) {  
    // 当检测到人体时  
    digitalWrite(ledPin, HIGH);  // 点亮LED  
    Serial.println("检测到人体!");  
  } else {  
    // 没有检测到人体  
    digitalWrite(ledPin, LOW);  // 熄灭LED  
    // 这里可以添加其他操作,如打印消息到串口  
  }  
  
  delay(100);  // 稍微延迟一下,减少CPU负担  
}

三。无源䗦鸣器测试,编程最好使用无源的,可以控制节拍

const int buzzerPin = 8;
int song[] = {
  
  /* 儿歌《小星星》*/
  277,277,415,415,466,466,415,
  370,370,330,330,311,311,277,
  415,415,370,370,330,330,311,
  415,415,370,370,330,330,311,
  277,277,415,415,466,466,415,
  370,370,330,330,311,311,277,
  
  /*《国际歌》*/
  370,494,466,554,494,370,311,415,330,
  415,554,494,466,415,370,330,311,
  370,494,466,554,494,370,311,415,330,
  415,554,494,466,554,659,494,
  622,554,466,415,466,494,415,466,370,
  370,330,370,415,415,554,494,466,
  554,554,466,370,370,330,370,622,494,
  415,466,494,466,554,494,415,370,
  622,554,494,370,311,415,330,
  554,494,466,415,370,
  370,622,554,370,494,466,
  466,415,415,415,554,554,
  622,554,494,370,311,415,330,330,
  554,494,466,415,370,622,311,622,
  740,659,622,554,622,659,
  659,622,622,554,554,494,
   
};
 
int noteDurations[] = {
 
  2,2,2,2,2,2,1,
  2,2,2,2,2,2,1,
  2,2,2,2,2,2,1,
  2,2,2,2,2,2,1,
  2,2,2,2,2,2,1,
  2,2,2,2,2,2,1,
  1,1,2,2,2,2,2,1,2,
  2,1,2,2,2,2,2,1,
  1,1,2,2,2,2,2,1,2,
  2,2,2,1,1,1,1,2,
  2,2,1,2,2,2,2,1,2,
  2,2,2,2,1,1,1,2,
  2,1,2,2,2,2,2,1,2,
  2,2,2,1,1,2,1,2,
  2,2,1,1,2,1,2,
  2,2,1,1,2,2,
  1,1,1,1,1,1,
  2,1,2,1,2,1,2,
  2,2,1,1,2,1,2,2,
  2,2,1,1,1,1,2,1,
  1,1,1,1,2,2,
  1,1,2,1,2,1,
 
};
void setup()
{
  for (int thisNote = 0; thisNote <154; thisNote++)
    {
    int noteDuration = 1000/noteDurations[thisNote];// 计算每个节拍的时间,以一个节拍一秒为例,四分之一拍就是1000/4毫秒,八分之一拍就是1000/8毫秒
    tone(buzzerPin, song[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.10; //每个音符间的停顿间隔,以该音符的130%为佳
      delay(pauseBetweenNotes);
      noTone(buzzerPin);
      }
}
void loop()
{
      setup();//反复唱
      
}

四。超声波测试

#define TrigPin 9  //输出
#define EchoPin 10  //输入
 
float Value_cm;
 
void setup()
{
	Serial.begin(9600);
	pinMode(TrigPin, OUTPUT);
	pinMode(EchoPin, INPUT);
}
void loop()
{
	digitalWrite(TrigPin, LOW);
	delayMicroseconds(2);
	digitalWrite(TrigPin, HIGH);
	delayMicroseconds(10);
	digitalWrite(TrigPin, LOW);
	Value_cm = float( pulseIn(EchoPin, HIGH) * 17 )/1000; //将回波时间换算成cm
  Serial.print("前方障碍距离当前位置:");
	Serial.print(Value_cm);
	Serial.println("cm");
	delay(1000);
}

五。有源蜂鸣器,有源蜂鸣器只有开和关两种状态,控制10s开10s关,适合做为警报。

cpp 复制代码
/*
  注意:低电平触发,给低电平就有声音,高电平没有声音,一定的工作频率
*/
/****************************************有源beep part****************************************/
#define beepPin 7                             //初始划蜂鸣器引脚
#define beepTimeInterval 1000                 //检测一次的时间间隔   
unsigned long beepTimes = 0;                  //记录设备运行时间
int beepCount = 0;                            //定义一个变量
/****************************************set up and loop part*********************************/
void setup() {
  Serial.begin(9600);                         //设置串口波特率为9600
  pinMode(beepPin, OUTPUT);                   //蜂鸣器引脚设置成输出模式
  Serial.println("设备上线!");                 //串口打印对应的值
}
void loop() {
  ControlBeep();                              //控制蜂鸣器工作
  delay(10000);  
}
/****************************************有源beep part****************************************/
/*控制蜂鸣器工作*/
void ControlBeep() {
  if (millis() - beepTimes >= beepTimeInterval) {
    beepTimes = millis();                    //一定时间执行一次
    beepCount++;
    if (beepCount % 2 == 1) {
      Serial.println("蜂鸣器打开!");
      digitalWrite(beepPin, LOW);            // 蜂鸣器工作
    } else {
      beepCount = 0;
      Serial.println("蜂鸣器关闭!");
      digitalWrite(beepPin, HIGH);          // 蜂鸣器停止工作
    }
  }

}

六,esp32和esp8266测试联网,虽然区分了,但验证可以互用。

cpp 复制代码
//const char* ssid = "CMCC-y4yk";
//const char* password = "hswy6bks";
#ifdef ESP32
#include <WiFi.h>

#define NTP "ntp.aliyun.com"	

//填写自己的WIFI信息
const char *ssid = "CMCC-y4yk";
const char *password = "hswy6bks";

void wifi_init(){
  
  WiFi.mode(WIFI_STA);//配置ESP32 工作模式
  WiFi.begin(ssid, password);
  Serial.println("正在连接 WiFi.");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected!");

}

void time_init() {
  struct tm timeinfo;  // 定义时间信息
  //如果获取失败,就开启联网模式,获取时间
  if (!getLocalTime(&timeinfo)){
    Serial.println("获取时间失败");
    //开启网络  
    wifi_init();
    // 从网络时间服务器上获取并设置时间
    configTime(8 * 3600, 0, NTP);//时区,夏令时,NTP地址
    return;
  }
  // 格式化输出:2021-10-24 23:00:44 Sunday
  Serial.println(&timeinfo, "%F %T %A"); 
  //   WiFi.disconnect(true);//在不需要开启网络的情况下,可以主动断开网络连接。
}

void setup(){
  Serial.begin(115200);
  wifi_init();

}

void loop()
{
  time_init();
  delay(1000);
}
#else
#include <ESP8266WiFi.h>
//  #include <WiFiClient.h>//3.0.2新增
//  #include <ESP8266HTTPClient.h>

// 获取网络时间相关库
#include <NTPClient.h>  //需要自行搜索并安装此库
#include <WiFiUdp.h>    //固件自带

const char* ssid = "CMCC-y4yk";  //填写个人WIFI信息
const char* password = "hswy6bks";

const char* ntpServer = "ntp.aliyun.com";
const char* ntpServer2 = "ntp2.aliyun.com";
const char* ntpServer3 = "time.nist.gov";
const long gmtOffset = 28800;  // 如果需要校准时区,可以根据需要进行调整
const int daylightOffset = 0;

uint32_t targetTime = 0;  // for next 1 second timeout

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServer, gmtOffset, daylightOffset);

void setup() {
  // put your setup code here, to run once:
  // 连接WiFi网络
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  // 启动NTP客户端
  timeClient.begin();
  targetTime = millis() + 1000;
}

void loop() {
  // put your main code here, to run repeatedly:
  if (targetTime < millis()) {
    targetTime = millis() + 1000;
    // 获取NTP时间
    timeClient.update();
    // 获取NTP时间的小时、分钟和秒
    int hours = timeClient.getHours();
    int minutes = timeClient.getMinutes();
    int seconds = timeClient.getSeconds();
    printf("%02d:%02d:%02d\r\n", hours, minutes, seconds);
  }
}
#endif

七。触摸感应器与有源蜂鸣器一起测试,蜂鸣器叫声很大,很有穿孔力,害怕扰人,测试时手指可以堵住孔

cpp 复制代码
#define beepPin 7                             // 蜂鸣器引脚  
#define touchPin 2                            // 假设的触摸开关引脚  
#define beepTimeInterval 1000                 // 检测一次的时间间隔  
  
unsigned long beepTimes = 0;                  // 记录设备运行时间  
  
void setup() {  
  Serial.begin(9600);                         // 设置串口波特率为9600  
  pinMode(beepPin, OUTPUT);                   // 蜂鸣器引脚设置成输出模式  
  pinMode(touchPin, INPUT_PULLUP);            // 触摸引脚设置为输入,并启用内部上拉电阻  
  Serial.println("设备上线!");  
}  
  
void loop() {  
  int touchState = digitalRead(touchPin);     // 读取触摸引脚的状态  
    
  if (touchState == HIGH) {                    // 如果触摸引脚被按下(假设低电平表示按下)  
    digitalWrite(beepPin, LOW);               // 蜂鸣器工作  
    Serial.println("蜂鸣器打开!");  
  } else {  
    digitalWrite(beepPin, HIGH);              // 蜂鸣器停止工作  
    Serial.println("蜂鸣器关闭!");  
  }  
    
  // 注意:这里去掉了原来的ControlBeep()函数和时间间隔控制,因为现在是即时响应触摸  
  delay(100);  // 可以稍微减少这里的延时,以便更快地响应触摸,但不建议完全去掉延时  
}  

参考:

机器人研究:基于安卓的视频遥控小车_废旧的android手机做了个机器人-CSDN博客

第二十一篇、基于Arduino uno,控制有源蜂鸣器和无源蜂鸣器发出声音------结果导向_arduino 无源蜂鸣器-CSDN博客Arduino ESP32 获取网络时间方法-CSDN博客

arduino学习笔记二十--无源蜂鸣器+ARDUINO开发板播放音乐_arduino无源蜂鸣器播放音乐-CSDN博客

相关推荐
小狗爱吃黄桃罐头20 分钟前
江协科技STM32学习- P14 示例程序(定时器定时中断和定时器外部时钟)
stm32·江科大stm32
@@庆1 小时前
stm32 PWR电源控制(修改主频&睡眠模式&停机模式&待机模式)
stm32·单片机·嵌入式硬件
JT灬新一1 小时前
STM32巡回研讨会总结(2024)
stm32·单片机·嵌入式硬件
爱桥代码的程序媛2 小时前
鸿蒙OpenHarmony【轻量系统芯片移植案例】标准系统方案之瑞芯微RK3568移植案例
嵌入式硬件·harmonyos·鸿蒙·鸿蒙系统·移植·openharmony·鸿蒙开发
Whappy0012 小时前
51单片机-DA(数字转模拟)
单片机·嵌入式硬件·51单片机
鸽子汤1972 小时前
想高效开发?从文件系统开始着手。。。
嵌入式硬件·物联网·硬件工程
Whappy0012 小时前
51单片机-AD(模拟信号转数字信号)-实验()
单片机·嵌入式硬件·51单片机
redcocal12 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
辰哥单片机设计15 小时前
门磁模块详解(防盗感应开关 STM32)
stm32·单片机·嵌入式硬件·传感器