旋转编码器
旋转编码器是一种用作检测自动化领域中的角度、速度、长度、位置和加速度的传感器。
有绝对式和增量式,这里使用增量式(相对)。
绝对输出只是周的当前位置,是他们成为角度传感器。增量输出关于轴的运动信息,需要进一步处理成速度、距离和位置等信息。
本案例通过左右旋转输出数值,按下清空数值。
/****** Arduino 接线 ***** Arduino 传感器* VCC 5v* GND GND* 2 CLK * 3 DT * 4 SW ***********************/int clkPin = 2;
int dtPin = 3;
int swPin = 4;void setup() {Serial.begin(9600); // 串口通信用于输出 logpinMode(clkPin, INPUT);pinMode(dtPin, INPUT);pinMode(swPin, INPUT);digitalWrite(swPin, HIGH);
}
// 旋转就会不断变大或者变小,按下就会清零
int encoderVal = 0;
void loop() {int change = getEncoderTurn();encoderVal = encoderVal + change;if (digitalRead(swPin) == LOW) {// 按钮按下encoderVal = 0;}Serial.println(encoderVal);
}int getEncoderTurn(void) {static int oldA = HIGH; //set the oldA as HIGHstatic int oldB = HIGH; //set the oldB as HIGHint result = 0;int newA = digitalRead(dtPin); //read the value of clkPin to newAint newB = digitalRead(clkPin); //read the value of dtPin to newBif (newA != oldA || newB != oldB) //if the value of clkPin or the dtPin has changed{if (oldA == HIGH && newA == LOW) {result = (oldB * 2 - 1);}}oldA = newA;oldB = newB;return result;
}
DS1302 实时时钟
可为程序实现时钟功能。下面程序将会一秒钟输出一次:2023-07-27 星期四 21:28:09
开始之前需要先安装依赖
/****** Arduino 接线 ***** Arduino 传感器* VCC 3.3v* GND GND* 5 RST * 6 SDA/DAT * 7 SCL/CLK ***********************/#include <Ds1302.h>uint8_t RST_PIN = 5;
uint8_t SDA_PIN = 6;
uint8_t SCL_PIN = 7;// DS1302 RTC instance
Ds1302 rtc(RST_PIN, SCL_PIN, SDA_PIN);const static char* WeekDays[] = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"
};void setup() {Serial.begin(9600);// 初始化rtc.init();/** 设置时间 **/// 变量定义见代码底部枚举Ds1302::DateTime dt = {.year = 23,.month = Ds1302::MONTH_JUL,.day = 27,.hour = 21,.minute = 26,.second = 0,// 周.dow = Ds1302::DOW_THU};rtc.setDateTime(&dt);
}void loop() {// 获取当前时间Ds1302::DateTime now;rtc.getDateTime(&now);static uint8_t last_second = 0;if (last_second != now.second) {last_second = now.second;Serial.print("20");Serial.print(now.year); // 00-99Serial.print('-');if (now.month < 10) Serial.print('0');Serial.print(now.month); // 01-12Serial.print('-');if (now.day < 10) Serial.print('0');Serial.print(now.day); // 01-31Serial.print(' ');Serial.print(WeekDays[now.dow - 1]); // 1-7Serial.print(' ');if (now.hour < 10) Serial.print('0');Serial.print(now.hour); // 00-23Serial.print(':');if (now.minute < 10) Serial.print('0');Serial.print(now.minute); // 00-59Serial.print(':');if (now.second < 10) Serial.print('0');Serial.print(now.second); // 00-59Serial.println();}delay(1000);
}// * 月份枚举
// enum MONTH : uint8_t {
// MONTH_JAN = 1,
// MONTH_FEB = 2,
// MONTH_MAR = 3,
// MONTH_APR = 4,
// MONTH_MAY = 5,
// MONTH_JUN = 6,
// MONTH_JUL = 7,
// MONTH_AUG = 8,
// MONTH_SET = 9,
// MONTH_OCT = 10,
// MONTH_NOV = 11,
// MONTH_DEC = 12
// };// * 周枚举
// enum DOW : uint8_t {
// DOW_MON = 1,
// DOW_TUE = 2,
// DOW_WED = 3,
// DOW_THU = 4,
// DOW_FRI = 5,
// DOW_SAT = 6,
// DOW_SUN = 7
// };
红外遥控模块
该模块由两部分组成:信号发射器和接收器组成。
开始之前先安装依赖
/****** Arduino 接线 ***** Arduino 传感器* VCC 5v* GND GND* A0 D0 ***********************/#include <IRremote.h>int pin = A0;IRrecv irrecv(pin);
decode_results results;void setup() {Serial.begin(115200); IrReceiver.begin(pin);
}void loop() {if (IrReceiver.decode()) {// IrReceiver.decodedIRData.command 每个按钮都是一个不一样的数字,根据数字判断即可Serial.println(IrReceiver.decodedIRData.command);IrReceiver.resume(); // 允许接收下一个值}
}
雨滴探测传感器
用来检测是否下雨,一般用于汽车的雨刷系统、只能照明和天窗系统
该模块由两部分组成:信号发射器和接收器组成。
/****** Arduino 接线 ***** Arduino 传感器* VCC 5v* GND GND* A0 A0 雨滴越多值越小* A1 D0 达到阈值时输入低电平***********************/ int pin1 = A0;
int pin2 = A1; int pin1Val = 0;
int pin2Val = 0; void setup() {Serial.begin(9600); pinMode(pin2, INPUT);
}void loop() { pin1Val = analogRead(pin1);pin2Val = digitalRead(pin2);Serial.print(pin1Val); // 雨滴越多值越小Serial.print(" - "); Serial.println(pin2Val); // 达到阈值时输入低电平delay(200);
}