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:9d9628bd3514, 2018-03-08 (annotated)
- Committer:
- oldmanturtle
- Date:
- Thu Mar 08 18:48:30 2018 +0000
- Revision:
- 3:9d9628bd3514
- Parent:
- 2:c76b070c2a55
- Child:
- 4:327441ad8cf6
LED with Phototransistor Reading
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
_laurentaylorrr | 0:46b2c955924b | 1 | #include "mbed.h" |
_laurentaylorrr | 0:46b2c955924b | 2 | AnalogIn lightSensor(p20); |
_laurentaylorrr | 1:279fcab0c394 | 3 | DigitalOut ledRed(p25); |
oldmanturtle | 2:c76b070c2a55 | 4 | DigitalOut ledBlue(p26); |
oldmanturtle | 2:c76b070c2a55 | 5 | |
oldmanturtle | 3:9d9628bd3514 | 6 | Serial pc(USBTX,USBRX); |
_laurentaylorrr | 1:279fcab0c394 | 7 | |
oldmanturtle | 3:9d9628bd3514 | 8 | float checkLightSensor(int n); |
oldmanturtle | 3:9d9628bd3514 | 9 | |
oldmanturtle | 3:9d9628bd3514 | 10 | void ledSwitch(); |
oldmanturtle | 3:9d9628bd3514 | 11 | |
_laurentaylorrr | 0:46b2c955924b | 12 | int main() { |
oldmanturtle | 3:9d9628bd3514 | 13 | int checkTimes = 10; |
oldmanturtle | 3:9d9628bd3514 | 14 | ledRed = true; |
oldmanturtle | 3:9d9628bd3514 | 15 | ledBlue = false; |
_laurentaylorrr | 1:279fcab0c394 | 16 | while(1) { |
oldmanturtle | 3:9d9628bd3514 | 17 | ledSwitch(); |
oldmanturtle | 3:9d9628bd3514 | 18 | pc.printf("%.4f\r\n", checkLightSensor(checkTimes)); |
oldmanturtle | 3:9d9628bd3514 | 19 | wait(0.2); |
oldmanturtle | 3:9d9628bd3514 | 20 | ledSwitch(); |
oldmanturtle | 3:9d9628bd3514 | 21 | pc.printf("%.4f\r\n", checkLightSensor(checkTimes)); |
_laurentaylorrr | 0:46b2c955924b | 22 | wait(0.2); |
_laurentaylorrr | 0:46b2c955924b | 23 | } |
oldmanturtle | 3:9d9628bd3514 | 24 | } |
oldmanturtle | 3:9d9628bd3514 | 25 | |
oldmanturtle | 3:9d9628bd3514 | 26 | // Average n readings of the light sensor |
oldmanturtle | 3:9d9628bd3514 | 27 | float checkLightSensor(int n){ |
oldmanturtle | 3:9d9628bd3514 | 28 | float x; |
oldmanturtle | 3:9d9628bd3514 | 29 | x = 0; |
oldmanturtle | 3:9d9628bd3514 | 30 | for (int i = 0; i<n; i++) |
oldmanturtle | 3:9d9628bd3514 | 31 | x = x + lightSensor; |
oldmanturtle | 3:9d9628bd3514 | 32 | x = x/n; |
oldmanturtle | 3:9d9628bd3514 | 33 | return x; |
oldmanturtle | 3:9d9628bd3514 | 34 | } |
oldmanturtle | 3:9d9628bd3514 | 35 | |
oldmanturtle | 3:9d9628bd3514 | 36 | void ledSwitch(){ |
oldmanturtle | 3:9d9628bd3514 | 37 | ledBlue = !ledBlue; |
oldmanturtle | 3:9d9628bd3514 | 38 | ledRed = !ledRed; |
oldmanturtle | 3:9d9628bd3514 | 39 | } |