pleasedontcode

Control Panel rev_01

Oct 27th, 2025
256
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: Control Panel
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-27 11:44:44
  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 */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. #include <Arduino.h>
  30.  
  31. // Pin definitions
  32. const int BTN_UP = 2;
  33. const int BTN_DOWN = 3;
  34. const int BTN_RESET = 4;
  35. const int SWITCH_MODE = 5;
  36. const int LED_MSB = 6;
  37. const int LED_LSB = 7;
  38.  
  39. // Variables
  40. int counter = 0;
  41. bool grayMode = false;
  42. bool ledsActive = false;
  43. unsigned long lastPress = 0;
  44. const unsigned long debounceDelay = 200;
  45.  
  46. // Function prototypes
  47. void updateLEDs();
  48. int binaryToGray(int num);
  49.  
  50. void setup() {
  51.   // Set input pins with internal pull-up
  52.   pinMode(BTN_DOWN, INPUT_PULLUP);
  53.   pinMode(BTN_UP, INPUT_PULLUP);
  54.   pinMode(BTN_RESET, INPUT_PULLUP);
  55.   pinMode(SWITCH_MODE, INPUT_PULLUP);
  56.  
  57.   // Set output pins for LEDs
  58.   pinMode(LED_MSB, OUTPUT);
  59.   pinMode(LED_LSB, OUTPUT);
  60.  
  61.   // Initialize LEDs
  62.   digitalWrite(LED_MSB, LOW);
  63.   digitalWrite(LED_LSB, LOW);
  64. }
  65.  
  66. void loop() {
  67.   // Read mode switch
  68.   grayMode = (digitalRead(SWITCH_MODE) == LOW);
  69.  
  70.   // Increment button
  71.   if (digitalRead(BTN_UP) == LOW && millis() - lastPress > debounceDelay) {
  72.     counter++;
  73.     if (counter > 3) counter = 0;  // Counter modulo 4
  74.     ledsActive = true;
  75.     lastPress = millis();
  76.     updateLEDs();
  77.   }
  78.  
  79.   // Decrement button
  80.   if (digitalRead(BTN_DOWN) == LOW && millis() - lastPress > debounceDelay) {
  81.     counter--;
  82.     if (counter < 0) counter = 3;
  83.     ledsActive = true;
  84.     lastPress = millis();
  85.     updateLEDs();
  86.   }
  87.  
  88.   // Reset button
  89.   if (digitalRead(BTN_RESET) == LOW && millis() - lastPress > debounceDelay) {
  90.     counter = 0;
  91.     ledsActive = false;
  92.     lastPress = millis();
  93.     updateLEDs();
  94.   }
  95. }
  96.  
  97. void updateLEDs() {
  98.   if (!ledsActive) {
  99.     digitalWrite(LED_MSB, LOW);
  100.     digitalWrite(LED_LSB, LOW);
  101.     return;
  102.   }
  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. int binaryToGray(int num) {
  113.   return num ^ (num >> 1);
  114. }
  115.  
  116. /* END CODE */
  117.  
Advertisement
Add Comment
Please, Sign In to add comment