{"id":1211,"date":"2018-04-12T11:39:53","date_gmt":"2018-04-12T16:39:53","guid":{"rendered":"http:\/\/blog.espol.edu.ec\/edelros\/?p=1211"},"modified":"2018-09-10T10:29:07","modified_gmt":"2018-09-10T15:29:07","slug":"morse-codec-led-y-buzzer","status":"publish","type":"post","link":"https:\/\/blog.espol.edu.ec\/edelros\/morse-codec-led-y-buzzer\/","title":{"rendered":"Morse Codec - LED y Buzzer"},"content":{"rendered":"<p>Se puede escuchar el tono del c\u00f3digo morse enviado a un Buzzer en el pin 9. <a href=\"http:\/\/blog.espol.edu.ec\/edelros\/files\/2018\/04\/morseBuzzer_01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1212 alignright\" src=\"http:\/\/blog.espol.edu.ec\/edelros\/files\/2018\/04\/morseBuzzer_01.png\" alt=\"\" width=\"242\" height=\"221\" \/><\/a><\/p>\n<p>Se a\u00f1ade al ejemplo anterior para un led la instrucci\u00f3n tone(PIN_BUZZER, NOTA, DURACION),<\/p>\n<p>que requiere PIN_BUZZER, la frecuencia de la NOTA para el sonido y la duraci\u00f3n.<\/p>\n<p>El pin de se\u00f1al S del Buzzer se conecta el Pin 9 del arduino, y se alimenta el VCC (+) y GND(-) completando todo lo necesario para que empiece a sonar el mensaje en el buzzer.<\/p>\n<pre>\/*  CODificador morse\r\n *   recibe un mensaje y controla un buzzer en pin 9\r\n *   equivale: estructura de datos char y string\r\n *   funciones: codificar y decodificar\r\n *\/\r\n\r\n\/\/ PIN para el LED, led integrado = 13\r\n#define PIN_LED 13\r\n#define PIN_BUZZER 9\r\n#define NOTA 440\r\n\/\/Duraci\u00f3n de un s\u00edmbolo en  ms\r\n#define DURACION 100\r\n\r\nvoid setup(){\r\n  pinMode( PIN_LED, OUTPUT );\r\n  pinMode( PIN_BUZZER, OUTPUT );\r\n  digitalWrite( PIN_LED, LOW );\r\n}\r\n\r\nvoid loop(){\r\n  String mensaje = \"SOS   \";\r\n  String mensajemorse;\r\n  char simbolo;\r\n  int n;\r\n  mensajemorse = codifica(mensaje);\r\n  \/\/ Transmite el mensaje\r\n  n = mensajemorse.length();\r\n  for(int i=0; i&lt;=n; i++){\r\n    simbolo = mensajemorse[i];\r\n    morseLedTx(simbolo);\r\n  }\r\n}\r\n\r\nvoid morseLedTx(char simbolo){\r\n    if (simbolo == '.'){\r\n      digitalWrite( PIN_LED, HIGH );\r\n      tone(PIN_BUZZER, NOTA, DURACION);\r\n      delay( DURACION );\r\n      digitalWrite( PIN_LED, LOW );\r\n      delay( DURACION );\r\n    }\r\n    if (simbolo == '-'){\r\n      digitalWrite( PIN_LED, HIGH );\r\n      tone(PIN_BUZZER, NOTA, DURACION*3);\r\n      delay( DURACION*3 );\r\n      digitalWrite( PIN_LED, LOW );\r\n      delay( DURACION );\r\n    }\r\n    if (simbolo == ' ') {\r\n      delay( DURACION );\r\n    }\r\n}\r\n\r\n\/\/ Equivalente letra a c\u00f3digo Morse. Estructura de datos\r\nstatic const struct {const char letra, *codigo;} equivale[] =\r\n{\r\n  { 'A', \".-\" }, { 'B', \"-...\" }, { 'C', \"-.-.\" }, \r\n  { 'D', \"-..\" }, { 'E', \".\" }, { 'F', \"..-.\" }, \r\n  { 'G', \"--.\" }, { 'H', \"....\" }, { 'I', \"..\" },\r\n  { 'J', \".---\" }, { 'K', \"-.-\" }, { 'L', \".-..\" },\r\n  { 'M', \"--\" }, { 'N', \"-.\" }, { 'O', \"---\" }, \r\n  { 'P', \".--.\" }, { 'Q', \"--.-\" }, { 'R', \".-.\" },\r\n  { 'S', \"...\" }, { 'T', \"-\" }, { 'U', \"..-\" }, \r\n  { 'V', \"...-\" }, { 'W', \".--\" }, { 'X', \"-..-\" },\r\n  { 'Y', \"-.--\" }, { 'Z', \"--..\" }, \r\n  { ' ', \" \" },   \/\/espacio entre palabras \r\n  { '1', \".----\" }, { '2', \"..---\" }, { '3', \"...--\" },\r\n  { '4', \"....-\" }, { '5', \".....\" }, { '6', \"-....\" }, \r\n  { '7', \"--...\" }, { '8', \"---..\" }, { '9', \"----.\" }, \r\n  { '0', \"-----\" },\r\n  { '.', \".\u2013.\u2013.\u2013\" }, { ',', \"--..--\" }, { '?', \"..--..\" },\r\n  { '!', \"-.-.--\" }, { ':', \"---...\" }, { ';', \"-.-.-.\" }, \r\n  { '(', \"-.--.\" }, { ')', \"-.--.-\" }, { '\"', \".-..-.\" },\r\n  { '@', \".--.-.\" }, { '&amp;', \".-...\" },\r\n};\r\n\r\nString codifica(String mensaje){\r\n  String mensajemorse = \"\";\r\n  int i, j, n, m;\r\n  bool encontre;\r\n  n = mensaje.length();\r\n  m = (sizeof equivale \/ sizeof *equivale);\r\n  for( i = 0; i&lt;n; i++ ){\r\n    encontre = 0;\r\n    j=0;\r\n    while(j&lt;m and encontre==0){\r\n      if(toupper(mensaje[i]) == equivale[j].letra){\r\n        mensajemorse += equivale[j].codigo;\r\n        encontre=1;\r\n       }\r\n      j++;\r\n    }\r\n    mensajemorse += \" \"; \/\/separador de caracteres\r\n  }\r\n  return mensajemorse;  \r\n}\r\n<\/pre>\n<p><em><strong>Referencia<\/strong><\/em>: <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/advanced-io\/tone\/\">https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/advanced-io\/tone\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Se puede escuchar el tono del c\u00f3digo morse enviado a un Buzzer en el pin 9. Se a\u00f1ade al ejemplo anterior para un led la instrucci\u00f3n tone(PIN_BUZZER, NOTA, DURACION), que requiere PIN_BUZZER, la frecuencia de la NOTA para el sonido y la duraci\u00f3n. El pin de se\u00f1al S del Buzzer se conecta el Pin 9 &hellip; <a href=\"https:\/\/blog.espol.edu.ec\/edelros\/morse-codec-led-y-buzzer\/\" class=\"more-link\">Sigue leyendo <span class=\"screen-reader-text\">Morse Codec - LED y Buzzer<\/span><\/a><\/p>\n","protected":false},"author":8043,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1293406],"tags":[],"class_list":["post-1211","post","type-post","status-publish","format-standard","hentry","category-morse"],"_links":{"self":[{"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/posts\/1211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/users\/8043"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/comments?post=1211"}],"version-history":[{"count":2,"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/posts\/1211\/revisions"}],"predecessor-version":[{"id":1214,"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/posts\/1211\/revisions\/1214"}],"wp:attachment":[{"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/media?parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/categories?post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/edelros\/wp-json\/wp\/v2\/tags?post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}