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:
- 2:daceae26f5df
- Parent:
- 1:aef8ec8d23d8
- Child:
- 3:00878bd4ddb8
diff -r aef8ec8d23d8 -r daceae26f5df main.cpp
--- a/main.cpp Fri Nov 27 09:25:06 2020 +0000
+++ b/main.cpp Fri Dec 18 13:14:47 2020 +0000
@@ -6,66 +6,250 @@
#include "mbed.h"
#include "platform/mbed_thread.h"
#include "stdio.h"
-/* Reference resistor in series with the thermistor is of 10 KOhm */
-#define R_REFERENCE (float)(10000)
-/* Beta constant of this thermistor is 3380 Kelvin. See the thermistor
- (NCP18XH103F03RB) data sheet for more details. */
-#define B_CONSTANT (float)(3380)
-/* Resistance of the thermistor is 10K at 25 degrees C (from data sheet)
- Therefore R0 = 10000 Ohm, and T0 = 298.15 Kelvin, which gives
- R_INFINITY = R0 e^(-B_CONSTANT / T0) = 0.1192855 */
-#define R_INFINITY (float)(0.1192855)
-/* Zero Kelvin in degree C */
-#define ABSOLUTE_ZERO (float)(-273.15)
-//static DigitalIn thermVDD(P10_0); // if wing is detached and powered from 3.3v
-//static DigitalIn thermGND(P10_3); // don't need to control power to thermistor
-
// Blinking rate in milliseconds
#define BLINKING_RATE_MS 500
#define SW2 P0_4
+// Specify different pins to test printing on UART other than the console UART.
+#define TARGET_TX_PIN USBTX
+#define TARGET_RX_PIN USBRX
+// Create a BufferedSerial object to be used by the system I/O retarget code.
+static Serial serial_port(TARGET_TX_PIN, TARGET_RX_PIN, 115200);
+
+// Global variables
char buffer[80];
+Thread thread;
+bool allowUpdate = true;
+
+FileHandle *mbed::mbed_override_console(int fd)
+{
+ return &serial_port;
+}
+
+// Initialise the digital pin LED1 as an output
+DigitalOut led(LED1);
+DigitalIn pushButton(SW2, PullUp);
+AnalogIn vTherm(P10_1);
+AnalogIn lightLevel(P10_4);
+
+/* Data Structures */
+
+struct dataSet {
+ float highTempThresh = 26.0;
+ float lowTempThresh = 23.0;
+ float ambientTemp;
+ float highLightThresh = 80.0;
+ float lowLightThresh = 20.0;
+ float ambientLight;
+} myData;
+
/* prototype of function */
void displayAt( int x, int y, char *buffer );
+void initialise();
+void readSensors();
+void displayData();
+
+/* console thread */
+void consoleThread()
+{
+ char inputStr[80];
+ int index = 0; // character input index in inputStr
+
+ int selection = 0; //{tempLow==0, tempHigh==1, lightLow==2, lightHigh==3}
+ while(1) {
+ if (serial_port.readable()) {
+ allowUpdate = false;
+ switch ( selection ) {
+ case 0: //tempLow:
+ printf("\033[4;31H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.lowTempThresh);
+ break;
+ case 1: //tempHigh:
+ printf("\033[4;47H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.highTempThresh);
+ break;
+ case 2: //lightLow:
+ printf("\033[5;31H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.lowLightThresh);
+ break;
+ case 3: //lightHigh:
+ printf("\033[5;47H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.highLightThresh);
+ break;
+ }
+ char inputChar;
+ inputChar = serial_port.getc();
+ switch (inputChar) {
+ case 0x08: // backspace
+
+ if (index > 0) inputStr[--index] = NULL;;
+
+ printf("\033[14;1HChange to: %s %c", inputStr, 0x08);
+ printf("\033[25h");
+ break;
+ case 0x09:
+ switch (selection) {
+ case 0:
+ printf("\033[4;31H\033[0;37m\033[40m%2.1f\033[4;47H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.lowTempThresh, myData.highTempThresh);
+ break;
+ case 1:
+ printf("\033[4;47H\033[0;37m\033[40m%2.1f\033[5;31H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.highTempThresh, myData.lowLightThresh);
+ break;
+ case 2:
+ printf("\033[5;31H\033[0;37m\033[40m%2.1f\033[5;47H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.lowLightThresh, myData.lowLightThresh);
+ break;
+ case 3:
+ printf("\033[5;47H\033[0;37m\033[40m%2.1f\033[4;31H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.lowLightThresh, myData.lowTempThresh);
+ break;
+ }
+ selection = (selection + 1)%4; // keep it within bounds
+
+ break;
+ case 0x0d: // enter key, accept input
+ if (index > 0) {
+ switch (selection) {
+ case 0:
+ myData.lowTempThresh = atof(inputStr);
+ break;
+ case 1:
+ myData.highTempThresh = atof(inputStr);
+ break;
+ case 2:
+ myData.lowLightThresh = atof(inputStr);
+ break;
+ case 3:
+ myData.highLightThresh = atof(inputStr);
+ break;
+ }
+ }
+ inputStr[index] = NULL;
+ index = 0;
+
+ allowUpdate = true;
+ printf("\033[14;1H\033[2K");
+ printf("\033[?25l");
+ break;
+ case 0x20: // Space bar data coming...
+ printf("\033[14;1HChange to: ");
+ break;
+ default: // should be a digit or decimal point
+ inputStr[index++] = inputChar;
+ inputStr[index] = NULL;
+ printf("\033[14;1HChange to: %s", inputStr);
+ printf("\033[?25h");
+ break;
+ }
+ }
+ thread_sleep_for(5); // let other task do stuff for 5ms
+ }
+}
+
int main()
{
- // Initialise the digital pin LED1 as an output
- DigitalOut led(LED1);
- DigitalIn pushButton(SW2, PullUp);
- AnalogIn vTherm(P10_1);
- DigitalOut thermVcc(P10_3);
- DigitalOut thermGnd(P10_0);
- printf("\033[2J\033[H"); // clear screen and move the cursor to 0, 0
- printf("\033[?25l"); // Turn off visible cursor
- fflush(stdout); // send the codes to the terminal
- thermVcc = 1;
- thermGnd = 0;
+ initialise(); // function to setup VT100 display
+ thread.start( consoleThread );
+ /****************************************************************************
+ *
+ * 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 = vTherm.read() * 2.4; // Range of ADC 0->2*Vref
float refCurrent = refVoltage / 10000.0; // 10k Reference Resistor
float thermVoltage = 3.3 - refVoltage; // Assume supply voltage is 3.3v
- float thermResistance = thermVoltage / refCurrent;
+ float thermResistance = thermVoltage / refCurrent;
float logrT = (float32_t)log((float64_t)thermResistance);
-
+
/* 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);
+ float stEqn = (float32_t)((0.0009032679) + ((0.000248772) * logrT) +
+ ((2.041094E-07) * pow((float64)logrT, (float32)3)));
- sprintf(buffer, "Temperature is %f\r\n", temperatureC);
- displayAt(0, 3, buffer);
- }
- thread_sleep_for(BLINKING_RATE_MS);
+ myData.ambientTemp = (float32_t)(((1.0 / stEqn) - 273.15) + 0.5);
+ myData.ambientLight = ( 1 - lightLevel.read()) * 100;
+}
+void displayData()
+{
+ int tCol, lCol;
+ if (myData.ambientTemp > myData.highTempThresh) {
+ tCol=41; //Red Text
+ printf("\033[1;31m");
+ } else if (myData.ambientTemp < myData.lowTempThresh) {
+ tCol=44; // Blue text
+ printf("\033[1;34m");
+ } else {
+ tCol=42; // Green Text
+ printf("\033[1;32m");
+ }
+ sprintf(buffer, "Temperature is: %2.1fC ", myData.ambientTemp);
+ displayAt(1, 4, buffer);
+ printf("\033[0;37m");
+ sprintf(buffer, "%2.1fC ", myData.lowTempThresh);
+ displayAt(31, 4, buffer);
+ sprintf(buffer, "%2.1fC ", myData.highTempThresh);
+ displayAt(47, 4, buffer);
+ sprintf(buffer,"\033[%dm \033[40m", tCol);
+ displayAt(26, 4, buffer);
+
+ if (myData.ambientLight > myData.highLightThresh) {
+ lCol=41; //Red Text
+ printf("\033[1;31m");
+ } else if (myData.ambientLight < myData.lowLightThresh) {
+ lCol=44; // Blue text
+ printf("\033[1;34m");
+ } else {
+ lCol=42; // Green Text
+ printf("\033[1;32m");
+ }
+ sprintf( buffer, "Ambient Light is: %3.1f%c ", myData.ambientLight, '%' );
+ displayAt(1, 5, buffer);
+ sprintf(buffer,"\033[%dm \033[40m", lCol);
+ displayAt(26, 5, buffer);
+ printf("\033[0;37m");
+ sprintf(buffer, "%2.1f%c ", myData.lowLightThresh, 0x25);
+ displayAt(31, 5, buffer);
+ sprintf(buffer, "%2.1f%c ", myData.highLightThresh, 0x25);
+ displayAt(47, 5, buffer);
+}
+
+void displayAt( int x, int y, char *buffer )
+{
+ if (allowUpdate) {
+ printf( "\033[%d;%dH%s", y, x, buffer);
+ fflush(stdout);
}
}
-void displayAt( int x, int y, char *buffer ) {
- printf( "\033[%d;%dH%s", y, x, buffer);
- fflush(stdout);
- }
\ No newline at end of file
+void initialise()
+{
+ printf("\033[2J\033[H"); // clear screen and move the cursor to 0, 0
+ printf("\033[?25l"); // Turn off visible cursor[?25 lower case L
+
+
+ /* what is the character set?
+
+ for (int i = 32; i < 255; i ++) {
+ printf(" %c ", i);
+ }
+ thread_sleep_for(10000);
+ */
+ printf("Environmental Control System");
+ printf( "\033[1;37m" );
+ printf("\033[3;28H\033[1;34mLow Threshold \033[1;31mHigh Threshold");
+ printf("\033[2;37m\033[16;1H* Press \"Space key\" to set threshold values\r\n");
+ printf(" Use \"Tab key\" to select each threshold setting\r\n");
+ printf(" Hit \"Enter key\" to store new threshold value");
+ fflush(stdout); // send the codes to the terminal
+}
\ No newline at end of file