// Define the pins for the RGB LED strips
const int redPin1 = 13;
const int greenPin1 = 12;
const int bluePin1 = 14;
const int redPin2 = 25;
const int greenPin2 = 32;
const int bluePin2 =35;
const int redPin3 = 34;
const int greenPin3 = 39;
const int bluePin3 = 36;
const int knockPin = 4; // The input pin for the piezoelectric sensor
int threshold = 500; // The minimum threshold value for detecting a knock
int intensity; // The intensity of the knock, from 0-255
void setup() {
// Set the pins for the RGB LED strips to output mode
pinMode(redPin1, OUTPUT);
pinMode(greenPin1, OUTPUT);
pinMode(bluePin1, OUTPUT);
pinMode(redPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
pinMode(bluePin2, OUTPUT);
pinMode(redPin3, OUTPUT);
pinMode(greenPin3, OUTPUT);
pinMode(bluePin3, OUTPUT);
// Set the pin for the piezoelectric sensor to input mode
pinMode(knockPin, INPUT);
// Start the serial communication
Serial.begin(9600);
}
void loop() {
// Read the piezoelectric sensor input
int sensorValue = analogRead(knockPin);
// If the sensor value exceeds the threshold, a knock has been detected
if (sensorValue > threshold) {
// Calculate the knock intensity, which is proportional to the sensor value
intensity = map(sensorValue, threshold, 1023, 0, 255);
}
// Print the intensity value to the serial monitor (for debugging purposes)
Serial.println(intensity);
// Determine the color for the RGB LED strips based on the intensity of the knock
if (intensity < 85) { // Low intensity knock
analogWrite(redPin1, intensity);
analogWrite(greenPin1, 0);
analogWrite(bluePin1, intensity);
analogWrite(redPin2, 0);
analogWrite(greenPin2, intensity);
analogWrite(bluePin2, intensity);
analogWrite(redPin3, intensity);
analogWrite(greenPin3, 0);
analogWrite(bluePin3, intensity);
}
else if (intensity >= 85 && intensity < 170) { // Medium intensity knock
analogWrite(redPin1, 0);
analogWrite(greenPin1, intensity - 85);
analogWrite(bluePin1, 170 - intensity);
analogWrite(redPin2, 85 - (intensity - 85));
analogWrite(greenPin2, 170 - intensity);
analogWrite(bluePin2, intensity - 85);
analogWrite(redPin3, 170 - intensity);
analogWrite(greenPin3, intensity - 85);
analogWrite(bluePin3, 0);
}
else { // High intensity knock
analogWrite(redPin1, 0);
analogWrite(greenPin1, intensity - 170);
analogWrite(bluePin1, 255);
analogWrite(redPin2, intensity - 170);
analogWrite(greenPin2, 0);
analogWrite(bluePin2, 255 - (intensity - 170));
analogWrite(redPin3, 255 - (intensity - 170));
analogWrite(greenPin3, intensity - 170);
analogWrite(bluePin3, 0);
}
// Wait a short time before repeating the loop to prevent rapid triggering
delay(10);
}