Advertisement
makispaiktis

ArduinoBasics - 8. Bit Counter

Mar 23rd, 2021 (edited)
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int button = 6;
  2. int led1 = 5;
  3. int led2 = 4;
  4. int led3 = 3;
  5. int led4 = 2;
  6. bool isPressed;
  7. int counter = 0;
  8.  
  9.  
  10. void setup()
  11. {
  12.   Serial.begin(9600);
  13.   pinMode(button, INPUT);
  14.   pinMode(led1, OUTPUT);
  15.   pinMode(led2, OUTPUT);
  16.   pinMode(led3, OUTPUT);
  17.   pinMode(led4, OUTPUT);
  18. }
  19.  
  20. void loop()
  21. {
  22.   isPressed = digitalRead(button);
  23.   if(isPressed == true){
  24.     counter++;
  25.   }
  26.   Serial.println(counter);
  27.   counter = counter % 16;
  28.  
  29.   if(counter == 1){
  30.     digitalWrite(led1, LOW);
  31.     digitalWrite(led2, LOW);
  32.     digitalWrite(led3, LOW);
  33.     digitalWrite(led4, HIGH);
  34.   }
  35.   if(counter == 2){
  36.     digitalWrite(led1, LOW);
  37.     digitalWrite(led2, LOW);
  38.     digitalWrite(led3, HIGH);
  39.     digitalWrite(led4, LOW);
  40.   }if(counter == 3){
  41.     digitalWrite(led1, LOW);
  42.     digitalWrite(led2, LOW);
  43.     digitalWrite(led3, HIGH);
  44.     digitalWrite(led4, HIGH);
  45.   }
  46.  
  47.   if(counter == 4){
  48.     digitalWrite(led1, LOW);
  49.     digitalWrite(led2, HIGH);
  50.     digitalWrite(led3, LOW);
  51.     digitalWrite(led4, LOW);
  52.   }
  53.   if(counter == 5){
  54.     digitalWrite(led1, LOW);
  55.     digitalWrite(led2, HIGH);
  56.     digitalWrite(led3, LOW);
  57.     digitalWrite(led4, HIGH);
  58.   }
  59.   if(counter == 6){
  60.     digitalWrite(led1, LOW);
  61.     digitalWrite(led2, HIGH);
  62.     digitalWrite(led3, HIGH);
  63.     digitalWrite(led4, LOW);
  64.   }if(counter == 7){
  65.     digitalWrite(led1, LOW);
  66.     digitalWrite(led2, HIGH);
  67.     digitalWrite(led3, HIGH);
  68.     digitalWrite(led4, HIGH);
  69.   }
  70.  
  71.   if(counter == 8){
  72.     digitalWrite(led1, HIGH);
  73.     digitalWrite(led2, LOW);
  74.     digitalWrite(led3, LOW);
  75.     digitalWrite(led4, LOW);
  76.   }
  77.   if(counter == 9){
  78.     digitalWrite(led1, HIGH);
  79.     digitalWrite(led2, LOW);
  80.     digitalWrite(led3, LOW);
  81.     digitalWrite(led4, HIGH);
  82.   }
  83.   if(counter == 10){
  84.     digitalWrite(led1, HIGH);
  85.     digitalWrite(led2, LOW);
  86.     digitalWrite(led3, HIGH);
  87.     digitalWrite(led4, LOW);
  88.   }if(counter == 11){
  89.     digitalWrite(led1, HIGH);
  90.     digitalWrite(led2, LOW);
  91.     digitalWrite(led3, HIGH);
  92.     digitalWrite(led4, HIGH);
  93.   }
  94.  
  95.   if(counter == 12){
  96.     digitalWrite(led1, HIGH);
  97.     digitalWrite(led2, HIGH);
  98.     digitalWrite(led3, LOW);
  99.     digitalWrite(led4, LOW);
  100.   }
  101.   if(counter == 13){
  102.     digitalWrite(led1, HIGH);
  103.     digitalWrite(led2, HIGH);
  104.     digitalWrite(led3, LOW);
  105.     digitalWrite(led4, HIGH);
  106.   }
  107.   if(counter == 14){
  108.     digitalWrite(led1, HIGH);
  109.     digitalWrite(led2, HIGH);
  110.     digitalWrite(led3, HIGH);
  111.     digitalWrite(led4, LOW);
  112.   }if(counter == 15){
  113.     digitalWrite(led1, HIGH);
  114.     digitalWrite(led2, HIGH);
  115.     digitalWrite(led3, HIGH);
  116.     digitalWrite(led4, HIGH);
  117.   }
  118.  
  119.   delay(400);
  120.   // I put delay(400), because I don't want the counter to be
  121.   // increased so quickly, so I suppose that the user presses
  122.   // the button with max "duration" = 400ms
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement