Advertisement
belrey10

binary_counter

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