Last active
November 9, 2025 01:07
-
-
Save markedagain/2c740d501acac8a37fecda0825a4783b to your computer and use it in GitHub Desktop.
ardiunbo project
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
| // Non-blocking 2-light chase example | |
| // LEDs on pins 0–7 | |
| int totalLed = 8; | |
| int LedPins[] = {2,3,4,5,6,7,8,9}; | |
| int LedBeat[] = {0,0,0,0,0,0,0,0}; | |
| const int SETBUTTON = A2; | |
| const int SELECTBUTTON = A1; | |
| const int SWITCHBUTTON = A3; | |
| const int StartLed = 0; | |
| int currentLed = StartLed; | |
| int cuurentCycleIndex = -1; | |
| unsigned long previousMillis = 0; | |
| const unsigned long tempo = 120; // time between steps in ms | |
| unsigned long beatDuration; // ms per beat | |
| const int buzzerPin = A0; | |
| bool playTone = true; | |
| int count = 0; | |
| int ButtonPressDone = 0; | |
| int ButtonPressTime = 0; | |
| int ButtonPressMaxTime = 100; | |
| bool BeatMode = true; | |
| void setup() { | |
| for (int i = 0; i < totalLed; i++) { | |
| pinMode(LedPins[i], OUTPUT); | |
| } | |
| pinMode(A0, OUTPUT); | |
| pinMode(SELECTBUTTON, INPUT); | |
| pinMode(A2, INPUT); | |
| beatDuration = 60000 / tempo; | |
| Serial.begin(9600); | |
| currentLed = -1; | |
| } | |
| void loop() { | |
| if (ButtonPressed(SWITCHBUTTON)){ | |
| BeatMode = BeatMode == false; | |
| } | |
| Beat(); | |
| BeatSelect(); | |
| if (ButtonPressTime > 0 ){ | |
| Serial.println(ButtonPressTime); | |
| } | |
| if (ButtonPressTime > 0){ | |
| ButtonPressTime = ButtonPressTime - 1; | |
| } | |
| } | |
| bool ButtonPressed(int BUTTONPRESS){ | |
| if (digitalRead(BUTTONPRESS) ==1){ | |
| if (ButtonPressDone == 0 and ButtonPressTime == 0){ | |
| ButtonPressDone = 1; | |
| ButtonPressTime = ButtonPressMaxTime; | |
| return true; | |
| } | |
| }else{ | |
| ButtonPressDone = 0; | |
| } | |
| return false; | |
| } | |
| void Beat(){ | |
| //currentMillis = 6:38:110 | |
| unsigned long currentMillis = millis(); | |
| // --- LIGHT CHASE SECTION --- | |
| //6:38:110 - 6:38:105 = 005 | |
| if (currentMillis - previousMillis >= beatDuration) { | |
| //previousMillis = 6:38:105 | |
| previousMillis = currentMillis; | |
| updateChase(); | |
| }else if (currentMillis - previousMillis >= beatDuration /2) { | |
| noTone(A0); | |
| } | |
| } | |
| void updateChase() { | |
| // Advance position | |
| currentLed = currentLed + 1 ; | |
| if (currentLed >= totalLed) currentLed = StartLed ; | |
| // Turn on two LEDs (current and next) | |
| // int nextLed = (currentLed + 1) % numLeds; | |
| if (BeatMode == true){ | |
| // Turn off all LEDs | |
| for (int i = 0; i < totalLed; i++) { | |
| digitalWrite(LedPins[i], LOW); | |
| } | |
| digitalWrite(LedPins[currentLed], HIGH); | |
| } | |
| //digitalWrite(nextLed, HIGH); | |
| if(playTone){ | |
| if(LedBeat[currentLed] == 1){ | |
| tone(A0, 240); | |
| } | |
| } | |
| } | |
| void BeatSelect(){ | |
| if (BeatMode == true){ | |
| return; | |
| } | |
| for (int i = 0; i < totalLed; i++) { | |
| if (i != cuurentCycleIndex){ | |
| digitalWrite(LedPins[i], LOW); | |
| } | |
| } | |
| buttonCycle(); | |
| for (int i = 0; i < totalLed; i++) { | |
| if (LedBeat[i] == 1){ | |
| digitalWrite(LedPins[i], HIGH); | |
| } | |
| } | |
| if (ButtonPressed(SETBUTTON) == true) | |
| { | |
| Serial.println(LedBeat[cuurentCycleIndex]); | |
| LedBeat[cuurentCycleIndex] = LedBeat[cuurentCycleIndex] == 0; | |
| } | |
| } | |
| void buttonCycle() { | |
| if (digitalRead(SELECTBUTTON) ==1){ | |
| digitalWrite(LedPins[cuurentCycleIndex], HIGH); | |
| if (ButtonPressDone == 0 and ButtonPressTime == 0){ | |
| cuurentCycleIndex = cuurentCycleIndex +1; | |
| ButtonPressDone = 1; | |
| ButtonPressTime = ButtonPressMaxTime; | |
| } | |
| }else{ ButtonPressDone = 0; } | |
| if (ButtonPressTime > 0){ | |
| ButtonPressTime = ButtonPressTime - 1; | |
| } | |
| if (cuurentCycleIndex >= totalLed) cuurentCycleIndex = StartLed ; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment