fading LED with pushbutton also Software only, not PWM

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

cross mob
KiTh_4442166
Level 1
Level 1

1) Using the PSoC 5 push button start out fading @ 1 second

2) Push button when button is down LED is off

when you release the button the first time the LED fades as twice as fast

When you release the button the second time the LED Fades twice as slow as the original

when you release it for the 3rd time, it will go back to its original speed

Cycle repeats with each button push.

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Have you ever tried the previous question/request?

I'm trying to fade a LED, without using PWM? only using software? anyone know how to do it?

Please let me know if it worked or not. (reply to the previous topic)

Although I'm wondering if I'm doing your homework,

here is my example for it.

main.c

====================

#include "project.h"

#define DELTA_VALUE  10

#define PWM_PERIOD  1000

#define MODE_NORMAL    0

#define MODE_TWICE    1

#define MODE_HALF      2

#define SW_OFF        1

#define SW_ON          0

#define LED_OFF        0

#define LED_ON        1

int count =          0 ;

int period = PWM_PERIOD ;

int comp =  PWM_PERIOD ;

int delta =  DELTA_VALUE ;

int mode =  MODE_NORMAL ;

void advance_state_machine(void)

{

    switch(mode) {

    case MODE_NORMAL:

        mode = MODE_TWICE ;

        delta = 2 * DELTA_VALUE ;

        break ;

    case MODE_TWICE:

        mode = MODE_HALF ;

        delta = DELTA_VALUE / 2 ;

        break ;

    case MODE_HALF:

    default:

        mode = MODE_NORMAL ;

        delta = DELTA_VALUE ;

        break ;

    }  

}

void cruise(void)

{

    int phase ;

  

    phase = count % period ;

  

    if (phase < comp) {

        LED_Write(LED_ON) ;

    } else {

        LED_Write(LED_OFF) ;

    }

  

    if (phase == 0) {

        comp -= delta ;

        if (comp <= 0) {

            comp = period ;

        }

    }

    count = (count + 1) % (PWM_PERIOD * DELTA_VALUE) ;

}

int main(void)

{

    int prev_sw ;

    int sw ;

    CyGlobalIntEnable; /* Enable global interrupts. */

      

    prev_sw = SW1_Read() ;

    for(;;) {

        sw = SW1_Read() ;

      

        if (sw == SW_ON) { /* when switch is pressed turn LED OFF */

            LED_Write(LED_OFF) ;

        } else if ((sw != prev_sw)&&(sw == SW_OFF)) { /* SW is just released */

            advance_state_machine() ;

        } else {

            cruise() ;

        }

      

        prev_sw = sw ;

        CyDelayUs(1) ;

    }

}

====================

moto

View solution in original post

0 Likes
4 Replies
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Have you ever tried the previous question/request?

I'm trying to fade a LED, without using PWM? only using software? anyone know how to do it?

Please let me know if it worked or not. (reply to the previous topic)

Although I'm wondering if I'm doing your homework,

here is my example for it.

main.c

====================

#include "project.h"

#define DELTA_VALUE  10

#define PWM_PERIOD  1000

#define MODE_NORMAL    0

#define MODE_TWICE    1

#define MODE_HALF      2

#define SW_OFF        1

#define SW_ON          0

#define LED_OFF        0

#define LED_ON        1

int count =          0 ;

int period = PWM_PERIOD ;

int comp =  PWM_PERIOD ;

int delta =  DELTA_VALUE ;

int mode =  MODE_NORMAL ;

void advance_state_machine(void)

{

    switch(mode) {

    case MODE_NORMAL:

        mode = MODE_TWICE ;

        delta = 2 * DELTA_VALUE ;

        break ;

    case MODE_TWICE:

        mode = MODE_HALF ;

        delta = DELTA_VALUE / 2 ;

        break ;

    case MODE_HALF:

    default:

        mode = MODE_NORMAL ;

        delta = DELTA_VALUE ;

        break ;

    }  

}

void cruise(void)

{

    int phase ;

  

    phase = count % period ;

  

    if (phase < comp) {

        LED_Write(LED_ON) ;

    } else {

        LED_Write(LED_OFF) ;

    }

  

    if (phase == 0) {

        comp -= delta ;

        if (comp <= 0) {

            comp = period ;

        }

    }

    count = (count + 1) % (PWM_PERIOD * DELTA_VALUE) ;

}

int main(void)

{

    int prev_sw ;

    int sw ;

    CyGlobalIntEnable; /* Enable global interrupts. */

      

    prev_sw = SW1_Read() ;

    for(;;) {

        sw = SW1_Read() ;

      

        if (sw == SW_ON) { /* when switch is pressed turn LED OFF */

            LED_Write(LED_OFF) ;

        } else if ((sw != prev_sw)&&(sw == SW_OFF)) { /* SW is just released */

            advance_state_machine() ;

        } else {

            cruise() ;

        }

      

        prev_sw = sw ;

        CyDelayUs(1) ;

    }

}

====================

moto

0 Likes

hi,

Thanks for the help, and yes it worked.

but I able to come up with this one for fading. And it is not a homework,

I'm just following tutorials and doing tasks using Psoc 5 to get

familiar with MC.

#include "project.h"

#define T 1000

int main(void)

{

//int T = 1000;

CyGlobalIntEnable; /* Enable global interrupts. */

/* Place your initialization/startup code here (e.g. MyInst_Start()) */

for(;;)

{

for(int i = 0; i<T; i++){

LED_Write(1);

CyDelayUs(i);

LED_Write(0);

CyDelayUs(T-i);

}

for(int i = T; i>0; i--){

LED_Write(1);

CyDelayUs(i);

LED_Write(0);

CyDelayUs(T-i);

}

}

}

On Tue, Oct 15, 2019 at 11:22 PM Motoo Tanaka <community-manager@cypress.com>

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Thank you for your quick response!

> Thanks for the help, and yes it worked.

It would be appreciated if you can mark my response to the previous topic as correct answer.

> but I able to come up with this one for fading.

Nice approach!

I'd like to have "int i ;" in the beginning of the main() tough.

> And it is not a homework,

> I'm just following tutorials and doing tasks using Psoc 5 to get

> familiar with MC.

OK, I'm glad hearing that.

moto

0 Likes

It may not be a homework, but it is awfully similar to the lab assignment (well, almost word for word) that was due.

I guess future assignments would have to me more complex, so that it is not easily answered within the context of a forum.

0 Likes