Felix HARERIMANA / Mbed 2 deprecated DISCO_F413ZH-trafficproject-SOUND

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
harerimana
Date:
Thu Nov 12 14:50:36 2020 +0000
Parent:
8:3dd7a4231f6e
Commit message:
TRAFFIC CODES WITH SOUND

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Nov 05 16:03:19 2020 +0000
+++ b/main.cpp	Thu Nov 12 14:50:36 2020 +0000
@@ -4,15 +4,17 @@
 #include "mbed_events.h"
 #include "LoRaWANInterface.h"
 #include "SX1276_LoRaRadio.h"
-#define DEVICEID "FELIX_220014243"
+
 #define YELLOWBLINKINGDURATION 10
 #define REDLIGHTONDURATION 15
 // The port we're sending and receiving on
-#define MBED_CONF_LORA_APP_PORT     1
+#define MBED_CONF_LORA_APP_PORT     15
+//#define DEVICEID Felix
 
-DigitalOut yellowled(LED4, 1); // Initializing the yellow light ON (same as yellowled = 1)
-DigitalOut redled(LED1, 0); // Initializing the Red light to OFF (same as redled = 0)
-InterruptIn pedestrianbutton(p5);
+DigitalOut yellowled(p15, 1); // Initializing the yellow light ON (same as yellowled = 1)
+DigitalOut redled(p17, 0); // Initializing the Red light to OFF (same as redled = 0)
+PwmOut speaker(p21);//pin for sound speaker
+InterruptIn pedestrianbutton(p16);
 
 C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11); // create an instance of LCD
 
@@ -20,7 +22,7 @@
 
 char pedestrianAdvert[] = "2 COFFEES FOR THE PRICE OF 1. ONLY AT CAMELIA";
 char carDriverAdvert[] = "RECEIVE 5%% REDUCTION ON YOUR FULL TANK ....";
-
+char deviceId[] = "Felix_220014243";
 
 // Device credentials, register device as OTAA in The Things Network and copy credentials here
 static uint8_t DEV_EUI[] = { 0x26, 0x10, 0x20, 0x20, 0x11, 0x11, 0x20, 0x20 };
@@ -41,10 +43,11 @@
 static void lora_event_handler(lorawan_event_t event);
 
 // Send a message over LoRaWAN
-static void send_message(long int timestamp, int value) {
+static void send_message(int value) {
     uint8_t tx_buffer[50] = { 0 };
+     time_t seconds = time(NULL);
     // Sending strings over LoRaWAN is not recommended
-    sprintf((char*) tx_buffer, "{\"deviceId\":\"%s\", \"timestamp\":%ld,\"value\":%d}", DEVICEID, timestamp, value);
+    sprintf((char*) tx_buffer, "{\"deviceId\":\"%s\", \"timestamp\":%ld,\"value\":%d}", deviceId, (long int)seconds, value);
     int packet_len = strlen((char*) tx_buffer);
     printf("Sending %d bytes: \"%s\"\n", packet_len, tx_buffer);
     int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG);
@@ -62,7 +65,7 @@
         printf("Set your LoRaWAN credentials first!\n");
         return -1;
     }
-    printf("Press BUTTON1 to send the current value of the temperature sensor!\n");
+    printf("Press BUTTON1 to stop cars and have right to cross the road!!!!\n");
     // Enable trace output for this demo, so we can see what the LoRaWAN stack does
     mbed_trace_init();
     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
@@ -89,7 +92,8 @@
 
     if (retcode == LORAWAN_STATUS_OK ||
         retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
-    } else {
+    }
+    else {
         printf("Connection error, code = %d\n", retcode);
         return -1;
     }
@@ -99,6 +103,13 @@
     ev_queue.dispatch_forever();
     return 0;
 }
+void play_tone(float frequency, float volume, int interval, int rest) {
+    speaker.period(1.0 / frequency);
+    speaker = volume;
+    wait(interval);
+    speaker = 0.0;
+    wait(rest);
+}
 
 /**
  * Blinking a yellow LED.
@@ -106,16 +117,17 @@
  * @param duration length of time to blink .
  */
 static void yellowblinking(int duration){
-    // Data to send
-   // send_message("FELIX", 220014243, 1);
     int counter=0; 
     while(counter < duration)
     {
-        counter+=1;
+           
+ counter+=1;
         yellowled = !yellowled;
         wait(1);
+  
     }
     yellowled = 0;
+   // state=2;
 }
 
 /**
@@ -124,18 +136,23 @@
  * @param duration length of time for pedestrian to cross.
  */
 static void pedestriancrossing(int duration){
-   // Data to send
-    //send_message("FELIX", 220014243, 2);
     redled = 1;
+    
     int counter=0; 
     while(counter < duration)
     {
+        play_tone(200.0, 0.5, 1, 0);//play sound to help people with disabilities to know time to cross the road
+        //play_tone(150.0, 0.5, 1, 0);
+        //play_tone(125.0, 0.5, 1, 0); 
         counter+=1;
+      
         wait(1);
     }
     redled = 0;
     yellowled = 1;
-}
+    //state=0;
+    }
+
 
 /**
  * Display content on LCD.
@@ -156,32 +173,38 @@
  */
 void pedestrian_isr() {
      wait(1);
+     //send_message(state);
      if (state == 0) // YELLOW IS ON AND RED IS OFF
      {
         state = 1; // YELLOW IS BLINKING AND RED IS OFF
-        send_message(220014243, state);
+        send_message(state);
+         //send_message(deviceId, 123456, 2);
         display(pedestrianAdvert);
         yellowblinking(YELLOWBLINKINGDURATION);
         wait(1);
-        state = 2;  // YELLOW IS OFF AND RED IS ON
-        send_message(220014243, state);
+        state = 2;// YELLOW IS OFF AND RED IS ON
+     }
+    if(state==2){
+        send_message(2);
         display(carDriverAdvert);
         pedestriancrossing(REDLIGHTONDURATION);
-        state = 0;
-        send_message(220014243, state);
+         wait(1);
+         state = 0;
+        }
+    if(state==0){
+        send_message(0);
         lcd.cls();
      }
-     if(state == 2) // RED 
-     {
+     }
+     //if(state == 2) // RED 
+     //{
          // extend the duration of RED by 5 seconds
-     }
-}
+    // }
+//}
 
 int main() {
     pedestrianbutton.rise(ev_queue.event(&pedestrian_isr)); // Registering an ISR for button click
     initialize_lora();
-    // Data to send
-//send_message("FELIX", 220014243, 0);
 }
 
 // Event handler