How to control outputs throught an array?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

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 && button )

{

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

               button_down = 1;

}

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

if( button_down && !button )

{

LED_1_Write(1);

                button_down = 0;

            }

            CapSense_UpdateAllBaselines();

CapSense_ScanAllWidgets();

            i++;

        }

        }

    }

}

/* [] END OF FILE */

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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 value.

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

Bob

View solution in original post

3 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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 value.

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

Bob

JobinT_31
Employee
Employee
50 solutions authored 25 solutions authored 10 solutions authored

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.

Thanks

Jobin GT

Anonymous
Not applicable

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){   

    (*myleds_write)(~(*myleds_read)());

}

its  workinking right now.

Tanks everybody for the help!

0 Likes