├── .DS_Store ├── .gitignore ├── LICENSE ├── README.md ├── TCLab-sketch-vocareum └── tclab-sketch-vocareum.ino ├── TCLab-sketch-webusb-old └── TCLab-sketch-webusb-old.ino ├── TCLab-sketch-webusb ├── .DS_Store └── TCLab-sketch │ └── TCLab-sketch.ino └── TCLab-sketch └── TCLab-sketch.ino /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jckantor/TCLab-sketch/51268f0179ef87a9203a9af3c9900188c7062854/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Development specific 2 | .idea/ 3 | .DS_Store 4 | 5 | # Byte-compiled / optimized / DLL files 6 | __pycache__/ 7 | *.py[cod] 8 | *$py.class 9 | 10 | # C extensions 11 | *.so 12 | 13 | # Distribution / packaging 14 | .Python 15 | env/ 16 | build/ 17 | develop-eggs/ 18 | dist/ 19 | downloads/ 20 | eggs/ 21 | .eggs/ 22 | lib/ 23 | lib64/ 24 | parts/ 25 | sdist/ 26 | var/ 27 | wheels/ 28 | *.egg-info/ 29 | .installed.cfg 30 | *.egg 31 | 32 | # PyInstaller 33 | # Usually these files are written by a python script from a template 34 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 35 | *.manifest 36 | *.spec 37 | 38 | # Installer logs 39 | pip-log.txt 40 | pip-delete-this-directory.txt 41 | 42 | # Unit test / coverage reports 43 | htmlcov/ 44 | .tox/ 45 | .coverage 46 | .coverage.* 47 | .cache 48 | nosetests.xml 49 | coverage.xml 50 | *.cover 51 | .hypothesis/ 52 | 53 | # Translations 54 | *.mo 55 | *.pot 56 | 57 | # Django stuff: 58 | *.log 59 | local_settings.py 60 | 61 | # Flask stuff: 62 | instance/ 63 | .webassets-cache 64 | 65 | # Scrapy stuff: 66 | .scrapy 67 | 68 | # Sphinx documentation 69 | docs/_build/ 70 | 71 | # PyBuilder 72 | target/ 73 | 74 | # Jupyter Notebook 75 | .ipynb_checkpoints 76 | 77 | # pyenv 78 | .python-version 79 | 80 | # celery beat schedule file 81 | celerybeat-schedule 82 | 83 | # SageMath parsed files 84 | *.sage.py 85 | 86 | # dotenv 87 | .env 88 | 89 | # virtualenv 90 | .venv 91 | venv/ 92 | ENV/ 93 | 94 | # Spyder project settings 95 | .spyderproject 96 | .spyproject 97 | 98 | # Rope project settings 99 | .ropeproject 100 | 101 | # mkdocs documentation 102 | /site 103 | 104 | # mypy 105 | .mypy_cache/ 106 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TCLab-sketch 2 | ============ 3 | 4 | TCLab-sketch repository is a set of Arduino sketches which, when downloaded and installed 5 | on a compatible Arduino device, supports the 6 | [Temperature Control Lab](http://apmonitor.com/pdc/index.php/Main/ArduinoTemperatureControl). 7 | The sketch is used in conjunction with the compatible Python library 8 | [TCLab](https://github.com/jckantor/TCLab) for programmable control of the Temperature 9 | Control Lab with Python. There are three versions of the sketch: 10 | 11 | * **TCLab-sketch** Provides access to the TCLab via Python Serial library. 12 | * **TClab-sketch-webusb** Developmental version for WebUSB access. 13 | * **TCLab-sketch-vocareum** Developmental version for Vocareum using WebUSB access. 14 | 15 | 16 | Hardware setup 17 | -------------- 18 | 19 | 1. Plug the Temperature Control Laboratory shield in an Arduino UNO or Leonardo. Connect your computer 20 | to the Arduino via the USB connection. Plug the DC power adapter into the wall and attach to the 21 | power input to the Temperature Control Laboratory sheid. 22 | 23 | 2. Install Arduino Drivers if needed. For most users there will be no need. If you are using Windows 10 the Arduino board should connect with no additional drivers. Mac OS users may need to install a serial driver for old models of the Temperature Control that used Arduino Uno clones. For clones using the CH340G, CH34G or CH34X chipset, a suitable driver can be found 24 | [here](https://github.com/MPParsley/ch340g-ch34g-ch34x-mac-os-x-driver>) 25 | or 26 | [here](https://github.com/adrianmihalko/ch340g-ch34g-ch34x-mac-os-x-driver) 27 | 28 | 3. Install Arduino Firmware 29 | 30 | * Download and install the [Arduino IDE](https://www.arduino.cc/en/Main/Software) application. 31 | * Download the file TCLab-sketch.ino located in this repository in the folder with the same name. 32 | * Open the Arduino IDE application. From the Tools menu, select the Arduino board type and 33 | verify the port connection. 34 | * Compile and upload TCLab-sketch.ino 35 | 36 | 4. Test 37 | 38 | Use the serial monitor (located under the tools menu of the Arduino IDE) to confirm operation. 39 | Select serial monitor, set baud rate to 9600 and line endings to 'newline'. If the firmware is 40 | operating correctly, entering the command 41 | 42 | LED 100 43 | 44 | will cause the LED to flash at 100% power for 10 seconds. 45 | -------------------------------------------------------------------------------- /TCLab-sketch-vocareum/tclab-sketch-vocareum.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | TCLab Temperature Control Lab Firmware 5 | Jeffrey Kantor 6 | October, 2017 7 | 8 | This firmware is loaded into the Temperature Control Laboratory Arduino to 9 | provide a high level interface to the Temperature Control Lab. The firmware 10 | scans the serial port looking for case-insensitive commands: 11 | 12 | A software restart 13 | Q1 int set Heater 1, range 0 to 255 subject to limit 14 | Q2 int set Heater 2, range 0 to 255 subject to limit 15 | T1 get Temperature T1, returns deg C as string 16 | T2 get Temperature T2, returns dec C as string 17 | V get firmware version string 18 | X stop, enter sleep mode 19 | 20 | Limits on the heater can be configured with the constants below. 21 | 22 | Status is indicated by LED1 on the Temperature Control Lab. The status 23 | conditions are: 24 | 25 | LED1 LED1 26 | Brightness State 27 | ---------- ----- 28 | dim steady Normal operation, heaters off 29 | bright steady Normal operation, heaters on 30 | dim blinking High temperature alarm on, heaters off 31 | bright blinking High temperature alarm on, heaters on 32 | dim slow blink Sleep mode, needs hardware or software reset 33 | 34 | The Temperature Control Lab enters sleep mode if it receives no host commands 35 | during a timeout period (configure below), receives an "X" command, or receives 36 | an unrecognized command from the host. Heaters are shutdown in sleep mode, 37 | though temperature can continue to be monitored, and high temperature alarm is 38 | still active on LED1. 39 | 40 | Sleep mode is exited upon either a hardware reset with the button on the Arduino 41 | board, or software reset with the "A" command. 42 | 43 | The constants can be used to configure the firmware. 44 | */ 45 | 46 | #include 47 | WebUSB WebUSBSerial(1, "labs.vocareum.com"); 48 | #define wSerial WebUSBSerial 49 | 50 | // constants 51 | const String vers = "0.03"; // version of this firmware 52 | const int baud = 9600; // serial baud rate 53 | const char sp = ' '; // command separator 54 | const char nl = '\n'; // command terminator 55 | const int timeout = 300; // device timeout in seconds 56 | 57 | // pin numbers corresponding to signals on the TC Lab Shield 58 | const int pinT1 = 0; // T1 59 | const int pinT2 = 2; // T2 60 | const int pinQ1 = 3; // Q1 61 | const int pinQ2 = 5; // Q2 62 | const int pinLED = 9; // LED 63 | const int pinLED1 = 9; // LED1 64 | 65 | // high limits expressed (units of pin values) 66 | const int limQ1 = 150; // Q1 limit 67 | const int limQ2 = 150; // Q2 limit 68 | const int limT1 = 310; // T1 high alarm (50 deg C) 69 | const int limT2 = 310; // T2 high alarm (50 deg C) 70 | 71 | // LED1 levels 72 | const int hiLED = 50; // hi LED 73 | const int loLED = hiLED/16; // lo LED 74 | 75 | // global variables 76 | char Buffer[64]; // buffer for parsing serial input 77 | int buffer_index = 0; // index for Buffer 78 | String cmd; // command 79 | int pv; // command pin value 80 | float level; // LED level (0-100%) 81 | int iwrite = 0; // integer value for writing 82 | int ledStatus; // 0: sleep mode 83 | // 1: loLED 84 | // 2: hiLED 85 | // 3: loLED blink 86 | // 4: hiLED blink 87 | int brdStatus = 1; // board status 0:sleep, 1:normal 88 | int Q1 = 0; // last value written to Q1 pin 89 | int Q2 = 0; // last value written to Q2 poin 90 | int alarmStatus; // hi temperature alarm status 91 | long tlast; // millis when last host command 92 | boolean webusb = true; // flag to select interface: local or webUSB 93 | boolean newData = false; // flag when data is present in serial port 94 | 95 | 96 | void selectSerial() { // Check and select the active interface 97 | if (Serial) webusb = 0; 98 | else if(wSerial) webusb = 1; 99 | } 100 | 101 | void readCommand() { 102 | if (!webusb) { 103 | while (Serial && (Serial.available() > 0) && (newData == false)) { 104 | int byte = Serial.read(); 105 | if ( (byte != '\r') && (byte != '\n') && (buffer_index < 64)) { 106 | Buffer[buffer_index] = byte; 107 | buffer_index++; 108 | } 109 | else { 110 | newData = true; 111 | } 112 | } 113 | } 114 | else { 115 | while (wSerial && (wSerial.available() > 0) && (newData == false)) { 116 | int byte = wSerial.read(); 117 | if ( (byte != '\r') && (byte != '\n') && (buffer_index < 64)) { 118 | Buffer[buffer_index] = byte; 119 | buffer_index++; 120 | } 121 | else { 122 | newData = true; 123 | } 124 | } 125 | } 126 | } 127 | 128 | void parseCommand(void) { 129 | 130 | if (newData) { 131 | String read_ = String(Buffer); 132 | 133 | // separate command from associated data 134 | int idx = read_.indexOf(sp); 135 | cmd = read_.substring(0, idx); 136 | cmd.trim(); 137 | cmd.toUpperCase(); 138 | 139 | // extract data. toInt() returns 0 on error 140 | String data = read_.substring(idx + 1); 141 | data.trim(); 142 | pv = data.toFloat(); 143 | 144 | // reset parameter for next command 145 | memset(Buffer, 0, sizeof(Buffer)); 146 | buffer_index = 0; 147 | newData = false; 148 | } 149 | } 150 | 151 | // set Heater 1 to pv with hard limits imposed. 152 | void setHeater1(int pv) { 153 | if (brdStatus > 0) { 154 | Q1 = max(0, min(limQ1, pv)); 155 | analogWrite(pinQ1, Q1); 156 | } 157 | } 158 | 159 | // set Heater 2 to pv with hard limits imposed. 160 | void setHeater2(int pv) { 161 | if (brdStatus > 0) { 162 | Q2 = max(0, min(limQ2, pv)); 163 | analogWrite(pinQ2, Q2); 164 | } 165 | } 166 | 167 | void checkTimeout(void) { 168 | if (cmd.length() > 0) { 169 | tlast = millis(); 170 | } else { 171 | if ((long) (millis() - tlast) > (long) 1000*timeout) { 172 | setHeater1(0); 173 | setHeater2(0); 174 | brdStatus = 0; 175 | } 176 | } 177 | } 178 | 179 | void dispatchCommand(void) { 180 | if (cmd == "A") { 181 | brdStatus = 1; 182 | setHeater1(0); 183 | setHeater2(0); 184 | if(webusb) wSerial.println("Start"); 185 | else Serial.println("Start"); 186 | } 187 | else if (cmd == "Q1") { 188 | setHeater1(pv); 189 | if(webusb) wSerial.println(Q1); 190 | else Serial.println(Q1); 191 | } 192 | else if (cmd == "Q2") { 193 | setHeater2(pv); 194 | if(webusb) wSerial.println(Q2); 195 | else Serial.println(Q2); 196 | } 197 | else if (cmd == "T1") { 198 | float mV = (float) analogRead(pinT1) * (3300.0/1024.0); 199 | float degC = (mV - 500.0)/10.0; 200 | if(webusb) wSerial.println(degC); 201 | else Serial.println(degC); 202 | } 203 | else if (cmd == "T2") { 204 | float mV = (float) analogRead(pinT2) * (3300.0/1024.0); 205 | float degC = (mV - 500.0)/10.0; 206 | if(webusb) wSerial.println(degC); 207 | else Serial.println(degC); 208 | } 209 | else if (cmd == "V") { 210 | if(webusb) wSerial.println("TClab Firmware Version " + vers); 211 | else Serial.println("TClab Firmware Version " + vers); 212 | } 213 | else if (cmd == "LED") { 214 | level = max(0.0, min(100.0, pv)); 215 | iwrite = int(level * 0.5); 216 | iwrite = max(0, min(50, iwrite)); 217 | analogWrite(pinLED, iwrite); 218 | if(webusb) wSerial.println(level); 219 | else Serial.println(level); 220 | } 221 | else if ((cmd == "X") or (cmd.length() > 0)) { 222 | setHeater1(0); 223 | setHeater2(0); 224 | brdStatus = 0; 225 | if(webusb) wSerial.println("Stop"); 226 | else Serial.println("Stop"); 227 | } 228 | // Clear command to keep entering the loop over and over 229 | cmd = ""; 230 | wSerial.flush(); 231 | Serial.flush(); 232 | } 233 | 234 | void checkAlarm(void) { 235 | if ((analogRead(pinT1) > limT1) or (analogRead(pinT2) > limT2)) { 236 | alarmStatus = 1; 237 | } 238 | else { 239 | alarmStatus = 0; 240 | } 241 | } 242 | 243 | void updateStatus(void) { 244 | // determine led status 245 | ledStatus = brdStatus; 246 | if ((Q1 > 0) or (Q2 > 0)) { 247 | ledStatus = 2; 248 | } 249 | if (alarmStatus > 0) { 250 | ledStatus = 3; 251 | if ((Q1 > 0) or (Q2 > 0)) { 252 | ledStatus = 4; 253 | } 254 | } 255 | // update led depending on ledStatus 256 | if (ledStatus == 0) { // board is in sleep mode 257 | if ((millis() % 5000) > 1000) { 258 | analogWrite(pinLED1, 0); 259 | } else { 260 | analogWrite(pinLED1, loLED); 261 | } 262 | } 263 | else if (ledStatus == 1) { // normal operation, heaters off 264 | analogWrite(pinLED1, loLED); 265 | } 266 | else if (ledStatus == 2) { // normal operation, a heater on 267 | analogWrite(pinLED1, hiLED); 268 | } 269 | else if (ledStatus == 3) { // high temperature alarm, heater off 270 | if ((millis() % 2000) > 1000) { 271 | analogWrite(pinLED1, loLED); 272 | } else { 273 | analogWrite(pinLED1, 0); 274 | } 275 | } 276 | else if (ledStatus == 4) { // hight temperature alarm, a heater on 277 | if ((millis() % 2000) > 1000) { 278 | analogWrite(pinLED1, hiLED); 279 | } else { 280 | analogWrite(pinLED1, 0); 281 | } 282 | } 283 | 284 | } 285 | 286 | // arduino startup 287 | void setup() { 288 | analogReference(EXTERNAL); 289 | wSerial.begin(baud); 290 | wSerial.flush(); 291 | Serial.begin(baud); 292 | Serial.flush(); 293 | setHeater1(0); 294 | setHeater2(0); 295 | } 296 | 297 | // arduino main event loop 298 | void loop() { 299 | selectSerial(); // select webUSB or PySerial 300 | readCommand(); 301 | parseCommand(); 302 | // checkTimeout(); 303 | dispatchCommand(); 304 | checkAlarm(); 305 | //updateStatus(); 306 | } 307 | -------------------------------------------------------------------------------- /TCLab-sketch-webusb-old/TCLab-sketch-webusb-old.ino: -------------------------------------------------------------------------------- 1 | /* 2 | TCLab Temperature Control Lab Firmware 3 | Jeffrey Kantor 4 | January, 2018 5 | This firmware provides a high level interface to the Temperature Control Lab. The 6 | firmware scans the serial port for case-insensitive commands. Each command returns 7 | a result string. 8 | A software restart. Returns "Start". 9 | LED float set LED to float for 10 sec. range 0 to 100. Returns actual float 10 | P1 float set pwm limit on heater 1, range 0 to 255. Default 200. Returns P1. 11 | P2 float set pwm limit on heater 2, range 0 to 255. Default 100. Returns P2. 12 | Q1 float set Heater 1, range 0 to 100. Returns value of Q1. 13 | Q2 float set Heater 2, range 0 to 100. Returns value of Q2. 14 | R1 get value of Heater 1, range 0 to 100 15 | R2 get value of Heater 2, range 0 to 100 16 | SCAN get values T1 T2 Q1 Q1 in line delimited values 17 | T1 get Temperature T1. Returns value of T1 in °C. 18 | T2 get Temperature T2. Returns value of T2 in °C. 19 | VER get firmware version string 20 | X stop, enter sleep mode. Returns "Stop". 21 | Limits on the heater can be configured with the constants below. 22 | Status is indicated by LED1 on the Temperature Control Lab. Status conditions are: 23 | LED1 LED1 24 | Brightness State 25 | ---------- ----- 26 | dim steady Normal operation, heaters off 27 | bright steady Normal operation, heaters on 28 | dim blinking High temperature alarm on, heaters off 29 | bright blinking High temperature alarm on, heaters on 30 | The Temperature Control Lab shuts down the heaters if it receives no host commands 31 | during a timeout period (configure below), receives an "X" command, or receives 32 | an unrecognized command from the host. 33 | The constants can be used to configure the firmware. 34 | Version History 35 | 1.0.1 first version included in the tclab package 36 | 1.1.0 added R1 and R2 commands to read current heater values 37 | modified heater values to units of percent of full power 38 | added P1 and P2 commands to set heater power limits 39 | rewrote readCommand to avoid busy states 40 | simplified LED status model 41 | 1.2.0 added LED command 42 | 1.2.1 correctly reset heater values on close 43 | added version history 44 | 1.2.2 shorten version string for better display by TCLab 45 | 1.2.3 move baudrate to from 9600 to 115200 46 | 1.3.0 add SCAN function 47 | report board type in version string 48 | */ 49 | 50 | // determine board type 51 | #if ARDUINO >= 100 52 | #include "Arduino.h" 53 | #else 54 | #include "WProgram.h" 55 | #endif 56 | 57 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) 58 | String boardType = "Arduino Uno"; 59 | #define wSerial Serial 60 | #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) 61 | String boardType = "Arduino Leonardo/Micro"; 62 | #include 63 | WebUSB WebUSBSerial(1, "webusb.github.io/arduino/demos/console/"); 64 | #define wSerial WebUSBSerial 65 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 66 | String boardType = "Arduino Mega"; 67 | #define wSerial "" 68 | #else 69 | String boardType = "Unknown board"; 70 | #endif 71 | 72 | 73 | // constants 74 | const String vers = "1.3.0"; // version of this firmware 75 | const long baud = 115200; // serial baud rate 76 | const char sp = ' '; // command separator 77 | const char nl = '\n'; // command terminator 78 | 79 | // pin numbers corresponding to signals on the TC Lab Shield 80 | const int pinT1 = 0; // T1 81 | const int pinT2 = 2; // T2 82 | const int pinQ1 = 3; // Q1 83 | const int pinQ2 = 5; // Q2 84 | const int pinLED1 = 9; // LED1 85 | 86 | // temperature alarm limits 87 | const int limT1 = 50; // T1 high alarm (°C) 88 | const int limT2 = 50; // T2 high alarm (°C) 89 | 90 | // LED1 levels 91 | const int hiLED = 60; // hi LED 92 | const int loLED = hiLED/16; // lo LED 93 | 94 | // global variables 95 | char Buffer[64]; // buffer for parsing serial input 96 | int buffer_index = 0; // index for Buffer 97 | String cmd; // command 98 | int val; // command value 99 | int ledStatus; // 1: loLED 100 | // 2: hiLED 101 | // 3: loLED blink 102 | // 4: hiLED blink 103 | long ledTimeout = 0; // when to return LED to normal operation 104 | float LED = 100; // LED override brightness 105 | float P1 = 200; // heater 1 power limit in units of pwm. Range 0 to 255 106 | float P2 = 100; // heater 2 power limit in units in pwm, range 0 to 255 107 | int Q1 = 0; // last value written to heater 1 in units of percent 108 | int Q2 = 0; // last value written to heater 2 in units of percent 109 | int alarmStatus; // hi temperature alarm status 110 | boolean newData = false; // boolean flag indicating new command 111 | boolean webusb = false; // boolean flag to select local or WebUSB interface 112 | 113 | // Check and select the active interface 114 | void selectSerial() { 115 | if (Serial) 116 | webusb = false; 117 | else if(wSerial) 118 | webusb = true; 119 | } 120 | 121 | void readCommand() { 122 | if (!webusb) { 123 | while (Serial && (Serial.available() > 0) && (newData == false)) { 124 | int byte = Serial.read(); 125 | if ((byte != '\r') && (byte != nl) && (buffer_index < 64)) { 126 | Buffer[buffer_index] = byte; 127 | buffer_index++; 128 | } 129 | else { 130 | newData = true; 131 | } 132 | } 133 | } else { 134 | while (wSerial && (wSerial.available() > 0) && (newData == false)) { 135 | int byte = wSerial.read(); 136 | if ((byte != '\r') && (byte != nl) && (buffer_index < 64)) { 137 | Buffer[buffer_index] = byte; 138 | buffer_index++; 139 | } 140 | else { 141 | newData = true; 142 | } 143 | } 144 | } 145 | } 146 | 147 | // for debugging with the serial monitor in Arduino IDE 148 | void echoCommand() { 149 | if (newData) { 150 | Serial.write("Received Command: "); 151 | Serial.write(Buffer, buffer_index); 152 | Serial.write(nl); 153 | Serial.flush(); 154 | } 155 | } 156 | 157 | // return thermister temperature in °C 158 | inline float readTemperature(int pin) { 159 | return analogRead(pin) * 0.3223 - 50.0; 160 | } 161 | 162 | void parseCommand(void) { 163 | if (newData) { 164 | String read_ = String(Buffer); 165 | 166 | // separate command from associated data 167 | int idx = read_.indexOf(sp); 168 | cmd = read_.substring(0, idx); 169 | cmd.trim(); 170 | cmd.toUpperCase(); 171 | 172 | // extract data. toInt() returns 0 on error 173 | String data = read_.substring(idx + 1); 174 | data.trim(); 175 | val = data.toFloat(); 176 | 177 | // reset parameter for next command 178 | memset(Buffer, 0, sizeof(Buffer)); 179 | buffer_index = 0; 180 | newData = false; 181 | } 182 | } 183 | 184 | void sendResponse(String msg) { 185 | if (!webusb) 186 | Serial.println(msg); 187 | else 188 | wSerial.println(msg); 189 | } 190 | 191 | void dispatchCommand(void) { 192 | if (cmd == "A") { 193 | setHeater1(0); 194 | setHeater2(0); 195 | sendResponse("Start"); 196 | } 197 | else if (cmd == "LED") { 198 | ledTimeout = millis() + 10000; 199 | LED = max(0, min(100, val)); 200 | sendResponse(String(LED)); 201 | } 202 | else if (cmd == "P1") { 203 | P1 = max(0, min(255, val)); 204 | sendResponse(String(P1)); 205 | } 206 | else if (cmd == "P2") { 207 | P2 = max(0, min(255, val)); 208 | sendResponse(String(P2)); 209 | } 210 | else if (cmd == "Q1") { 211 | setHeater1(val); 212 | sendResponse(String(Q1)); 213 | } 214 | else if (cmd == "Q2") { 215 | setHeater2(val); 216 | sendResponse(String(Q2)); 217 | } 218 | else if (cmd == "R1") { 219 | sendResponse(String(Q1)); 220 | } 221 | else if (cmd == "R2") { 222 | sendResponse(String(Q2)); 223 | } 224 | else if (cmd == "SCAN") { 225 | sendResponse(String(readTemperature(pinT1))); 226 | sendResponse(String(readTemperature(pinT2))); 227 | sendResponse(String(Q1)); 228 | sendResponse(String(Q2)); 229 | } 230 | else if (cmd == "T1") { 231 | sendResponse(String(readTemperature(pinT1))); 232 | } 233 | else if (cmd == "T2") { 234 | sendResponse(String(readTemperature(pinT2))); 235 | } 236 | else if (cmd == "VER") { 237 | sendResponse("TCLab Firmware " + vers + " " + boardType); 238 | } 239 | else if ((cmd == "X") or (cmd.length() > 0)) { 240 | setHeater1(0); 241 | setHeater2(0); 242 | sendResponse(cmd); 243 | } 244 | if (boardType == "Arduino Leonardo/Micro") wSerial.flush(); 245 | else Serial.flush(); 246 | cmd = ""; 247 | } 248 | 249 | void checkAlarm(void) { 250 | if ((readTemperature(pinT1) > limT1) or (readTemperature(pinT2) > limT2)) { 251 | alarmStatus = 1; 252 | } 253 | else { 254 | alarmStatus = 0; 255 | } 256 | } 257 | 258 | void updateStatus(void) { 259 | // determine led status 260 | ledStatus = 1; 261 | if ((Q1 > 0) or (Q2 > 0)) { 262 | ledStatus = 2; 263 | } 264 | if (alarmStatus > 0) { 265 | ledStatus += 2; 266 | } 267 | // update led depending on ledStatus 268 | if (millis() < ledTimeout) { // override led operation 269 | analogWrite(pinLED1, LED); 270 | } 271 | else { 272 | switch (ledStatus) { 273 | case 1: // normal operation, heaters off 274 | analogWrite(pinLED1, loLED); 275 | break; 276 | case 2: // normal operation, heater on 277 | analogWrite(pinLED1, hiLED); 278 | break; 279 | case 3: // high temperature alarm, heater off 280 | if ((millis() % 2000) > 1000) { 281 | analogWrite(pinLED1, loLED); 282 | } else { 283 | analogWrite(pinLED1, loLED/4); 284 | } 285 | break; 286 | case 4: // high temperature alarm, heater on 287 | if ((millis() % 2000) > 1000) { 288 | analogWrite(pinLED1, hiLED); 289 | } else { 290 | analogWrite(pinLED1, loLED); 291 | } 292 | break; 293 | } 294 | } 295 | } 296 | 297 | // set Heater 1 298 | void setHeater1(float qval) { 299 | Q1 = max(0, min(qval, 100)); 300 | analogWrite(pinQ1, Q1*P1/100); 301 | } 302 | 303 | // set Heater 2 304 | void setHeater2(float qval) { 305 | Q2 = max(0, min(qval, 100)); 306 | analogWrite(pinQ2, Q2*P2/100); 307 | } 308 | 309 | // arduino startup 310 | void setup() { 311 | analogReference(EXTERNAL); 312 | Serial.begin(baud); 313 | Serial.flush(); 314 | if (boardType == "Arduino Leonardo/Micro") { 315 | wSerial.begin(baud); 316 | wSerial.flush(); 317 | } 318 | setHeater1(0); 319 | setHeater2(0); 320 | ledTimeout = millis() + 1000; 321 | } 322 | 323 | // arduino main event loop 324 | void loop() { 325 | selectSerial(); 326 | readCommand(); 327 | //echoCommand(); 328 | parseCommand(); 329 | dispatchCommand(); 330 | checkAlarm(); 331 | updateStatus(); 332 | } 333 | -------------------------------------------------------------------------------- /TCLab-sketch-webusb/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jckantor/TCLab-sketch/51268f0179ef87a9203a9af3c9900188c7062854/TCLab-sketch-webusb/.DS_Store -------------------------------------------------------------------------------- /TCLab-sketch-webusb/TCLab-sketch/TCLab-sketch.ino: -------------------------------------------------------------------------------- 1 | /* 2 | TCLab Temperature Control Lab Firmware 3 | Jeffrey Kantor 4 | February, 2019 5 | 6 | This firmware provides a high level interface to the Temperature Control Lab. The 7 | firmware scans the serial port for commands. Commands are case-insensitive. Any 8 | unrecognized command results in sleep model. Each command returns a result string. 9 | 10 | A software restart. Returns "Start". 11 | LED float set LED to float for 10 sec. range 0 to 100. Returns actual float 12 | P1 float set pwm limit on heater 1, range 0 to 255. Default 200. Returns P1. 13 | P2 float set pwm limit on heater 2, range 0 to 255. Default 100. Returns P2. 14 | Q1 float set Heater 1, range 0 to 100. Returns value of Q1. 15 | Q2 float set Heater 2, range 0 to 100. Returns value of Q2. 16 | R1 get value of Heater 1, range 0 to 100 17 | R2 get value of Heater 2, range 0 to 100 18 | SCAN get values T1 T2 Q1 Q1 in line delimited values 19 | T1 get Temperature T1. Returns value of T1 in °C. 20 | T2 get Temperature T2. Returns value of T2 in °C. 21 | VER get firmware version string 22 | X stop, enter sleep mode. Returns "Stop" 23 | 24 | Limits on the heater can be configured with the constants below. 25 | 26 | Status is indicated by LED1 on the Temperature Control Lab. Status conditions are: 27 | 28 | LED1 LED1 29 | Brightness State 30 | ---------- ----- 31 | dim steady Normal operation, heaters off 32 | bright steady Normal operation, heaters on 33 | dim blinking High temperature alarm on, heaters off 34 | bright blinking High temperature alarm on, heaters on 35 | 36 | The Temperature Control Lab shuts down the heaters if it receives no host commands 37 | during a timeout period (configure below), receives an "X" command, or receives 38 | an unrecognized command from the host. 39 | 40 | The constants can be used to configure the firmware. 41 | 42 | Version History 43 | 1.0.1 first version included in the tclab package 44 | 1.1.0 added R1 and R2 commands to read current heater values 45 | modified heater values to units of percent of full power 46 | added P1 and P2 commands to set heater power limits 47 | rewrote readCommand to avoid busy states 48 | simplified LED status model 49 | 1.2.0 added LED command 50 | 1.2.1 correctly reset heater values on close 51 | added version history 52 | 1.2.2 shorten version string for better display by TCLab 53 | 1.2.3 move baudrate to from 9600 to 115200 54 | 1.3.0 add SCAN function 55 | report board type in version string 56 | 1.4.0 changed Q1 and Q2 to float from int 57 | 1.4.1 fix missing Serial.flush() at end of command loop 58 | 1.4.2 fix bug with X command 59 | 1.4.3 required Arduino IDE Version >= 1.0.0 60 | 1.5.0 remove webusb 61 | */ 62 | 63 | #include "Arduino.h" 64 | 65 | // determine board type 66 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) 67 | String boardType = "Arduino Uno"; 68 | #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) 69 | String boardType = "Arduino Leonardo/Micro"; 70 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 71 | String boardType = "Arduino Mega"; 72 | #else 73 | String boardType = "Unknown board"; 74 | #endif 75 | 76 | // Enable debugging output 77 | const bool DEBUG = false; 78 | 79 | // constants 80 | const String vers = "1.4.3"; // version of this firmware 81 | const long baud = 115200; // serial baud rate 82 | const char sp = ' '; // command separator 83 | const char nl = '\n'; // command terminator 84 | 85 | // pin numbers corresponding to signals on the TC Lab Shield 86 | const int pinT1 = 0; // T1 87 | const int pinT2 = 2; // T2 88 | const int pinQ1 = 3; // Q1 89 | const int pinQ2 = 5; // Q2 90 | const int pinLED1 = 9; // LED1 91 | 92 | // temperature alarm limits 93 | const int limT1 = 50; // T1 high alarm (°C) 94 | const int limT2 = 50; // T2 high alarm (°C) 95 | 96 | // LED1 levels 97 | const int hiLED = 60; // hi LED 98 | const int loLED = hiLED/16; // lo LED 99 | 100 | // global variables 101 | char Buffer[64]; // buffer for parsing serial input 102 | int buffer_index = 0; // index for Buffer 103 | String cmd; // command 104 | float val; // command value 105 | int ledStatus; // 1: loLED 106 | // 2: hiLED 107 | // 3: loLED blink 108 | // 4: hiLED blink 109 | long ledTimeout = 0; // when to return LED to normal operation 110 | float LED = 100; // LED override brightness 111 | float P1 = 200; // heater 1 power limit in units of pwm. Range 0 to 255 112 | float P2 = 100; // heater 2 power limit in units in pwm, range 0 to 255 113 | float Q1 = 0; // last value written to heater 1 in units of percent 114 | float Q2 = 0; // last value written to heater 2 in units of percent 115 | int alarmStatus; // hi temperature alarm status 116 | boolean newData = false; // boolean flag indicating new command 117 | 118 | 119 | void readCommand() { 120 | while (Serial && (Serial.available() > 0) && (newData == false)) { 121 | int byte = Serial.read(); 122 | if ((byte != '\r') && (byte != nl) && (buffer_index < 64)) { 123 | Buffer[buffer_index] = byte; 124 | buffer_index++; 125 | } 126 | else { 127 | newData = true; 128 | } 129 | } 130 | } 131 | 132 | // for debugging with the serial monitor in Arduino IDE 133 | void echoCommand() { 134 | if (newData) { 135 | Serial.write("Received Command: "); 136 | Serial.write(Buffer, buffer_index); 137 | Serial.write(nl); 138 | Serial.flush(); 139 | } 140 | } 141 | 142 | // return thermister temperature in °C 143 | inline float readTemperature(int pin) { 144 | return analogRead(pin) * 0.3223 - 50.0; 145 | } 146 | 147 | void parseCommand(void) { 148 | if (newData) { 149 | String read_ = String(Buffer); 150 | 151 | // separate command from associated data 152 | int idx = read_.indexOf(sp); 153 | cmd = read_.substring(0, idx); 154 | cmd.trim(); 155 | cmd.toUpperCase(); 156 | 157 | // extract data. toFloat() returns 0 on error 158 | String data = read_.substring(idx + 1); 159 | data.trim(); 160 | val = data.toFloat(); 161 | 162 | // reset parameter for next command 163 | memset(Buffer, 0, sizeof(Buffer)); 164 | buffer_index = 0; 165 | newData = false; 166 | } 167 | } 168 | 169 | void sendResponse(String msg) { 170 | Serial.println(msg); 171 | } 172 | 173 | void dispatchCommand(void) { 174 | if (cmd == "A") { 175 | setHeater1(0); 176 | setHeater2(0); 177 | sendResponse("Start"); 178 | } 179 | else if (cmd == "LED") { 180 | ledTimeout = millis() + 10000; 181 | LED = max(0, min(100, val)); 182 | sendResponse(String(LED)); 183 | } 184 | else if (cmd == "P1") { 185 | P1 = max(0, min(255, val)); 186 | sendResponse(String(P1)); 187 | } 188 | else if (cmd == "P2") { 189 | P2 = max(0, min(255, val)); 190 | sendResponse(String(P2)); 191 | } 192 | else if (cmd == "Q1") { 193 | setHeater1(val); 194 | sendResponse(String(Q1)); 195 | } 196 | else if (cmd == "Q2") { 197 | setHeater2(val); 198 | sendResponse(String(Q2)); 199 | } 200 | else if (cmd == "R1") { 201 | sendResponse(String(Q1)); 202 | } 203 | else if (cmd == "R2") { 204 | sendResponse(String(Q2)); 205 | } 206 | else if (cmd == "SCAN") { 207 | sendResponse(String(readTemperature(pinT1))); 208 | sendResponse(String(readTemperature(pinT2))); 209 | sendResponse(String(Q1)); 210 | sendResponse(String(Q2)); 211 | } 212 | else if (cmd == "T1") { 213 | sendResponse(String(readTemperature(pinT1))); 214 | } 215 | else if (cmd == "T2") { 216 | sendResponse(String(readTemperature(pinT2))); 217 | } 218 | else if (cmd == "VER") { 219 | sendResponse("TCLab Firmware " + vers + " " + boardType); 220 | } 221 | else if (cmd == "X") { 222 | setHeater1(0); 223 | setHeater2(0); 224 | sendResponse("Stop"); 225 | } 226 | else if (cmd.length() > 0) { 227 | setHeater1(0); 228 | setHeater2(0); 229 | sendResponse(cmd); 230 | } 231 | Serial.flush(); 232 | cmd = ""; 233 | } 234 | 235 | void checkAlarm(void) { 236 | if ((readTemperature(pinT1) > limT1) or (readTemperature(pinT2) > limT2)) { 237 | alarmStatus = 1; 238 | } 239 | else { 240 | alarmStatus = 0; 241 | } 242 | } 243 | 244 | void updateStatus(void) { 245 | // determine led status 246 | ledStatus = 1; 247 | if ((Q1 > 0) or (Q2 > 0)) { 248 | ledStatus = 2; 249 | } 250 | if (alarmStatus > 0) { 251 | ledStatus += 2; 252 | } 253 | // update led depending on ledStatus 254 | if (millis() < ledTimeout) { // override led operation 255 | analogWrite(pinLED1, LED); 256 | } 257 | else { 258 | switch (ledStatus) { 259 | case 1: // normal operation, heaters off 260 | analogWrite(pinLED1, loLED); 261 | break; 262 | case 2: // normal operation, heater on 263 | analogWrite(pinLED1, hiLED); 264 | break; 265 | case 3: // high temperature alarm, heater off 266 | if ((millis() % 2000) > 1000) { 267 | analogWrite(pinLED1, loLED); 268 | } else { 269 | analogWrite(pinLED1, loLED/4); 270 | } 271 | break; 272 | case 4: // high temperature alarm, heater on 273 | if ((millis() % 2000) > 1000) { 274 | analogWrite(pinLED1, hiLED); 275 | } else { 276 | analogWrite(pinLED1, loLED); 277 | } 278 | break; 279 | } 280 | } 281 | } 282 | 283 | // set Heater 1 284 | void setHeater1(float qval) { 285 | Q1 = max(0., min(qval, 100.)); 286 | analogWrite(pinQ1, (Q1*P1)/100); 287 | } 288 | 289 | // set Heater 2 290 | void setHeater2(float qval) { 291 | Q2 = max(0., min(qval, 100.)); 292 | analogWrite(pinQ2, (Q2*P2)/100); 293 | } 294 | 295 | // arduino startup 296 | void setup() { 297 | analogReference(EXTERNAL); 298 | while (!Serial) { 299 | ; // wait for serial port to connect. 300 | } 301 | Serial.begin(baud); 302 | Serial.flush(); 303 | setHeater1(0); 304 | setHeater2(0); 305 | ledTimeout = millis() + 1000; 306 | } 307 | 308 | // arduino main event loop 309 | void loop() { 310 | readCommand(); 311 | if (DEBUG) echoCommand(); 312 | parseCommand(); 313 | dispatchCommand(); 314 | checkAlarm(); 315 | updateStatus(); 316 | } 317 | -------------------------------------------------------------------------------- /TCLab-sketch/TCLab-sketch.ino: -------------------------------------------------------------------------------- 1 | /* 2 | TCLab Temperature Control Lab Firmware 3 | Jeffrey Kantor, Bill Tubbs, John Hedengren, Shawn Summey 4 | February 2021 5 | 6 | This firmware provides a high level interface to the Temperature Control Lab. The 7 | firmware scans the serial port for commands. Commands are case-insensitive. Any 8 | unrecognized command results in sleep model. Each command returns a result string. 9 | 10 | A software restart. Returns "Start". 11 | LED float set LED to float for 10 sec. range 0 to 100. Returns actual float 12 | P1 float set pwm limit on heater 1, range 0 to 255. Default 200. Returns P1. 13 | P2 float set pwm limit on heater 2, range 0 to 255. Default 100. Returns P2. 14 | Q1 float set Heater 1, range 0 to 100. Returns value of Q1. 15 | Q2 float set Heater 2, range 0 to 100. Returns value of Q2. 16 | Q1B float set Heater 1, range 0 to 100. Returns value of Q1 as a 32-bit float. 17 | Q2B float set Heater 2, range 0 to 100. Returns value of Q2 as a 32-bit float. 18 | R1 get value of Heater 1, range 0 to 100 19 | R2 get value of Heater 2, range 0 to 100 20 | SCAN get values T1 T2 Q1 Q1 in line delimited values 21 | T1 get Temperature T1. Returns value of T1 in °C. 22 | T2 get Temperature T2. Returns value of T2 in °C. 23 | T1B get Temperature T1. Returns value of T1 in °C as a 32-bit float. 24 | T2B get Temperature T2. Returns value of T2 in °C as a 32-bit float. 25 | VER get firmware version string 26 | X stop, enter sleep mode. Returns "Stop" 27 | 28 | Limits on the heater power can be configured with the constants below. 29 | 30 | Status is indicated by LED1 on the Temperature Control Lab. Status conditions are: 31 | 32 | LED1 LED1 33 | Brightness State 34 | ---------- ----- 35 | dim steady Normal operation, heaters off 36 | bright steady Normal operation, heaters on 37 | dim blinking High temperature alarm on, heaters off 38 | bright blinking High temperature alarm on, heaters on 39 | 40 | The Temperature Control Lab shuts down the heaters if it receives no host commands 41 | during a timeout period (configure below), receives an "X" command, or receives 42 | an unrecognized command from the host. 43 | 44 | Constants are used to configure the firmware. 45 | 46 | Changelog ordered by Semantic Version 47 | 48 | 1.0.1 first version included in the tclab package 49 | 1.1.0 added R1 and R2 commands to read current heater values 50 | changed heater values to units of percent of full power 51 | added P1 and P2 commands to set heater power limits 52 | changed readCommand to avoid busy states 53 | changed simplified LED status model 54 | 1.2.0 added LED command 55 | 1.2.1 fixed reset heater values on close 56 | added version history 57 | 1.2.2 changed version string for better display by TCLab 58 | 1.2.3 changed baudrate to from 9600 to 115200 59 | 1.3.0 added SCAN function 60 | added board type in version string 61 | 1.4.0 changed Q1 and Q2 to float from int 62 | 1.4.1 fixed missing Serial.flush() at end of command loop 63 | 1.4.2 fixed bug with X command 64 | 1.4.3 deprecated use of Arduino IDE Version < 1.0.0 65 | 1.5.0 removed webusb 66 | 1.6.0 changed temperature to average 10 measurements to reduce noise 67 | 2.0.0 added binary communications. 68 | added T1B and T2B commands return 32-bit float 69 | added Q1B and Q2B commands return 32-bit float confirmation of heater setting 70 | added calculation to use 1.75 AREF to match TMP36 voltage range 71 | 2.0.1 added updates to Notre Dame and BYU versions of this firmware 72 | changed version history to standard change log practices 73 | 74 | */ 75 | 76 | #include "Arduino.h" 77 | 78 | // determine board type 79 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) 80 | String boardType = "Arduino Uno"; 81 | #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) 82 | String boardType = "Arduino Leonardo/Micro"; 83 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 84 | String boardType = "Arduino Mega"; 85 | #else 86 | String boardType = "Unknown board"; 87 | #endif 88 | 89 | // Enable debugging output 90 | const bool DEBUG = false; 91 | 92 | // constants 93 | const String vers = "2.0.1"; // version of this firmware 94 | const long baud = 115200; // serial baud rate 95 | const char sp = ' '; // command separator 96 | const char nl = '\n'; // command terminator 97 | 98 | // pin numbers corresponding to signals on the TC Lab Shield 99 | const int pinT1 = 0; // T1 100 | const int pinT2 = 2; // T2 101 | const int pinQ1 = 3; // Q1 102 | const int pinQ2 = 5; // Q2 103 | const int pinLED1 = 9; // LED1 104 | 105 | // temperature alarm limits 106 | const int limT1 = 50; // T1 high alarm (°C) 107 | const int limT2 = 50; // T2 high alarm (°C) 108 | 109 | // LED1 levels 110 | const int hiLED = 60; // hi LED 111 | const int loLED = hiLED/16; // lo LED 112 | 113 | // global variables 114 | char Buffer[64]; // buffer for parsing serial input 115 | int buffer_index = 0; // index for Buffer 116 | String cmd; // command 117 | float val; // command value 118 | int ledStatus; // 1: loLED 119 | // 2: hiLED 120 | // 3: loLED blink 121 | // 4: hiLED blink 122 | long ledTimeout = 0; // when to return LED to normal operation 123 | float LED = 100; // LED override brightness 124 | float P1 = 200; // heater 1 power limit in units of pwm. Range 0 to 255 125 | float P2 = 100; // heater 2 power limit in units in pwm, range 0 to 255 126 | float Q1 = 0; // last value written to heater 1 in units of percent 127 | float Q2 = 0; // last value written to heater 2 in units of percent 128 | int alarmStatus; // hi temperature alarm status 129 | boolean newData = false; // boolean flag indicating new command 130 | int n = 10; // number of samples for each temperature measurement 131 | 132 | 133 | void readCommand() { 134 | while (Serial && (Serial.available() > 0) && (newData == false)) { 135 | int byte = Serial.read(); 136 | if ((byte != '\r') && (byte != nl) && (buffer_index < 64)) { 137 | Buffer[buffer_index] = byte; 138 | buffer_index++; 139 | } 140 | else { 141 | newData = true; 142 | } 143 | } 144 | } 145 | 146 | // for debugging with the serial monitor in Arduino IDE 147 | void echoCommand() { 148 | if (newData) { 149 | Serial.write("Received Command: "); 150 | Serial.write(Buffer, buffer_index); 151 | Serial.write(nl); 152 | Serial.flush(); 153 | } 154 | } 155 | 156 | // return average of n reads of thermister temperature in °C 157 | inline float readTemperature(int pin) { 158 | float degC = 0.0; 159 | for (int i = 0; i < n; i++) { 160 | degC += analogRead(pin) * 0.322265625 - 50.0; // use for 3.3v AREF 161 | //degC += analogRead(pin) * 0.170898438 - 50.0; // use for 1.75v AREF 162 | } 163 | return degC / float(n); 164 | } 165 | 166 | void parseCommand(void) { 167 | if (newData) { 168 | String read_ = String(Buffer); 169 | 170 | // separate command from associated data 171 | int idx = read_.indexOf(sp); 172 | cmd = read_.substring(0, idx); 173 | cmd.trim(); 174 | cmd.toUpperCase(); 175 | 176 | // extract data. toFloat() returns 0 on error 177 | String data = read_.substring(idx + 1); 178 | data.trim(); 179 | val = data.toFloat(); 180 | 181 | // reset parameter for next command 182 | memset(Buffer, 0, sizeof(Buffer)); 183 | buffer_index = 0; 184 | newData = false; 185 | } 186 | } 187 | 188 | void sendResponse(String msg) { 189 | Serial.println(msg); 190 | } 191 | 192 | void sendFloatResponse(float val) { 193 | Serial.println(String(val, 3)); 194 | } 195 | 196 | void sendBinaryResponse(float val) { 197 | byte *b = (byte*)&val; 198 | Serial.write(b, 4); 199 | } 200 | 201 | void dispatchCommand(void) { 202 | if (cmd == "A") { 203 | setHeater1(0); 204 | setHeater2(0); 205 | sendResponse("Start"); 206 | } 207 | else if (cmd == "LED") { 208 | ledTimeout = millis() + 10000; 209 | LED = max(0, min(100, val)); 210 | sendResponse(String(LED)); 211 | } 212 | else if (cmd == "P1") { 213 | P1 = max(0, min(255, val)); 214 | sendResponse(String(P1)); 215 | } 216 | else if (cmd == "P2") { 217 | P2 = max(0, min(255, val)); 218 | sendResponse(String(P2)); 219 | } 220 | else if (cmd == "Q1") { 221 | setHeater1(val); 222 | sendFloatResponse(Q1); 223 | } 224 | else if (cmd == "Q1B") { 225 | setHeater1(val); 226 | sendBinaryResponse(Q1); 227 | } 228 | else if (cmd == "Q2") { 229 | setHeater2(val); 230 | sendFloatResponse(Q2); 231 | } 232 | else if (cmd == "Q2B") { 233 | setHeater1(val); 234 | sendBinaryResponse(Q2); 235 | } 236 | else if (cmd == "R1") { 237 | sendFloatResponse(Q1); 238 | } 239 | else if (cmd == "R2") { 240 | sendFloatResponse(Q2); 241 | } 242 | else if (cmd == "SCAN") { 243 | sendFloatResponse(readTemperature(pinT1)); 244 | sendFloatResponse(readTemperature(pinT2)); 245 | sendFloatResponse(Q1); 246 | sendFloatResponse(Q2); 247 | } 248 | else if (cmd == "T1") { 249 | sendFloatResponse(readTemperature(pinT1)); 250 | } 251 | else if (cmd == "T1B") { 252 | sendBinaryResponse(readTemperature(pinT1)); 253 | } 254 | else if (cmd == "T2") { 255 | sendFloatResponse(readTemperature(pinT2)); 256 | } 257 | else if (cmd == "T2B") { 258 | sendBinaryResponse(readTemperature(pinT2)); 259 | } 260 | else if (cmd == "VER") { 261 | sendResponse("TCLab Firmware " + vers + " " + boardType); 262 | } 263 | else if (cmd == "X") { 264 | setHeater1(0); 265 | setHeater2(0); 266 | sendResponse("Stop"); 267 | } 268 | else if (cmd.length() > 0) { 269 | setHeater1(0); 270 | setHeater2(0); 271 | sendResponse(cmd); 272 | } 273 | Serial.flush(); 274 | cmd = ""; 275 | } 276 | 277 | void checkAlarm(void) { 278 | if ((readTemperature(pinT1) > limT1) or (readTemperature(pinT2) > limT2)) { 279 | alarmStatus = 1; 280 | } 281 | else { 282 | alarmStatus = 0; 283 | } 284 | } 285 | 286 | void updateStatus(void) { 287 | // determine led status 288 | ledStatus = 1; 289 | if ((Q1 > 0) or (Q2 > 0)) { 290 | ledStatus = 2; 291 | } 292 | if (alarmStatus > 0) { 293 | ledStatus += 2; 294 | } 295 | // update led depending on ledStatus 296 | if (millis() < ledTimeout) { // override led operation 297 | analogWrite(pinLED1, LED); 298 | } 299 | else { 300 | switch (ledStatus) { 301 | case 1: // normal operation, heaters off 302 | analogWrite(pinLED1, loLED); 303 | break; 304 | case 2: // normal operation, heater on 305 | analogWrite(pinLED1, hiLED); 306 | break; 307 | case 3: // high temperature alarm, heater off 308 | if ((millis() % 2000) > 1000) { 309 | analogWrite(pinLED1, loLED); 310 | } else { 311 | analogWrite(pinLED1, loLED/4); 312 | } 313 | break; 314 | case 4: // high temperature alarm, heater on 315 | if ((millis() % 2000) > 1000) { 316 | analogWrite(pinLED1, hiLED); 317 | } else { 318 | analogWrite(pinLED1, loLED); 319 | } 320 | break; 321 | } 322 | } 323 | } 324 | 325 | // set Heater 1 326 | void setHeater1(float qval) { 327 | Q1 = max(0., min(qval, 100.)); 328 | analogWrite(pinQ1, (Q1*P1)/100); 329 | } 330 | 331 | // set Heater 2 332 | void setHeater2(float qval) { 333 | Q2 = max(0., min(qval, 100.)); 334 | analogWrite(pinQ2, (Q2*P2)/100); 335 | } 336 | 337 | // arduino startup 338 | void setup() { 339 | analogReference(EXTERNAL); 340 | while (!Serial) { 341 | ; // wait for serial port to connect. 342 | } 343 | Serial.begin(baud); 344 | Serial.flush(); 345 | setHeater1(0); 346 | setHeater2(0); 347 | ledTimeout = millis() + 1000; 348 | } 349 | 350 | // arduino main event loop 351 | void loop() { 352 | readCommand(); 353 | if (DEBUG) echoCommand(); 354 | parseCommand(); 355 | dispatchCommand(); 356 | checkAlarm(); 357 | updateStatus(); 358 | } 359 | --------------------------------------------------------------------------------