Source Code of the C program that
when debugged will run the system

Notes to this program:  This program is no where near complete, there are still situations that occur when they shouldn't.  Some of these situations arise because the program will sometimes set carinside equal to 2, which is a state that has no consequences.  However, we have not yet fixed this error as we are decided whether or not a state of carinside=2 is beneficial or not.  It has its benefits either way.  Also this main program is intended for visual purposes.  The use of printf statements is only so that you, the interested viewer, can try to understand what is going on, and so that we the programmers can see what is happening and when, so that we can iron out any bugs. ...If only Microsoft did this when programming Windows 98 (or any software for that matter)!  This source code now has quite a lot of comments, so that you can understand what is going on with less required thought.  As this program is no where near complete, its user- friendliness is rather lacking but you are quite free to trial the program by following this link here.

PROGRAM STARTS HERE:
/*
  A - SensorA is the motion sensor inside  the garage.
  B - SensorB is the motion sensor outside the garage.
  C is the sensor at the door (which will determine whether or not it is safe to close the door.
  The power for the chip on the car will run from the auxiliary power in the car.
  ...So that any movement of the door will not occur unless the car is running.
*/

#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>

int scancar(void);                                        //controls transmitter & receiver & decodes information
void delay(int);                                          //useless, clock-based delaying function
void opendoor();                                       //Opens the garage door and flags door as open
void closedoor();                                        //Closes the garage door and flags door as closed
void leavegar();                                          //Sequence for leaving the garage -Seen only if car is inside
void entergar();                                         //Sequence for entering the garage -Seen only if car is outside
void notmovin();                                       //Security functions when there is movement, where there is no car
void updatinp();                                         //Routine that checks the inputs until a change occurs

int A, B, C;                                               //input signals (refer to top of page)
int door=0;                                              //door is 1 if closed and 0 if open
int carinside, carhome;                             //carinside is 1 if car is in garage.  Carhome is 1 if detected by 'scanner'
int carcoming=0;                                       //carcoming is 1 if car is coming in and is determined by a series of events
int mcarleaving=0, carleaving=0, m5leave=0;  //maybe car leaving, car is leaving, car maybe leaving after 5 minutes/
int mcoming=0, maybe=0;        //maybe car coming in, 'maybe' may be used later to replace mcarleaving and mcoming (=0,1,2)
int count=1;

void main()
{
 A=B=C=0;                                                //change later
 printf("Note: when entering in values for A, B, and C, they must be entered");
 printf(" as 1s or 0s and separated by spaces. Ie to say that Sensor A ");
 printf("is on, Sensor B is off and the Door Sensor (C) is off, you would ");
 printf("key in: 1 0 0.\nThe following is the sequence that would happen ");
 printf("if the car was going out:\n1 0 0 \n1 0 1 or 0 0 1\n0 0 1 or 0 1 1 ");
 printf("or 1 1 1\nnoting that the car would have to be turned on (carhome=1)");
 printf("\n\nInitial state...Door is open\n");
 if (C==0)                                                //No object is in doorway
 {closedoor();                                         //Close the door as it will be installed in an open state.
  printf("because no object was in the way....Car is inside.\n");
  carinside=1;                                          //Initial value, In final program, user will have option to change this.
 }  //end if (C==0)
 updatinp();
 do
 {notmovin();
  if (carinside==0)                                   //If the car is already inside, there is no need to
   entergar();                                            // initiate the sequence of entering the garage if the
  else if (carinside==1)                            // car is leaving.  And vise verse.  These if statements
   leavegar();                                           // merely save potential time
  else if (carinside==2)                             //this is only temporary and its use will be
   {entergar(); leavegar();}                       //determined later.
  else
   updatinp();
  printf("\nA%d, B%d, C%d, door%d\n",A ,B ,C ,door);
  printf("carinside%d, carleaving%d, mcarleaving%d, carhome%d,\n"
         ,carinside,carleaving,mcarleaving, carhome);
 }while ((A==0 || A==1) && (B==0 || B==1) && (C==0 || C==1));
 printf("At total end of program\n");
}                                                            // end program
 /* The do-while statement will keep the program looping while there are valid
  inputs.  The program will, at a later stage be able to have the user
  stop the process.  This will be pass-word protected or something similar.
  There may also be 2 passwords: 1 for the user, and one for the technician.
  This will mean that only the non-vital parts of the system can be modified
  by the user, while the technician can have more liberal access.  */
Back to the top of the page

int scancar()
{int carhome;
//#define btrans(*(base address of PIT +0x1))  /*base transmitter*/
//#define brecev(*(base address of PIT +0x2)) /*base receiver*/
/* The above addresses are not yet correct and will be defined at the
   begining of this program, but for now are here as reminders. */

 flushall();
 printf("Is the car home? 1 for yes, 0 for no\n");
 scanf("%d",&carhome);
 /*
 btrans=sound(440);                                // For now the signal will be a 440Hz signal
 delay(500);                                             // of duration 500ms.
 btrans=nosound();
 if (brecev==sound(500))                       // Until we figure this out, the mask will be a
                                                              // frequency addition of 60Hz
 carhome=1;
 */
return (carhome);
}
Back to the top of the pageBack to the breakdown of the program

void opendoor()
{printf("Opening door...");
 door=0;                                                 //Door is remembered as open
}
Back to the top of the pageBack to the breakdown of the program

void closedoor()
{printf("Closing door...");
 door=1;                                                 //Door is remembered as closed
}
Back to the top of the pageBack to the breakdown of the program

void delay(int delayer)
{clock_t start, finish;
 start = clock();
 do
 {finish = clock();
  printf("The time is: %.0fseconds\r", (finish - start) / CLK_TCK);
 }while ((finish - start)<=delayer*1000/12);  //for real time in seconds, multiply by 1000
}
Back to the top of the pageBack to the breakdown of the program

void leavegar(void)
{                                                               /* Sequence for car leaving the garage */
 clock_t beg, end;
 beg=clock();
 if (A==1)                                                 //Movement inside garage, may be car...Have a look.
 {printf("Car is inside & movement inside, looking if car is turned on\n");
  carhome=scancar();                                //Send a message out, if message recieved, then decode it.
  if (carhome==1 && door==1)               // Car is detected and door is closed, open door
  {opendoor();
   beg=clock();
   printf("car maybe leaving\n");
   mcarleaving=1;
   updatinp();                                             //wait for inputs to change.
  }                                                              //end nested if (carhome==1 && door==1)
  else if (carhome!=1)
   updatinp();                                             //wait for inputs to change.
 }  // end if (A==1 && carinside)
 else
  updatinp();
 do
 {if (mcarleaving==1 && C==1)
  {mcarleaving=0;                                       //car is now no longer possible leaving,
   carleaving=1;                                           //it is definitely leaving.
   carinside=2;                                             //car is neither inside or outside, so for now=2, may use later.
   printf("Car is leaving and no longer maybe leaving\n");
   updatinp();                                               //Waiting for a change in inputs
  }                                                               //end if (mcarleaving==1 && C==1)
  end=clock();
 }while (mcarleaving==1 && ((end-beg)<=60000/12)); //will be set to 30sec not 5
 if (mcarleaving==1  && ((end-beg)>=60000/12))
 {closedoor();
  mcarleaving=0;
  printf("Car is not leaving\n");
  updatinp();
 }
 count=1;
 if (B==1 && carleaving==1)
 {                                                               //let car leave
  printf("Movement oustide and car is leaving, waiting for car to leave\n");
  carleaving=carinside=0;                            //car is now outside and left the garage
  updatinp();
  if (C==0)
   closedoor();
  updatinp();
 }                                                                // end if (B==1 && carleaving==1)
}                                                                //end void leavegar
Back to the top of the pageBack to the breakdown of the program

void entergar()
{if (B==1)                                                  //Movement outside and car not inside garage...Check if car is home.
 {printf("Car not in garage, movement outside, look for car\n");
  carhome=scancar();                                 //Send a message out, if message recieved, then decode it.
  if (carhome==1 && door==1)                //If decoded message was good, and door is closed, then open it.
  {opendoor();
   mcoming=1;
   printf("Car is home, door is opening\n");
  }                                                             //end nested if (carhome==1 && door==1)
  updatinp();
 }                                                             //end if (B==1 && carinside==0)
 else
  updatinp();
 if (mcoming==1 && C==1)                     //car wants to come back in
 {carcoming=1;
  mcoming=2;
  printf("Car is coming in\n");
  updatinp();
 }
 if (A==1 && carcoming==1 && C==0)
 {closedoor();
  printf("car has come in\n");
  carinside=1;
  mcoming=0;
  updatinp();
 }
}                                                                //end void entergar
Back to the top of the pageBack to the breakdown of the program

void updatinp()
{int Atemp, Btemp, Ctemp;                       //to remember input the inputs
 Atemp=A;Btemp=B;Ctemp=C;
 flushall();                                                  //clears all input buffers
 printf("enter in values for A, B, C \n");     //asks the user for the inputs
 scanf("%d %d %d",&A, &B, &C);          //get the inputs from the user
 if ((Atemp==A)&&(Btemp==B)&&(Ctemp==C)) //if the inputs don't change
 updatinp();                                               // Keep checking the inputs until they change.
}Back to the top of the pageBack to the breakdown of the program

void notmovin()
{
 if (A==1 && carinside==0)                      //Car isn't home & movement in garage
 {                                                              //state=0; //or sound alarm  (To start with...do nothing)
  printf("Someone is triggering the sensor inside...but car is not inside.\n");
  updatinp();
 }
 if (B==1 && carinside==1)                     //Movement outside but car is inside...Do nothing.
 {printf("Car is inside and movement outside.....Doing nothing\n");
 updatinp();
                                                                  //  state=0; //donothing state
 }                                                             //end if(B==1 && carinside==1)
}Back to the top of the pageBack to the breakdown of the program
 
 

Breakdown of the souce codeBack to the Index Page