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