This circuit is designed to interface an Arduino Nano with an APDS-9960 RGB and Gesture Sensor, a KY-038 sound sensor module, a 4-channel relay module, and a power transformer through various terminal PCBs. The Arduino Nano serves as the central processing unit, reading gestures from the APDS-9960 sensor and sound signals from the KY-038 to control the state of the relays. The relays are used to switch loads on and off, and the power transformer is included to step down or step up voltage as required for the circuit.
#include <Wire.h>
#include <SparkFun_APDS9960.h> // Include the SparkFun Gesture Sensor library
SparkFun_APDS9960 apds = SparkFun_APDS9960();
const int soundSensorPin = 2; // Sound sensor digital pin
const int relayPins[4] = {4, 5, 6, 7}; // Relay pins for controlling the 4 loads
bool loadStates[4] = {false, false, false, false}; // Track each load's state (on/off)
unsigned long lastClapTime = 0;
int clapCount = 0;
const int clapTimeout = 800; // Timeout between claps (in milliseconds)
void setup() {
Serial.begin(9600);
// Initialize the gesture sensor
if (apds.init()) {
Serial.println("APDS-9960 initialized successfully");
} else {
Serial.println("Error initializing APDS-9960");
}
// Enable gesture mode
if (apds.enableGestureSensor(true)) {
Serial.println("Gesture sensor enabled");
} else {
Serial.println("Gesture sensor failed to enable");
}
// Set up relay pins
for (int i = 0; i < 4; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], HIGH); // Relays are off by default (HIGH = off)
}
// Set up sound sensor pin
pinMode(soundSensorPin, INPUT);
Serial.println("System Initialized: Waiting for gestures or claps.");
}
void loop() {
// Handle gesture control
if (apds.isGestureAvailable()) {
int gesture = apds.readGesture();
handleGesture(gesture);
}
// Handle clap detection
if (digitalRead(soundSensorPin) == HIGH) {
unsigned long currentTime = millis();
if (currentTime - lastClapTime > clapTimeout) {
lastClapTime = currentTime;
clapCount++;
Serial.print("Clap detected! Clap count: ");
Serial.println(clapCount);
}
}
// Evaluate clap counts after timeout
if (millis() - lastClapTime > clapTimeout && clapCount > 0) {
handleClap(clapCount);
clapCount = 0;
}
}
// Function to handle gestures
void handleGesture(int gesture) {
switch (gesture) {
case DIR_UP:
Serial.println("Gesture UP - Toggle Load 1");
toggleRelay(0); // Toggle Load 1
break;
case DIR_DOWN:
Serial.println("Gesture DOWN - Toggle Load 1");
toggleRelay(0); // Toggle Load 1
break;
case DIR_LEFT:
Serial.println("Gesture LEFT - Toggle Load 2");
toggleRelay(1); // Toggle Load 2
break;
case DIR_RIGHT:
Serial.println("Gesture RIGHT - Toggle Load 2");
toggleRelay(1); // Toggle Load 2
break;
default:
Serial.println("Unknown Gesture");
}
}
// Function to handle claps
void handleClap(int count) {
if (count == 1) {
Serial.println("Single Clap - Toggle Load 3");
toggleRelay(2); // Toggle Load 3
} else if (count == 2) {
Serial.println("Double Clap - Toggle Load 4");
toggleRelay(3); // Toggle Load 4
} else {
Serial.println("Multiple Claps Detected");
}
}
// Function to toggle relays
void toggleRelay(int relay) {
loadStates[relay] = !loadStates[relay]; // Toggle the state
digitalWrite(relayPins[relay], loadStates[relay] ? LOW : HIGH); // LOW = ON, HIGH = OFF
Serial.print("Load ");
Serial.print(relay + 1);
Serial.println(loadStates[relay] ? " ON" : " OFF");
}
This code is designed to run on the Arduino Nano and controls the circuit based on gesture inputs from the APDS-9960 sensor and sound detection from the KY-038 module. It toggles the state of the relays based on these inputs, with specific gestures and clap patterns mapped to different relay channels.