#include #include #include #define NUM_LEDS 12 #define DATA_PIN 6 #define REMOTEXY_MODE__ESP8266_HARDSERIAL_POINT #define REMOTEXY_SERIAL Serial #define REMOTEXY_SERIAL_SPEED 115200 #define REMOTEXY_WIFI_SSID "DNALamp" #define REMOTEXY_WIFI_PASSWORD "lampaadn" #define REMOTEXY_SERVER_PORT 6377 #pragma pack(push, 1) uint8_t RemoteXY_CONF[] = // 93 bytes { 255,2,0,30,0,86,0,16,31,1,1,1,38,16,12,12,2,31,65,85, 84,79,0,3,4,10,13,9,31,2,26,72,4,5,52,19,19,93,26,161, 35,0,0,0,0,0,0,200,66,0,0,0,0,72,4,35,52,19,19,51, 26,161,35,0,0,0,0,0,0,200,66,0,0,0,0,67,4,4,73,20, 5,93,26,11,67,4,35,73,20,5,51,26,11 }; // this structure defines all the variables and events of your control interface struct { // input variables uint8_t auto_mode; // =1 if button pressed, else =0 uint8_t manual_mode; // =0 if select position A, =1 if position B, =2 if position C, ... // output variables float light_sensor; // from 0 to 100 float temp_sensor; // from 0 to 100 char light_text[11]; // string UTF8 end zero char temp_text[11]; // string UTF8 end zero // other variable uint8_t connect_flag; // =1 if wire connected, else =0 } RemoteXY; #pragma pack(pop) void RemoteXY_Handler(); // Declaration of RemoteXY_Handler function void RemoteXY_Init(); // Declaration of RemoteXY_Init function CRGB leds[NUM_LEDS]; // Declaration of the 'leds' array to hold the LED colors boolean isOn = false; boolean isMotorRunning = false; boolean isAutoMode = false; boolean isManualMode = false; int mode = 0; // Variable to track the current mode int rotationPattern = 0; // Variable to track the rotation pattern in manual mode const int temperaturePin = A1; float temperatureThreshold = 25.0; const int photoresistorPin = A0; int photoresistorValue = 0; int luminosityThreshold = 500; // Adjust this value based on your ambient light conditions const int buttonAutoPin = 2; const int buttonManualPin = 3; const int stepPin = 5; const int dirPin = 4; AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin); const CRGBPalette16 temperatureGradientPalette = CRGBPalette16( CRGB::Blue, CRGB::Blue, CRGB::Cyan, CRGB::Green, CRGB::Green, CRGB::Yellow, CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red ); void setup() { RemoteXY_Init(); FastLED.addLeds(leds, NUM_LEDS); FastLED.clear(); FastLED.show(); pinMode(buttonAutoPin, INPUT_PULLUP); pinMode(buttonManualPin, INPUT_PULLUP); pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); stepper.setMaxSpeed(2000); stepper.setAcceleration(1000); } void loop() { RemoteXY_Handler(); int buttonAutoState = digitalRead(buttonAutoPin); int buttonManualState = digitalRead(buttonManualPin); photoresistorValue = analogRead(photoresistorPin); int brightness = map(photoresistorValue, 0, luminosityThreshold, 0, 255); float voltage = (analogRead(temperaturePin) * 5.0) / 1023.0; float temperature = (voltage - 0.5) * 100.0; // Check if Auto mode button is pressed if (buttonAutoState == HIGH || RemoteXY.auto_mode) { isAutoMode = true; isManualMode = false; } // Check if Manual mode button is pressed if (buttonManualState == HIGH || RemoteXY.manual_mode != 0) { isAutoMode = false; isManualMode = true; int selectedMode = RemoteXY.manual_mode - 1; // Set the mode based on the selectedMode value, only if the physical button is not pressed if (selectedMode >= 0 && selectedMode <= 3) { mode = selectedMode; } else { // Toggle between rotation and lighting patterns in manual mode mode++; if (mode > 3) { mode = 0; } } } // Handle Auto mode if (isAutoMode) { handleAutoMode(brightness, temperature); } // Handle Manual mode if (isManualMode) { handleManualMode(brightness, temperature); } // Check if the motor should be running if (isMotorRunning) { stepper.runSpeed(); // Send a step pulse to the motor driver digitalWrite(stepPin, HIGH); delayMicroseconds(10); digitalWrite(stepPin, LOW); delayMicroseconds(10); } } void handleAutoMode(int brightness, float temperature) { // Adjust brightness based on luminosity threshold int adjustedBrightness = map(brightness, 0, 255, 255, 0); FastLED.setBrightness(adjustedBrightness); // Map temperature to a color in the gradient CRGB color = ColorFromPalette(temperatureGradientPalette, map(temperature, 15, 50, 0, 255)); fill_solid(leds, NUM_LEDS, color); FastLED.show(); // Activate motor rotation based on brightness if (brightness < luminosityThreshold) { isMotorRunning = true; } else { isMotorRunning = false; } RemoteXY.light_sensor = map(photoresistorValue, 0, 1023, 0, 100); RemoteXY.temp_sensor = map(temperature, 0, 100, 0, 100); snprintf(RemoteXY.light_text, sizeof(RemoteXY.light_text), "%.2f", photoresistorValue); snprintf(RemoteXY.temp_text, sizeof(RemoteXY.temp_text), "%.2f", temperature); } int XY(int x, int y) { // Convert x and y coordinates to an LED index on the LED ring if (x >= 0 && x < NUM_LEDS && y >= 0 && y < NUM_LEDS) { if (y % 2 == 0) { return y * (NUM_LEDS / 2) + x / 2; } else { return y * (NUM_LEDS / 2) + ((NUM_LEDS / 2) - 1 - x / 2); } } else { return -1; // Invalid LED index } } CRGB calculateColor(float temperature) { int hue = map(temperature, 0, 100, 0, 255); return CHSV(hue, 255, 255); } void handleManualMode(int brightness, float temperature) { FastLED.setBrightness(255); // Handle different lighting and rotation patterns switch (mode) { case 0: // Rotation and Pink color alternatingColors(); isMotorRunning = true; break; case 1: // Rotation and Circles with multiple colors rainbowCircles(); isMotorRunning = true; break; case 2: // No Rotation and Yellow color fill_solid(leds, NUM_LEDS, CRGB::Yellow); FastLED.show(); isMotorRunning = false; break; case 3: // Rotation and Alternating colors fill_solid(leds, NUM_LEDS, CRGB::Purple); FastLED.show(); isMotorRunning = true; break; default: break; } } void rainbowCircles() { static uint8_t startIndex = 0; startIndex = startIndex + 1; // Change the value to adjust the speed for (int i = 0; i < NUM_LEDS; i++) { leds[i] = CHSV(startIndex + (i * 32), 255, 255); } FastLED.show(); } void alternatingColors() { static uint8_t hue = 0; // Start hue value for color generation static uint8_t index = 0; // Start index for LED color assignment hue += 2; // Adjust the value to control the color change speed // Calculate the number of LEDs to assign the same color int numLedsPerColor = NUM_LEDS / 2; for (int i = 0; i < NUM_LEDS; i++) { // Calculate the hue value based on the current LED index uint8_t currentHue = hue + (i * (255 / numLedsPerColor)); // Assign the color to the LED leds[i] = CHSV(currentHue, 255, 255); } FastLED.show(); // Increment the hue for the next frame }