Thread: Need help with a Analog and Digital Intruder alarm

  1. #1
    Registered User
    Join Date
    Mar 2021

    Unhappy Need help with a Analog and Digital Intruder alarm

    Hey guys I'm new here and I'm struggling on my electronics project.

    I have to create an embedded system of my choice (i went for an Intruder alarm). It has 3 inputs - a sliding switch (digital), PIR sensor (digital), IR sensor (Analog).

    I have attached my c code.
    I just want to know how the main function coudl work for having 3 inputs (2 digital and 1 analog) into 1 named function I've created.

    C code is attached.

    Thanks for anyone who can help!LCDACDCombinedTrue.c

  2. #2
    Registered User
    Join Date
    Sep 2020
    I reformatted your code's intentation
    void main (void)
        //SET UP
        ANSEL  = 0;                                    //turn off all analog inputs
        ANSELH = 0; 
        ANSELbits.ANS1 = 0;                            //RA1 setting to Digital
        ANSELbits.ANS2 = 0;                            //RA2 setting to Digital
        TRISD = 0b00000000;                            //Port D bits 7:0 are all outputs
        LATD   = 0b00000000;                        //turns off PORTD outputs, good start position
        TRISC = 0b00000000;                            //Port C bits 7:0 are all outputs
        LATC   = 0b00000000;                        //turns off PORTC outputs, good start position
        TRISAbits.TRISA0 = 1;                        //TRISA0 Input (IR Sensor)
        TRISAbits.TRISA1 = 1;                        //TRISA1 Input (PIR Sensor)
        TRISAbits.TRISA2 = 1;                        //TRISA2 Input (Switch)
        // this code configures the display  
        WriteCmd ( 0x02 );                            // sets 4bit operation
        WriteCmd ( CLEAR_SCREEN);                    
        WriteCmd ( FOUR_BIT & LINES_5X7 );            // sets 5x7 and multiline operation.
        WriteCmd ( CURSOR_BLINK );                    // blinks cursor
        WriteCmd ( CURSOR_RIGHT  );                    // moves cursor right    
        while(1)                            // Start of user program
            //Init ADC
            //LATA = ADRESH;
            DistanceValue = ADRESH;
            READING = (DistanceValue);
            if (READING > 0b100110)  // Binary Number is 38 which is Number coming 
                                     // from 500mV which is     around     30cm away from Sensor
            READING = (DistanceValue);
            if (READING < 0b100110)
        if (PORTAbits.RA2 == 0 && READING > 0b100110)        //Checks if Button is Pressed
    It is pretty clear that your "Checks if Button is Pressed" is out side of the main 'while()' loop, and will not get run.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in an Analog to Digital converter program
    By hiubaocong in forum C Programming
    Replies: 4
    Last Post: 01-18-2011, 01:37 AM
  2. cd analog
    By prospb in forum C Programming
    Replies: 2
    Last Post: 04-25-2010, 06:27 AM
  3. Analog IO
    By Downbound in forum C++ Programming
    Replies: 5
    Last Post: 04-29-2004, 02:12 AM
  4. Analog sound over a digital modem?
    By Geolingo in forum Tech Board
    Replies: 2
    Last Post: 10-11-2003, 01:34 PM
  5. Analog to Digital (conversion time)
    By Johnny5 in forum Tech Board
    Replies: 6
    Last Post: 12-13-2002, 02:10 PM

Tags for this Thread