xxxxxxxxxx
278
#include "Servo.h"
#define NUM_LEDS 12
#define LED_PIN 13
RGB leds[NUM_LEDS];
// Pin Definitions
Servo servoMotor1;
Servo servoMotor2;
int servoPin1 = 9;
int servoPin2 = 10;
int ledPin1 = 2;
int ledPin2 = 3;
int buttonPin = 4;
int sensorPin = A0;
int fxPin1 = 8;
int fxPin2 = 7;
int fxPin3 = 6;
int fxPin4 = 5;
int rgbPin = 13;
// Variables for sensor loops
int sensorLoopCount = 0; // Tracks how many sensor loops completed
bool inSensorLoop = false; // True if we're in an ongoing sensor loop
long sensorStartTime = 0; // Start time of current sensor loop
long durationAbove500 = 0; // How long the sensor has been above 500
long durationBelow500 = 0;
long timeBelow500 = 0; // How long the sensor has been below 500
unsigned long detectStartTime = 0; // Debounce start time
const unsigned long debounceThreshold = 500; // 500 ms debounce threshold
bool buttonStatus = false;
bool handStatus = false;
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B,LED_PIN,RGB> (leds,NUM_LEDS);
FastLED.setBrightness(255);
// Servo setup
servoMotor1.attach(servoPin1);
servoMotor2.attach(servoPin2);
// Pin mode setup
pinMode(fxPin1, OUTPUT);
pinMode(fxPin2, OUTPUT);
pinMode(fxPin3, OUTPUT);
pinMode(fxPin4, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(rgbPin, OUTPUT);
digitalWrite(fxPin1, HIGH);
digitalWrite(fxPin2, HIGH);
digitalWrite(fxPin3, HIGH);
digitalWrite(fxPin4, HIGH);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(rgbPin, HIGH);
servoMotor1.write(0);
servoMotor2.write(0);
}
void loop() {
led[]= RGB ::Red;
FastLED.show();
checkSensor(); // Check the sensor value and handle logic
handleButton(); // Check if button is pressed
Serial.print("Button: ");
Serial.print(buttonStatus);
Serial.print(" | ");
Serial.print("Hand: ");
Serial.println(handStatus);
delay(10);// Small delay to reduce noise
}
// Function to check sensor and handle sensor loop logic
void checkSensor() {
int sensorValue = analogRead(sensorPin); // Read sensor value
if (sensorValue > 500) { //detected something
//if (detectStartTime == 0) {
// detectStartTime = millis(); // Start debounce timer
// }
//if ((millis() - detectStartTime) > debounceThreshold) {
// Sensor value has stayed above 500 for the debounce threshold
// If we are not already in a sensor loop, start a new one
if (!inSensorLoop) {
inSensorLoop = true;
handStatus = true;
sensorStartTime = millis(); // Record start time
}
durationAbove500 = millis() - sensorStartTime; // Calculate duration above 500 durationAbove500 > debounceThreshold &&
// If sensor stays above 500 for more than 8 seconds, handle it
if (durationAbove500 > debounceThreshold && durationAbove500 < 8000) {
sensorLoopCount++; // Increment sensor loop count
Serial.println("Hand has been detected: " + String(sensorLoopCount));
handleSensorLoop(); // Check what to do based on loop count
}
// If the loop duration is between 500 ms and 8 seconds, consider it a valid sensorLoop
//if (durationAbove500 > 8000) {
///playSong2();
//blinkLEDs();
//rotateServosFast();
//handStatus = false;
//resetLoop(); // Reset after playing song and LED blink
//}
// }
}
// If sensor value is below 500
else {
handStatus = false;
//detectStartTime = 0; // Reset debounce timer when sensor value goes below 500
if (inSensorLoop) {
inSensorLoop = false; // End the current sensor loop
}
if (timeBelow500 == 0) {
timeBelow500 = millis(); // Start debounce timer
}
durationBelow500 = millis() - timeBelow500;
if (durationBelow500 > 8000) {
Serial.println("Detect nothing, reset loop");
resetLoop();// Start tracking time below 500
}
}
}
// Function to handle logic based on the completed sensor loops
void handleSensorLoop() {
if (sensorLoopCount <= 3) {
playSong1(); // Play song1 after 3 sensor loops
}
if (sensorLoopCount == 4) {
playSong3(); // Play song3 after 4 sensor loops
rotateServosFast();
blinkLEDs();
resetLoop(); // Reset after playing song3 and rotating servos
}
}
// Function to reset sensor loop variables
void resetLoop() {
sensorLoopCount = 0;
durationAbove500 = 0;
timeBelow500 = 0;
detectStartTime = 0;
inSensorLoop = false;
digitalWrite(fxPin1, HIGH);
digitalWrite(fxPin2, HIGH);
digitalWrite(fxPin3, HIGH);
digitalWrite(fxPin4, HIGH);
Serial.println("Loop has been reset");
delay(10);
}
// Function to play song1 on fxPin1
void playSong1() {
digitalWrite(fxPin1, LOW);
delay(500);
digitalWrite(fxPin1, HIGH);
}
// Function to play song2 on fxPin2
void playSong2() {
digitalWrite(fxPin2, LOW);
delay(500);
digitalWrite(fxPin2, HIGH);
}
// Function to play song3 on fxPin3
void playSong3() {
digitalWrite(fxPin3, LOW);
delay(500);
digitalWrite(fxPin3, HIGH);
}
// Function to blink LEDs 5 times
void blinkLEDs() {
for (int i = 0; i < 5; i++) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(100);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(100);
}
}
// Function to turn on LEDs for 5 seconds
void turnOnLEDs() {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(800);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
// Function to rotate servos fast (5 cycles)
void rotateServosFast() {
for (int i = 0; i < 5; i++) {
servoMotor1.write(90);
servoMotor2.write(90);
delay(200);
servoMotor1.write(0);
servoMotor2.write(0);
delay(200);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(100);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(100);
}
}
// Function to rotate servos slow (once, hold for 5 seconds)
void rotateServosSlow() {
servoMotor1.write(180);
servoMotor2.write(180);
delay(5000);
servoMotor1.write(0);
servoMotor2.write(0);
}
// Function to check if button is pressed and handle LED fade & sound
void handleButton() {
if (digitalRead(buttonPin) == LOW) {
buttonStatus =true;
playSong4();
fadeLEDs();
} else if (digitalRead(buttonPin) == HIGH) {
// Reset button status after a short delay (allow time for it to show in Serial Monitor)
delay(500); // Delay to keep buttonStatus true for a moment
buttonStatus = false; // Reset to false after the delay
}
}
// Function to play song4 on fxPin4
void playSong4() {
digitalWrite(fxPin4, LOW);
delay(500);
digitalWrite(fxPin4, HIGH);
}
// Function to fade LEDs in and out
void fadeLEDs() {
for (int brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(ledPin1, brightness);
analogWrite(ledPin2, brightness);
delay(30);
}
for (int brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(ledPin1, brightness);
analogWrite(ledPin2, brightness);
delay(30);
}
}