Alarm
Dependencies: mbed HTTPClient mbed-rtos 4DGL-uLCD-SE NTPClient EthernetInterface
Diff: alarm.h
- Revision:
- 0:070d52cbaed0
- Child:
- 1:2dcc0acbaa0b
diff -r 000000000000 -r 070d52cbaed0 alarm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/alarm.h Wed Dec 16 18:52:50 2020 +0000 @@ -0,0 +1,79 @@ + +#include "mbed.h" +#include "EthernetInterface.h" +#include "NTPClient.h" + +#define VOLUME 1 + +Serial pc(USBTX, USBRX); + +// Parameters +char* domain_name = "us.pool.ntp.org"; +int port_number = 123; + +// Networking +EthernetInterface eth; +NTPClient ntpClient; + +//Pins +PwmOut pwm_pin(p21); +DigitalIn button(p8); +BusOut LEDS(LED1,LED2,LED3,LED4); + + + +// Plays a sound with the defined frequency, duration, and volume +void playNote(float frequency, float duration, float volume) { + pwm_pin.period(1.0/frequency); + pwm_pin = volume/2.0; + wait(duration); + pwm_pin = 0.0; +} + +void soundalarm() +{ + time_t ctTime; + eth.init(); + eth.connect(); + + + pc.printf("IP address is \n%s\n\n",eth.getIPAddress()); + wait(1); + + char* domainName="0.uk.pool.ntp.org"; + + + // Read time from server + pc.printf("Reading time...\n\r"); + ntpClient.setTime(domainName,123,0x00005000); + char buffer[80]; + wait(2); + eth.disconnect(); + ctTime = time(NULL); + pc.printf("Alarm began on\n"); + strftime(buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \ + localtime(&ctTime)); + pc.printf(" UTC/GMT:\n%s", buffer); + + // Loop + while((button==1)) + { + playNote(960,1.2,VOLUME); + LEDS=0x0F; + wait(0.1); + playNote(770,1.2,VOLUME); + LEDS=0; + } + + pc.printf("Alarm stopped on\n"); + ctTime = time(NULL); + strftime(buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \ + localtime(&ctTime)); + pc.printf(" UTC/GMT:\n%s", buffer); +} + + + + + +