5. Pushbuttons: Counters

Goals

  • Read the time and pulsation counters associated with a button.
  • Perform actions associated with time counters.

The keyCount function

int keyCount(int keyNum)

This function returns the number of times a button has been pressed. The value is incremented by one immediately after the push button is pressed. If the button is held down for half a second, the value increases at the rate of 5 pulsations per second. If the button is held down for two seconds, the pulsing value increases at the rate of 20 pulsations per second.

keyNum: número del 1 al 6 que representa al pulsador del que se solicita su valor.

The keyTimeOn function

int keyTimeOn(int keyNum)

This function returns the time that the button has been pressed. Time is measured by counting 250 steps per second.

keyNum: número del 1 al 6 que representa al pulsador del que se solicita su valor.

The keyTimeOff function

int keyTimeOff(int keyNum)

This function returns the time that the button has not been pressed. Time is measured by counting 250 steps per second.

keyNum: número del 1 al 6 que representa al pulsador del que se solicita su valor.

Exercises

Program the code needed to solve the following problems.

  1. The following program moves a led to the right when button 2 is pressed. Modify the program so that the led moves to the left when button 1 is pressed.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    // Mueve una luz a izquierda y derecha con los pulsadores 1 y 2
    
    #include <Picuino.h>
    
    int led, count;
    
    void setup() {
       pio.begin();            // Inicializa el shield Picuino UNO
       led = 1;                // Enciende primero el led D1
       pio.ledWrite(led, LED_ON);
    }
    
    void loop() {
    
       // Mueve a la derecha con la tecla derecha
    
       // Lee el número de pulsaciones del pulsador 'derecha'
       count = pio.keyCount(KEY_RIGHT);
    
       // Si el número de pulsaciones es mayor que cero
       if (count > 0) {
    
          // Apaga el led actual
          pio.ledWrite(led, LED_OFF);
    
          // Incrementa la posición del led
          led = led + count;
    
          // Si la posición del led es mayor que 6
          // vuelve a la posición 1
          if (led > 6) led = 1;
    
          // Enciende el led en la nueva posición
          pio.ledWrite(led, LED_ON);
       }
    }
    
  2. The following program is a game to check the ability to tell time. You must wait until button 1 is not pressed. At that moment, the time you have been pressed will be measured. The closer the time pressed is to 3 seconds, the more score you will get. Modify the program so that before starting the game, led D1 blinks once per second. Once button 1 is pressed, the flashing should go off so as not to give clues about the time it should be on.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    // Juego de medida de tiempo
    
    #include <Picuino.h>
    
    int time, points;
    
    void setup() {
       pio.begin();   // Inicializa el shield Picuino UNO
    }
    
    void loop() {
    
       // Espera hasta que se presione el pulsador 1
       while(pio.keyPressed(1) == 0);
    
       // Espera hasta que se deje de presionar el pulsador 1
       while(pio.keyPressed(1) == 1);
    
       // Lee el tiempo que ha estado presionado el pulsador 1
       time = pio.keyTimeOn(1);
    
       // Calcula la puntuación
       points = time - 3*250;
    
       // Muestra la puntuación por el display
       pio.dispWrite(abs(points));
       if (points < 0)
          pio.dispWrite(1, 0x02);
       delay(1000);
    }
    
  3. Modify the program that appears below, similar to the previous one. In this game the score will be higher if a button is pressed for the same time twice in a row. First the program must measure the time that button 1 has been pressed, then it must wait until it is not pressed. Finally, the second time that the button is pressed will be measured.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    // Juego de medida de tiempo. Versión de dos pulsaciones
    
    #include <Picuino.h>
    
    int time1, time2;
    int points;
    
    void setup() {
       pio.begin();  // Inicializa el shield Picuino UNO
    }
    
    void loop() {
       // Comienza parpadeando el led D1
       pio.ledBlink(1, 50, 50);
    
       // Espera hasta que se presione el pulsador 1
       while(pio.keyPressed(1) == 0);
    
       // Cuenta el tiempo que está presionado el pulsador 1
       while(pio.keyPressed(1) == 1) {
         time1 = pio.keyTimeOn(1);
         pio.dispWrite(time1);
       }
    
       // Espera hasta que se presione el pulsador 1
       while(pio.keyPressed(1) == 0);
    
       // Cuenta el tiempo que está presionado el pulsador 1
       while(pio.keyPressed(1) == 1) {
          time2 = pio.keyTimeOn(1);
          pio.dispWrite(time2);
       }
    
       // Apaga el led D1 y espera un segundo
       pio.ledWrite(1, LED_OFF);
    
       // Calcula la puntuación
       points = time1 - time2;
    
       // Muestra la puntuación por el display
       while(1) {
          pio.dispWrite(abs(points));
          delay(500);
          pio.dispBegin();
          delay(500);
          // Sal si se presiona el pulsador 1
          if (pio.keyEvents(1) == KEY_PRESSED_TIME2)
             break;
       }
    
       // Espera hasta que no se presione el pulsador 1
       while(pio.keyPressed(1) == 1);
    }