0
votes

I am trying to make a smart home automation device where when u enter the room the lights automatically turn on and vice versa I use for my project two lasers to specify whether people enter or leave the room i also want the pic to count how many people entered the room and turn the lights off after the last one that enter the room leave to do this i use pic16f877a ic and mplab as my ide with xc8 compiler , so far the program works so good but it doesn't count the people who entered the room but turn off the lights only after one leaves which is not cool and i don't really know where the problem is my code looks alright. here is the code:

            /* 
             * File:   main.c
             * Author: Fady
             *
             * Created on September 23, 2014, 9:56 PM
             */

            #include <stdio.h>
            #include <stdlib.h>

            /*
             * 
             */


            // PIC16F877A Configuration Bit Settings

            // 'C' source line config statements

            #include <xc.h>

            // #pragma config statements should precede project file includes.
            // Use project enums instead of #define for ON and OFF.

            // CONFIG
            #pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)
            #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
            #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
            #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
            #pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
            #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
            #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
            #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

            #define _XTAL_FREQ 4000000
            #define laser1 PORTBbits.RB0
            #define laser2 PORTBbits.RB1
            #define isOn ==0
            #define isOff ==1

            int x1 = 0;
            int x2 = 0;

            int main() {
                char people = 0;
                nRBPU = 0;
                TRISBbits.TRISB0 = 1;   //laser 1 for input
                TRISBbits.TRISB0 = 1;   //laser 2 for input
                TRISCbits.TRISC0 = 0;   //output LED for output
                PORTCbits.RC0 = 0;
                while(1){
                    beginning:
                    if(( laser1 isOn && laser2 isOn ) || ( laser1 isOff && laser2 isOff )){                     //if both lasers are on
                       goto beginning;
                    }else if(laser1 isOff && laser2 isOn){               //if laser1 is off and laser2 is on
                        readO:
                        if(laser2 isOff){
                            if(people == 0){
                                people = 1;
                                PORTCbits.RC0 = 1;
                            }if(people >= 1){
                                people++;
                            }

                        }else{
                            for(; x2 <= 1000; x2++){
                                __delay_ms(1);
                                goto readO;
                            }
                        }
                        x2 = 0;

                    }else if(laser1 isOn && laser2 isOff){               //if laser1 is on and laser2 is off
                        readC:
                        if(laser1 isOff){
                            people--;
                            if(people == 0){
                                PORTCbits.RC0 = 0;
                            }
                        }else{
                            for(; x1 <= 1000; x1++){
                                __delay_ms(1);
                                goto readC;
                            }
                        }

                        x1 = 0;

                    }
                }

            }

i think it has no problem but i dont know what is wrong here anyways thanks in advance for any replier who is gonna solve this it would really be a great help for me

2

2 Answers

1
votes

First try to reduce a code in an understandable form. 1st: Avoid jumping operations like goto: 2nd: Too many if statements with unclear results, try to use SWITCH statement.

beginning:
                if(( laser1 isOn && laser2 isOn ) || ( laser1 isOff && laser2 isOff )){                     //if both lasers are on
                   goto beginning;

why doing this? You are in forever loop!

0
votes

ok, let's keep things simple.

countPeople = 0;

while(1) {

if(PORTBbits.RB0 == 0) // People enter
{
    countPeople++; // 
    //TODO: some thing if you want      
}
elseif(PORTBbits.RB0 == 1) // people out or something like this
{
    countPeople--;
}

//if nothing happend 

}