Fully working code
Revision 0:966ecf8af5b3, committed 23 months ago
- Comitter:
- michael_antonucci
- Date:
- Wed Nov 16 04:54:57 2022 +0000
- Commit message:
- Fully working code for thermistor and photocell using functions
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Nov 16 04:54:57 2022 +0000
@@ -0,0 +1,80 @@
+#include "mbed.h"
+//Assignment 5, Exercise 1
+//Author: Michael Antonucci
+//Date Modified: 11/15/22
+
+DigitalOut LEDOut1(LED1); //digital output to LED1
+DigitalOut LEDOut2(LED2); //digital output to LED2
+DigitalOut LEDOut3(LED3); //digital output to LED3
+DigitalOut LEDOut4(LED4); //digital output to LED4
+Timer out_timer; //timer
+AnalogIn therm(p19); //thermistor
+AnalogIn photo(p20); //photocell
+Serial pc(USBTX, USBRX);
+
+DigitalOut myled(LED1);
+
+float thermistor_volt(AnalogIn therm)
+{
+ float temp_v; // thermistor voltage
+ temp_v=therm.read()*3.3; // thermistor output
+ return temp_v;
+}
+
+float photocell_resistance(AnalogIn photo)
+{
+ float photo_v;
+ photo_v=photo.read()*3.3; // photocell output
+ if (photo_v<1.8) {
+ LEDOut4=1;
+ } else {
+ LEDOut4=0;
+ }
+ return photo_v;
+}
+
+float thermistor_temp(AnalogIn therm)
+{
+ float temp;
+ temp= -1481.96+sqrt((2196200)+((1.8639-(3.3*therm.read()))/0.00000388)); // voltage to degrees C
+
+ if(temp>=13) {
+ LEDOut1=1;
+ } else {
+ LEDOut1=0;
+ }
+ if(temp>=20) {
+ LEDOut2=1;
+ } else {
+ LEDOut2=0;
+ }
+ if(temp>=27) {
+ LEDOut3=1;
+ } else {
+ LEDOut3=0;
+ }
+ return temp;
+}
+
+int main()
+{
+ DigitalOut LEDOut1(LED1); //digital output to LED1
+ DigitalOut LEDOut2(LED2); //digital output to LED2
+ DigitalOut LEDOut3(LED3); //digital output to LED3
+ DigitalOut LEDOut4(LED4); //digital output to LED4
+ float temp; // temperature, thermistor
+ float temp_v; // voltage of thermistor
+ float photo_v; // voltage of photocell
+
+ while(1) {
+ temp = thermistor_temp(therm);
+ temp_v = thermistor_volt(therm);
+ photo_v = photocell_resistance(photo);
+ out_timer.start();
+
+ if(out_timer.read()>1) {
+ pc.printf("Thermistor Voltage: %f Volts \r\n Temperature: %f Celsius \r\n Photocell Voltage: %f Volts \r\n ",temp_v,temp,photo_v);
+ out_timer.reset();
+ }
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Nov 16 04:54:57 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file