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@3:0170897aa173, 2021-10-29 (annotated)
- Committer:
- mgimple
- Date:
- Fri Oct 29 14:52:42 2021 +0000
- Revision:
- 3:0170897aa173
- Parent:
- 2:38d752717527
photocell working ; final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgimple | 0:61f876d07ef5 | 1 | #include "mbed.h" |
mgimple | 3:0170897aa173 | 2 | #include "math.h" |
mgimple | 0:61f876d07ef5 | 3 | |
mgimple | 0:61f876d07ef5 | 4 | Serial pc(USBTX,USBRX); |
mgimple | 0:61f876d07ef5 | 5 | DigitalOut Led1(LED1); |
mgimple | 0:61f876d07ef5 | 6 | DigitalOut Led2(LED2); |
mgimple | 0:61f876d07ef5 | 7 | DigitalOut Led3(LED3); |
mgimple | 0:61f876d07ef5 | 8 | DigitalOut Led4(LED4); |
mgimple | 0:61f876d07ef5 | 9 | AnalogIn therm(p19); |
mgimple | 0:61f876d07ef5 | 10 | AnalogIn light(p20); |
mgimple | 0:61f876d07ef5 | 11 | |
mgimple | 0:61f876d07ef5 | 12 | |
mgimple | 0:61f876d07ef5 | 13 | float volt_raw; //raw data read from thermistor |
mgimple | 0:61f876d07ef5 | 14 | float volt; //final voltage after conversion |
mgimple | 0:61f876d07ef5 | 15 | float temp; //converted temperature in degrees celsius |
mgimple | 0:61f876d07ef5 | 16 | float light_volt; //light voltage multiplied by 3.3 from mbed |
mgimple | 0:61f876d07ef5 | 17 | float light_res; //resistance through photocell |
mgimple | 0:61f876d07ef5 | 18 | float light_volt_raw; //raw voltage through photocell |
mgimple | 2:38d752717527 | 19 | |
mgimple | 0:61f876d07ef5 | 20 | |
mgimple | 0:61f876d07ef5 | 21 | int main() |
mgimple | 0:61f876d07ef5 | 22 | { |
mgimple | 1:d706fb07041f | 23 | while(1) { |
mgimple | 3:0170897aa173 | 24 | volt_raw = therm.read(); //sets the raw data as a variable |
mgimple | 3:0170897aa173 | 25 | volt = volt_raw*3.3; //converts raw voltage to the correct scale |
mgimple | 3:0170897aa173 | 26 | float temp = (-1481.96)+ sqrt((2.1962*(powf(10,6)))+((1.8639-volt)/(3.88*(powf(10,-6))))); //converts voltage to temperature |
mgimple | 3:0170897aa173 | 27 | pc.printf("Thermocell Voltage: %f V\r\n",volt); |
mgimple | 3:0170897aa173 | 28 | pc.printf("Temperature: %f C\r\n",temp); |
mgimple | 0:61f876d07ef5 | 29 | |
mgimple | 3:0170897aa173 | 30 | if (temp>=15 && temp<20) { |
mgimple | 3:0170897aa173 | 31 | Led1=1; |
mgimple | 3:0170897aa173 | 32 | } |
mgimple | 3:0170897aa173 | 33 | if (temp<15 && temp>=20) { |
mgimple | 3:0170897aa173 | 34 | Led2=1; |
mgimple | 3:0170897aa173 | 35 | } |
mgimple | 3:0170897aa173 | 36 | if (temp>=25) { |
mgimple | 3:0170897aa173 | 37 | Led3=1; |
mgimple | 3:0170897aa173 | 38 | } |
mgimple | 3:0170897aa173 | 39 | |
mgimple | 2:38d752717527 | 40 | |
mgimple | 3:0170897aa173 | 41 | light_volt_raw=light.read(); //sets the raw data as a variable |
mgimple | 3:0170897aa173 | 42 | light_res=(10000/light_volt_raw)-10000; |
mgimple | 3:0170897aa173 | 43 | light_volt=light_volt_raw*3.3; |
mgimple | 3:0170897aa173 | 44 | pc.printf("Photocell Resistance: %f Ohms\r\n",light_res); |
mgimple | 3:0170897aa173 | 45 | pc.printf("Photocell Voltage: %f \r\n",light_volt); |
mgimple | 3:0170897aa173 | 46 | if (light_volt<1.55) { //the sensor is "completely blocked" by a finger at most values under 0.3 volts |
mgimple | 3:0170897aa173 | 47 | Led4=1; |
mgimple | 3:0170897aa173 | 48 | } |
mgimple | 3:0170897aa173 | 49 | if (light_volt>1.55) { |
mgimple | 3:0170897aa173 | 50 | Led4=0; |
mgimple | 0:61f876d07ef5 | 51 | } |
mgimple | 2:38d752717527 | 52 | } |
mgimple | 3:0170897aa173 | 53 | } |