/*
Arduino | coding-help
i want to make mu LED flash first, and then change the number
ベリー berry — December 11, 2024 at 11:25 AM
how should i modify my code?
i have already asked chatgpt for help, but it still doesn’t work
NOTE: Each digit should have a transistor driver.
*/
const int NUM_BTNS = 2;
const int NUM_BLINKS = 3;
const unsigned long BLINK_DELAY = 500;
const unsigned long DEBOUNCE_DELAY = 50;
const int SEG_BYTES[] = { // a b c d e f g dp
0xFC, /* 0 */
0x60, /* 1 */
0xDA, /* 2 */
0xF2, /* 3 */
0x66, /* 4 */
0xB6, /* 5 */
0xBE, /* 6 */
0xE0, /* 7 */
0xFE, /* 8 */
0xF6, /* 9 */
0x00 /* blank */
};
const int LATCH_PIN = 3;
const int CLOCK_PIN = 4;
const int DATA_PIN = 2;
const int DIGIT_PINS[] = {11, 12};
const int BUTTON_PINS[] = {10, 9};
const int LED_PIN = 8;
int buttonState[NUM_BTNS];
int lastButtonState[NUM_BTNS];
unsigned long lastDebounceTime[NUM_BTNS];
unsigned long prevBlinkTime = 0;
bool wasTriggered = false;
int blinkCount = 0;
int displayCount = 0;
int newCount = 0;
int ledState = 0;
void blinkLED() {
if (millis() - prevBlinkTime >= BLINK_DELAY) {
prevBlinkTime = millis();
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
blinkCount++;
if (blinkCount >= NUM_BLINKS * 2) {
blinkCount = 0;
wasTriggered = false;
displayCount = newCount;
}
}
}
int checkSensors() {
int sense = 0;
// Read and debounce buttons (sensors)
for (int i = 0; i < NUM_BTNS; i++) {
int reading = digitalRead(BUTTON_PINS[i]);
if (reading != lastButtonState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > DEBOUNCE_DELAY) {
if (reading != buttonState[i]) {
buttonState[i] = reading;
if (buttonState[i] == LOW) {
wasTriggered = true;
if (i == 0 && newCount < 99) {
newCount++;
sense = 1;
}
if (i == 1 && newCount > 0) {
newCount--;
sense = 2;
}
}
}
}
lastButtonState[i] = reading;
}
return sense;
}
void writeShiftDigit(int digit, int number) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, ~SEG_BYTES[number]); // inverts bits
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(DIGIT_PINS[digit], HIGH);
digitalWrite(DIGIT_PINS[digit], LOW);
}
void updateDisplay() {
writeShiftDigit(0, displayCount / 10);
writeShiftDigit(1, displayCount % 10);
}
void setup() {
Serial.begin(9600);
// Set up pins
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
pinMode(DIGIT_PINS[i], OUTPUT);
digitalWrite(DIGIT_PINS[i], HIGH);
}
}
void loop() {
int senseNum = checkSensors();
if (senseNum != 0) {
wasTriggered = true;
Serial.print("Sense Num: ");
Serial.println(senseNum);
}
if (wasTriggered) {
blinkLED();
}
updateDisplay();
}
IR A
IR B