Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help-c language code problem

Status
Not open for further replies.

sfchew7

Junior Member level 2
Joined
Sep 29, 2011
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,423
C LANGUAGE CODE PROBLEM
hi, I want to make the code below will in each condition will only execute printf 1 time. so how should I change it ?
thanks
Code:
while(1)
{

if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==1))
{
delay_ms(1000);
lcd_putc("\fPL available:0");
delay_ms(1000);


printf("\fPL available:0");
delay_ms(1000);
}

else if((input(pin_a0)==0) && (input(pin_a1)==0) && (input(pin_a2)==0))
{
delay_ms(1000);
lcd_putc("\fPL available:3");
delay_ms(2000);
lcd_putc("\fPL1-empty");
lcd_putc("\nPL2-empty");
delay_ms(1000);
lcd_putc("\fPL3-empty");
delay_ms(1000);

printf("\fPL available:3");
delay_ms(1000);
}


}
 

C LANGUAGE CODE PROBLEM
hi, I want to make the code below will in each condition will only execute printf 1 time. so how should I change it ?
thanks
Code:
while(1)
{

if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==1))
{
delay_ms(1000);
lcd_putc("\fPL available:0");
delay_ms(1000);


printf("\fPL available:0");
delay_ms(1000);
}

else if((input(pin_a0)==0) && (input(pin_a1)==0) && (input(pin_a2)==0))
{
delay_ms(1000);
lcd_putc("\fPL available:3");
delay_ms(2000);
lcd_putc("\fPL1-empty");
lcd_putc("\nPL2-empty");
delay_ms(1000);
lcd_putc("\fPL3-empty");
delay_ms(1000);

printf("\fPL available:3");
delay_ms(1000);
}


}


you try to modify the code

Code:
int count=0;
int count1=0;
while(1)
{

if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==1))
{
delay_ms(1000);
lcd_putc("\fPL available:0");
delay_ms(1000);


if(count ==0)
{
printf("\fPL available:0");
delay_ms(1000);
}

if(count==10)
{
count=1;
}

count =count+1;
}

else if((input(pin_a0)==0) && (input(pin_a1)==0) && (input(pin_a2)==0))
{
delay_ms(1000);
lcd_putc("\fPL available:3");
delay_ms(2000);
lcd_putc("\fPL1-empty");
lcd_putc("\nPL2-empty");
delay_ms(1000);
lcd_putc("\fPL3-empty");
delay_ms(1000);


if(count1==0)
{

printf("\fPL available:3");
delay_ms(1000);
}

if(count==10)
{
count=1;
}
count1=count1+1;

}

should work.....

good luck
 

milind.a.kulkarni said:
Code:
if(count1==0)
{

printf("\fPL available:3");
delay_ms(1000);
}

if(count==10)
{
count=1;
}
count1=count1+1;

This part of code in the else if statement will not work, unless you replace "count" with "count1" everywhere. Furthermore, you could simplify things as follows:

Code:
unsigned char flag=0;
unsigned char flag1=0;
while(1)
{

if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==1))
{
delay_ms(1000);
lcd_putc("\fPL available:0");
delay_ms(1000);


if(!flag)
{
printf("\fPL available:0");
delay_ms(1000);
flag=1;  //This makes sure that code will not pass from here again
}
}

else if((input(pin_a0)==0) && (input(pin_a1)==0) && (input(pin_a2)==0))
{
delay_ms(1000);
lcd_putc("\fPL available:3");
delay_ms(2000);
lcd_putc("\fPL1-empty");
lcd_putc("\nPL2-empty");
delay_ms(1000);
lcd_putc("\fPL3-empty");
delay_ms(1000);


if(!flag1)
{
printf("\fPL available:3");
delay_ms(1000);
flag1=1;  //This makes sure that code will not pass from here again
}
}

So actually it is not a counter that you need in this example, but a flag.

Hope this helps.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top