belrey10

Lesson #9: Binary Counting

Nov 4th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Binary Counter
  3. *
  4. * This example uses 6 LEDs to count in binary from 0 to 63.
  5. *
  6. * There are two buttons. One button will add 1 to the count
  7. * each time it is pressed, while the other button will
  8. * subtract 1 from the count.
  9. *
  10. * The 6 LEDs are connected to digital pins 2 through 7.
  11. * The "add" button is connected to digital pin 12.
  12. * The "subtract" button is connected to digital pin 13.
  13. *
  14. */
  15.  
  16. // Here we set some global constants
  17. // In other words, variables whose values do not change
  18.  
  19. const int add_button=12,
  20.           subtract_button=13,
  21.           NumberOfBits=6;
  22.  
  23.          
  24.  
  25. // Here we set some global non-constant variables.
  26. // In other words, variables whose values can change throughout execution.
  27.  
  28. int OurSixBitNumber[]={0,0,0,0,0,0}, LED_Selector, ButtonIsPressed;
  29.  
  30. void setup() {
  31.  
  32.   pinMode(2,OUTPUT);
  33.   pinMode(3,OUTPUT);
  34.   pinMode(4,OUTPUT);
  35.   pinMode(5,OUTPUT);
  36.   pinMode(6,OUTPUT);
  37.   pinMode(7,OUTPUT);
  38.  
  39.   pinMode(add_button,INPUT);
  40.   pinMode(subtract_button,INPUT);
  41.  
  42. }
  43.  
  44. void loop() {
  45.  
  46.   ButtonIsPressed = digitalRead(add_button); // Equals 1 if button being pressed, 0 if not.  
  47.   if(ButtonIsPressed == 1) { addButtonPressed(); }
  48.  
  49.   delay(100); // To prevent spamming
  50.  
  51.   ButtonIsPressed = digitalRead(subtract_button);
  52.   if(ButtonIsPressed == 1) { subButtonPressed(); }
  53.  
  54.   resetSelector();
  55.   writeLEDs();
  56.  
  57. }
  58.  
  59. /**************************/
  60.  
  61. /*
  62.  
  63. /*  ADDITIONAL FUNCTIONS:
  64.  
  65. /*     addButtonPressed()
  66. /*     subButtonPressed()
  67. /*     writeLEDs()
  68. /*     resetSelector()
  69. /*
  70. /**************************/
  71.  
  72. void addButtonPressed() {
  73.  
  74.   while(digitalRead(add_button) == 1) { /* do nothing until let go of button */ } //AKA: Add Button being held down still
  75.  
  76.   delay(100); //Delay for 100 milliseconds to prevent spamming of buttons
  77.   resetSelector();
  78.  
  79.   while(LED_Selector >= 0 && ButtonIsPressed != 0)
  80.   {
  81.     if(OurSixBitNumber[LED_Selector] == 1)
  82.     {
  83.       OurSixBitNumber[LED_Selector] = 0;
  84.       LED_Selector--;
  85.     }
  86.     else
  87.     {
  88.       OurSixBitNumber[LED_Selector] = 1;
  89.       ButtonIsPressed = 0;
  90.     }
  91.   }
  92. }
  93.  
  94. void subButtonPressed() {
  95.  
  96.   while(digitalRead(subtract_button) == 1) { /* do nothing until let go of button */ } //AKA: Subtract Button being held down still
  97.  
  98.   delay(100); //Delay for 100 milliseconds to prevent spamming of buttons
  99.  
  100.   resetSelector();
  101.  
  102.   while(LED_Selector >= 0 && ButtonIsPressed != 0) // While the LED Selector is greater than zero and the subtract button is 1, then starting from the last LED to the first, do the following:
  103.  
  104.   {
  105.     if(OurSixBitNumber[LED_Selector] == 0) // IF that LED is OFF, turn it on, and continue to the next LED (aka the one before because backwards)
  106.     {
  107.       OurSixBitNumber[LED_Selector] = 1; // Turning LED on
  108.       LED_Selector--; // Continuing to the next (aka the one before because going backwards) LED
  109.     }
  110.     else // ELSE (the LED is ON) turn off the LED and set the button state to NOT pressed.
  111.     {
  112.       OurSixBitNumber[LED_Selector] = 0; // Turn it off
  113.       ButtonIsPressed = 0; // Reset Button State
  114.     }
  115.   }
  116. }
  117.  
  118. void writeLEDs() {
  119.  
  120.   for(int temp = 0; temp < NumberOfBits; temp++)
  121.   {
  122.     if(OurSixBitNumber[temp] == 1)
  123.     {
  124.       digitalWrite(NumberOfBits - temp + 1, HIGH);
  125.     }
  126.     else
  127.     {
  128.       digitalWrite(NumberOfBits - temp + 1, LOW);
  129.     }
  130.     LED_Selector--;
  131.   }
  132. }
  133.  
  134. void resetSelector() {
  135.  LED_Selector = NumberOfBits - 1; // because arrays start at 0;
  136. }
  137.  
Add Comment
Please, Sign In to add comment