Jump to content
43oh

LED Flash Rate Change


Recommended Posts

Can you flash an LED right now? is it setup via a timer? If yes, just change the timer period when you press a button.

 

I cannot tell what you need/(also your knowledge level) to do as you have provided no information at all. Let us know what you have done and then take it from there... or just post your code below. We are here to help anyway. :thumbup:

 

Moving to General.

Link to post
Share on other sites

i can flash an led with the press of a button. i am using an if/else statement for reconizing the button press and i am useing the folowing code to control the leds

void delay_ms(unsigned int ms ){       // Not really a milliseconds delay!
     unsigned int i;
     for (i = 0; i <= ms; i++){       
       __delay_cycles(500);
     }
}
void do_led( int led, int delay ) {
  P1OUT = led;
  delay_ms( delay );
}

witch allows me to control the leds easier by typing do_led(BIT1 , 500) as an example.

i just thought about maybe using a variable for time and maybe changing it with a second button press or have several subroutines that have different times and it the button press would switch to the next one. but again i am not sure how to do this.

Link to post
Share on other sites

You can count your button presses using a variable. Every press increments a variable. When you are at the end of the no. of presses, roll over.

 

eg:

For 4 types of delays for a button counter i.

i=0;

button press, increment counter

1 button press : do_led(led, 500)

 

button press, increment counter

2 button press : do_led(led, 500)

 

button press, increment counter

3 button press : do_led(led, 500)

 

button press, increment counter

4 button press : do_led(led, 500)

i=0;

 

Make sure your button is software debounced or you will get a lot of button registrations.

Link to post
Share on other sites

I see what you mean but i am not sure how to put it all together. i have made a rough code that does change the rate but i cant figure out how to make it come on and off with a button press. also when i press the button to change the rate it sometimes skips over most of them. here is the code.

#include  
#define     LED0               	BIT0
#define     LED1             	BIT6
#define     LED2             	BIT1
#define     LED3             	BIT4
#define     LED4             	BIT5
#define		BUTTON0				BIT7
#define   	BUTTON1				BIT3
int e;

void delay_ms(unsigned int ms ){       
     unsigned int i;
     for (i = 0; i <= ms; i++){   
       __delay_cycles(500);
     }
}
void do_led( int led, int delay ) {
  P1OUT = led;
  delay_ms( delay );
}
void main (void)
{
  WDTCTL = WDTPW | WDTHOLD;
  P1DIR = LED0 | LED1 | LED2 | LED3 | LED4; //Sets the LEDs to output

e=0;

  for(; //Infinite loop
  { 


     if((P1IN & BUTTON1) ==0){
     e++;

     }       
    else{}

    if((e)==1){
    do_led(LED0 , 50);
    do_led(LED1 , 50);
    }
    else{}

    if((e)==2){
    do_led(LED0 , 100);
    do_led(LED1 , 100);
    }
    else{}


            if((e)==3){
    do_led(LED0 , 200);
    do_led(LED1 , 200);
    }
    else{}
         if((e)==4){
    do_led(LED0 , 400);
    do_led(LED1 , 400);
    }
    else{}
         if((e)==5){
    e=0;
    }
     else{}



  }
}

Link to post
Share on other sites
hat does change the rate but i cant figure out how to make it come on and off with a button press. also when i press the button to change the rate it sometimes skips over most of them.

You need to debounce your circuit in hardware or implement a software debounce by gw.

Link to post
Share on other sites

Isnt BIT3 on the launchpad aready debounced?

No, it just has an onboard pullup resistor. There's no onboard circuit to measure RC time. You'll have to build one or simply do it in software.

 

Following your code, it should continue to increment while holding the button. It won't allow anything else to happen until you release the button.

if((P1IN & BUTTON1) ==0){
     e++;

     }       
    else{}

Link to post
Share on other sites

I wrote some code to alternate the LED's flashing on the launchpad, which alters the rate of flash based on P1.3 being pressed. Short press increases flash rate, long press decreases it (by a factor of 2). It has a crude software PWM output (coded as 1 in 10 below), and an equally crude debounce (how many program cycles has the button been pressed).

 

This was my first microcontroller code, and I managed to do it all without learning interrupts or timers. How pleased I was with myself! Luckily my code was irrelevant to the project, but if you're code is being judged in any way, I recommend you ignoring it all.

 

Anyway, the code is below if you can make use of it. Everything is very basic, using code cycles as the "time" to measure/set LED flash times, button press times and PWM times.

 

#include 

long test, a=0, delay=2000, delay1=2000, half;

void main(void) {
P1DIR |= BIT0; 					//Set P1.0 as an output
P1DIR |= BIT6;					//Set P1.6 as an output
P1OUT &= ~BIT6;				//Set P1.0 and P1.6 low
P1OUT &= ~BIT0;					

WDTCTL = WDTPW + WDTHOLD;		// Hold the WDT (WatchDog Timer)

while(1){					//loop forever
	delay1--;				//decrement counter
	half = (delay/2);		//set value of half to be delay/2
	if (delay1<=half){		//if counter value is less than			
               if (delay1 % 10==0){		//half
		P1OUT &= ~BIT0;	//
	}					
	else if (delay1 % 10!=0){	
		P1OUT |= BIT0;			
		P1OUT |= BIT6;			

	}							




	}
	if (delay1>half) {		//as above but for second half of
			if (delay1 % 10==0){	//cycle
			P1OUT &= ~BIT6;
		}
		else if (delay1 % 10!=0){
			P1OUT |= BIT6;
			P1OUT |= BIT0;
		}

	}
	if (delay1<=0){
		delay1=delay;
	} 



	if (P1IN & BIT3){	//Rudimentary debounce software also measures 
		test=a;	//duration button pressed in program cycles
		a=0;		//and alters delay value accordingly.
	}

	if (!(P1IN & BIT3)){
		a++;
	}
	if (test>=500){
		delay=(delay*2);
		delay1=delay;

		test = 0;		
}
if ((test>=100)&&(test<500)){
	delay=(delay/2);

	delay1=delay;
	test = 0;

}
}

}

Link to post
Share on other sites

Tidied it up, now it just flashes P1.0, the rate of flash doubling with a short press of S1, halving with a long press - tested on a fresh out of the box LP and G2231 - HTH

 

#include 

long test, a=0, delay=2000, delay1=2000, half;

void main(void) {
  P1DIR |= BIT0;                //Set P1.0 as an output
  P1OUT &= ~BIT0;               //Set P1.0 low

  WDTCTL = WDTPW + WDTHOLD;      // Hold the WDT (WatchDog Timer)

  while(1){               //loop forever
     delay1--;            //decrement counter
     half = (delay/2);      //set value of half to be delay/2
     if (delay1<=half){      //if counter value is less than         

        P1OUT &= ~BIT0;   
     }               
     else {   
        P1OUT |= BIT0;         


     }                     




     if (delay1<=0){
        delay1=delay;
     } 



     if (P1IN & BIT3){   //Rudimentary debounce software also measures 
        test=a;   //duration button pressed in program cycles
        a=0;      //and alters delay value accordingly.
     }

     if (!(P1IN & BIT3)){
        a++;
     }
     if (test>=500){
        delay=(delay*2);
        delay1=delay;

        test = 0;      
  }
  if ((test>=100)&&(test<500)){
     delay=(delay/2);

     delay1=delay;
     test = 0;

  }
  }
}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...