Série: C++
c
239 lignes
· Mis à jour 2026-07-21
finalproject.c
C++/Mavis_Homework/FinalProject/finalproject.c
/*
program: finalproject.c
author: firstName lastName
date: July 30 2026
purpose: Home Security System simulation using the DeviceAlpha board
*/
#include <stdio.h>
#include "deviceAlpha.h"
/* system states */
#define STATE_DISARMED 0
#define STATE_ARMED 1
#define STATE_ALARM 2
/* user-defined function prototypes */
void waitForStartCommand(void);
void initializeSystem(void);
void waitForButtonRelease(int button);
void runArmingCountdown(void);
void activateAlarm(void);
void resetSystem(void);
int main(void)
{
int currentState = STATE_DISARMED; /* current state of the system */
int systemRunning = 1; /* 1 = keep running, 0 = exit */
/* step 1: validated start prompt (only 'Y' or 'y' is accepted) */
waitForStartCommand();
/* step 2: initialize the board and show the ambient temperature */
initializeSystem();
/* step 3: main state machine loop */
while (systemRunning)
{
switch (currentState)
{
case STATE_DISARMED:
if (GetPushButtonState(PB1) == PRESSED)
{
/* start the arming sequence */
waitForButtonRelease(PB1);
runArmingCountdown();
currentState = STATE_ARMED;
}
else if (GetPushButtonState(PB4) == PRESSED)
{
/* exit is only allowed while disarmed */
waitForButtonRelease(PB4);
printf("Exiting the Home Security System. Goodbye!\n");
systemRunning = 0;
}
break;
case STATE_ARMED:
if (GetPushButtonState(PB2) == PRESSED)
{
/* simulated motion detection -> alarm */
waitForButtonRelease(PB2);
printf("Motion detected! ALARM activated!\n");
activateAlarm();
currentState = STATE_ALARM;
}
else if (GetPushButtonState(PB3) == PRESSED)
{
/* disarm before any motion was detected */
waitForButtonRelease(PB3);
resetSystem();
currentState = STATE_DISARMED;
}
else if (GetPushButtonState(PB4) == PRESSED)
{
/* cannot exit while the system is armed */
waitForButtonRelease(PB4);
printf("System is ARMED! Please disarm the system (PB3) before exiting.\n");
}
break;
case STATE_ALARM:
if (GetPushButtonState(PB3) == PRESSED)
{
/* disarm stops the alarm and resets everything */
waitForButtonRelease(PB3);
resetSystem();
currentState = STATE_DISARMED;
}
else if (GetPushButtonState(PB4) == PRESSED)
{
/* cannot exit while the alarm is active */
waitForButtonRelease(PB4);
printf("ALARM is active! Please disarm the system (PB3) before exiting.\n");
}
break;
}
Wait(50); /* small pause between button polls (debounce) */
}
return 0;
}
/*
* waitForStartCommand
* Keeps prompting until the user enters 'Y' or 'y'.
* Any other input (wrong letter, number, empty line) is rejected.
*/
void waitForStartCommand(void)
{
char startCharacter = '\0';
int extraCharacter;
int validStart = 0;
while (!validStart)
{
printf("Enter 'Y' to start the Home Security System: ");
startCharacter = (char)getchar();
/* clear the rest of the input line */
if (startCharacter != '\n')
{
extraCharacter = getchar();
while (extraCharacter != '\n' && extraCharacter != EOF)
{
extraCharacter = getchar();
}
}
if (startCharacter == 'Y' || startCharacter == 'y')
{
validStart = 1;
}
else
{
printf("Invalid input! You must enter 'Y' or 'y' to start.\n");
}
}
}
/*
* initializeSystem
* Turns every component off, shows the current temperature on the
* 7-Segment display for a short time, then clears the display and
* reports that the system is in the idle (disarmed) state.
*/
void initializeSystem(void)
{
double currentTemperature;
SetLEDState(YELLOW_LED, OFF);
SetLEDState(GREEN_LED, OFF);
SetLEDState(RED_LED, OFF);
SetBuzzerState(BUZZER, OFF);
currentTemperature = GetTemperature(TEMPERATURE_SENSOR);
printf("Current temperature: %.2f degrees Celsius\n", currentTemperature);
DisplayDoubleValue(currentTemperature, 2);
Wait(2000); /* let the user observe the ambient temperature */
ClearSevenSegmentDisplay();
printf("System is DISARMED. PB1 = arm, PB4 = exit.\n");
}
/*
* waitForButtonRelease
* Busy-waits until the given push button is released, so one physical
* press is handled exactly once.
*/
void waitForButtonRelease(int button)
{
while (GetPushButtonState(button) == PRESSED)
{
; /* wait for the release */
}
}
/*
* runArmingCountdown
* Shows a 3 second countdown on the 7-Segment display while blinking
* the YELLOW led, then turns the GREEN led on (system armed).
*/
void runArmingCountdown(void)
{
int countdownSecond;
int blinkNumber;
printf("Arming the system...\n");
for (countdownSecond = 3; countdownSecond >= 1; countdownSecond--)
{
DisplayIntValue(countdownSecond);
/* two yellow blinks per second (4 x 250 ms = 1 second) */
for (blinkNumber = 0; blinkNumber < 2; blinkNumber++)
{
SetLEDState(YELLOW_LED, ON);
Wait(250);
SetLEDState(YELLOW_LED, OFF);
Wait(250);
}
}
DisplayIntValue(0);
SetLEDState(GREEN_LED, ON);
Wait(500);
ClearSevenSegmentDisplay();
printf("System is ARMED. PB2 = motion event, PB3 = disarm.\n");
}
/*
* activateAlarm
* Intrusion detected: RED led on and BUZZER on. The alarm stays active
* until the user disarms the system with PB3.
*/
void activateAlarm(void)
{
SetLEDState(GREEN_LED, OFF);
SetLEDState(RED_LED, ON);
SetBuzzerState(BUZZER, ON);
}
/*
* resetSystem
* Disarms the system: all LEDs off, buzzer silenced, display cleared.
* The system returns to the idle (disarmed) state.
*/
void resetSystem(void)
{
SetLEDState(YELLOW_LED, OFF);
SetLEDState(GREEN_LED, OFF);
SetLEDState(RED_LED, OFF);
SetBuzzerState(BUZZER, OFF);
ClearSevenSegmentDisplay();
printf("System DISARMED. PB1 = arm, PB4 = exit.\n");
}
Articles liés
C++
c
Mis à jour 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/deviceAlpha.h).
Lire l'article →
C++
cpp
Mis à jour 2026-07-21
finalproject.cpp
finalproject.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.cpp).
Lire l'article →
C++
c
Mis à jour 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/deviceAlpha.h).
Lire l'article →
C++
c
Mis à jour 2026-07-21
lab8.c
lab8.c — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.c).
Lire l'article →
C++
cpp
Mis à jour 2026-07-21
lab8.cpp
lab8.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.cpp).
Lire l'article →
C++
cpp
Mis à jour 2026-04-03
basic_output.cpp
basic_output.cpp — cpp source code from the C++ learning materials (C++/Part1_基礎入門/Ch01_認識CPP與開發環境/basic_output.cpp).
Lire l'article →