Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:448ff9efcefa
- Parent:
- 0:cd8526d38aea
- Child:
- 2:2ff3b0d8c52e
diff -r cd8526d38aea -r 448ff9efcefa main.cpp
--- a/main.cpp Fri Nov 20 15:14:39 2020 +0000
+++ b/main.cpp Fri Dec 11 15:38:14 2020 +0000
@@ -5,31 +5,64 @@
#include "mbed.h"
#include "platform/mbed_thread.h"
-
+#include "stdio.h"
// Blinking rate in milliseconds
#define BLINKING_RATE_MS 500
#define SW2 P0_4
// #define alsOut P10_0
-
-
-int main()
-{
+//Global variables
+char buffer[80];
// Initialise the digital pin LED1 as an output
DigitalOut led(LED1);
DigitalIn pushButton(SW2, PullUp);
DigitalOut thermVcc(P10_3);
DigitalOut thermGnd(P10_0);
AnalogIn thermVal(P10_1);
-
- thermVcc = 1;
- thermGnd = 0;
+ AnalogIn lightLevel(P10_4);
+
+ /* Data Structures */
+
+ struct dataSet {
+ float highTempThresh;
+ float lowTempThresh;
+ float ambientTemp;
+ float highLightThresh;
+ float lowLightThresh;
+ float ambientLight;
+ } myData;
+
+/* prototype of function */
+void displayAt( int y, int x, char *buffer );
+void initialise();
+void readSensors();
+void displayData();
+
+int main()
+{
+ initialise(); // function to setup VT100 display
+
+/******************************************************************************************
+*
+* Main loop:=
+*
+* flash status led
+* read sensors
+* display data read from the sensors and threshold values
+* sleep for 0.5seconds
+*
+*******************************************************************************************/
+
while (true) {
- if (pushButton == 0)
- {
- led=!led;
- /*read thermistor Voltage*/
+ led=!led; // flashing status signal
+ readSensors();
+ displayData();
+ thread_sleep_for(BLINKING_RATE_MS);
+ }
+}
+void readSensors() {
+/*read thermistor Voltage*/
float refVoltage = thermVal.read() * 2.4;// Range of ADC 0->2*Vref
float refCurrent = refVoltage / 10000.0; //10k Reference Resistor
float therVoltage = 3.3 - refVoltage; // Assume supply voltage is 3.3v
@@ -39,11 +72,25 @@
/*Calculate temperature from the resistance of thermistor using Steinhart-Hart Equation*/
float stEqn = (float32_t) ((0.0009032679) + ((0.000248772) * logrT) + ((2.041094E-07) * pow((float64)logrT, (float32)3)));
- float temperatureC = (float32_t)(((1.0 / stEqn) - 273.15) + 0.5);
+ myData.ambientTemp = (float32_t)(((1.0 / stEqn) - 273.15) + 0.5);
+ myData.ambientLight = lightLevel.read() * 100;
+}
+void displayData() {
+ sprintf(buffer, "Temerpature is %2.1f C", myData.ambientTemp);
+ displayAt (1, 3, buffer);
-
- printf("Temerpature is %f C\r\n", temperatureC);
- }
- thread_sleep_for(BLINKING_RATE_MS);
+ sprintf( buffer, "Ambient Light is: %3.1f%c", myData.ambientLight, 0x25 );
+ displayAt(1,4, buffer);
+ }
+void displayAt( int y, int x, char *buffer ) {
+ printf("\033[%d;%dH%s", y, x, buffer);
+ fflush(stdout);
}
-}
+
+void initialise() {
+ printf("\033[2J\033[H"); // clear screen and move the cursor to 0, 0
+ printf("\033[?25l"); // Turn off visable cursor
+ fflush(stdout); // send the codes to the terminal
+ printf("Environmental Control System");
+ // printf( "|033[34m" );
+ }
\ No newline at end of file