pleasedontcode

Counter Display rev_02

Oct 27th, 2025
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Counter Display
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-27 11:51:55
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* realizza un contatore up/down che conta sia */
  21.     /* tramite codice gray che binario, alternandoli */
  22.     /* tramite un deviatore. Ci sono 3 pulsanti, */
  23.     /* incremento, decremento e reset, dettati */
  24.     /* dall'utente, */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. // Definitions of button and switch pins
  31. const int BTN_UP = 2;
  32. const int BTN_DOWN = 3;
  33. const int BTN_RESET = 4;
  34. const int SWITCH_MODE = 5;
  35.  
  36. // LED output pins
  37. const int LED_MSB = 6;
  38. const int LED_LSB = 7;
  39.  
  40. // Variables
  41. int counter = 0;
  42. bool grayMode = false;
  43. bool ledsActive = false;
  44. unsigned long lastPress = 0;
  45. const unsigned long debounceDelay = 200;
  46.  
  47. // Setup function
  48. void setup() {
  49.   // Imposta ingressi con pull-up interno
  50.   pinMode(BTN_DOWN, INPUT_PULLUP);
  51.   pinMode(BTN_UP, INPUT_PULLUP);
  52.   pinMode(BTN_RESET, INPUT_PULLUP);
  53.   pinMode(SWITCH_MODE, INPUT_PULLUP);
  54.  
  55.   // Imposta uscite per i LED
  56.   pinMode(LED_MSB, OUTPUT);
  57.   pinMode(LED_LSB, OUTPUT);
  58.  
  59.   // LED inizialmente spenti
  60.   digitalWrite(LED_MSB, LOW);
  61.   digitalWrite(LED_LSB, LOW);
  62. }
  63.  
  64. // Loop function
  65. void loop() {
  66.   // Lettura deviatore modalitΓ 
  67.   grayMode = (digitalRead(SWITCH_MODE) == LOW);
  68.  
  69.   // Pulsante incremento
  70.   if (digitalRead(BTN_UP) == LOW && millis() - lastPress > debounceDelay) {
  71.     counter++;
  72.     if (counter > 3) counter = 0;
  73.     ledsActive = true;
  74.     lastPress = millis();
  75.     updateLEDs();
  76.   }
  77.  
  78.   // Pulsante decremento
  79.   if (digitalRead(BTN_DOWN) == LOW && millis() - lastPress > debounceDelay) {
  80.     counter--;
  81.     if (counter < 0) counter = 3;
  82.     ledsActive = true;
  83.     lastPress = millis();
  84.     updateLEDs();
  85.   }
  86.  
  87.   // Pulsante reset
  88.   if (digitalRead(BTN_RESET) == LOW && millis() - lastPress > debounceDelay) {
  89.     counter = 0;
  90.     ledsActive = false;
  91.     lastPress = millis();
  92.     updateLEDs();
  93.   }
  94.   delay(10);
  95. }
  96.  
  97. // Update LEDs
  98. void updateLEDs() {
  99.   if (!ledsActive) {
  100.     digitalWrite(LED_MSB, LOW);
  101.     digitalWrite(LED_LSB, LOW);
  102.     return;
  103.   }
  104.   int value = counter;
  105.   if (grayMode) {
  106.     value = binaryToGray(counter);
  107.   }
  108.   digitalWrite(LED_MSB, (value >> 1) & 1);
  109.   digitalWrite(LED_LSB, (value >> 0) & 1);
  110. }
  111.  
  112. // Conversione Binario β†’ Gray
  113. int binaryToGray(int num) {
  114.   return num ^ (num >> 1);
  115. }
  116.  
  117. /* END CODE */
  118.  
Advertisement
Add Comment
Please, Sign In to add comment