0% found this document useful (0 votes)
3 views4 pages

Code

This document provides a complete Arduino code for a Cube Timer designed for the Arduino Nano, featuring dual-button control, best time tracking, and PC communication. The code initializes the timer, manages button inputs, tracks solve times, and provides feedback through a buzzer and LCD display. It is ready for upload and use, with instructions for operation and an offer for a Python desktop GUI to follow.

Uploaded by

jujutsu.coding01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Code

This document provides a complete Arduino code for a Cube Timer designed for the Arduino Nano, featuring dual-button control, best time tracking, and PC communication. The code initializes the timer, manages button inputs, tracks solve times, and provides feedback through a buzzer and LCD display. It is ready for upload and use, with instructions for operation and an offer for a Python desktop GUI to follow.

Uploaded by

jujutsu.coding01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Here's your **corrected full Arduino code** with `ULONG_MAX` replaced by its actual

value `4294967295` (maximum for `unsigned long`):

```cpp
// Cube Timer for Arduino Nano
// Features: Dual-button control, best time tracking, Ao5, buzzer feedback, PC
communication

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int buttonLeft = 2;


const int buttonRight = 4;
const int buzzer = 3;

const int maxSolves = 20;


unsigned long solveTimes[maxSolves];
int solveIndex = 0;
int solveCount = 0;
unsigned long bestTime = 4294967295; // Max value for unsigned long

unsigned long startTime = 0;


bool timing = false;
bool pcConnected = false;

unsigned long buttonPressStart = 0;


bool wasHeld = false;

unsigned long lastHelloTime = 0;


char serialBuffer[10];
byte bufferIndex = 0;

unsigned long lastInteractionTime = 0;


bool backlightOn = true;
const unsigned long backlightTimeout = 30000; // 30 seconds

void setup() {
pinMode(buttonLeft, INPUT_PULLUP);
pinMode(buttonRight, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);

lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initializing...");
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timer Ready");
lcd.setCursor(0, 1);
lcd.print("Hold both to Start");

Serial.begin(115200);
lastInteractionTime = millis();
}

void loop() {
handlePCHandshake();
handleButtons();
updateDisplay();
manageBacklight();
}

void handlePCHandshake() {
if (millis() - lastHelloTime > 2000) {
Serial.println("HELLO");
lastHelloTime = millis();
}

while (Serial.available()) {
char c = Serial.read();
if (c == '\n') {
serialBuffer[bufferIndex] = '\0';
if (strcmp(serialBuffer, "OK") == 0) {
pcConnected = true;
}
bufferIndex = 0;
} else if (bufferIndex < sizeof(serialBuffer) - 1) {
serialBuffer[bufferIndex++] = c;
}
}
}

void handleButtons() {
bool left = digitalRead(buttonLeft) == LOW;
bool right = digitalRead(buttonRight) == LOW;

if (!timing && left && right) {


if (buttonPressStart == 0) {
buttonPressStart = millis();
} else if (millis() - buttonPressStart >= 1500 && !wasHeld) {
beepHold();
startTimer();
wasHeld = true;
}
} else if (timing && left && right) {
stopTimer();
} else if (!left || !right) {
buttonPressStart = 0;
wasHeld = false;
}

if (left || right) {
lastInteractionTime = millis();
if (!backlightOn) {
lcd.backlight();
backlightOn = true;
}
}
}

void startTimer() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timing...");
startTime = millis();
timing = true;
beepStart();
lastInteractionTime = millis();
}

void stopTimer() {
timing = false;
unsigned long elapsed = millis() - startTime;
beepStop();

if (solveCount < maxSolves) {


solveTimes[solveIndex++] = elapsed;
solveCount++;
if (solveIndex >= maxSolves) solveIndex = 0;
}

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(elapsed / 1000.0, 2);
lcd.print("s");

if (elapsed < bestTime) {


bestTime = elapsed;
celebratePB();
lcd.setCursor(0, 1);
lcd.print("NEW BEST!");
} else {
lcd.setCursor(0, 1);
lcd.print("Solve ");
lcd.print(solveCount);
}

if (pcConnected) {
Serial.print("SOLVE:");
Serial.println(elapsed);
}

delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hold both to Start");
lastInteractionTime = millis();
}

void updateDisplay() {
if (timing) {
unsigned long now = millis();
unsigned long elapsed = now - startTime;
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(elapsed / 1000.0, 2);
lcd.print("s ");
}
}

void manageBacklight() {
if (millis() - lastInteractionTime > backlightTimeout && backlightOn) {
lcd.noBacklight();
backlightOn = false;
}
}

void beepStart() {
tone(buzzer, 1500, 150);
delay(160);
noTone(buzzer);
}

void beepStop() {
tone(buzzer, 800, 150);
delay(160);
noTone(buzzer);
}

void beepHold() {
tone(buzzer, 1000, 100);
delay(110);
noTone(buzzer);
}

void celebratePB() {
tone(buzzer, 1500, 100);
delay(120);
tone(buzzer, 2000, 100);
delay(120);
tone(buzzer, 2500, 150);
delay(200);
noTone(buzzer);
}
```

---

### ✅ Now Ready To:

* Upload to your **Arduino Nano**


* Power on — it shows `Initializing...`, then `Hold both to Start`
* Track and send solves via USB

Would you like the Python desktop GUI next?

You might also like