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.
main.cpp@0:61f876d07ef5, 2021-10-27 (annotated)
- Committer:
- mgimple
- Date:
- Wed Oct 27 16:31:41 2021 +0000
- Revision:
- 0:61f876d07ef5
- Child:
- 1:d706fb07041f
lights up led 1,2,3 with temperature cell; photo cell and led 4 not responding
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgimple | 0:61f876d07ef5 | 1 | #include "mbed.h" |
mgimple | 0:61f876d07ef5 | 2 | |
mgimple | 0:61f876d07ef5 | 3 | Serial pc(USBTX,USBRX); |
mgimple | 0:61f876d07ef5 | 4 | DigitalOut Led1(LED1); |
mgimple | 0:61f876d07ef5 | 5 | DigitalOut Led2(LED2); |
mgimple | 0:61f876d07ef5 | 6 | DigitalOut Led3(LED3); |
mgimple | 0:61f876d07ef5 | 7 | DigitalOut Led4(LED4); |
mgimple | 0:61f876d07ef5 | 8 | AnalogIn therm(p19); |
mgimple | 0:61f876d07ef5 | 9 | AnalogIn light(p20); |
mgimple | 0:61f876d07ef5 | 10 | |
mgimple | 0:61f876d07ef5 | 11 | |
mgimple | 0:61f876d07ef5 | 12 | float volt_raw; //raw data read from thermistor |
mgimple | 0:61f876d07ef5 | 13 | float volt; //final voltage after conversion |
mgimple | 0:61f876d07ef5 | 14 | float temp; //converted temperature in degrees celsius |
mgimple | 0:61f876d07ef5 | 15 | float light_volt; //light voltage multiplied by 3.3 from mbed |
mgimple | 0:61f876d07ef5 | 16 | float light_res; //resistance through photocell |
mgimple | 0:61f876d07ef5 | 17 | float light_volt_raw; //raw voltage through photocell |
mgimple | 0:61f876d07ef5 | 18 | void temp_read(); //temperature reading function |
mgimple | 0:61f876d07ef5 | 19 | void light_read(); //light reading function |
mgimple | 0:61f876d07ef5 | 20 | void templight(); //lgith and temp reading function |
mgimple | 0:61f876d07ef5 | 21 | |
mgimple | 0:61f876d07ef5 | 22 | int main() |
mgimple | 0:61f876d07ef5 | 23 | { |
mgimple | 0:61f876d07ef5 | 24 | |
mgimple | 0:61f876d07ef5 | 25 | void temp_read(); //main temperature reading command to be called upon |
mgimple | 0:61f876d07ef5 | 26 | { |
mgimple | 0:61f876d07ef5 | 27 | volt_raw = therm.read(); //sets the raw data as a variable |
mgimple | 0:61f876d07ef5 | 28 | volt = volt_raw*3.3; //converts raw voltage to the correct scale for our mbed |
mgimple | 0:61f876d07ef5 | 29 | temp = -1481.96+sqrt(((2.1962e6)+((1.8639-volt)/(3.88e-6)))); //converts voltage to temperature |
mgimple | 0:61f876d07ef5 | 30 | pc.printf("TEMP %f Volts %3.1f Celsius \r\n",volt, temp); |
mgimple | 0:61f876d07ef5 | 31 | wait(1); |
mgimple | 0:61f876d07ef5 | 32 | if (temp<50) { |
mgimple | 0:61f876d07ef5 | 33 | |
mgimple | 0:61f876d07ef5 | 34 | if (temp>=20) { |
mgimple | 0:61f876d07ef5 | 35 | Led1=1; |
mgimple | 0:61f876d07ef5 | 36 | if (temp>=25) { |
mgimple | 0:61f876d07ef5 | 37 | Led2=1; |
mgimple | 0:61f876d07ef5 | 38 | if (temp>=30) { |
mgimple | 0:61f876d07ef5 | 39 | Led3=1; |
mgimple | 0:61f876d07ef5 | 40 | } |
mgimple | 0:61f876d07ef5 | 41 | } |
mgimple | 0:61f876d07ef5 | 42 | } |
mgimple | 0:61f876d07ef5 | 43 | } |
mgimple | 0:61f876d07ef5 | 44 | } |
mgimple | 0:61f876d07ef5 | 45 | |
mgimple | 0:61f876d07ef5 | 46 | void light_read(); //main light reading command to be called upon |
mgimple | 0:61f876d07ef5 | 47 | { |
mgimple | 0:61f876d07ef5 | 48 | light_volt_raw=light.read(); //sets the raw data as a variable |
mgimple | 0:61f876d07ef5 | 49 | light_res=(10000/light_volt_raw)-10000; //converting voltage to resistance |
mgimple | 0:61f876d07ef5 | 50 | light_volt=light_volt_raw*3.3; //mbed outputs 3.3 volts |
mgimple | 0:61f876d07ef5 | 51 | pc.printf("LIGHT %1.3f Voltz 6.2%f Ohms\r\n",light_volt,light_res); |
mgimple | 0:61f876d07ef5 | 52 | wait(1); |
mgimple | 0:61f876d07ef5 | 53 | if (light_volt<0.3) { //the sensor is "completely blocked" by a finger at most values under 0.3 volts. The whole photocell has to be covered however because even if a bit is showing it will read higher than 0.3 |
mgimple | 0:61f876d07ef5 | 54 | Led4=1; |
mgimple | 0:61f876d07ef5 | 55 | } |
mgimple | 0:61f876d07ef5 | 56 | if (light_volt>0.3) { |
mgimple | 0:61f876d07ef5 | 57 | Led4=0; |
mgimple | 0:61f876d07ef5 | 58 | } |
mgimple | 0:61f876d07ef5 | 59 | } |
mgimple | 0:61f876d07ef5 | 60 | |
mgimple | 0:61f876d07ef5 | 61 | } |