Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
main task 3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Omar2.Zayed@live.uwe.ac.uk
main task 3
Commits
eaac4532
Commit
eaac4532
authored
2 months ago
by
Omar2.Zayed@live.uwe.ac.uk
Browse files
Options
Downloads
Patches
Plain Diff
the final code for every question
parent
886dba55
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
final code for task 3
+86
-0
86 additions, 0 deletions
final code for task 3
with
86 additions
and
0 deletions
final code for task 3
0 → 100644
+
86
−
0
View file @
eaac4532
#include "mbed.h"
#include "arm_book_lib.h"
DigitalIn key2(D2);
DigitalIn key3(D3);
DigitalIn gasSensor(D4);
DigitalIn tempSensor(D5);
DigitalOut alarmLed(LED1);
UnbufferedSerial uart(USBTX, USBRX);
bool debounce(DigitalIn& button);
void sendStatus();
void checkWarnings();
bool alarmState = false;
bool previousGas = false;
bool previousTemp = false;
int main()
{
key2.mode(PullDown);
key3.mode(PullDown);
gasSensor.mode(PullDown);
tempSensor.mode(PullDown);
uart.baud(115200);
uart.format(8, SerialBase::None, 1);
while (true) {
if (debounce(key2)) {
const char* msg = gasSensor ? "\r\nGas detected.\r\n" : "\r\nGas not detected.\r\n";
uart.write(msg, strlen(msg));
}
if (debounce(key3)) {
const char* msg = tempSensor ? "\r\nHigh temperature detected.\r\n" : "\r\nTemperature normal.\r\n";
uart.write(msg, strlen(msg));
}
alarmState = gasSensor || tempSensor;
alarmLed = alarmState;
sendStatus();
checkWarnings();
ThisThread::sleep_for(1s);
}
}
bool debounce(DigitalIn& button)
{
if (button.read()) {
ThisThread::sleep_for(150ms);
return button.read();
}
return false;
}
void sendStatus()
{
const char* alarmMsg = alarmState ? "ON" : "OFF";
const char* gasMsg = gasSensor ? "DETECTED" : "CLEAR";
const char* tempMsg = tempSensor ? "HIGH" : "NORMAL";
char buffer[80];
sprintf(buffer, "\r\n[STATUS] Alarm: %s | Gas: %s | Temp: %s\r\n", alarmMsg, gasMsg, tempMsg);
uart.write(buffer, strlen(buffer));
}
void checkWarnings()
{
bool currentGas = gasSensor.read();
bool currentTemp = tempSensor.read();
if (currentGas && !previousGas) {
uart.write("\r\n[WARNING] Gas level exceeds safe limit!\r\n", 42);
}
if (currentTemp && !previousTemp) {
uart.write("\r\n[WARNING] Temperature exceeds safe limit!\r\n", 47);
}
previousGas = currentGas;
previousTemp = currentTemp;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment