Push ON switch to turn "ON" and "OFF" LED.

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Push ON switch to turn "ON" and "OFF" LED.

Postby mdfarizal » Thu Nov 24, 2011 2:32 pm

Very simple question, but i have difficulty to sort it out.

with one push ON button how i can program it (in C language) to turn ON and OFF a LED in single push.

First time Push -> LED = ON,
Second time Push -> LED = OFF,
Third time Push -> LED = ON,
4th time Push -> LED = OFF,
repeated....

Thanks....
mdfarizal
Fledgling
 
Posts: 1
Joined: Thu Nov 24, 2011 2:23 pm

Re: Push ON switch to turn "ON" and "OFF" LED.

Postby Brian Griffin » Thu Nov 24, 2011 3:29 pm

There is a thing in the C language called "toggle". <- The hint is already there.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Push ON switch to turn "ON" and "OFF" LED.

Postby robosang » Fri Nov 25, 2011 9:23 am

You say you have difficulty to sort it out, so what is the difficulty? Any code to show us and what is the response?

Yup, brian is pointing you to correct direction, but I just want add some extra idea :mrgreen: , you might need debouncing algorithm to wait for switch to release. Again, I will not give answer, just guidance. Try to find and try it, that's learning.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Push ON switch to turn "ON" and "OFF" LED.

Postby ZaM » Mon Nov 28, 2011 12:29 pm

if(RB0==0)
{
led1 != led1;
}
ZaM
Moderator
 
Posts: 78
Joined: Tue Nov 23, 2010 4:16 pm

Re: Push ON switch to turn "ON" and "OFF" LED.

Postby ZaM » Mon Nov 28, 2011 12:31 pm

if(sw1==0)
{
while(sw1==0); //wait button release
led1 != led1;
}
ZaM
Moderator
 
Posts: 78
Joined: Tue Nov 23, 2010 4:16 pm

Re: Push ON switch to turn "ON" and "OFF" LED.

Postby robosang » Mon Nov 28, 2011 9:44 pm

ZaM WROTE:if(sw1==0)
{
while(sw1==0); //wait button release
led1 != led1;
}

So what is the outcome of this code? How is the LED response if this code is being written into PIC?
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Push ON switch to turn "ON" and "OFF" LED.

Postby ZaM » Tue Nov 29, 2011 12:25 am

sorry, tq robosang


int constant=1;
while(1) //condition for looping 1-true, 0-false
{

led = constant;

if(sw1==0)
{
while(sw1==0);
constant = !constant;

}
}
ZaM
Moderator
 
Posts: 78
Joined: Tue Nov 23, 2010 4:16 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: Bing [Bot] and 14 guests

cron