Skip to content

Instantly share code, notes, and snippets.

@AlexLynd
Created December 23, 2024 15:54
Show Gist options
  • Save AlexLynd/3949a2ab20d28c343f4e70d7ce03b57c to your computer and use it in GitHub Desktop.
Save AlexLynd/3949a2ab20d28c343f4e70d7ce03b57c to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include "Adafruit_MPR121.h"
#include <DFRobotDFPlayerMini.h>
#include <Arduino.h>
#include <MPR121.h>
#include <Streaming.h>
#include "Adafruit_VL53L0X.h"
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
#define MOTOR_PIN MOSI
#define RESET_PIN A2
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
DFRobotDFPlayerMini myDFPlayer;
// Flag to indicate when the interrupt has occurred
volatile bool interruptFlag = false;
void interruptHandler() {
interruptFlag = true;
}
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
pinMode(A1, INPUT_PULLUP);
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, HIGH);
// while (!Serial) { // needed to keep leonardo/micro from starting too fast!
delay(500);
// }
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?"); delay(500);
reset_mcu();
}
Serial.println("MPR121 found!");
attachInterrupt(digitalPinToInterrupt(A1), interruptHandler, FALLING);
if (!myDFPlayer.begin(Serial1)) { // Initialize DFPlayer
Serial.println("DFPlayer Mini not detected");
reset_mcu();
}
myDFPlayer.volume(100);
if (!lox.begin()) {
Serial.println(F("Failed to boot VL53L0X"));
reset_mcu();
// while(1);
}
lox.startRangeContinuous();
pinMode(MOTOR_PIN, OUTPUT);
}
unsigned long coolDownTime = millis();
unsigned long ccTime = millis();
unsigned long tlToggle; // last millisTimestamp
unsigned long dlToggle; // current toggleTime
unsigned long currTime;
bool activated = true;
bool lactivated = false;
unsigned long lastDetectionTime = 0; // Store the last time the sensor triggered
bool presenceDetected = false; // Track if presence is detected
unsigned long debounceDelay = 500; // 250 ms debounce time
void loop() {
VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false);
// Serial.println(measure.RangeMi/lliMeter);
Serial.printf("Rng: %i Tl: %i\r\n",measure.RangeMilliMeter, (currTime-tlToggle));
currTime = millis();
if (measure.RangeStatus != 4 && measure.RangeMilliMeter >= 100 && measure.RangeMilliMeter <= 280) {
if (!presenceDetected) { // Check if the light hasn't already been triggered
// Check if at least 250 ms have passed since the last detection
if (currTime - lastDetectionTime >= debounceDelay) {
// Toggle the light status
activated = !activated;
digitalWrite(MOTOR_PIN, activated);
presenceDetected = true; // Presence detected, light remains on
lastDetectionTime = currTime; // Update last detection time
}
}
} else {
// If no presence is detected and the light is on, allow it to stay on until next detection
if (presenceDetected && currTime - lastDetectionTime >= debounceDelay) {
presenceDetected = false; // No presence anymore, light stays off after debounce time
}
}
// if (measure.RangeMilliMeter > (8000)) {
// coolDownTime = millis();
// }
// ccTime = millis();
// if (ccTime-coolDownTime > 500) {
// digitalWrite(MOTOR_PIN, LOW);
// }
//
// Check if interrupt flag is set
if (interruptFlag) {
interruptFlag = false; // Reset the interrupt flag
currtouched = cap.touched();
for (uint8_t i = 0; i < 12; i++) {
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i))) {
playAudio(i);
// Serial.print(i); Serial.println(" touched");
}
// if (!(currtouched & _BV(i)) && (lasttouched & _BV(i))) {
// Serial.print(i); Serial.println(" released");k.l
// }
}
lasttouched = currtouched;
}
}
void playAudio(int n) {
Serial.print("[ ] Playing track: "); Serial.println(n);
myDFPlayer.play(n);
}
void reset_mcu() {
Serial.println("[ ] Resetting MCU...");
digitalWrite(RESET_PIN, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment