{"id":317,"date":"2014-11-04T17:56:00","date_gmt":"2014-11-04T17:56:00","guid":{"rendered":"http:\/\/fabriquilla25.com\/?p=317"},"modified":"2015-04-29T19:04:56","modified_gmt":"2015-04-29T19:04:56","slug":"curso-de-introduccion-a-arduino-2a-sesion","status":"publish","type":"post","link":"http:\/\/fabriquilla25.com\/?p=317","title":{"rendered":"Curso de introducci\u00f3n a Arduino. 2a sesi\u00f3n"},"content":{"rendered":"<p>La segunda sesi\u00f3n del curso Introducci\u00f3n a Arduino se ha celebrado el lunes 3 de noviembre.<\/p>\n<p>Durante esta sesi\u00f3n hemos empezado descubriendo el c\u00f3digo de programaci\u00f3n de Arduino. Variables, operadores l\u00f3gicos, estructuras de control&#8230;. tan s\u00f3lo una peque\u00f1a descripci\u00f3n para saber que nos vamos a encontrar conforme avance el curso. Hemos usado una gu\u00eda que los chicos de <a href=\"http:\/\/www.ardumania.es\/\" target=\"_blank\">www.ardumania.es<\/a> se han encargado de traducir al castellano. \u00a1Gracias!. Pod\u00e9is encontrarla en el siguiente enlace:<\/p>\n<div style=\"text-align: center;\"><a href=\"http:\/\/www.ardumania.es\/wp-content\/uploads\/2011\/10\/Arduino_programing_notebook_ES.pdf\" target=\"_blank\">Arduino programming notebook<\/a><\/div>\n<p>Durante el curso alguien me pregunt\u00f3 d\u00f3nde obtener gu\u00edas y ejercicios. Aparte de Google, hay bastante documentaci\u00f3n en castellano en el <a href=\"http:\/\/playground.arduino.cc\/Es\/Guias\" target=\"_blank\">&#8220;playground&#8221; de arduino<\/a><\/p>\n<p>La presentaci\u00f3n tambi\u00e9n est\u00e1 disponible p\u00fablicamente:<\/p>\n<div style=\"text-align: center;\"><a href=\"http:\/\/prezi.com\/2rhxvdi_gpvg\/?utm_campaign=share&amp;utm_medium=copy\" target=\"_blank\">Presentaci\u00f3n 2\u00aa sesi\u00f3n curso Introducci\u00f3n a Arduino<\/a><\/div>\n<p>En la parte pr\u00e1ctica pusimos en com\u00fan los ejemplos de la sesi\u00f3n anterior y controlamos un led de forma pseudo-anal\u00f3gica usando una salida PWM haciendo variar su intensidad por medio de un potenci\u00f3metro. El montaje es de la siguiente imagen:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/192.168.1.88\/wordpress\/wp-content\/uploads\/2014\/11\/Arduino-2Bpotentiometr.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" border=\"0\" src=\"http:\/\/192.168.1.88\/wordpress\/wp-content\/uploads\/2014\/11\/Arduino-2Bpotentiometr.jpg\" height=\"400\" width=\"281\" \/><\/a><\/div>\n<p>Y el c\u00f3digo utilizado:<\/p>\n<p><span style=\"font-family: Courier New, Courier, monospace;\">int led = 6;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">int i;<\/span><\/p>\n<p><span style=\"font-family: Courier New, Courier, monospace;\">void setup(){<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; Serial.begin(9600);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; pinMode(led, OUTPUT);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">}<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace;\">void loop(){<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; int sensorValue = analogRea(A0);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; i = map(sensorValue, 0, 1023, 0, 255);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; Serial.println(i);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; analogWrite(led, i);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; delay(10);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">}<\/span><\/p>\n<p>En la siguiente pr\u00e1ctica el objetivo era aprender a utilizar una librer\u00eda. Hicimos un montaje t\u00edpico de bot\u00f3n para contar las pulsaciones:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/192.168.1.88\/wordpress\/wp-content\/uploads\/2014\/11\/FGFS_basics_pushbutton.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" border=\"0\" src=\"http:\/\/192.168.1.88\/wordpress\/wp-content\/uploads\/2014\/11\/FGFS_basics_pushbutton.jpg\" height=\"320\" width=\"640\" \/><\/a><\/div>\n<p>Y escribimos un c\u00f3digo simple:<\/p>\n<p><span style=\"font-family: Courier New, Courier, monospace;\">\/\/ contador de pulsaciones<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">\/\/ incrementa un contador en funcion de las pulsaciones recibidas por un boton<\/span><\/p>\n<p><span style=\"font-family: Courier New, Courier, monospace;\">int cont = 0;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace;\">void setup(){<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; pinMode(7, INPUT);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; Serial.begin(9600);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace;\">}<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace;\">void loop(){<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; if (digitalRead(7) == HIGH){<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; cont = cont + 1;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; }<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; Serial.println(cont);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; delay(50);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">}<\/span><\/p>\n<p>En seguida los primeros en acabar el montaje empezaron a preguntarse por qu\u00e9 aquello no funcionaba bien. Fu\u00e9 el momento de explicar el efecto del rebote debido a las peque\u00f1as micro-variacines que se producen al pulsar un interruptor<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/192.168.1.88\/wordpress\/wp-content\/uploads\/2014\/11\/Bouncy_Switch.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" border=\"0\" src=\"http:\/\/192.168.1.88\/wordpress\/wp-content\/uploads\/2014\/11\/Bouncy_Switch.png\" height=\"180\" width=\"320\" \/><\/a><\/div>\n<p>Esto se puede evitar mediante c\u00f3digo (en Archivo -&gt; Ejemplos -&gt; Digital -&gt; Debounce) o mediante el uso de una librer\u00eda. Para ello usamos la librer\u00eda <a href=\"https:\/\/github.com\/blackhack\/ArduLibraries\/tree\/master\/EasyButton\" target=\"_blank\">EasyButton<\/a> (&nbsp;playground.arduino.cc\/Code\/EasyButton ). Para usarla primero descargamos la librer\u00eda y descomprimimos el archivo dentro de la carpeta de librer\u00edas de Arduino ( \/home\/sketches\/libraries en Linux \u00f3 C:\/ArchivosdePrograma\/Arduino\/libraries en Windows). Una vez descomprimida hemos de cerrar Arduino y volver a arrancar el programa para que los cambios tomen efecto. El c\u00f3digo del programa modificado para usar la librer\u00eda es:<\/p>\n<p><span style=\"font-family: Courier New, Courier, monospace;\">\/\/Sketch que incorpora antirebote usando la librera Easybutton<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace;\">#include &lt;EasyButton.h&gt;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace;\">int cont = 0;<\/span><br \/><span style=\"font-family: 'Courier New', Courier, monospace;\">EasyButton button(2);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace;\">void setup()&nbsp;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">{<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; Serial.begin(9600);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">}<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace;\">void loop(){<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; button.update();<\/span><br \/><span style=\"font-family: 'Courier New', Courier, monospace;\">&nbsp; if (button.IsPushed()){<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; &nbsp; cont = cont + 1;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; }<\/span><br \/><span style=\"font-family: 'Courier New', Courier, monospace;\">&nbsp; Serial.println(cont);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp; delay(50);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\">}<\/span><\/p>\n<p>Una vez cargado iniciamos el monitor serie y vemos que ahora cuenta correctamente las pulsaciones.<\/p>\n<p>La \u00faltima pr\u00e1ctica del d\u00eda consisti\u00f3 en elaborar un term\u00f3metro. La peque\u00f1a presentaci\u00f3n &nbsp;del proyecto es accesible a trav\u00e9s del enlace:<\/p>\n<div style=\"text-align: center;\"><a href=\"http:\/\/prezi.com\/funhriewmmcp\/?utm_campaign=share&amp;utm_medium=copy\" target=\"_blank\">Term\u00f3metro<\/a><\/div>\n<p>El montaje es el siguiente:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/192.168.1.88\/wordpress\/wp-content\/uploads\/2014\/11\/temperature_tmp36fritz.gif\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" border=\"0\" src=\"http:\/\/192.168.1.88\/wordpress\/wp-content\/uploads\/2014\/11\/temperature_tmp36fritz.gif\" height=\"326\" width=\"640\" \/><\/a><\/div>\n<p>El servo se conecta: rojo a 5V, negro a Gnd y blanco (se\u00f1al) al pin 9. El c\u00f3digo es el siguiente:<\/p>\n<p><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">\/\/TMP36 Sensor de temperatura<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">int sensorPin = 0; &nbsp; \/\/conectaremos el pin de datos al pin A0<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/La resolucin es de &nbsp;10 mV \/ grado centigrado&nbsp;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/0\u00baC son 500mV&nbsp;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">int angulo = 0; &nbsp; &nbsp; &nbsp;\/\/a\u00f1adimos una variable para controlar el servo<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">#include &lt;Servo.h&gt; &nbsp; \/\/ Incluimos la libreria Servo<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">Servo nuestroServo; &nbsp;\/\/ Generamos el elemento nuestroServo<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\"><br \/><\/span><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">void setup(){ &nbsp; &nbsp; &nbsp; &nbsp;\/\/Este bucle se ejecuta 1 sola vez<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp; Serial.begin(9600);\/\/Inicia el puerto serie para que podamos usarlo<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp; nuestroServo.attach(9); \/\/ indicamos el pin al que esta conectado el servo<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">}<\/span><\/p>\n<p><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">void loop(){ &nbsp;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;\/\/leemos el pin<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;int lectura = analogRead(sensorPin); &nbsp;<\/span><br \/><span style=\"font-family: 'Courier New', Courier, monospace; font-size: x-small;\"><br \/><\/span><span style=\"font-family: 'Courier New', Courier, monospace; font-size: x-small;\">&nbsp;\/\/ convertimos la lectura a voltaje<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;float voltaje = lectura * 5.0;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;voltaje \/= 1024.0;&nbsp;<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;angulo = map(lectura, 0, 1023, 0, 179);<\/span><\/p>\n<p><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;\/\/ imprimimos el voltaje<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;Serial.print(voltaje); Serial.println(&#8221; voltios&#8221;);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;nuestroServo.write(angulo);<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;\/\/ imprimimos la temperatura<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">&nbsp;float temperaturaC = (voltaje &#8211; 0.5) * 100 ; &nbsp;\/\/10 mv por grado con 500 mV de salto<\/span><br \/><span style=\"font-family: 'Courier New', Courier, monospace; font-size: x-small;\">&nbsp;Serial.print(temperaturaC); Serial.println(&#8221; grados C&#8221;);<\/span><br \/><span style=\"font-size: x-small;\"><span style=\"font-family: Courier New, Courier, monospace;\">&nbsp;<\/span><span style=\"font-family: 'Courier New', Courier, monospace;\">&nbsp;delay(1000); &nbsp; &nbsp; &nbsp; &nbsp;\/\/esperamos 1 segundo<\/span><\/span><br \/><span style=\"font-family: Courier New, Courier, monospace; font-size: x-small;\">}<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><span style=\"font-family: inherit;\">Ojo: Este programa est\u00e1 modificado, el que mostr\u00e9 ten\u00eda un peque\u00f1o error. Ahora el servo responde a cambios de temperatura.<\/span><br \/><span style=\"font-family: inherit;\"><br \/><\/span><span style=\"font-family: inherit;\">Y eso es todo. Hasta la tercera sesi\u00f3n \ud83d\ude09<\/span><br \/><span style=\"font-family: Courier New, Courier, monospace;\"><br \/><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>La segunda sesi\u00f3n del curso Introducci\u00f3n a Arduino se ha celebrado el lunes 3 de noviembre. Durante esta sesi\u00f3n hemos empezado descubriendo el c\u00f3digo de programaci\u00f3n de Arduino. Variables, operadores l\u00f3gicos, estructuras de control&#8230;. tan s\u00f3lo una peque\u00f1a descripci\u00f3n para saber que nos vamos a encontrar conforme avance el curso. Hemos usado una gu\u00eda que [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":340,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,2],"tags":[],"_links":{"self":[{"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=\/wp\/v2\/posts\/317"}],"collection":[{"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317"}],"version-history":[{"count":1,"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions"}],"predecessor-version":[{"id":412,"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions\/412"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=\/wp\/v2\/media\/340"}],"wp:attachment":[{"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/fabriquilla25.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}