Created
December 31, 2025 12:14
-
-
Save hemalchevli/f5f70cf00d1f77278ad226bfa6ef2fe4 to your computer and use it in GitHub Desktop.
unihiker_timelapse_V2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "unihiker_k10.h" | |
| #include "SD.h" | |
| // UNIHIKER board object | |
| UNIHIKER_K10 unihiker; | |
| // State variables | |
| bool timelapseActive = false; | |
| uint32_t pictureCount = 0; | |
| String currentFolder = ""; | |
| void updateDisplay(String text) { | |
| unihiker.canvas->canvasClear(); | |
| unihiker.canvas->canvasText(text, 1, 0xFFFFFF); // White text | |
| unihiker.canvas->updateCanvas(); | |
| } | |
| void setup() { | |
| // Callback function for starting/stopping the timelapse | |
| auto startStopCallback = []() { | |
| timelapseActive = !timelapseActive; | |
| if (timelapseActive) { | |
| // Find the next available folder name | |
| int folderIndex = 0; | |
| while (true) { | |
| String folderName = "/timelapse_" + String(folderIndex); | |
| if (!SD.exists(folderName)) { | |
| currentFolder = folderName; | |
| break; | |
| } | |
| folderIndex++; | |
| } | |
| // Create the new folder | |
| SD.mkdir(currentFolder); | |
| pictureCount = 1; // Reset picture count on start | |
| unihiker.rgb->setRangeColor(0, 2, 0x00FF00); // Green | |
| String message = "Started: " + currentFolder; | |
| updateDisplay(message); | |
| Serial.println(message); | |
| } else { | |
| updateDisplay("Timelapse stopped. Ready to start."); | |
| unihiker.rgb->setRangeColor(0, 2, 0xFF0000); // Red | |
| Serial.println("Timelapse stopped."); | |
| } | |
| }; | |
| // Callback function for taking a picture | |
| auto takePictureCallback = []() { | |
| if (!timelapseActive) return; | |
| String message = "Taking picture " + String(pictureCount); | |
| updateDisplay(message); | |
| Serial.println(message); | |
| // Path for the new picture on the SD Card | |
| String path = "S:"+currentFolder + "/picture" + String(pictureCount) + ".bmp"; | |
| //String path = "S:/time" + String(pictureCount) + ".bmp"; //this works | |
| unihiker.photoSaveToTFCard(path); | |
| // Verify that the file was created | |
| if(SD.exists(path)) { | |
| message = "Saved: " + path; | |
| } else { | |
| message = "Error saving " + path; | |
| } | |
| updateDisplay(message); // Show save status | |
| Serial.println(message); | |
| // Display the saved photo on the screen | |
| // unihiker.canvas->canvasDrawImage(0, 0, path); | |
| // unihiker.canvas->updateCanvas(); | |
| // delay(2000); // Show the image for 2 seconds | |
| updateDisplay("Waiting for trigger..."); | |
| pictureCount++; | |
| }; | |
| Serial.begin(115200); | |
| Serial.println("UNHIKER K10 Timelapse Camera for Prusa"); | |
| // Initialize UNIHIKER board | |
| unihiker.begin(); | |
| unihiker.initScreen(2); | |
| unihiker.setScreenBackground(0x000000); // Black background | |
| unihiker.initBgCamerImage(); | |
| unihiker.setBgCamerImage(false); | |
| unihiker.creatCanvas(); | |
| unihiker.setBgCamerImage(true); | |
| unihiker.initSDFile(); | |
| // Initialize RGB | |
| unihiker.rgb->brightness(10); // Set brightness (0-255) | |
| unihiker.rgb->setRangeColor(0, 2, 0xFF0000); // Red, indicating stopped | |
| updateDisplay("Ready. Waiting for start signal..."); | |
| Serial.println("Ready. Waiting for start signal..."); | |
| // Set callbacks for button presses | |
| unihiker.buttonA->setPressedCallback(startStopCallback); | |
| unihiker.buttonB->setPressedCallback(takePictureCallback); | |
| } | |
| void loop() { | |
| // The main loop is now empty because the button library handles everything | |
| // in the background via its own task. | |
| delay(1000); // A delay to keep the main task from consuming too much CPU. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment