#include "FirebaseESP32.h" #include const char* ssid = "Bwifi"; const char* password = "REDACTED"; const char* host = "academy.cba.mit.edu"; const char* URL = "/classes/networking_communications/hello.html"; const int port = 80; WiFiClient client; FirebaseData firebaseData; FirebaseData subData; String BALL_GUID="Ball1"; #include #ifdef __AVR__ #include // Required for 16 MHz Adafruit Trinket #endif #define PIN 5 #define PIN2 4 #define TOUCH_PIN T4 // Touch0 is T0 which is on GPIO 4. // touch4, GPIO13 // How many NeoPixels are attached to the Arduino? #define LED_COUNT 24 #define NUMPIXELS 24 // NeoPixel brightness, 0 (min) to 255 (max) #define BRIGHTNESS 100 // Declare our NeoPixel strip object: Adafruit_NeoPixel pixels(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel pixels2(LED_COUNT, PIN2, NEO_GRB + NEO_KHZ800); // Argument 1 = Number of pixels in NeoPixel strip // Argument 2 = Arduino pin number (most are valid) // Argument 3 = Pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) #define DELAYVAL 50 // Time (in milliseconds) to pause between updates int loopDelay = DELAYVAL; bool wasOn = false; unsigned long nextConfigGetTime; unsigned long nextCapPushTime; void setup() { Serial.begin(115200); pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) pixels2.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) printf("\nConnecting to WiFi "); WiFi.begin(ssid,password); while (WiFi.status() != WL_CONNECTED) { delay(100); printf("."); } printf("\nWiFi connected with address %s\n",WiFi.localIP().toString().c_str()); Firebase.begin("REDACTED", "REDACTED"); Firebase.reconnectWiFi(true); Firebase.setMaxRetry(firebaseData, 3); Firebase.setMaxErrorQueue(firebaseData, 30); } int currentIntensity = BRIGHTNESS; void streamTimeoutCallback(bool timeout) { if(timeout){ //Stream timeout occurred Serial.println("Stream timeout, resume streaming..."); } } int currentColorR = 255; int currentColorG = 0; int currentColorB = 255; int currentBrightness = 255; int catchBelow = 16; int rainbowDelay = 20; #define CONFIG_UPDATE_INTERVAL 250 // ms #define LOW_PRIORITY_CONFIG_INTERVAL 3250 // ms #define CAP_UPDATE_INTERVAL 250 // ms int configUpdateInterval = CONFIG_UPDATE_INTERVAL; int capUpdateInterval = CAP_UPDATE_INTERVAL; int updateIntFromPath(String path, int fallbackDefault) { if (Firebase.getInt(firebaseData, path)) { if (firebaseData.dataType() == "int") { return firebaseData.intData(); } } else { //Failed to get JSON data at defined database path, print out the error reason Serial.println(firebaseData.errorReason()); } return fallbackDefault; } void setData(String path, String value) { FirebaseJson json; json.set(path, value); if (Firebase.updateNode(firebaseData, "/data/" + BALL_GUID, json)) { } else { Serial.println(firebaseData.errorReason()); } } long nextLowPriConfigGetTime = 0; bool wasCaught = false; // Time ms / RGB const int throwAnimation[][4] = { {0, 255, 0, 0}, {250, 0, 255, 0}, {500, 0, 0, 255}, {750, 255, 255, 0}, {1000, 0, 0, 0} }; int currentAnimationStep = 0; bool isAnimating = false; int animationStartTime = 0; // Input a value 0 to 255 to get a color value. // The colours are a transition r - g - b - back to r. uint32_t Wheel(byte WheelPos) { if(WheelPos < 85) { return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if(WheelPos < 170) { WheelPos -= 85; return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3); } } int wheelPos = 0; bool isTouched() { return touchRead(TOUCH_PIN) < catchBelow; } void loop() { // if (isAnimating) { // if (millis() > animationStartTime + throwAnimation[currentAnimationStep][0]) { // setAllColor(throwAnimation[currentAnimationStep][1], throwAnimation[currentAnimationStep][2], throwAnimation[currentAnimationStep][3]); // currentAnimationStep++; // } // if (currentAnimationStep >= 4) { // isAnimating = false; // currentAnimationStep = 0; // } // } if (isAnimating) { while (wheelPos < 180 && !isTouched()) { wheelPos = wheelPos + 3; setAllWheel(Wheel(wheelPos)); delay(rainbowDelay); } isAnimating = false; wheelPos = 0; } if (millis() > nextConfigGetTime) { nextConfigGetTime = millis() + configUpdateInterval; currentColorR = updateIntFromPath("/config/" + BALL_GUID + "/color/r", currentColorR); currentColorG = updateIntFromPath("/config/" + BALL_GUID + "/color/g", currentColorG); currentColorB = updateIntFromPath("/config/" + BALL_GUID + "/color/b", currentColorB); currentBrightness = updateIntFromPath("/config/" + BALL_GUID + "/intensity", currentBrightness); rainbowDelay = updateIntFromPath("/config/" + BALL_GUID + "/intensity", rainbowDelay); } if (millis() > nextLowPriConfigGetTime) { nextLowPriConfigGetTime = millis() + LOW_PRIORITY_CONFIG_INTERVAL; catchBelow = updateIntFromPath("/config/" + BALL_GUID + "/catchBelow", catchBelow); configUpdateInterval = updateIntFromPath("/config/" + BALL_GUID + "/configUpdateInterval", configUpdateInterval); capUpdateInterval = updateIntFromPath("/config/" + BALL_GUID + "/capUpdateInterval", capUpdateInterval); loopDelay = updateIntFromPath("/config/" + BALL_GUID + "/loopDelay", loopDelay); } if (millis() > nextCapPushTime) { nextCapPushTime = millis() + capUpdateInterval; setData("capTouch", String(touchRead(TOUCH_PIN))); } bool onThisLoop = wasOn; bool isBeingTouched = isTouched();; if (isBeingTouched) { isAnimating = false; if (!wasCaught) { turnAllOn(); setData("lastCatchTime", String(millis())); } wasCaught = true; } else { if (wasCaught) { pixels.clear(); // Set all pixel colors to 'off' pixels2.clear(); // Set all pixel colors to 'off' pixels.show(); // Send the updated pixel colors to the hardware. pixels2.show(); // Send the updated pixel colors to the hardware. isAnimating = true; currentAnimationStep = 0; animationStartTime = millis(); } wasCaught = false; } delay(loopDelay); } void setAllWheel(uint32_t color) { for(int i=0; i