3 Replies Latest reply on Nov 22, 2017 5:08 AM by rodesposito_2800731

    How to control outputs throught an array?

    rodesposito_2800731

      I would like to know how to do multiple controller outputs through a single loop. I have 16 capacitive buttons on one board and would like to control the leds according to the touch of the buttons. Since there are 16 buttons I do not want to write line by line. I would like to use a loop loop to save computational effort. Attached is my coda. I am using the CY8CKIT-042-BLE-A kit.

       

      #include <project.h>

      #include <stdio.h>

       

       

       

       

         

       

       

       

       

      int main()

      {

          uint32 button[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* Current button state */

          uint32 button_down[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* Previous button state */

          const void *myleds[]={LED_1_Write,LED_2_Write,LED_3_Write,LED_4_Write,LED_5_Write,LED_6_Write,LED_7_Write,LED_8_Write,LED_9_Write,LED_10_Write,LED_11_Write,LED_12_Write,LED_13_Write,LED_14_Write,LED_15_Write,LED_16_Write};

       

       

         

         

         

         

          int i=0;   

        

         

      CyGlobalIntEnable; /* Enable global interrupts */

       

      /* Start the components */

       

       

      CapSense_Start(); /* Initialize Component */

      CapSense_ScanAllWidgets(); /* Scan all widgets */

      CapSense_InitializeAllBaselines();

      CapSense_ScanAllWidgets();

       

       

       

       

          for(;;)

          {

              if( ! CapSense_IsBusy() )

      {

                  while(i<16)

                  {

                  /* Read the buttons */

                  button[0] = CapSense_IsWidgetActive(CapSense_BUTTON0_SNS0_ID);

                  button[1] = CapSense_IsWidgetActive(CapSense_BUTTON1_SNS0_ID);

                  button[2] = CapSense_IsWidgetActive(CapSense_BUTTON2_SNS0_ID);

                  button[3] = CapSense_IsWidgetActive(CapSense_BUTTON3_SNS0_ID);

                  button[4] = CapSense_IsWidgetActive(CapSense_BUTTON4_SNS0_ID);

                  button[5] = CapSense_IsWidgetActive(CapSense_BUTTON5_SNS0_ID);

                  button[6] = CapSense_IsWidgetActive(CapSense_BUTTON6_SNS0_ID);

                  button[7] = CapSense_IsWidgetActive(CapSense_BUTTON7_SNS0_ID);

                  button[8] = CapSense_IsWidgetActive(CapSense_BUTTON8_SNS0_ID);

                  button[9] = CapSense_IsWidgetActive(CapSense_BUTTON9_SNS0_ID);

                  button[10] = CapSense_IsWidgetActive(CapSense_BUTTON10_SNS0_ID);

                  button[11] = CapSense_IsWidgetActive(CapSense_BUTTON11_SNS0_ID);

                  button[12] = CapSense_IsWidgetActive(CapSense_BUTTON12_SNS0_ID);

                  button[13] = CapSense_IsWidgetActive(CapSense_BUTTON13_SNS0_ID);

                  button[14] = CapSense_IsWidgetActive(CapSense_BUTTON14_SNS0_ID);

                  button[15] = CapSense_IsWidgetActive(CapSense_BUTTON15_SNS0_ID);

                 

                  // 1 = OFF ; 0 = ON

                  /* If button was not down and is now down, it was pressed */

       

       

                  if( !button_down[i] && button[i] )

      {

                    LED_i_Write(0);   // i want to drive the outputs using some kind of index here.

                     button_down[i] = 1;

      }

                  /* If button was down and is now not down, it was released */

      if( button_down[i] && !button[i] )

      {

      LED_1_Write(1);

                      button_down[i] = 0;

                  }

                  CapSense_UpdateAllBaselines();

      CapSense_ScanAllWidgets();

                  i++;

              }

              }

          }

      }

       

       

      /* [] END OF FILE */