Copy of Andrew Reed's code from Google Classroom (revision from 2 weeks ago) with alterations made on 08-01-2021
Revision 22:99cdb20369be, committed 2021-02-23
- Comitter:
- 10782824
- Date:
- Tue Feb 23 11:00:50 2021 +0000
- Parent:
- 21:7b62bebdaef9
- Commit message:
- Variables redefined
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Feb 16 10:24:58 2021 +0000 +++ b/main.cpp Tue Feb 23 11:00:50 2021 +0000 @@ -2,15 +2,15 @@ * Copyright (c) 2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 */ - - /******************************************************************/ - /******************************************************************/ - /**** Environmental System Manager ****/ - /**** Boiler and Lighting Control ****/ - /**** on a Cypress PSoC 6 WiFi BT ****/ - /**** Prototyping Board Feb 2021 ****/ - /******************************************************************/ - /******************************************************************/ + +/******************************************************************/ +/******************************************************************/ +/**** Environmental System Manager ****/ +/**** Boiler and Lighting Control ****/ +/**** on a Cypress PSoC 6 WiFi BT ****/ +/**** Prototyping Board Feb 2021 ****/ +/******************************************************************/ +/******************************************************************/ #include "mbed.h" #include "platform/mbed_thread.h" @@ -28,11 +28,9 @@ // Global variables char buffer[80]; // buffer containing any data to be sent to VT100 terminal -Thread thread; // Thread to produce console +Thread thread; // Thread to produce console Thread thread2; // Thread to flash yellow LED Thread thread3; // Thread to take sensor readings -float currentHighTemp = 0; // highest temperature reached since startup -float currentLowTemp = 100; // lowest temperature reached since startup bool allowUpdate = true; // can the threshold be updated? @@ -55,41 +53,49 @@ /* Data Structures */ /* Create a structure type "DataSet" for data "myData" */ -struct DataSet { +struct DataSet1 { float highTempThresh = 25.0; // Boiler turns off level float lowTempThresh = 23.0; // Boiler turns on level float ambientTemp; // Current temperature float highLightThresh = 72.0; // Light turns off level float lowLightThresh = 45.0; // Light turns on level float ambientLight; // Current light level + } myData; + +/* Create a structure type "DataSet" for data "addedData" */ + +struct DataSet2 { + float currentHighTemp = 0; // highest temperature reached since startup + float currentLowTemp = 100; // lowest temperature reached since startup float potValue; // Potentiometer value - } myData; +} addedData; /* prototype of function */ /* Tell the compiler the following functions will be defined */ - void displayAt( int x, int y, char *buffer ); // For sending output values at specific positions to the terminal - void initialise(); // Clear screen, move cursor and turn off cursor - void readSensors(); // Read thermister and LDR - void setActuators(); //Compare read values to thresholds - void displayData(); // Send results to UART terminal - void consoleThread(); // checks if thresholds are to be modified - void yellowFlashThread(); // flashed yellow LED at 4Hz - void maxMin(); // Maximum and minimum temperatures are recorded +void displayAt( int x, int y, char *buffer ); // For sending output values at specific positions to the terminal +void initialise(); // Clear screen, move cursor and turn off cursor +void readSensors(); // Read thermister and LDR +void setActuators(); //Compare read values to thresholds +void displayData(); // Send results to UART terminal +void consoleThread(); // checks if thresholds are to be modified +void yellowFlashThread(); // flashed yellow LED at 4Hz +void maxMin(); // Maximum and minimum temperatures are recorded /* Yellow Flash Thread */ -/* Indication that system is still active +/* Indication that system is still active but both boiler and lighting indicators are below thresholds and are just off. If no lights are on the system has crashed */ - + void yellowFlashThread() { while(true) { if (boilerLed == false && lightingLed == false) { //check if both LED's are off - yellowLed = !yellowLed; // Flash yellow LED at ~4Hz - myData.potValue = ((pot.read()*1000)+100); // Read potentiometer to produce value 100-1100 - thread_sleep_for(myData.potValue); } // Adjust flashing rate dependent on pot reading + yellowLed = !yellowLed; // Flash yellow LED at ~4Hz + addedData.potValue = ((pot.read()*1000)+100); // Read potentiometer to produce value 100-1100 + thread_sleep_for(addedData.potValue); // Adjust flashing rate dependent on pot reading + } else yellowLed = 0; // When boiler or heating are on yellow LED is off } } @@ -102,52 +108,51 @@ float cumulativeVoltage = 0; //initialise cumulative reference voltage float meanRefVoltage; // Mean value of reference voltage /* variables to convert from voltage to ADC to degrees celcius */ - float refCurrent; + float refCurrent; float thermVoltage; float thermResistance; float logrT; - float stEqn; - + float stEqn; + /* Read ADC 10 times and average reading to minimise spikes */ for (int x=0; x<10; x++) { refVoltage = vTherm.read() * 2.4; // Range of ADC 0->2*Vref cumulativeVoltage = cumulativeVoltage + refVoltage; - thread_sleep_for (10); // 10ms between readings - } + thread_sleep_for (10); } // 10ms between readings meanRefVoltage = cumulativeVoltage/10; // Average reference voltage calc - + /* convert to degrees celsius */ refCurrent = meanRefVoltage / 10000.0; // 10k Reference Resistor thermVoltage = 3.3 - meanRefVoltage; // Assume supply voltage is 3.3v thermResistance = thermVoltage / refCurrent; logrT = (float32_t)log((float64_t)thermResistance); - + /* Calculate temperature from the resistance of thermistor using Steinhart-Hart Equation */ stEqn = (float32_t)((0.0009032679) + ((0.000248772) * logrT) + - ((2.041094E-07) * pow((float64)logrT, (float32)3))); - + ((2.041094E-07) * pow((float64)logrT, (float32)3))); + myData.ambientTemp = (float32_t)(((1.0 / stEqn) - 273.15) + 0.5); - - + + /* Read light levels from LDR potential divider */ float refLight; float cumulativeLight=0; - + /* Read light level 10 times and average to smooth spikes */ for (int x = 0; x<10; x++) { refLight = ( 1 - lightLevel.read()) * 100; // read analogue input from LDR, convert to % cumulativeLight += refLight; - thread_sleep_for(10); // + thread_sleep_for(10); // } myData.ambientLight = cumulativeLight/10; // Calculate mean value of light level thread_sleep_for(100); - } } +} /* console thread - to define display and threshold settings, - determine which keys have been pressed and to update the + determine which keys have been pressed and to update the relevant threshold value */ - + void consoleThread() { char inputStr[80]; // array to store input threshold values @@ -157,11 +162,11 @@ while(1) { if (serial_port.readable()) { allowUpdate = false; - + // switch to determine which threshold is active switch ( selection ) { case 0: // show that lowTemp threshold is selected - printf("\033[4;31H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.lowTempThresh); + printf("\033[4;31H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.lowTempThresh); break; case 1: // show that highTemp threshold is selected printf("\033[4;47H\033[1;30m\033[47m%2.1f\033[1;37m\033[40m\033[14;1H", myData.highTempThresh); @@ -175,12 +180,12 @@ } char inputChar; inputChar = serial_port.getc(); // read a keypress from serial port - + // switch to determine which instruction key is pressed switch (inputChar) { case 0x08: // has backspace been pressed? - if (index > 0) inputStr[--index] = NULL;; // delete the last character + if (index > 0) inputStr[--index] = NULL;; // delete the last character printf("\033[14;1HChange to: %s %c", inputStr, 0x08); // move cursor to 14,1 show space printf("\033[25h"); // make cursor visible break; @@ -200,7 +205,7 @@ 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.highLightThresh); break; case 3: - // show cursor moving from high light to low temp + // show cursor moving from high light to low temp 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.highLightThresh, myData.lowTempThresh); break; } @@ -212,7 +217,7 @@ // update the relevant threshold once enter is pressed switch (selection) { case 0: - myData.lowTempThresh = atof(inputStr); + myData.lowTempThresh = atof(inputStr); break; case 1: myData.highTempThresh = atof(inputStr); @@ -232,11 +237,11 @@ 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; @@ -256,9 +261,8 @@ thread.start(consoleThread ); // start checking if keys are pressed thread2.start(yellowFlashThread); //start flashing yellow LED thread3.start(readSensors); // read sensors -// currentLowTemp = currentHighTemp = myData.ambientTemp; - - + + /**************************************************************************** * * Main loop:- @@ -272,12 +276,12 @@ ***************************************************************************/ while (true) { - - led = !led; - setActuators(); - maxMin(); - displayData(); - thread_sleep_for(BLINKING_RATE_MS); + + led = !led; + setActuators(); + maxMin(); + displayData(); + thread_sleep_for(BLINKING_RATE_MS); } } @@ -291,21 +295,23 @@ if (myData.ambientLight < myData.lowLightThresh) lightingLed = true; // below lower light threshold } -void maxMin() +void maxMin() /* Compares existing max and min temperatures to current and updates them if the previous high has been exceeded or previous low is now colder */ { thread_sleep_for(200); // needs to pause to allow ambient temp to take a reading - if (myData.ambientTemp > currentHighTemp) { - currentHighTemp = myData.ambientTemp;} - if (myData.ambientTemp < currentLowTemp) { - currentLowTemp = myData.ambientTemp;} - - // set min and max to current temp if button pressed + if (myData.ambientTemp > addedData.currentHighTemp) { + addedData.currentHighTemp = myData.ambientTemp; + } + if (myData.ambientTemp < addedData.currentLowTemp) { + addedData.currentLowTemp = myData.ambientTemp; + } + + // set min and max to current temp if button pressed if (pushButton == 0) { - currentHighTemp = currentLowTemp = myData.ambientTemp; - } -} + addedData.currentHighTemp = addedData.currentLowTemp = myData.ambientTemp; + } +} void displayData() { @@ -332,18 +338,18 @@ displayAt(26, 4, buffer); sprintf(buffer, "The boiler is %s", boilerLed?"On ":"Off" ); // state whether boiler LED is on or off displayAt(1, 6, buffer); - + // Print the current highest and lowest temperatuers since startup - + printf("\033[1;36m"); - sprintf(buffer, "%3.1f°C ", currentLowTemp); //write lowest temp measured + sprintf(buffer, "%3.1f°C ", addedData.currentLowTemp); //write lowest temp measured displayAt(61, 4, buffer); - + printf("\033[1;35m"); - sprintf(buffer, "%3.1f°C ", currentHighTemp); //write highest temp measured + sprintf(buffer, "%3.1f°C ", addedData.currentHighTemp); //write highest temp measured displayAt(73, 4, buffer); - - // Colour code the current light values + + // Colour code the current light values if (myData.ambientLight > myData.highLightThresh) { lCol=41; //Red Text printf("\033[1;31m"); @@ -354,6 +360,7 @@ lCol=42; // Green Text printf("\033[1;32m"); } + sprintf( buffer, "Ambient Light is: %3.1f%c ", myData.ambientLight, '%' ); //write current light to serial buffer displayAt(1, 5, buffer); sprintf(buffer,"\033[%dm \033[40m", lCol); // simulate coloured LED @@ -367,7 +374,7 @@ displayAt(1, 7, buffer); } -void displayAt( int x, int y, char *buffer ) +void displayAt( int x, int y, char *buffer ) /* Function to place data to be displayed in correct location */ /* and send using UART to terminal */ { @@ -390,7 +397,7 @@ printf( "\033[0m" ); // Turn attributes off //Print at line 3, column 28 Low threshold in blue and High threshold in red printf("\033[3;28H\033[1;34mLow Threshold \033[1;31mHigh Threshold \033[1;36mMinimum Temp \033[1;35mMaximum Temp"); - + //print at line 1, column 16 in faint white 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");