Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

checking sequence using IF statement

Status
Not open for further replies.

ycho

New Member
Hi

I am trying to check the sequence using if statement.
first : IR1 and IR3 goes low and IR2 and IR4 stay high
check by while loop, if it is true then go to next if statement.
second : IR1 ,IR2,IR3 and IR4 all goes low.
check by while loop, if it is true then go to next if statement.
lastly :IR1 and IR3 goes high and IR2 and IR4 stay low.

if fulfill above requirement, LCD will increment by one.
I had wrote the program but the program doesn't work.
can someone figure out the problem for me.
below is my code

Code:
#include <16f877a.h>
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt,nolvp
#define use_portb_lcd TRUE 
#include <lcd.c>

#define IR1 Pin_A0
#define IR2 Pin_A1
#define IR3 Pin_A3
#define IR4 Pin_A4


void main()
{  
   int people_in = 0, people_out = 0;
   
   set_tris_a(0b00001111);
   lcd_init();

   lcd_putc("\fAutomatic Room");
   lcd_putc("\nLight Controller");
   delay_ms(5000);
   lcd_putc("\f");


display_1st :
   
   lcd_gotoxy(1,1);
   lcd_putc("People in  =  \n");
   lcd_putc("People out =  \n");
   
   lcd_gotoxy(14,1);
   printf(lcd_putc,"%3u",people_in);
       
 do{
   
if (input(IR1)==0 && input(IR3) ==0 && input(IR2)==1 && input(IR4)==1)  
{
while((input(IR1) && input(IR3)) ^ (input(IR2) && input(IR4)));         // (0 AND 0) = 0  (1 AND 1) = 1      0 XOR 1 = 1= TRUE
if (input(IR1)==0 && input(IR3) ==0 && input(IR2)==0 && input(IR4)==0)
{
while((!input(IR1) && !input(IR3)) && (!input(IR2) && !input(IR4)));    // 1 AND 1 AND 1 AND 1 = 1 = TRUE
if (input(IR1)==1 && input(IR3) ==1 && input(IR2)==0 && input(IR4)==0)
            {
               people_in += 1;
               lcd_gotoxy(14,1);
               printf(lcd_putc,"%3u",people_in);
            }
             while((input(IR1) && input(IR3)) ^ (input(IR2) && input(IR4)));
         }
}
   }while(1); 
}
 
its more like:

if ((input(IR1)==0) && (input(IR3) ==0) && (input(IR2)==1) && (input(IR4)==1))

You need each check in its own Parenthesis ()
 
while((input(IR1) && input(IR3)) ^ (input(IR2) && input(IR4))); // (0 AND 0) = 0 (1 AND 1) = 1 0 XOR 1 = 1= TRUE
This looks ambiguous as you're looking for ALL inputs on and not what your comments imply.

Cheers Ian
 
The code is very poor, hard to understand and slow to execute.

Why not try detecting all bits at once? That's faster and much neater.

Something like this;
Code:
inputs = (PORTA & 0b00001111);  // keep only 4 bits
if(inputs == 0b00000011) blah;
if(inputs == 0b00001111) blah;
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top