Untitled

 avatar
unknown
plain_text
a month ago
1.9 kB
3
Indexable
int a = 10;  // Segment A
int b = 3;  // Segment B
int c = 4;  // Segment C
int d = 5;  // Segment D
int e = 6;  // Segment E
int f = 8;  // Segment F
int g = 9;  // Segment G
int buttonPin = 7; // Button input pin

volatile int counter = 0; // Must be volatile for ISR
volatile bool buttonPressed = false; // Flag for button press

void setup() {
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  
  Serial.begin(9600);
  pinMode(buttonPin, INPUT); 

  // Attach interrupt to buttonPin (pin 7 on Arduino UNO is INT4)
  attachInterrupt(0, buttonISR, FALLING);
}

void loop() {
  if (buttonPressed) {
    buttonPressed = false; // Reset flag
    counter = (counter + 1) % 10; // Increment counter (0-9)
  }

  boolean A, B, C, D, E, F, G;

  switch (counter) {
    case 0: A=HIGH; B=HIGH; C=HIGH; D=HIGH; E=HIGH; F=HIGH; G=LOW; break;    
    case 1: A=LOW; B=HIGH; C=HIGH; D=LOW; E=LOW; F=LOW; G=LOW; break;   
    case 2: A=HIGH; B=HIGH; C=LOW; D=HIGH; E=HIGH; F=LOW; G=HIGH; break;
    case 3: A=HIGH; B=HIGH; C=HIGH; D=HIGH; E=LOW; F=LOW; G=HIGH; break;
    case 4: A=LOW; B=HIGH; C=HIGH; D=LOW; E=LOW; F=HIGH; G=HIGH; break;
    case 5: A=HIGH; B=LOW; C=HIGH; D=HIGH; E=LOW; F=HIGH; G=HIGH; break;
    case 6: A=HIGH; B=LOW; C=HIGH; D=HIGH; E=HIGH; F=HIGH; G=HIGH; break;
    case 7: A=HIGH; B=HIGH; C=HIGH; D=LOW; E=LOW; F=LOW; G=LOW; break;
    case 8: A=HIGH; B=HIGH; C=HIGH; D=HIGH; E=HIGH; F=HIGH; G=HIGH; break;
    case 9: A=HIGH; B=HIGH; C=HIGH; D=HIGH; E=LOW; F=HIGH; G=HIGH; break;
  }

  // Write values to the display
  digitalWrite(a, A);
  digitalWrite(b, B);
  digitalWrite(c, C);
  digitalWrite(d, D);
  digitalWrite(e, E);
  digitalWrite(f, F);
  digitalWrite(g, G);
}

// Interrupt Service Routine (ISR)
void buttonISR() {
  Serial.print("ISR Fired");
  buttonPressed = true; // Set flag to update counter in loop
}
Editor is loading...
Leave a Comment