Skip to content

Commit

Permalink
fix some error & add atoms3 4relay example
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyu-Zhao committed Jun 12, 2024
1 parent e56d49a commit f66e9fb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
10 changes: 5 additions & 5 deletions examples/Unit_4RELAY_M5Atom/Unit_4RELAY_M5Atom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
UNIT_4RELAY relay;

void setup() {
M5.begin(); // Init M5Atom. 初始化 M5Atom
M5.begin(true, false, false); // Init M5Atom. 初始化 M5Atom
Serial.print(relay.begin(&Wire, 26, 32));
relay.Init(1); // Set the lamp and relay to synchronous mode(Async =
// 0,Sync = 1). 将灯和继电器设为同步模式
Expand All @@ -45,10 +45,10 @@ void setup() {
char count_i = 0;

void loop() {
relay.relayAll(1);
delay(1000);
relay.relayAll(0);
delay(1000);
// relay.relayAll(1);
// delay(1000);
// relay.relayAll(0);
// delay(1000);

if (M5.Btn.wasPressed()) { // If button A is pressed. 如果按键A按下
if ((count_i < 4)) { // Control relays turn on/off in sequence.
Expand Down
69 changes: 69 additions & 0 deletions examples/Unit_4RELAY_M5AtomS3/Unit_4RELAY_M5AtomS3.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
*******************************************************************************
* Copyright (c) 2024 by M5Stack
* Equipped with M5AtomS3 sample source code
* 配套 M5AtomS3 示例源代码
* Visit for more information: https://docs.m5stack.com/en/unit/4relay
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/4relay
*
* Product: Unit 4RELAY.
* Date: 2024/6/12
*******************************************************************************
Please connect to port ,Control 4 relays and demonstrate the asynchronous
control relay LED 请连接端口,控制4继电器,并演示异步控制继电器LED
-------------------------------------------------------------------------------
RELAY control reg | 0x10
-----------------------------------------------------------------------------
Relay_ctrl_mode_reg[0] | R/W | System control
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| R | R | R | R | R | R | R | Sync Mode |
| -Sync Mode:0 LED&Relay Async
| -Sync Mode:1 LED&Relay Sync
---------------------------------------------------------------------------------
Relay_ctrl_mode_reg[1] | R/W | Relay & LED control
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| LED1| LED2| LED3| LED4| RLY1| RLY2| RLY3| RLY4|
-------------------------------------------------------------------------------*/
// Note: The relay can only be controlled in synchronous mode, if the relay is
// controlled in asynchronous mode, it will be invalid.
// 注意:只能在同步模式下控制继电器,如果在异步模式下对继电器进行控制将无效,.

#include "M5AtomS3.h"
#include "Unit_4RELAY.h"

UNIT_4RELAY relay;

void setup() {
auto cfg = M5.config();
M5.begin(cfg);
M5.Lcd.setCursor(25, 2);
M5.Lcd.print("4-RELAY UNIT\n");
Serial.print(relay.begin(&Wire, 2, 1));
relay.Init(1); // Set the lamp and relay to synchronous mode(Async =
// 0,Sync = 1). 将灯和继电器设为同步模式
M5.Lcd.printf(" Please pressed Btn. \n");
relay.relayAll(0);
}

char count_i = 0;

void loop() {
// relay.relayAll(1);
// delay(1000);
// relay.relayAll(0);
// delay(1000);

if (M5.BtnA.wasPressed()) { // If button A is pressed. 如果按键A按下
if ((count_i < 4)) { // Control relays turn on/off in sequence.
// 控制继电器依次接通/断开
Serial.printf("%d ON\n", count_i);
relay.relayWrite(count_i, 1);
} else if ((count_i >= 4)) {
Serial.printf("%d OFF\n", (count_i - 4));
relay.relayWrite((count_i - 4), 0);
}
count_i++;
if (count_i >= 8) count_i = 0;
}
M5.update();
}
17 changes: 8 additions & 9 deletions examples/Unit_4RELAY_M5Core2/Unit_4RELAY_M5Core2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ UNIT_4RELAY relay;
void setup() {
M5.begin(true, false, true, true); // Init M5Core2 And the I2C port(21,22).
// 初始化 M5Core2 和I2C(21,22)端口
M5.Lcd.setCursor(
80, 0, 4); // Set the cursor position to (80,0). 将光标位置设置为(80,0)
M5.Lcd.setCursor(80, 0,
4); // Set the cursor position to (80,0). 将光标位置设置为(80,0)
M5.Lcd.print("4-RELAY UNIT\n\n");
M5.Lcd.print(" Relay State: \n Sync Mode: ");
M5.Lcd.setCursor(0, 220, 2);
M5.Lcd.print("Independent switch Sync/Async ALL relay");
relay.begin();
relay.Init(0); // Set the lamp and relay to asynchronous mode(Async =
// 0,Sync = 1). 将灯和继电器设为非同步模式
}
Expand All @@ -50,16 +51,15 @@ char count_i = 0;
bool sync_flag = 0, all_flag = 0;

void loop() {
M5.update(); // Check button down state. 检测按键按下状态
M5.update(); // Check button down state. 检测按键按下状态
if (M5.BtnA.wasPressed()) { // If button A is pressed. 如果按键A按下
M5.Lcd.fillRect(160, 50, 100, 20, BLACK);
M5.Lcd.setCursor(160, 50, 4);
if (count_i < 4) {
M5.Lcd.printf("%d ON", count_i + 1);
if (sync_flag) {
relay.relayWrite(
count_i,
1); // Open the relay at Count_i. 打开count_i处的继电器
relay.relayWrite(count_i,
1); // Open the relay at Count_i. 打开count_i处的继电器
} else
relay.ledWrite(count_i,
1); // Turn on count_I to get led lights.
Expand All @@ -71,9 +71,8 @@ void loop() {
(count_i - 4),
0); // Close the relay at Count_i. 关闭count_i处的继电器
} else
relay.ledWrite(
(count_i - 4),
0); // Turn off the COUNt_I leds. 关闭count_i出得led灯
relay.ledWrite((count_i - 4),
0); // Turn off the COUNt_I leds. 关闭count_i出得led灯
}
count_i++;
if (count_i >= 8) count_i = 0;
Expand Down

0 comments on commit f66e9fb

Please sign in to comment.