Fully working code

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //Assignment 5, Exercise 1
00003 //Author: Michael Antonucci
00004 //Date Modified: 11/15/22
00005  
00006 DigitalOut LEDOut1(LED1); //digital output to LED1
00007 DigitalOut LEDOut2(LED2); //digital output to LED2
00008 DigitalOut LEDOut3(LED3); //digital output to LED3
00009 DigitalOut LEDOut4(LED4); //digital output to LED4
00010 Timer out_timer; //timer
00011 AnalogIn therm(p19); //thermistor
00012 AnalogIn photo(p20); //photocell
00013 Serial pc(USBTX, USBRX);
00014  
00015 DigitalOut myled(LED1);
00016 
00017 float thermistor_volt(AnalogIn therm)
00018 {
00019     float temp_v; // thermistor voltage
00020         temp_v=therm.read()*3.3; // thermistor output
00021         return temp_v;
00022 }
00023 
00024 float photocell_resistance(AnalogIn photo)
00025 {
00026     float photo_v;
00027         photo_v=photo.read()*3.3; // photocell output
00028         if (photo_v<1.8) { 
00029             LEDOut4=1; 
00030         } else {
00031             LEDOut4=0;
00032         }
00033         return photo_v;
00034 }
00035 
00036 float thermistor_temp(AnalogIn therm)
00037 {
00038     float temp;
00039         temp= -1481.96+sqrt((2196200)+((1.8639-(3.3*therm.read()))/0.00000388)); // voltage to degrees C
00040 
00041         if(temp>=13) { 
00042             LEDOut1=1;
00043         } else {
00044             LEDOut1=0;
00045         }
00046         if(temp>=20) { 
00047             LEDOut2=1; 
00048         } else {
00049             LEDOut2=0;
00050         }
00051         if(temp>=27) { 
00052             LEDOut3=1; 
00053         } else {
00054             LEDOut3=0; 
00055         }
00056         return temp;
00057 }
00058  
00059 int main()
00060 {
00061     DigitalOut LEDOut1(LED1); //digital output to LED1
00062     DigitalOut LEDOut2(LED2); //digital output to LED2
00063     DigitalOut LEDOut3(LED3); //digital output to LED3
00064     DigitalOut LEDOut4(LED4); //digital output to LED4
00065     float temp; // temperature, thermistor
00066     float temp_v; // voltage of thermistor
00067     float photo_v; // voltage of photocell
00068 
00069     while(1) {
00070         temp = thermistor_temp(therm);
00071         temp_v = thermistor_volt(therm);
00072         photo_v = photocell_resistance(photo);
00073         out_timer.start();
00074  
00075         if(out_timer.read()>1) {
00076             pc.printf("Thermistor Voltage: %f Volts \r\n Temperature: %f Celsius \r\n Photocell Voltage: %f Volts \r\n ",temp_v,temp,photo_v);
00077             out_timer.reset();
00078         }
00079     }
00080 }