0
votes

My first useful project with pic which was to make a smart home automation system with 2 lasers that are put at the entrance of a room and detects whether you are getting in or out according to which laser you cut first from the LDR so if you are getting in you cut the first laser then the second and vise versa and accordingly it turns the light on or off and it also counts how much people entered and turn the lights off after the last gets out, the code is not working whatever i try with it and IDK why the lights get on whenever i cut only 1 laser alone, code is written with MPLAB and compiled with XC8, mcu: pic16f877a

main.cpp:

#include "define.h"

int main(){
    int counter = 0;
    TRISB = 255;
    OPTION_REG = 0b01111111;
    TRISC = 0;
    PORTC = 0;

    while(1){
        if(laser1 == 1 && laser2 == 0 && counter == 0){
            for(int x = 0; x <= 3000; x++){
                if(laser2 == 1){
                    PORTCbits.RC0 = 1;
                    counter++;
                    x = 0;
                    break;
                }
                __delay_ms(1);
            }

        }else if(laser1 == 1 && laser2 == 0 && counter >= 1){
            for(int y = 0; y <= 3000; y++){
                if(laser2 == 1){
                    counter++;
                    y = 0;
                    break;
                }
                __delay_ms(1);
            }
        }else if(laser1 == 0 && laser2 == 1){
            for(int z = 0; z <= 3000; z++){
                if(laser1 == 1){
                    counter--;
                    z = 0;
                    if(counter == 0){
                        PORTCbits.RC0 = 0;
                    }else{
                        PORTCbits.RC0 = 1;
                    }
                    break;
                }
                __delay_ms(1);
            }

        }

    }
}

define.h:

/* 
 * File:   define.h
 * Author: Fady
 *
 * Created on March 11, 2015, 10:51 PM
 */

#ifndef DEFINE_H
#define DEFINE_H

#ifdef  __cplusplus
extern "C" {
#endif


// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

#define laser1 PORTBbits.RB0
#define laser2 PORTBbits.RB1
#define LED PORTCbits.RC0
#define _XTAL_FREQ 4000000
// #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)


#ifdef  __cplusplus
}
#endif

#endif  /* DEFINE_H */

the circuit is a very simple one tho,:

where i only connected the led from PORTC pin 0 to ground and the 2 LDRs on pinb 1 & 2 in addition to power, of course, in vcc and MCLR and gnd and the 4MHz crystal

1
Can you provide some more info what does happen? Do the lights turn on at all, are you able to test debug in some form, does it throw any exceptions?CalebB
yup the led turns on when only any ldr of the two has laser falling on it without having any relation with the otherilouy
sorry forgot the header file will put it nowilouy
@CalebB any suggestions? :)ilouy
I suggest breaking the problem down into smaller parts. Try a simple case just to test functionality first. Such as if laser 1 is tripped turn the LED on, if it's tripped again turn the LED off. Then do the same thing with laser 2. Just to be certain your I/O and the MCU is setup and functioning properly. Then make it more complicated from there.DigitalNinja

1 Answers

0
votes

i tried messing in the code a bit, at last it worked i removed the else from the else if statements to be all if and it magically worked great,, here is the full code:

#include "define.h"

int main(){
    int counter = 0;
    TRISB = 255;
    OPTION_REG = 0b01111111;
    TRISC = 0;
    PORTC = 0;

    while(1){
        if(laser1 == 1 && laser2 == 0){
            for(int x = 0; x <= 3000; x++){
                if(laser2 == 1){
                    LED = 1;
                    counter++;
                    x = 0;
                    break;
                }
                __delay_ms(1);
            }

            __delay_ms(250);
        }
        if(laser1 == 0 && laser2 == 1){
            for(int x = 0; x <= 3000; x++){
                if(laser1 == 1){
                    counter--;
                    if(counter == 0){
                        LED = 0;
                    }
                    x = 0;
                    break;
                }
                __delay_ms(1);
            }
            __delay_ms(250);

        }

    }
}