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:303fe443c3d4, 2015-10-21 (annotated)
- Committer:
- ibose3
- Date:
- Wed Oct 21 18:06:05 2015 +0000
- Revision:
- 0:303fe443c3d4
Takes in light from a photocell and translates it to English characters on an LCD Screen
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ibose3 | 0:303fe443c3d4 | 1 | #include "mbed.h" |
| ibose3 | 0:303fe443c3d4 | 2 | #include "MorseCode.h" |
| ibose3 | 0:303fe443c3d4 | 3 | |
| ibose3 | 0:303fe443c3d4 | 4 | DigitalOut LED(LED1); // LED that blinks every .5 seconds |
| ibose3 | 0:303fe443c3d4 | 5 | PwmOut Speaker(p21); // Speaker |
| ibose3 | 0:303fe443c3d4 | 6 | Timer Time; |
| ibose3 | 0:303fe443c3d4 | 7 | AnalogIn photocell(p15); //CdS Powercell |
| ibose3 | 0:303fe443c3d4 | 8 | |
| ibose3 | 0:303fe443c3d4 | 9 | int main() { |
| ibose3 | 0:303fe443c3d4 | 10 | int up = 0; |
| ibose3 | 0:303fe443c3d4 | 11 | int read = 0; |
| ibose3 | 0:303fe443c3d4 | 12 | Speaker.period(1.0/800.0); //initializes speaker tone |
| ibose3 | 0:303fe443c3d4 | 13 | Time.start(); //starts timer |
| ibose3 | 0:303fe443c3d4 | 14 | int value = 0; |
| ibose3 | 0:303fe443c3d4 | 15 | |
| ibose3 | 0:303fe443c3d4 | 16 | while(1) { |
| ibose3 | 0:303fe443c3d4 | 17 | |
| ibose3 | 0:303fe443c3d4 | 18 | //Timer |
| ibose3 | 0:303fe443c3d4 | 19 | if(Time.read_ms()%500<15) { |
| ibose3 | 0:303fe443c3d4 | 20 | up = !up; |
| ibose3 | 0:303fe443c3d4 | 21 | read = 0; |
| ibose3 | 0:303fe443c3d4 | 22 | if((Time.read_ms() - 1740000) > 0) { |
| ibose3 | 0:303fe443c3d4 | 23 | Time.reset(); |
| ibose3 | 0:303fe443c3d4 | 24 | } |
| ibose3 | 0:303fe443c3d4 | 25 | wait_ms(15); |
| ibose3 | 0:303fe443c3d4 | 26 | } |
| ibose3 | 0:303fe443c3d4 | 27 | |
| ibose3 | 0:303fe443c3d4 | 28 | //LED |
| ibose3 | 0:303fe443c3d4 | 29 | LED = (up)? 1 : 0; //LED blinks every other .5 second |
| ibose3 | 0:303fe443c3d4 | 30 | |
| ibose3 | 0:303fe443c3d4 | 31 | //Reads in Morse Code |
| ibose3 | 0:303fe443c3d4 | 32 | if(Time.read_ms()%500 > 250 && read == 0) { // .25 Second Has Passed |
| ibose3 | 0:303fe443c3d4 | 33 | read = 1; |
| ibose3 | 0:303fe443c3d4 | 34 | if (photocell.read() < 0.5) { |
| ibose3 | 0:303fe443c3d4 | 35 | value = 0; //if the light is not on |
| ibose3 | 0:303fe443c3d4 | 36 | } else { |
| ibose3 | 0:303fe443c3d4 | 37 | value = 1; //if the light is on |
| ibose3 | 0:303fe443c3d4 | 38 | } |
| ibose3 | 0:303fe443c3d4 | 39 | MorseCode(value); //Translates the Morse code |
| ibose3 | 0:303fe443c3d4 | 40 | } |
| ibose3 | 0:303fe443c3d4 | 41 | |
| ibose3 | 0:303fe443c3d4 | 42 | //Speaker |
| ibose3 | 0:303fe443c3d4 | 43 | if(value) { |
| ibose3 | 0:303fe443c3d4 | 44 | Speaker = 0.1; //Plays tone if light is on |
| ibose3 | 0:303fe443c3d4 | 45 | wait_ms(15); |
| ibose3 | 0:303fe443c3d4 | 46 | } |
| ibose3 | 0:303fe443c3d4 | 47 | else { |
| ibose3 | 0:303fe443c3d4 | 48 | Speaker = 0.0; //Mutes tone if light is off |
| ibose3 | 0:303fe443c3d4 | 49 | wait_ms(15); |
| ibose3 | 0:303fe443c3d4 | 50 | } |
| ibose3 | 0:303fe443c3d4 | 51 | |
| ibose3 | 0:303fe443c3d4 | 52 | } |
| ibose3 | 0:303fe443c3d4 | 53 | } |