Skip to content

Commit b031d6a

Browse files
committed
first
0 parents  commit b031d6a

File tree

4 files changed

+277
-0
lines changed

4 files changed

+277
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# M5Cardputer Movement Detector Alarm
2+
3+
An alarm system using the M5Cardputer platform, designed to detect movement using a PIR sensor. When motion is detected, the system will sound an alarm and indicate the detection through the screen/builtin LED.
4+
5+
## Components
6+
7+
- M5CardPuter
8+
- PIR Sensor [Grove](https://wiki.seeedstudio.com/Grove-Digital-PIR-Sensor/) connected to Pin 1
9+
10+
It should work with any digital PIR Sensor
11+
12+
## Features
13+
14+
- Detects movement using a PIR sensor
15+
- Sounds an alarm with adjustable volume
16+
- Displays detection status on the M5Cardputer screen
17+
- Built-in RGB LED indicates the status
18+
- Control over display brightness and volume
19+
20+
## Installation
21+
22+
- <b>M5Burner</b> : Search into M5CARDPUTER section and simply burn it
23+
- <b>Old school</b> : Build or take the firmware.bin from the github release and flash it
24+
25+
## Usage
26+
27+
1. The PIR sensor continuously monitors for movement.
28+
29+
2. When movement is detected, the RGB LED turns red, and an alarm sounds with a frequency pattern.
30+
31+
# Controls
32+
33+
- <b>Volume down:</b> Arrow Down
34+
- <b>Volume up: </b>Arrow Up
35+
- <b>Brightness down:</b> Arrow Left
36+
- <b>Brightness up:</b> Arrow Right

platformio.ini

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:m5stack-stamps3]
12+
platform = espressif32
13+
board = m5stack-stamps3
14+
framework = arduino
15+
lib_deps =
16+
m5stack/M5Cardputer@^1.0.3
17+
fastled/FastLED@^3.3.3

src/main.cpp

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#include <Arduino.h>
2+
#include <M5Cardputer.h>
3+
#include <FastLED.h>
4+
5+
#define PIN_PIR 1 // Movement Detector
6+
#define PIN_LED 21 // Builtin
7+
#define NUM_LEDS 1
8+
9+
CRGB leds[NUM_LEDS]; // init RGB LED builtin
10+
int lastState = -1; // Save last detection state
11+
12+
void displayMainView() {
13+
14+
// Config
15+
M5Cardputer.Display.setRotation(1);
16+
M5Cardputer.Display.setTextColor(GREEN);
17+
M5Cardputer.Display.setFont(&fonts::Orbitron_Light_24);
18+
M5Cardputer.Display.setTextSize(1);
19+
20+
// Main Title
21+
M5Cardputer.Display.drawRoundRect(0, 0, 240, 30, 5, TFT_DARKGREY); // frame around titlte
22+
M5Cardputer.Display.setTextColor(TFT_LIGHTGREY);
23+
M5Cardputer.Display.setTextSize(0.8);
24+
M5Cardputer.Display.setCursor(16, 3);
25+
M5Cardputer.Display.printf("Movement Detector");
26+
27+
// Menu
28+
M5Cardputer.Display.drawRoundRect(0, 108, 240, 30, 4, TFT_DARKGREY); // frame around controls
29+
M5Cardputer.Display.setTextSize(0.65);
30+
M5Cardputer.Display.setCursor(14, 112);
31+
M5Cardputer.Display.printf("Brightness : < > Vol : v ");
32+
33+
// Volume area
34+
M5Cardputer.Display.drawRoundRect(153, 40, 82, 58, 5, TFT_LIGHTGREY);
35+
M5Cardputer.Display.setCursor(162, 45);
36+
M5Cardputer.Display.setTextSize(0.5);
37+
M5Cardputer.Display.setTextColor(TFT_DARKGREY);
38+
M5Cardputer.Display.println("VOLUME");
39+
40+
// Volume value
41+
M5Cardputer.Display.setCursor(165, 60);
42+
M5Cardputer.Display.setTextSize(1);
43+
M5Cardputer.Display.setTextColor(TFT_LIGHTGREY);
44+
M5Cardputer.Speaker.setVolume(40);
45+
M5Cardputer.Display.print(M5Cardputer.Speaker.getVolume());
46+
47+
// Dessiner la lettre "V" à l'envers pour volume controls
48+
int startX = 218; // Position de départ X
49+
int startY = 119; // Position de départ Y
50+
int height = 8; // Hauteur de la lettre
51+
int width = 10; // Largeur de la lettre
52+
M5.Lcd.drawLine(startX, startY + height, startX + width / 2, startY, TFT_LIGHTGREY); // Ligne gauche
53+
M5.Lcd.drawLine(startX + width / 2, startY, startX + width, startY + height, TFT_LIGHTGREY); // Ligne droite
54+
}
55+
56+
void displayVolume(uint8_t volume) {
57+
M5Cardputer.Display.fillRect(155, 65, 68, 25, BLACK); // reset area
58+
M5Cardputer.Display.setTextSize(1);
59+
M5Cardputer.Display.setTextColor(TFT_LIGHTGREY);
60+
M5Cardputer.Display.setCursor(165, 60);
61+
M5Cardputer.Display.print(volume);
62+
}
63+
64+
void displayDetection(int state) {
65+
if (state == LOW) { // No move
66+
M5Cardputer.Display.fillRect(0, 40, 145, 58, BLACK);
67+
M5Cardputer.Display.drawRoundRect(5, 40, 140, 58, 5, GREEN);
68+
M5Cardputer.Display.setCursor(22, 55);
69+
M5Cardputer.Display.setTextColor(GREEN);
70+
M5Cardputer.Display.setTextSize(0.9);
71+
M5Cardputer.Display.println("No move");
72+
return;
73+
}
74+
75+
// Movement detected
76+
M5Cardputer.Display.fillRect(0, 40, 145, 58, BLACK); // reset area
77+
M5Cardputer.Display.fillRoundRect(5, 40, 140, 58, 5, RED); // frame around
78+
M5Cardputer.Display.setTextColor(TFT_LIGHTGREY);
79+
M5Cardputer.Display.setCursor(23, 48);
80+
M5Cardputer.Display.setTextSize(0.75);
81+
M5Cardputer.Display.println("Movement");
82+
M5Cardputer.Display.setCursor(30, 70);
83+
M5Cardputer.Display.println("detected");
84+
}
85+
86+
void toggleLED() {
87+
if (M5Cardputer.Display.getBrightness() != 0) {
88+
FastLED.show(); // If screen is on, led is on
89+
} else {
90+
FastLED.clear(true); // led off
91+
}
92+
}
93+
94+
void handleKeyboard() {
95+
if (M5Cardputer.Keyboard.isChange()) {
96+
if (M5Cardputer.Keyboard.isPressed()) {
97+
// VOLUME UP
98+
if (M5Cardputer.Keyboard.isKeyPressed(';')) {
99+
uint8_t volume = M5Cardputer.Speaker.getVolume();
100+
if (volume+20 < 255) {
101+
volume += 20;
102+
} else {
103+
volume = 255;
104+
}
105+
displayVolume(volume);
106+
M5Cardputer.Speaker.setVolume(volume);
107+
}
108+
109+
// VOLUME DOWN
110+
else if (M5Cardputer.Keyboard.isKeyPressed('.')) {
111+
uint8_t volume = M5Cardputer.Speaker.getVolume();
112+
if (volume-20 > 0) {
113+
volume -= 20;
114+
} else {
115+
volume = 0;
116+
}
117+
displayVolume(volume);
118+
M5Cardputer.Speaker.setVolume(volume);
119+
}
120+
121+
// BRIGHTNESS UP
122+
else if (M5Cardputer.Keyboard.isKeyPressed('/')) {
123+
uint8_t brightness = M5Cardputer.Display.getBrightness();
124+
if (brightness+20 < 255) {
125+
brightness += 20;
126+
} else {
127+
brightness = 255;
128+
}
129+
M5Cardputer.Display.setBrightness(brightness);
130+
}
131+
132+
// BRIGHTNESS DOWN
133+
else if (M5Cardputer.Keyboard.isKeyPressed(',')) {
134+
uint8_t brightness = M5Cardputer.Display.getBrightness();
135+
if (brightness-20 > 0) {
136+
brightness -= 20;
137+
} else {
138+
brightness = 0;
139+
}
140+
M5Cardputer.Display.setBrightness(brightness);
141+
}
142+
}
143+
}
144+
}
145+
146+
/*
147+
##############################################################################################
148+
# #
149+
# SETUP & LOOP #
150+
# #
151+
##############################################################################################
152+
*/
153+
154+
void setup() {
155+
auto cfg = M5.config(); // M5 default config
156+
M5Cardputer.begin(cfg, true);
157+
M5Cardputer.Speaker.setVolume(10);
158+
159+
// Initialiser mvmnt detector
160+
pinMode(PIN_PIR, INPUT); // Configurer la broche du détecteur PIR comme entrée
161+
162+
// Initialiser la LED BUILTIN à l'état éteint
163+
FastLED.addLeds<WS2812, PIN_LED, GRB>(leds, 1);
164+
165+
// Display the main frame
166+
displayMainView();
167+
}
168+
169+
void loop() {
170+
171+
// Update state
172+
M5Cardputer.update();
173+
174+
// Lire l'état du détecteur de mouvement
175+
int motionState = digitalRead(PIN_PIR);
176+
177+
// Handle brightness/volume controls
178+
handleKeyboard();
179+
180+
// Si une présence est détectée
181+
if (motionState == HIGH) {
182+
// Red LED
183+
leds[0] = CRGB::Red;
184+
toggleLED();
185+
186+
// Set alarm
187+
M5Cardputer.Speaker.tone(4000, 20);
188+
M5Cardputer.Speaker.tone(3000, 10);
189+
190+
} else {
191+
// Green LED
192+
leds[0] = CRGB::Green;
193+
toggleLED();
194+
}
195+
196+
// Render only if motionState has changed
197+
if (lastState != motionState) {
198+
displayDetection(motionState);
199+
lastState = motionState;
200+
}
201+
202+
delay(20);
203+
}

0 commit comments

Comments
 (0)