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

    How to control outputs throught an array?

      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 */









              if( ! CapSense_IsBusy() )




                  /* 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] )



                      button_down[i] = 0;











      /* [] END OF FILE */

        • 1. Re: How to control outputs throught an array?

          You need an array that holds all the values of the CapSense_BUTTONx_SNS0_ID.

          then you can access the CapSense_IsWidgetActive calls with the table[n] value.


          For the LEDs you'll need an array of functions, see here how to.



          • 2. Re: How to control outputs throught an array?

            LED_i_Write(0) ;

            There is no option to do this in PSoC Creator for PSoC4 devices. The LED_1_Write function is implemented to write to corresponding register. And the register depends on the pin selected in CYDWR.

            Please consider using a switch case.



            Jobin GT

            • 3. Re: How to control outputs throught an array?

              I'm using a function over here.



              void  (*myleds_write[])(uint8)={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};

              uint8  (*myleds_read[])(void)={LED_1_Read,LED_2_Read,LED_3_Read,LED_4_Read,LED_5_Read,LED_6_Read,LED_7_Read,LED_8_Read,LED_9_Read,LED_10_Read,LED_11_Read,LED_12_Read,LED_13_Read,LED_14_Read,LED_15_Read,LED_16_Read};





              void drive_led(int x){   




              its  workinking right now.

              Tanks everybody for the help!