From badb6ac5f08d136e126fdc74630589a4b80b2d7e Mon Sep 17 00:00:00 2001 From: Alessevan <44125445+Alessevan@users.noreply.github.com> Date: Fri, 16 May 2025 14:37:08 +0200 Subject: [PATCH] feat: colored gradient by height --- prog/prog.ino | 153 ++++++++++++++++++++++++++++---------------------- 1 file changed, 85 insertions(+), 68 deletions(-) diff --git a/prog/prog.ino b/prog/prog.ino index 203428e..aa39ea0 100644 --- a/prog/prog.ino +++ b/prog/prog.ino @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -9,8 +9,9 @@ #ifdef __AVR__ #include #endif -#define PIN 5 -#define NUMPIXELS 6 +#define PIN_PIXELS 4 +#define NUM_PIXELS 6 +#define TUBES_COUNT 6 // Web server configuration const char* ssid = "ESP_ping"; @@ -21,7 +22,7 @@ const char* PARAM_INPUT_2 = "state"; AsyncWebServer server(80); -Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); +CRGB leds[NUM_PIXELS]; struct pin { int id; @@ -33,6 +34,7 @@ enum state { STATE_NONE, STATE_INIT, STATE_OK, + STATE_MOVING, STATE_ERROR }; @@ -57,39 +59,32 @@ struct tube { enum mode { NONE, SINUS, + MAX_MODE, }; enum mode current_mode = NONE; + const struct pin all_xshuts[6] = { - {.id = 17, .is_extender = 0}, - {.id = 2, .is_extender = 1}, - {.id = 3, .is_extender = 1}, - {.id = 5, .is_extender = 1}, {.id = 7, .is_extender = 1}, - {.id = 1, .is_extender = 1} + {.id = 0, .is_extender = 1}, + {.id = 1, .is_extender = 1}, + {.id = 6, .is_extender = 1}, + {.id = 5, .is_extender = 1}, + {.id = 4, .is_extender = 1} }; -const struct pin all_gpios[6] = { - {.id = -1, .is_extender = 0}, - {.id = 18, .is_extender = 0}, - {.id = 19, .is_extender = 0}, - {.id = -1, .is_extender = 1}, - {.id = 6, .is_extender = 1}, - {.id = 0, .is_extender = 1} -}; const struct pin all_servos[6] = { {.id = 32, .is_extender = 0}, {.id = 33, .is_extender = 0}, - {.id = 23, .is_extender = 0}, - {.id = 16, .is_extender = 0}, - {.id = 4, .is_extender = 0}, - {.id = 2, .is_extender = 0} + {.id = 18, .is_extender = 0}, + {.id = 27, .is_extender = 0}, + {.id = 14, .is_extender = 0}, + {.id = 19, .is_extender = 0} }; struct tube all_tubes[6]; -const int TUBES_COUNT = 2; const float minHeight = 50; const float maxHeight = 445; @@ -164,25 +159,36 @@ void changePinMode(struct pin pin, int state) { void change_led(int id) { Serial.print("Changing led color of #"); Serial.println(id); - int led_id = NUMPIXELS -1 - id; + int led_id = NUM_PIXELS - 1 - id; switch (all_tubes[id].state) { case STATE_INIT: { - pixels.setPixelColor(led_id, pixels.Color(255, 210, 70)); + leds[led_id] = CRGB(255, 210, 70); break; } case STATE_OK: { - pixels.setPixelColor(led_id, pixels.Color(0, 0, 190)); + leds[led_id] = CRGB::Blue; + break; + } + case STATE_MOVING: { + float ratio = (all_tubes[id].height) / (maxHeight); + if (ratio > 1.0) { + ratio = 1.0; + } else if (ratio < 0.0f) { + ratio = 0.0; + } + leds[led_id] = CHSV((int) (ratio * 128.0 + (1 - ratio) * 240.0) - 90, 255, 255); break; } case STATE_ERROR: { - pixels.setPixelColor(led_id, pixels.Color(0, 190, 0)); + leds[led_id] = CRGB::Red; break; } default: { - pixels.setPixelColor(led_id, pixels.Color(3, 3, 3)); + leds[led_id] = CRGB::Black; break; } } + FastLED.show(); } // Web Server configuration @@ -209,6 +215,8 @@ const char index_html[] PROGMEM = R"rawliteral(

Balle Ping Pong flottante

%BUTTONPLACEHOLDER% + +
@@ -243,9 +262,6 @@ String processor(const String& var){ void wifisetup() { -#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) - clock_prescale_set(clock_div_1); -#endif Serial.print("Setting AP (Access Point)…"); WiFi.softAP(ssid); @@ -260,6 +276,10 @@ void wifisetup() { // Send a GET request to /update?output=&state= server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) { + if (current_mode != NONE) { + request->send(401, "text/plain", "NOK"); + return; + } int inputMessage1; int inputMessage2; // GET input1 value on /update?output=&state= @@ -281,6 +301,12 @@ void wifisetup() { request->send(200, "text/plain", "OK"); }); + // Send a GET request to /update?output=&state= + server.on("/switch_mode", HTTP_GET, [] (AsyncWebServerRequest *request) { + current_mode = static_cast(NONE + (current_mode + 1) % MAX_MODE); + request->send(200, "text/plain", "OK"); + }); + // Start server server.begin(); } @@ -303,7 +329,6 @@ void setup_all_tofs() { Serial.println(F("Failed to boot VL53L0X")); all_tubes[i].state = STATE_ERROR; change_led(i); - pixels.show(); goto error; } all_tubes[i].tof.hard.setAddress(0x31 + i); @@ -321,15 +346,20 @@ void calibrate_tube(int i){ delay(5000); Serial.println("Setuping downards"); uint16_t h = mesureHeight(i); + if (h < 100) { + all_tubes[i].state = STATE_ERROR; + Serial.printf("Tube %d HS ! (pas assez haut)\n", i); + change_led(i); + return; + } int angle = minServo; while (h > maxHeight) { Serial.printf("%dmm %d°\n",h, angle); ++angle; if (angle > maxServo) { all_tubes[i].state = STATE_ERROR; - Serial.printf("Tube %d HS !\n", i); + Serial.printf("Tube %d HS ! (moteur ne tourne pas ?)\n", i); change_led(i); - pixels.show(); return; } all_tubes[i].servo.hard.write(angle); @@ -343,7 +373,6 @@ void calibrate_tube(int i){ Serial.printf("Tube %d calibrated.\n", i); all_tubes[i].state = STATE_OK; change_led(i); - pixels.show(); } void setup_servos() { @@ -365,17 +394,17 @@ int servoPosition(float ratio){ } void setup() { -#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) - clock_prescale_set(clock_div_1); -#endif Serial.begin(115200); // Starts the serial communication while (! Serial) { delay(1); } - pixels.begin(); - pixels.setBrightness(255); - pixels.show(); + FastLED.addLeds(leds, NUM_PIXELS); + FastLED.setBrightness(255); + for (int i = 0; i < NUM_PIXELS; ++i) { + leds[i] = CRGB::Black; + } + FastLED.show(); setup_servos(); delay(5000); @@ -403,44 +432,33 @@ void setup() { for (int i = 0; i %f\n", all_tubes[i].height, all_tubes[i].target, angle); } + step += 0.1; } for (int i = 0; i %d\n", all_tubes[i].height, all_tubes[i].target, diff, angle); + //Serial.printf("%d - %d = %d => %d\n", all_tubes[i].height, all_tubes[i].target, diff, angle); all_tubes[i].servo.last_angle = min(max(angle, minServo), maxServo); all_tubes[i].servo.hard.write(all_tubes[i].servo.last_angle); } - pixels.show(); // Serial.println();