Skip to content

Commit 8af0926

Browse files
committed
v1.1.3, Add some sensors support
1 parent 7abcc1c commit 8af0926

File tree

8 files changed

+979
-1
lines changed

8 files changed

+979
-1
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MatrixMiniR4
2-
version=1.1.2
2+
version=1.1.3
33
author=KKITC
44
maintainer=Matrix Robotics & KKITC <[email protected]>
55
sentence=An Arduino R4 based Robotics controller made by MATRIX Robotics.

src/Modules/MiniR4Digital.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#define MiniR4Digital_H
33

44
#include "MiniR4HC04.h"
5+
#include "MiniR4_DHT11.h"
6+
#include "MiniR4_DS18B20.h"
7+
#include "MiniR4_Grove_US.h"
8+
// #include "MiniR4_Grove_TM1637.h"
59
#include <Arduino.h>
610

711
template<uint8_t PIN1, uint8_t PIN2> class MiniR4Digital
@@ -58,6 +62,10 @@ template<uint8_t PIN1, uint8_t PIN2> class MiniR4Digital
5862
}
5963

6064
MiniR4HC04<PIN1, PIN2> US;
65+
MiniR4DHT11<PIN1, PIN2> DHT11;
66+
MiniR4DS18B20<PIN1, PIN2> DS18B20;
67+
MiniR4_Grove_US<PIN1, PIN2> GroveUS;
68+
// MiniR4_Grove_TM1637<PIN1, PIN2> GroveTM1637;
6169

6270
private:
6371
uint8_t _pin1;

src/Modules/MiniR4I2C.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "MiniR4MxCtrlExt.h"
77
#include "MiniR4MotionExt.h"
88
#include "MiniR4TCS34725.h"
9+
#include "MiniR4_GroveI2C_BME280.h"
910

1011
template<uint8_t ID, TwoWire* WIRE> class MiniR4I2C
1112
{
@@ -16,17 +17,20 @@ template<uint8_t ID, TwoWire* WIRE> class MiniR4I2C
1617
MXLaser._ch = ID;
1718
MXColor._ch = ID;
1819
MXCtrl._ch = ID;
20+
GroveBME280._ch = ID;
1921

2022
MXMotion._pWire = WIRE;
2123
MXLaser._pWire = WIRE;
2224
MXColor._pWire = WIRE;
2325
MXCtrl._pWire = WIRE;
26+
GroveBME280._pWire = WIRE;
2427
}
2528

2629
MatrixMotion MXMotion;
2730
MatrixLaser MXLaser;
2831
MatrixColor MXColor;
2932
MatrixController MXCtrl;
33+
GroveI2C_BME280 GroveBME280;
3034

3135
// Adafruit_TCS34725 MXColor = Adafruit_TCS34725(
3236
// TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X, TCS34725_ADDRESS, WIRE, ID);

src/Modules/MiniR4_DHT11.h

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* DHT11.h DHT11.cpp
3+
* Header file for the DHT11 library, providing functionalities to interface with
4+
* the DHT11 temperature & humidity sensor.
5+
*
6+
* Author: Dhruba Saha
7+
* Version: 2.1.0
8+
* License: MIT
9+
* 241016 Anthony:
10+
* Add float temp return.
11+
*/
12+
#ifndef MINIR4_DHT11_H
13+
#define MINIR4_DHT11_H
14+
15+
#include <Arduino.h>
16+
17+
template <uint8_t PIN1, uint8_t PIN2> class MiniR4DHT11 {
18+
public:
19+
MiniR4DHT11() {
20+
_pin = PIN2; //目前都在DI_R位置,未來可以改成LEFT跟MATRIX現行感應器一樣
21+
pinMode(_pin, OUTPUT);
22+
digitalWrite(_pin, HIGH);
23+
}
24+
25+
static const int ERROR_CHECKSUM = 254; // Error code for checksum mismatch.
26+
static const int ERROR_TIMEOUT = 253; // Error code for timeout.
27+
// static const int ERROR_NOT_READY = 252; // Error code for not ready (delay not passed).
28+
static const int TIMEOUT_DURATION = 1000; // Timeout duration in milliseconds.
29+
30+
/**
31+
* Sets the delay between consecutive sensor readings.
32+
* Default is 250ms if not set.
33+
*/
34+
void setDelay(unsigned long delay) {
35+
_delayMS = delay;
36+
}
37+
38+
/**
39+
* Reads and returns the temperature from the DHT11 sensor.
40+
*
41+
* @return Temperature in Celsius, or error codes if reading fails.
42+
*/
43+
float readTemperature() {
44+
byte data[5];
45+
int error = readRawData(data);
46+
if (error != 0) {
47+
return error;
48+
}
49+
50+
float t = (float)data[3] / 10;
51+
return data[2] + t;
52+
}
53+
54+
/**
55+
* Reads and returns the humidity from the DHT11 sensor.
56+
*
57+
* @return Humidity percentage, or error codes if reading fails.
58+
*/
59+
int readHumidity() {
60+
byte data[5];
61+
int error = readRawData(data);
62+
if (error != 0) {
63+
return error;
64+
}
65+
66+
return data[0];
67+
}
68+
69+
/**
70+
* Reads temperature and humidity.
71+
*
72+
* @param temperature Reference to store temperature.
73+
* @param humidity Reference to store humidity.
74+
* @return 0 on success, otherwise error code.
75+
*/
76+
int readTemperatureHumidity(int &temperature, int &humidity) {
77+
byte data[5];
78+
int error = readRawData(data);
79+
if (error != 0) {
80+
return error;
81+
}
82+
83+
humidity = data[0];
84+
float t = (float)data[3] / 10;
85+
temperature = data[2] + t;
86+
return 0;
87+
}
88+
89+
/**
90+
* Converts error codes into human-readable strings.
91+
*
92+
* @param errorCode Error code to convert.
93+
* @return String describing the error.
94+
*/
95+
String getErrorString(int errorCode) {
96+
switch (errorCode) {
97+
case ERROR_TIMEOUT:
98+
return "Error 253: Timeout reading from DHT11.";
99+
case ERROR_CHECKSUM:
100+
return "Error 254: Checksum mismatch reading from DHT11.";
101+
// case ERROR_NOT_READY:
102+
// return "Error 252: Not ready to read from DHT11. Delay not passed.";
103+
default:
104+
return "Error: Unknown.";
105+
}
106+
}
107+
108+
private:
109+
int _pin; // Pin number for the DHT11 sensor.
110+
unsigned long _delayMS = 250; // Delay between readings.
111+
112+
/**
113+
* Sends a start signal to the DHT11 sensor.
114+
*/
115+
void startSignal() {
116+
pinMode(_pin, OUTPUT);
117+
digitalWrite(_pin, LOW);
118+
delay(18);
119+
digitalWrite(_pin, HIGH);
120+
delayMicroseconds(40);
121+
pinMode(_pin, INPUT);
122+
}
123+
124+
/**
125+
* Reads raw data from the DHT11 sensor.
126+
*
127+
* @param data Array to store the raw data.
128+
* @return 0 on success, otherwise error code.
129+
*/
130+
int readRawData(byte data[5]) {
131+
delay(_delayMS);
132+
// if (millis() - _lastReadTime < _delayMS) {
133+
// return ERROR_NOT_READY; // if not reach delayMS, bypass.
134+
// }
135+
136+
startSignal();
137+
unsigned long timeout_start = millis();
138+
139+
while (digitalRead(_pin) == HIGH) {
140+
if (millis() - timeout_start > TIMEOUT_DURATION) {
141+
return ERROR_TIMEOUT;
142+
}
143+
}
144+
145+
delayMicroseconds(80);
146+
if (digitalRead(_pin) == HIGH) {
147+
delayMicroseconds(80);
148+
for (int i = 0; i < 5; i++) {
149+
data[i] = readByte();
150+
}
151+
152+
if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
153+
// _lastReadTime = millis(); // update daley timer
154+
return 0; // Success
155+
} else {
156+
return ERROR_CHECKSUM;
157+
}
158+
}
159+
return ERROR_TIMEOUT;
160+
}
161+
162+
/**
163+
* Reads a byte of data from the sensor.
164+
*
165+
* @return The byte read from the sensor.
166+
*/
167+
byte readByte() {
168+
byte value = 0;
169+
for (int i = 0; i < 8; i++) {
170+
while (digitalRead(_pin) == LOW);
171+
delayMicroseconds(30);
172+
if (digitalRead(_pin) == HIGH) {
173+
value |= (1 << (7 - i));
174+
}
175+
while (digitalRead(_pin) == HIGH);
176+
}
177+
return value;
178+
}
179+
};
180+
181+
#endif // MINIR4_DHT11_H

0 commit comments

Comments
 (0)