├── .gitignore ├── arduino-pin-timer.code-workspace ├── platformio.ini └── src └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode 3 | -------------------------------------------------------------------------------- /arduino-pin-timer.code-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "folders": [ 3 | { 4 | "path": "." 5 | } 6 | ], 7 | "settings": {} 8 | } -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:nodemcuv2] 12 | platform = espressif8266 13 | board = nodemcuv2 14 | framework = arduino 15 | build_type = release 16 | upload_speed = 921600 17 | upload_port = /dev/ttyUSB0 -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: main.cpp 3 | * Project: arduino-pin-timer 4 | * Created Date: 01.02.2023 21:01:00 5 | * Author: 3urobeat 6 | * 7 | * Last Modified: 02.02.2023 11:59:34 8 | * Modified By: 3urobeat 9 | * 10 | * Copyright (c) 2023 3urobeat 11 | * 12 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 14 | * You should have received a copy of the GNU General Public License along with this program. If not, see . 15 | */ 16 | 17 | 18 | // Script intended for turning on & off 4 pins with increasing length 19 | // The script will alternate between all 4 pins instead of leaving on all of them at the same time to reduce heat buildup in the connected device 20 | 21 | 22 | #include 23 | 24 | 25 | // Config vars 26 | const int startAfterMin = 30; // Min time in seconds to start after poweron 27 | const int startAfterMax = 90; // Max time in seconds to start after poweron 28 | 29 | const int totalTime = 2; // Total time in minutes 30 | 31 | const int pauseTime = 30; // Time in seconds between intervals 32 | const int baseTime = 0; // Base time in seconds where stepSize will be added to 33 | const int stepSize = 10; // Time in seconds to increase 34 | 35 | const int pins[] = { D6, D7, D8 }; 36 | 37 | 38 | // Runtime vars 39 | unsigned int pinSize = sizeof(pins) / sizeof(pins[0]); 40 | unsigned int counter = 0; 41 | 42 | 43 | // Functions 44 | void setAllPins(int val) { 45 | for (uint8_t i = 0; i < pinSize; i++) { 46 | digitalWrite(pins[i], val); 47 | } 48 | } 49 | 50 | 51 | void setup() 52 | { 53 | // Initialize pins 54 | for (uint8_t i = 0; i < pinSize; i++) { 55 | pinMode(pins[i], OUTPUT); 56 | } 57 | 58 | // Indicate function by turning on and off 2 times shortly 59 | for (uint8_t i = 0; i < 2; i++) { 60 | setAllPins(HIGH); 61 | delay(500); 62 | setAllPins(LOW); 63 | delay(500); 64 | } 65 | 66 | // Wait random amount of time between min and max before starting 67 | unsigned long waitTime = (rand() / (startAfterMax * 1000)) + (startAfterMin * 1000); 68 | 69 | while (waitTime > millis()) { delay(250); } 70 | } 71 | 72 | 73 | void loop() 74 | { 75 | // Stop if total time was reached and signal it with two short power ons & offs 76 | if (millis() >= (totalTime * 60 * 1000)) { 77 | for (uint8_t i = 0; i < 2; i++) { 78 | setAllPins(HIGH); 79 | delay(500); 80 | setAllPins(LOW); 81 | delay(500); 82 | } 83 | 84 | // Wait a really long time, aka turn off 85 | delay(276447230); 86 | } 87 | 88 | // Increase counter 89 | counter++; 90 | 91 | // Iterate over all pins and turn them on after another 92 | for (uint8_t i = 0; i < pinSize; i++) { 93 | digitalWrite(pins[i], HIGH); 94 | 95 | unsigned long waitTime2 = millis() + (((baseTime + (counter * stepSize)) * 1000) / pinSize); // Add stepSize counter times to baseTime, convert to ms and divide by amount of pins so that each pin 96 | while (waitTime2 > millis()) { delay(250); } 97 | 98 | digitalWrite(pins[i], LOW); 99 | } 100 | 101 | // Wait before next iteration 102 | unsigned long waitTime3 = millis() + (pauseTime * 1000); 103 | 104 | while (waitTime3 > millis()) { delay(250); } 105 | } --------------------------------------------------------------------------------