#include #include #include #include // Create an ADXL345 sensor instance Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); // Define pins for LED and vibration motor const int ledPin = D6; // Pin for the LED const int motorPin = D10; // Pin for the vibration motor // Define the pin where the NeoPixel strip is connected #define NeoPixelPin A0 // Define the number of NeoPixels #define NUMPIXELS 8 // Create the NeoPixel object Adafruit_NeoPixel pixels(NUMPIXELS, NeoPixelPin, NEO_GRB + NEO_KHZ800); // Define the colors uint32_t colors[] = { pixels.Color(254, 120, 10), // Color 1 pixels.Color(254, 116, 0), // Color 2 pixels.Color(240, 90, 0), // Color 3 pixels.Color(244, 54, 0), // Color 4 pixels.Color(250, 30, 0), // Color 5 pixels.Color(250, 20, 0), // Color 6 pixels.Color(253, 10, 0), // Color 7 pixels.Color(255, 0, 0) // Color 8 }; // Variables to store previous acceleration values float previousX = 0; float previousY = 0; float previousZ = 0; unsigned long changeStartTime = 0; bool showLava = false; unsigned long lavaStartTime = 0; void setup() { Serial.begin(9600); Serial.println("ADXL345 Accelerometer Test"); // Initialize the sensor if (!accel.begin()) { Serial.println("No ADXL345 detected ... Check your wiring!"); while (1); } // Set the range to whatever is appropriate for your project accel.setRange(ADXL345_RANGE_16_G); // Set pin modes for LED and vibration motor pinMode(ledPin, OUTPUT); pinMode(motorPin, OUTPUT); // Initialize the NeoPixel strip pixels.begin(); // Display the sensor details sensor_t sensor; accel.getSensor(&sensor); Serial.println("------------------------------------"); Serial.print ("Sensor: "); Serial.println(sensor.name); Serial.print ("Driver Ver: "); Serial.println(sensor.version); Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" m/s^2"); Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" m/s^2"); Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" m/s^2"); Serial.println("------------------------------------"); Serial.println(""); // Set the range to whatever is appropriate for your project accel.setRange(ADXL345_RANGE_16_G); // Display the range settings Serial.print ("Range: +/- "); switch(accel.getRange()) { case ADXL345_RANGE_16_G: Serial.print("16 "); break; case ADXL345_RANGE_8_G: Serial.print("8 "); break; case ADXL345_RANGE_4_G: Serial.print("4 "); break; case ADXL345_RANGE_2_G: Serial.print("2 "); break; } Serial.println(" g"); Serial.println(""); delay(500); } void loop() { // Get a new sensor event sensors_event_t event; accel.getEvent(&event); // Display the results (acceleration is measured in m/s^2) Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" "); Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" "); Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.println(); // Map the accelerometer values to RGB values (0-255) int blueValue = map(event.acceleration.x, -16, 16, 0, 255); int greenValue = map(event.acceleration.y, -16, 16, 0, 255); int redValue = map(event.acceleration.z, -16, 16, 0, 255); // Set the NeoPixel color for(int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(redValue, greenValue, blueValue)); } pixels.show(); // Check for changes in acceleration values if (abs(event.acceleration.x - previousX) < 1 && abs(event.acceleration.y - previousY) < 1 && abs(event.acceleration.z - previousZ) < 1) { // If the values have not changed significantly for 4000ms if (millis() - changeStartTime > 4000 && !showLava) { // Activate motor and LED for 3000ms digitalWrite(ledPin, HIGH); digitalWrite(motorPin, HIGH); delay(3000); digitalWrite(ledPin, LOW); digitalWrite(motorPin, LOW); // Start NeoPixel lava color show showLava = true; lavaStartTime = millis(); } } else { // Update the previous values and reset the timer previousX = event.acceleration.x; previousY = event.acceleration.y; previousZ = event.acceleration.z; changeStartTime = millis(); showLava = false; // Stop the lava show if values change } // Display the lava color show for 5000ms if showLava is true if (showLava) { showLavaColors(); if (millis() - lavaStartTime > 5000) { showLava = false; } } // Reduce the delay to make the loop more responsive delay(100); } void showLavaColors() { // Loop through each color for (int j = 0; j < sizeof(colors) / sizeof(colors[0]); j++) { uint32_t currentColor = colors[j]; uint32_t nextColor = colors[(j + 1) % (sizeof(colors) / sizeof(colors[0]))]; for (int k = 0; k <= 255; k += 10) { // Increase step size for faster transitions uint32_t blendedColor = blendColors(currentColor, nextColor, k); for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, blendedColor); } pixels.show(); delay(50); // Shorter delay for smoother and faster transitions } } } uint32_t blendColors(uint32_t color1, uint32_t color2, uint8_t blend) { uint8_t r1 = (color1 >> 16) & 0xFF; uint8_t g1 = (color1 >> 8) & 0xFF; uint8_t b1 = color1 & 0xFF; uint8_t r2 = (color2 >> 16) & 0xFF; uint8_t g2 = (color2 >> 8) & 0xFF; uint8_t b2 = color2 & 0xFF; uint8_t r = ((r1 * (255 - blend)) + (r2 * blend)) / 255; uint8_t g = ((g1 * (255 - blend)) + (g2 * blend)) / 255; uint8_t b = ((b1 * (255 - blend)) + (b2 * blend)) / 255; return pixels.Color(r, g, b); }