{"id":1845,"date":"2014-07-11T18:07:00","date_gmt":"2014-07-11T23:07:00","guid":{"rendered":"http:\/\/linuxitos.com\/main\/punteros-en-programas-arduino\/"},"modified":"2014-07-11T18:07:00","modified_gmt":"2014-07-11T23:07:00","slug":"punteros-en-programas-arduino","status":"publish","type":"post","link":"https:\/\/linuxitos.com\/main\/punteros-en-programas-arduino\/","title":{"rendered":"Punteros en programas arduino"},"content":{"rendered":"<div style=\"text-align: justify;\">Recientemente realic\u00e9 un proyecto con arduino. Necesitaba que al tener presionado un bot\u00f3n desde el protoboard conectado a un pin de arduino se encendiera un led, pero que \u00fanicamente validara el primer pulso sobre el bot\u00f3n.<\/div>\n<div style=\"text-align: justify;\">Un programa en arduino se ejecuta en loop, es decir en un ciclo infinito, para detener un proceso se usan retardos, esto con la funci\u00f3n delay(100), el par\u00e1metro que recibe son el total de milisengundos que se retardar\u00e1 le programa.<\/div>\n<div style=\"text-align: justify;\">El problema que tuve en primer lugar es c\u00f3mo validar \u00fanicamente el primer pulso, e ignorar los dem\u00e1s pulsos, lo que ocurre es que arduino sensa los pines cada 100 milisegundos, al menos ese es el tiempo que yo le d\u00ed, entonces cada 100 ms realizaba la misma acci\u00f3n, algo que no quer\u00eda.<\/div>\n<div style=\"text-align: justify;\">EL segundo problema fue usar apuntadores.<\/div>\n<div style=\"text-align: justify;\">La soluci\u00f3n qued\u00f3 de la siguiente manera:<br \/>Primero vea el circuito y el material que us\u00e9 para esta parte:<\/div>\n<div style=\"text-align: justify;\">\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/4.bp.blogspot.com\/-Y73IG6uQMs0\/U8Bj05mpc4I\/AAAAAAAAAIU\/j3AK8yNDwSU\/s1600\/unicopulso.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" border=\"0\" src=\"http:\/\/4.bp.blogspot.com\/-Y73IG6uQMs0\/U8Bj05mpc4I\/AAAAAAAAAIU\/j3AK8yNDwSU\/s1600\/unicopulso.png\" height=\"185\" title=\"\" width=\"400\" \/><\/a><\/div>\n<div style=\"clear: both; text-align: center;\"><\/div>\n<div style=\"clear: both; text-align: justify;\">Ah\u00ed pueden ver los materiales usados, as\u00ed como la conexi\u00f3n de cada componente. La idea es que cuando se presione un bot\u00f3n se encienda el led de salida, solo tomando en cueta el primer pulso, es decir, tanto en pantalla como en el resultado solo mostrar\u00e1 un uno s\u00f3lo una vez.<\/div>\n<div style=\"clear: both; text-align: justify;\">El c\u00f3digo es el siguiente:<\/div>\n<div style=\"clear: both; text-align: justify;\"><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><span style=\"color: orange;\">const int<\/span> buttonPinEntrada1 = 2;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><span style=\"color: orange;\">const int<\/span> buttonPinEntrada2 = 3;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><span style=\"color: orange;\">const int<\/span> ledPinSalida1 = &nbsp;8;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><span style=\"color: orange;\">const int<\/span> ledPinSalida2 = &nbsp;9;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"color: orange; font-family: Verdana, sans-serif;\">int<\/span><span style=\"font-family: Verdana, sans-serif;\">&nbsp;buttonState1 = 0;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"color: orange; font-family: Verdana, sans-serif;\">int<\/span><span style=\"font-family: Verdana, sans-serif;\">&nbsp;buttonState2 = 0;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"color: orange; font-family: Verdana, sans-serif;\">int<\/span><span style=\"font-family: Verdana, sans-serif;\">&nbsp;bBoton1=0;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"color: orange; font-family: Verdana, sans-serif;\">int<\/span><span style=\"font-family: Verdana, sans-serif;\">&nbsp;bBoton2=0;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"color: orange; font-family: Verdana, sans-serif;\">int<\/span><span style=\"font-family: Verdana, sans-serif;\">&nbsp;*band;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><br \/><\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><span style=\"color: orange;\">void<\/span> <span style=\"color: orange;\"><b>setup<\/b><\/span>(){<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; <b><span style=\"color: orange;\">Serial<\/span><\/b><span style=\"color: orange;\">.begin<\/span>(9600);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; <span style=\"color: orange;\">pinMode<\/span>(ledPinSalida1, <span style=\"color: blue;\">OUTPUT<\/span>);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp;&nbsp;<\/span><span style=\"color: orange; font-family: Verdana, sans-serif;\">pinMode<\/span><span style=\"font-family: Verdana, sans-serif;\">(ledPinSalida2, <span style=\"color: blue;\">OUTPUT<\/span>);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp;&nbsp;<\/span><span style=\"color: orange; font-family: Verdana, sans-serif;\">pinMode<\/span><span style=\"font-family: Verdana, sans-serif;\">(buttonPinEntrada1, <span style=\"color: blue;\">INPUT<\/span>);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp;&nbsp;<\/span><span style=\"color: orange; font-family: Verdana, sans-serif;\">pinMode<\/span><span style=\"font-family: Verdana, sans-serif;\">(buttonPinEntrada2, <span style=\"color: blue;\">INPUT<\/span>);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">}<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><br \/><\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><span style=\"color: #b45f06;\">void<\/span> <span style=\"color: orange;\"><b>loop<\/b><\/span>(){<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; \/*boton 1*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; buttonState1 = <span style=\"color: orange;\">digitalRead<\/span>(buttonPinEntrada1);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; band=&amp;bBoton1;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; validarUnicoPulsoBoton(ledPinSalida1,band,buttonState1);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; \/*boton 2*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; buttonState2 = <span style=\"color: orange;\">digitalRead<\/span>(buttonPinEntrada2);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; band=&amp;bBoton2;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; validarUnicoPulsoBoton(ledPinSalida2,band,buttonState2);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; <span style=\"color: orange;\">delay<\/span>(100);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">}<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><br \/><\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">\/*procedimiento que se usa para validar una sola pulsacion de un boton*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\"><span style=\"color: orange;\">void<\/span> validarUnicoPulsoBoton(<span style=\"color: orange;\">int<\/span> ledPinSalida,<\/span><span style=\"color: orange; font-family: Verdana, sans-serif;\">int<\/span><span style=\"font-family: Verdana, sans-serif;\">&nbsp;*band,<\/span><span style=\"color: orange; font-family: Verdana, sans-serif;\">int<\/span><span style=\"font-family: Verdana, sans-serif;\">&nbsp;buttonState){<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; \/*se verifica si el estado del buton es igual a uno, es decir, si esta presionado, de tal forma que si el boton se mantiene presionado<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; por mucho tiempo, pasara la siguiente condicion, esto evita que se realize la misma accion muchas veces*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; <span style=\"color: orange;\">if<\/span>(buttonState == 1){<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; \/*se veirifica si la bandera de ese boton esta desactivada, lo que significa que no se ha realizado ninguna accion<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; tras a ver pulsado dicho boton*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; <span style=\"color: orange;\">if<\/span>(*band == 0){<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; \/*se realiza la accion, en este caso se enciende un led*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; <span style=\"color: orange;\">digitalWrite<\/span>(ledPinSalida, HIGH);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; \/*se actualiza la bandera para indicar que la accion ya se ha realizado al menos una vez*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; *band=1;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; \/*se imprime en pantalla el valor de la bandera, solo para asegurar en que estado se encuentra (opcional)*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; <span style=\"color: orange;\"><b>Serial<\/b><\/span>.<span style=\"color: orange;\">println<\/span>(*band);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; }<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; }<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; \/*se verifica si el estado del boton es igual a cero, esto indicara que el boton ha cambiado de valor, presionado a no presionado*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp;&nbsp;<\/span><span style=\"color: orange; font-family: Verdana, sans-serif;\">if<\/span><span style=\"font-family: Verdana, sans-serif;\">(buttonState == 0){<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; \/*se valida que la bandera sea igual a 1, indica que la accion que se realiza cuando es presionado el boton se ha realizado al menos<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; una vez*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp;&nbsp;<\/span><span style=\"color: orange; font-family: Verdana, sans-serif;\">if<\/span><span style=\"font-family: Verdana, sans-serif;\">(*band == 1){<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; \/*sin la bandera esta activada, en este caso se encendio el led, entonces es necesario apagarlo*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; <span style=\"color: orange;\">digitalWrite<\/span>(ledPinSalida, LOW);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; \/*se actuliza el valor de la bandera, para indicar que el &nbsp;estado del boton ha sido actualizado*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; *band=0;<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; \/*opcional se imprime el nuevo valor de la bandera*\/<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; &nbsp; <span style=\"color: orange;\"><b>Serial<\/b><\/span>.<span style=\"color: orange;\">println<\/span>(*band);<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; &nbsp; }<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">&nbsp; }<\/span><\/div>\n<div style=\"clear: both;\"><span style=\"font-family: Verdana, sans-serif;\">}<\/span><\/div>\n<p>Y eso es todo, buena suerte.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Recientemente realic\u00e9 un proyecto con arduino. Necesitaba que al tener presionado un bot\u00f3n desde el protoboard conectado a un pin de arduino se encendiera un led, pero que \u00fanicamente validara el primer pulso sobre el bot\u00f3n. Un programa en arduino se ejecuta en loop, es decir en un ciclo infinito, para detener un proceso se [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1507,606,76,7,1510,4,1509],"tags":[],"class_list":["post-1845","post","type-post","status-publish","format-standard","hentry","category-arduino","category-arduino-uno","category-c","category-fedora","category-lenguaje-c","category-linux","category-programacion-arduino"],"_links":{"self":[{"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/posts\/1845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/comments?post=1845"}],"version-history":[{"count":0,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/posts\/1845\/revisions"}],"wp:attachment":[{"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/media?parent=1845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/categories?post=1845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxitos.com\/main\/wp-json\/wp\/v2\/tags?post=1845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}