旋转编码器、DS1302 实时时钟、红外遥控模块、雨滴探测传感器 | 配合Arduino使用案例

旋转编码器

src=http___cbu01.alicdn.com_img_ibank_O1CN01sMzEe52N30BZzdOqH_!!2210293519906-0-cib.jpg&refer=http___cbu01.alicdn.webp

旋转编码器是一种用作检测自动化领域中的角度、速度、长度、位置和加速度的传感器。

有绝对式和增量式,这里使用增量式(相对)。
绝对输出只是周的当前位置,是他们成为角度传感器。增量输出关于轴的运动信息,需要进一步处理成速度、距离和位置等信息。

本案例通过左右旋转输出数值,按下清空数值。

/**
 **** 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);  // 串口通信用于输出 log
  pinMode(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 HIGH
  static int oldB = HIGH;  //set the oldB as HIGH
  int result = 0;
  int newA = digitalRead(dtPin);     //read the value of clkPin to newA
  int newB = digitalRead(clkPin);    //read the value of dtPin to newB
  if (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 实时时钟

u=3208019454,1113893018&fm=253&fmt=auto&app=138&f=JPEG.webp

可为程序实现时钟功能。下面程序将会一秒钟输出一次:2023-07-27 星期四 21:28:09

开始之前需要先安装依赖

image.png

/**
 **** 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-99
    Serial.print('-');
    if (now.month < 10) Serial.print('0');
    Serial.print(now.month);  // 01-12
    Serial.print('-');
    if (now.day < 10) Serial.print('0');
    Serial.print(now.day);  // 01-31
    Serial.print(' ');
    Serial.print(WeekDays[now.dow - 1]);  // 1-7
    Serial.print(' ');
    if (now.hour < 10) Serial.print('0');
    Serial.print(now.hour);  // 00-23
    Serial.print(':');
    if (now.minute < 10) Serial.print('0');
    Serial.print(now.minute);  // 00-59
    Serial.print(':');
    if (now.second < 10) Serial.print('0');
    Serial.print(now.second);  // 00-59
    Serial.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
// };
 

红外遥控模块

.jpg

该模块由两部分组成:信号发射器和接收器组成。

开始之前先安装依赖

image.png

/**
 **** 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();  // 允许接收下一个值
  } 
} 

雨滴探测传感器

u=99622666,1154093227&fm=253&fmt=auto&app=138&f=JPEG.webp

用来检测是否下雨,一般用于汽车的雨刷系统、只能照明和天窗系统

该模块由两部分组成:信号发射器和接收器组成。

/**
 **** 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);
}

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小明IO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值