├── Ejercicio01-Blink └── Ejercicio01-Blink.ino ├── Ejercicio02-Funciones ├── Ejercicio02-Funciones.ino └── funciones.ino ├── Ejercicio03-Librerias ├── Ejercicio03-Librerias.ino └── funciones.h ├── Ejercicio04-DigitalReadSerial └── Ejercicio04-DigitalReadSerial.ino ├── Ejercicio05-Memoria ├── Memoria1 │ └── Memoria1.ino └── Memoria2 │ └── Memoria2.ino ├── Ejercicio06-ASCII └── Ejercicio06-ASCII.ino ├── Ejercicio07-dimmer └── Ejercicio07-dimmer.ino ├── Ejercicio07b-dimmer_mejorado └── Ejercicio07b-dimmer_mejorado.ino ├── Ejercicio08-pullup └── Ejercicio08-pullup.ino ├── Ejercicio08-pullup_interruptor └── Ejercicio08-pullup_interruptor.ino ├── Ejercicio08-pullup_interruptor_rebote └── Ejercicio08-pullup_interruptor_rebote.ino ├── Ejercicio08-pullup_tiempo └── Ejercicio08-pullup_tiempo.ino ├── Ejercicio09-cuentapulsos └── Ejercicio09-cuentapulsos.ino ├── Ejercicio09-cuentapulsos_mejorado └── Ejercicio09-cuentapulsos_mejorado.ino ├── Ejercicio10-tone ├── Ejercicio10-tone.ino └── pitches.h ├── Ejercicio11-Analog └── Ejercicio11-Analog.ino ├── Ejercicio12-Smoothing └── Ejercicio12-Smoothing.ino ├── Ejercicio13-sogatira └── Ejercicio13-sogatira.ino ├── Ejercicio14-Knock └── Ejercicio14-Knock.ino ├── Ejercicio15-Temperatura └── Ejercicio15-Temperatura.ino ├── Ejercicio15-Temperatura_con_histeresis └── Ejercicio15-Temperatura_con_histeresis.ino ├── Ejercicio16-TiltSensor └── Ejercicio16-TiltSensor.ino ├── Ejercicio17-CocheFantastico_1 └── Ejercicio17-CocheFantastico_1.ino ├── Ejercicio17-CocheFantastico_2 └── Ejercicio17-CocheFantastico_2.ino ├── Ejercicio17-CocheFantastico_3 └── Ejercicio17-CocheFantastico_3.ino ├── Ejercicio17-CocheFantastico_4 └── Ejercicio17-CocheFantastico_4.ino ├── Ejercicio18-strings ├── Ejercicio18-strings-old │ └── Ejercicio18-strings-old.ino ├── _1-StringConstructors │ └── _1-StringConstructors.ino ├── _2-StringCharacters │ └── _2-StringCharacters.ino ├── _3-StringComparisonOperators │ └── _3-StringComparisonOperators.ino ├── _4-StringCaseChanges │ └── _4-StringCaseChanges.ino ├── _5-String_vs_string │ └── _5-String_vs_string.ino ├── _6-interactivo │ └── _6-interactivo.ino └── _7-String_Numeros │ └── _7-String_Numeros.ino ├── Ejercicio19-BlinkSinDelay └── Ejercicio19-BlinkSinDelay.ino ├── Ejercicio20-Debug └── Ejercicio20-Debug.ino ├── Ejercicio21-MotorDC_1 ├── Ejercicio21-MotorDC_1.ino ├── Ejercicio21.fzz └── Ejercicio21_bb.png ├── Ejercicio21-MotorDC_2 └── Ejercicio21-MotorDC_2.ino ├── Ejercicio21-MotorDC_3 └── Ejercicio21-MotorDC_3.ino ├── Ejercicio22-ChatSerie └── Ejercicio22-ChatSerie.ino ├── Ejercicio22-ChatSerieMejorado └── Ejercicio22-ChatSerieMejorado.ino ├── Ejercicio23-LCD └── Ejercicio23-LCD.ino ├── Ejercicio24-Simon ├── Ejercicio24-Simon.ino └── Tone │ ├── Tone.cpp │ ├── Tone.h │ ├── changelog.txt │ ├── examples │ ├── DTMFTest │ │ └── DTMFTest.pde │ ├── RTTTL │ │ └── RTTTL.pde │ └── ToneTest │ │ └── ToneTest.pde │ └── keywords.txt ├── Ejercicio25-EthernetClient └── Ejercicio25-EthernetClient.ino ├── Ejercicio25-EthernetClient_DHCP └── Ejercicio25-EthernetClient_DHCP.ino ├── Ejercicio26-EthernetServer └── Ejercicio26-EthernetServer.ino ├── Ejercicio26-EthernetServer_DHCP └── Ejercicio26-EthernetServer_DHCP.ino ├── Ejercicio27-Boton └── Ejercicio27-Boton.ino ├── Ejercicio27-Boton_Mejorado └── Ejercicio27-Boton_Mejorado.ino ├── Ejercicio27-Boton_Mejorado2 └── Ejercicio27-Boton_Mejorado2.ino ├── Ejercicio27-Boton_Mejorado2_DHCP └── Ejercicio27-Boton_Mejorado2_DHCP.ino ├── Ejercicio27-Boton_Mejorado_DHCP └── Ejercicio27-Boton_Mejorado_DHCP.ino ├── Ejercicio27-Boton_Mejorado_UNO_WIFI └── Ejercicio27-Boton_Mejorado_UNO_WIFI.ino ├── Ejercicio27-Boton_Mejorado_WIFI └── Ejercicio27-Boton_Mejorado_WIFI.ino ├── Ejercicio28-ChatEthernet ├── Ejercicio28-ChatEthernet_cliente │ └── Ejercicio28-ChatEthernet_cliente.ino └── Ejercicio28-ChatEthernet_servidor │ └── Ejercicio28-ChatEthernet_servidor.ino ├── Ejercicio28-ChatEthernet_DHCP ├── Ejercicio28-ChatEthernet_cliente │ └── Ejercicio28-ChatEthernet_cliente.ino └── Ejercicio28-ChatEthernet_servidor │ └── Ejercicio28-ChatEthernet_servidor.ino ├── Ejercicio29-Webduino ├── Ejercicio29-Webduino.ino ├── helloWord.txt └── webDemo.txt ├── Ejercicio30-SD ├── Ejercicio30-SD.ino ├── SDwrite │ └── SDwrite.ino ├── dataLogger │ └── dataLogger.ino ├── dumpFile │ └── dumpFile.ino └── index.htm ├── Ejercicio31-MandaCorreo └── Ejercicio31-MandaCorreo.ino ├── Ejercicio32-EnviaDatos ├── Ejercicio32-EnviaDatos.ino └── grabaDatos.php ├── Ejercicio32-EnviaDatos_DHCP ├── Ejercicio32-EnviaDatos_DHCP.ino └── grabaDatos.php ├── Ejercicio32-EnviaDatos_aprendiendoarduino └── Ejercicio32-EnviaDatos_aprendiendoarduino.ino ├── Ejercicio32-EnviaDatos_aprendiendoarduino_DHCP └── Ejercicio32-EnviaDatos_aprendiendoarduino_DHCP.ino ├── Ejercicio33-I2C ├── enviar_I2C │ └── enviar_I2C.ino ├── enviar_I2C_slave │ └── enviar_I2C_slave.ino ├── recibir_I2C │ └── recibir_I2C.ino ├── recibir_I2C_master │ └── recibir_I2C_master.ino └── scanI2CBus │ └── scanI2CBus.ino ├── Ejercicio34-SPI ├── master │ └── master.ino ├── master_2 │ └── master_2.ino ├── slave │ └── slave.ino └── slave_2 │ └── slave_2.ino ├── Ejercicio35-EEPROM ├── EEPROM_Dump │ └── EEPROM_Dump.ino ├── Ejercicio35-EEPROM.ino ├── borraEEPROM │ └── borraEEPROM.ino ├── ejemploAnything │ ├── EEPROMAnything.h │ └── ejemploAnything.ino └── leeEEPROM │ └── leeEEPROM.ino ├── Ejercicio36-ArduinoISP └── Ejercicio36-ArduinoISP.ino ├── Ejercicio37-IoT └── readme.md ├── Ejercicio38-Firmata ├── Entendiendo_Firmata │ └── Entendiendo_Firmata.ino └── Python_Firmata │ ├── Medidor_Luz.py │ └── blink.py ├── Ejercicio39-NTP ├── NTP_Basic │ └── NTP_Basic.ino └── readme.md ├── Ejercicio39-NTP_DHCP ├── NTP_Basic │ └── NTP_Basic.ino └── readme.md ├── Ejercicio39-NTP_MKR1000 └── Ejercicio39-NTP_MKR1000.ino ├── Ejercicio40-Modulos_RF ├── VirtualWire │ ├── CHANGES │ ├── LICENSE │ ├── MANIFEST │ ├── Makefile │ ├── README │ ├── VirtualWire.cpp │ ├── VirtualWire.h │ ├── examples │ │ ├── client │ │ │ └── client.pde │ │ ├── receiver │ │ │ └── receiver.pde │ │ ├── server │ │ │ └── server.pde │ │ └── transmitter │ │ │ └── transmitter.pde │ ├── keywords.txt │ └── util │ │ └── crc16.h ├── receiver │ └── receiver.ino └── transmitter │ └── transmitter.ino ├── Ejercicio41-Bluetooth ├── HC05 │ └── HC05.ino ├── ardudroid │ └── ardudroid.ino └── basic_bluetooth │ └── basic_bluetooth.ino ├── Ejercicio42-Ajax ├── Ejercicio42-Ajax.ino └── web.html ├── Ejercicio42-Ajax_DHCP ├── Ejercicio42-Ajax_DHCP.ino └── web.html ├── Ejercicio43-RTC ├── Display_Hora │ └── Display_Hora.ino └── actualiza_hora_manual │ └── actualiza_hora_manual.ino ├── Ejercicio44-Test_Library ├── Test │ ├── Test.cpp │ ├── Test.h │ ├── examples │ │ └── doing_something │ │ │ └── doing_something.ino │ ├── keywords.txt │ └── readme.txt └── readme.md ├── Ejercicio45-Serial_Plotter ├── analogica │ └── analogica.ino └── ecuacion │ └── ecuacion.ino ├── Ejercicio46-Estructuras_de_Control └── Ejercicio46-Estructuras_de_Control.ino ├── Ejercicio47-Motor_Shield └── Ejercicio47-Motor_Shield.ino ├── Ejercicio48-Efectos_Led └── Ejercicio48-Efectos_Led.ino ├── Ejercicio49-Calibracion └── Ejercicio49-Calibracion.ino ├── Ejercicio50-DAQ_SD ├── Ejercicio50-DAQ_SD.ino ├── README.md └── dumpFile │ └── dumpFile.ino ├── Ejercicio51-Musica_StarWars └── Ejercicio51-Musica_StarWars.ino ├── Ejercicio52-Detectar_Alarma └── Ejercicio52-Detectar_Alarma.ino ├── Ejercicio52-Detectar_Alarma_Histeresis └── Ejercicio52-Detectar_Alarma_Histeresis.ino ├── Ejercicio53-Watchdog └── Ejercicio53-Watchdog.ino ├── Ejercicio54-MotorDC_Basico └── Ejercicio54-MotorDC_Basico.ino ├── Ejercicio55-Servo ├── Knob │ └── Knob.ino ├── Knob_Serial │ └── Knob_Serial.ino └── Sweep │ └── Sweep.ino ├── Ejercicio56-Libreria_Morse ├── Morse.cpp ├── Morse.h ├── examples │ └── SOS │ │ └── SOS.ino └── keywords.txt ├── Ejercicio57-Medidor_Condensadores └── Ejercicio57-Medidor_Condensadores.ino ├── Ejercicio58-Manipular_Registros ├── InternalTemperature │ └── InternalTemperature.ino └── showInfo │ └── showInfo.ino ├── Ejercicio59-Puertos_Digitales ├── Leer_pin │ └── Leer_pin.ino ├── Registos_PORT_simple │ └── Registos_PORT_simple.ino └── Velocidad_Read │ └── Velocidad_Read.ino ├── Ejercicio60-Barra_Desplazamiento └── Ejercicio60-Barra_Desplazamiento.ino ├── Ejercicio60-Barra_Desplazamiento_DHCP └── Ejercicio60-Barra_Desplazamiento_DHCP.ino ├── Ejercicio61-ESP8266 ├── conectar_wifi │ └── conectar_wifi.ino └── conexion_serial │ └── conexion_serial.ino ├── Ejercicio62-MPU6050 ├── MPU6050_DMP6_SERVO │ └── MPU6050_DMP6_SERVO.ino ├── advanced_values │ └── advanced_values.ino └── raw_values │ └── raw_values.ino ├── Ejercicio63-Uso_Libreria_NTP └── Ejercicio63-Uso_Libreria_NTP.ino ├── Ejercicio64-Interrupciones └── Ejercicio64-Interrupciones.ino ├── Ejercicio65-Thingspeak └── Ejercicio65-Thingspeak.ino ├── Ejercicio65-Thingspeak_DHCP └── Ejercicio65-Thingspeak.ino ├── Ejercicio66-Servo_IMU ├── IMU_1servo │ └── IMU_1servo.ino └── IMU_2servos │ └── IMU_2servos.ino ├── Ejercicio67-operacionesBitwise └── Ejercicio67-operacionesBitwise.ino ├── Ejercicio68-toCharArray └── Ejercicio68-toCharArray.ino ├── Ejercicio69-Split_String └── Ejercicio69-Split_String.ino ├── Ejercicio70-TMP36_Histeresis └── Ejercicio70-TMP36_Histeresis.ino ├── Ejercicio71-DHT11_Histeresis └── Ejercicio71-DHT11_Histeresis.ino ├── Otros ├── ArduinoExplorer │ ├── ArduinoExplorer.ino │ ├── ToneNotes.h │ └── avrinterruptnames.h ├── DHCP_Arduino │ └── DHCP_Arduino.ino ├── Ethernet Modificada │ ├── EthernetClient.cpp │ └── EthernetClient.h ├── Flaco_Multitarea │ └── Flaco_Multitarea.ino ├── Histeresis │ └── Histeresis.ino ├── IP_fija_Arduino │ └── IP_fija_Arduino.ino ├── Juego_arduino_Chus │ └── Juego_arduino_Chus.ino ├── Metodologia_Sketch │ ├── paso1 │ │ └── paso1.ino │ └── paso2 │ │ └── paso2.ino ├── Neopixel_contador │ └── Neopixel_contador.ino ├── README.md ├── Test_L298_Mario │ └── Test_L298_Mario.ino ├── bar │ └── bar.ino ├── compara_timers │ └── compara_timers.ino ├── control_motor_laura │ └── control_motor_laura.ino ├── ejemplo_modbus │ └── ejemplo_modbus.ino ├── ejemplo_sleep_arduino │ └── ejemplo_sleep_arduino.ino ├── entenderSerial │ └── entenderSerial.ino ├── example_time_edison │ └── example_time_edison.ino ├── llenarMemoria │ ├── README.md │ └── llenarMemoria.ino ├── timerlocalblink │ ├── Event.cpp │ ├── Event.h │ ├── Timer.cpp │ ├── Timer.h │ └── timerlocalblink.ino └── velocidadMemoria │ └── velocidadMemoria.ino └── README.md /Ejercicio01-Blink/Ejercicio01-Blink.ino: -------------------------------------------------------------------------------- 1 | // the setup function runs once when you press reset or power the board 2 | void setup() { 3 | // initialize digital pin 13 as an output. 4 | Serial.begin(9600); 5 | pinMode(13, OUTPUT); 6 | } 7 | 8 | // the loop function runs over and over again forever 9 | void loop() { 10 | digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) 11 | Serial.println("Encendido"); 12 | delay(1000); // wait for a second 13 | digitalWrite(13, LOW); // turn the LED off by making the voltage LOW 14 | Serial.println("Apagado"); 15 | delay(1000); // wait for a second 16 | } 17 | -------------------------------------------------------------------------------- /Ejercicio02-Funciones/Ejercicio02-Funciones.ino: -------------------------------------------------------------------------------- 1 | void setup() { 2 | Serial.begin(9600); 3 | pinMode(13, OUTPUT); 4 | } 5 | 6 | void loop() { 7 | digitalWrite(13, HIGH); 8 | enciendo(); 9 | delay(1000); 10 | digitalWrite(13, LOW); 11 | apago(); 12 | delay(1000); 13 | } 14 | -------------------------------------------------------------------------------- /Ejercicio02-Funciones/funciones.ino: -------------------------------------------------------------------------------- 1 | void enciendo() { 2 | Serial.println("Enciendo..."); 3 | } 4 | 5 | void apago(){ 6 | Serial.println("Apago..."); 7 | } 8 | -------------------------------------------------------------------------------- /Ejercicio03-Librerias/Ejercicio03-Librerias.ino: -------------------------------------------------------------------------------- 1 | #include "funciones.h" 2 | 3 | void setup() { 4 | Serial.begin(9600); 5 | pinMode(led, OUTPUT); 6 | } 7 | 8 | void loop() { 9 | digitalWrite(led, HIGH); 10 | enciendo(); 11 | delay(1000); 12 | digitalWrite(led, LOW); 13 | apago(); 14 | delay(1000); 15 | } 16 | -------------------------------------------------------------------------------- /Ejercicio03-Librerias/funciones.h: -------------------------------------------------------------------------------- 1 | int led = 13; 2 | void enciendo() { 3 | Serial.println("Enciendo..."); 4 | } 5 | 6 | void apago(){ 7 | Serial.println("Apago..."); 8 | } 9 | -------------------------------------------------------------------------------- /Ejercicio04-DigitalReadSerial/Ejercicio04-DigitalReadSerial.ino: -------------------------------------------------------------------------------- 1 | int pushButton = 2; 2 | 3 | 4 | void setup() { 5 | Serial.begin(9600); 6 | pinMode(pushButton, INPUT); 7 | } 8 | 9 | void loop() { 10 | int buttonState = digitalRead(pushButton); 11 | Serial.println(buttonState); 12 | delay(1); 13 | } -------------------------------------------------------------------------------- /Ejercicio05-Memoria/Memoria1/Memoria1.ino: -------------------------------------------------------------------------------- 1 | void setup(){ 2 | Serial.begin(9600); 3 | Serial.println("Comienza..."); 4 | } 5 | 6 | void loop(){ 7 | Serial.println("Arduino es una plataforma de hardware libre, basada en una placa con un microcontrolador y un entorno de desarrollo, diseñada para facilitar el uso"); 8 | Serial.println("de la electrónica en proyectos multidisciplinares.El hardware consiste en una placa con un microcontrolador Atmel AVR y puertos de entrada/salida.4"); 9 | Serial.println("Los microcontroladores más usados son el Atmega168, Atmega328, Atmega1280, ATmega8 por su sencillez y bajo coste que permiten el desarrollo de"); 10 | Serial.println("múltiples diseños. Por otro lado el software consiste en un entorno de desarrollo que implementa el lenguaje de programación Processing/Wiring"); 11 | Serial.println("y el cargador de arranque que es ejecutado en la placa.Desde octubre de 2012, Arduino se usa también con microcontroladoras CortexM3 de ARM de"); 12 | Serial.println("32 bits,5 que coexistirán con las más limitadas, pero también económicas AVR de 8 bits. ARM y AVR no son plataformas compatibles a nivel binario"); 13 | Serial.println(", pero se pueden programar con el mismo IDE de Arduino y hacerse programas que compilen sin cambios en las dos plataformas. Eso sí, las microcontroladoras"); 14 | Serial.println("CortexM3 usan 3,3V, a diferencia de la mayoría de las placas con AVR que generalmente usan 5V. Sin embargo ya anteriormente se lanzaron placas Arduino"); 15 | Serial.println("con Atmel AVR a 3,3V como la Arduino Fio y existen compatibles de Arduino Nano y Pro como Meduino en que se puede conmutar el voltaje."); 16 | 17 | Serial.println(freeRam()); 18 | 19 | delay(5000); 20 | } 21 | 22 | int freeRam () 23 | { 24 | extern int __heap_start, *__brkval; 25 | int v; 26 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 27 | } 28 | -------------------------------------------------------------------------------- /Ejercicio05-Memoria/Memoria2/Memoria2.ino: -------------------------------------------------------------------------------- 1 | void setup(){ 2 | Serial.begin(9600); 3 | } 4 | 5 | void loop(){ 6 | Serial.println(F("Arduino es una plataforma de hardware libre, basada en una placa con un microcontrolador y un entorno de desarrollo, diseñada para facilitar el uso")); 7 | Serial.println(F("de la electrónica en proyectos multidisciplinares.El hardware consiste en una placa con un microcontrolador Atmel AVR y puertos de entrada/salida.4")); 8 | Serial.println(F("Los microcontroladores más usados son el Atmega168, Atmega328, Atmega1280, ATmega8 por su sencillez y bajo coste que permiten el desarrollo de")); 9 | Serial.println(F("múltiples diseños. Por otro lado el software consiste en un entorno de desarrollo que implementa el lenguaje de programación Processing/Wiring")); 10 | Serial.println(F("y el cargador de arranque que es ejecutado en la placa.Desde octubre de 2012, Arduino se usa también con microcontroladoras CortexM3 de ARM de")); 11 | Serial.println(F("32 bits,5 que coexistirán con las más limitadas, pero también económicas AVR de 8 bits. ARM y AVR no son plataformas compatibles a nivel binario")); 12 | Serial.println(F(", pero se pueden programar con el mismo IDE de Arduino y hacerse programas que compilen sin cambios en las dos plataformas. Eso sí, las microcontroladoras")); 13 | Serial.println(F("CortexM3 usan 3,3V, a diferencia de la mayoría de las placas con AVR que generalmente usan 5V. Sin embargo ya anteriormente se lanzaron placas Arduino")); 14 | Serial.println(F("con Atmel AVR a 3,3V como la Arduino Fio y existen compatibles de Arduino Nano y Pro como Meduino en que se puede conmutar el voltaje.")); 15 | 16 | Serial.println(freeRam()); 17 | 18 | delay(5000); 19 | } 20 | 21 | int freeRam () 22 | { 23 | extern int __heap_start, *__brkval; 24 | int v; 25 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 26 | } 27 | -------------------------------------------------------------------------------- /Ejercicio06-ASCII/Ejercicio06-ASCII.ino: -------------------------------------------------------------------------------- 1 | void setup() { 2 | Serial.begin(9600); 3 | Serial.println("ASCII Table ~ Character Map"); 4 | } 5 | 6 | int thisByte = 0; 7 | 8 | void loop() { 9 | Serial.write(thisByte); 10 | Serial.print(", dec: "); 11 | Serial.print(thisByte); 12 | Serial.print(", hex: "); 13 | Serial.print(thisByte, HEX); 14 | Serial.print(", oct: "); 15 | Serial.print(thisByte, OCT); 16 | Serial.print(", bin: "); 17 | Serial.println(thisByte, BIN); 18 | if(thisByte == 255) { 19 | while(true) { 20 | continue; 21 | } 22 | } 23 | // go on to the next character 24 | thisByte++; 25 | } 26 | -------------------------------------------------------------------------------- /Ejercicio07-dimmer/Ejercicio07-dimmer.ino: -------------------------------------------------------------------------------- 1 | 2 | const int ledPin = 9; 3 | 4 | void setup() 5 | { 6 | Serial.begin(9600); 7 | pinMode(ledPin, OUTPUT); 8 | } 9 | 10 | void loop() { 11 | byte brightness; 12 | if (Serial.available()) { 13 | brightness = Serial.read(); 14 | Serial.print("He leido por el puerto serie: "); 15 | Serial.print(brightness); 16 | Serial.print("\t---->\t"); 17 | Serial.write(brightness); 18 | Serial.println(); 19 | analogWrite(ledPin, brightness); 20 | delay(1000); 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Ejercicio07b-dimmer_mejorado/Ejercicio07b-dimmer_mejorado.ino: -------------------------------------------------------------------------------- 1 | 2 | const int ledPin = 9; 3 | 4 | void setup() 5 | { 6 | Serial.begin(9600); 7 | pinMode(ledPin, OUTPUT); 8 | } 9 | 10 | void loop() { 11 | byte brightness; 12 | String cadenaLeida; 13 | int dimmer; 14 | Serial.print("------------------------------------\r\nIntroduce un valor entre 0 y 100 para encender el led ---->>>> "); 15 | while (Serial.available() == 0){ 16 | //Ho hago nada 17 | } 18 | do{ 19 | char caracter_leido = Serial.read(); 20 | cadenaLeida += caracter_leido; 21 | delay(5); 22 | } while (Serial.available() > 0); 23 | Serial.print("He Leido la cadena: "); 24 | Serial.println(cadenaLeida); 25 | 26 | //Necesario para poder leer un 0 27 | if (cadenaLeida == "0") 28 | dimmer = 0; 29 | else{ 30 | dimmer = cadenaLeida.toInt(); 31 | (dimmer == 0)?dimmer = -1:dimmer = dimmer; //operador ternario 32 | } 33 | 34 | //Con operador ternario 35 | //(cadenaLeida == '0')?dimmer = 0:dimmer = cadenaLeida.toInt(); 36 | 37 | if (dimmer >= 0) { 38 | if (dimmer <= 100){ 39 | dimmer = map(dimmer, 0, 100, 0, 255); 40 | Serial.print("El valor a aplicar al LED es: "); 41 | Serial.println(dimmer); 42 | analogWrite(ledPin, dimmer); 43 | } 44 | else Serial.println("El valor es muy alto, solo es valido un numero entre 0 y 100"); 45 | } 46 | else Serial.println("No has introducido un numero"); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Ejercicio08-pullup/Ejercicio08-pullup.ino: -------------------------------------------------------------------------------- 1 | void setup(){ 2 | Serial.begin(9600); 3 | pinMode(2, INPUT_PULLUP); 4 | pinMode(13, OUTPUT); 5 | } 6 | 7 | void loop(){ 8 | int sensorVal = digitalRead(2); 9 | Serial.println(sensorVal); 10 | if (sensorVal == HIGH) { 11 | digitalWrite(13, LOW); 12 | } 13 | else { 14 | digitalWrite(13, HIGH); 15 | Serial.println("Pulsado..."); 16 | } 17 | } -------------------------------------------------------------------------------- /Ejercicio08-pullup_interruptor/Ejercicio08-pullup_interruptor.ino: -------------------------------------------------------------------------------- 1 | int estado_anterior; 2 | 3 | void setup() { 4 | Serial.begin(9600); 5 | pinMode(2, INPUT_PULLUP); //Boton 6 | pinMode(10, OUTPUT); //Led 7 | estado_anterior = digitalRead(2); //Inicializo con el estado del boton al iniciar programa 8 | } 9 | 10 | void loop() { 11 | int sensorVal = digitalRead(2); 12 | Serial.println(sensorVal); 13 | if (sensorVal != estado_anterior) { 14 | if (sensorVal == HIGH) { //Detecto flanco ascendente, poner LOW para detectar flanco descendente 15 | int estado_led = digitalRead(10); 16 | digitalWrite(10, !estado_led); 17 | } 18 | } 19 | estado_anterior = sensorVal; //Sin esto no funciona, actualizo para el siguente loop 20 | } 21 | -------------------------------------------------------------------------------- /Ejercicio08-pullup_interruptor_rebote/Ejercicio08-pullup_interruptor_rebote.ino: -------------------------------------------------------------------------------- 1 | int estado_anterior; 2 | int estado_boton; //Estado real del boton sin rebotes 3 | unsigned long lastDebounceTime = 0; // ultimo momento que el pin ha cambiado 4 | unsigned long debounceDelay = 50; // tiempo de rebote, aumentar si hay rebotes 5 | 6 | void setup() { 7 | Serial.begin(9600); 8 | pinMode(2, INPUT_PULLUP); //Boton 9 | pinMode(10, OUTPUT); //Led 10 | estado_anterior = digitalRead(2); //Inicializo con el estado del boton al iniciar programa 11 | estado_boton = estado_anterior; 12 | } 13 | 14 | void loop() { 15 | int sensorVal = digitalRead(2); 16 | Serial.println(sensorVal); 17 | if (sensorVal != estado_anterior) { 18 | lastDebounceTime = millis(); //momento en que detecto un cambio de estado del boton 19 | } 20 | 21 | if ((millis() - lastDebounceTime) > debounceDelay) { 22 | //Cuando ha pasado el tiempo con una señal estable 23 | if (sensorVal != estado_boton) { 24 | estado_boton = sensorVal; 25 | if (estado_boton == HIGH) { //Detecto flanco ascendente, poner LOW para detectar flanco descendente 26 | int estado_led = digitalRead(10); 27 | digitalWrite(10, !estado_led); 28 | } 29 | } 30 | } 31 | estado_anterior = sensorVal; //Sin esto no funciona, actualizo para el siguente loop 32 | } 33 | -------------------------------------------------------------------------------- /Ejercicio08-pullup_tiempo/Ejercicio08-pullup_tiempo.ino: -------------------------------------------------------------------------------- 1 | unsigned long tiempo_loop; 2 | 3 | void setup(){ 4 | Serial.begin(9600); 5 | pinMode(2, INPUT_PULLUP); 6 | pinMode(13, OUTPUT); 7 | tiempo_loop = millis(); 8 | } 9 | 10 | void loop(){ 11 | Serial.print("Tiempo Loop = "); 12 | Serial.println(millis() - tiempo_loop); 13 | tiempo_loop = millis(); 14 | int sensorVal = digitalRead(2); 15 | Serial.println(sensorVal); 16 | if (sensorVal == HIGH) { 17 | digitalWrite(13, LOW); 18 | } 19 | else { 20 | digitalWrite(13, HIGH); 21 | Serial.println("Pulsado..."); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Ejercicio09-cuentapulsos/Ejercicio09-cuentapulsos.ino: -------------------------------------------------------------------------------- 1 | // this constant won't change: 2 | const int buttonPin = 2; // the pin that the pushbutton is attached to 3 | const int ledPin = 13; // the pin that the LED is attached to 4 | 5 | // Variables will change: 6 | int buttonPushCounter = 0; // counter for the number of button presses 7 | int buttonState = 0; // current state of the button 8 | int lastButtonState = 0; // previous state of the button 9 | 10 | void setup() { 11 | // initialize the button pin as a input: 12 | pinMode(buttonPin, INPUT_PULLUP); //Resistencia de pullup interna 13 | // initialize the LED as an output: 14 | pinMode(ledPin, OUTPUT); 15 | // initialize serial communication: 16 | Serial.begin(9600); 17 | } 18 | 19 | 20 | void loop() { 21 | // read the pushbutton input pin: 22 | buttonState = digitalRead(buttonPin); 23 | 24 | // compare the buttonState to its previous state 25 | if (buttonState != lastButtonState) { 26 | // if the state has changed, increment the counter 27 | if (buttonState == HIGH) { 28 | // if the current state is HIGH then the button 29 | // wend from off to on: 30 | buttonPushCounter++; 31 | Serial.println("on"); 32 | Serial.print("number of button pushes: "); 33 | Serial.println(buttonPushCounter); 34 | } 35 | else { 36 | // if the current state is LOW then the button 37 | // wend from on to off: 38 | Serial.println("off"); 39 | } 40 | } 41 | // save the current state as the last state, 42 | //for next time through the loop 43 | lastButtonState = buttonState; 44 | 45 | // turns on the LED every four button pushes by 46 | // checking the modulo of the button push counter. 47 | // the modulo function gives you the remainder of 48 | // the division of two numbers: 49 | if (buttonPushCounter % 4 == 0) { 50 | digitalWrite(ledPin, HIGH); 51 | } else { 52 | digitalWrite(ledPin, LOW); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Ejercicio09-cuentapulsos_mejorado/Ejercicio09-cuentapulsos_mejorado.ino: -------------------------------------------------------------------------------- 1 | // this constant won't change: 2 | const int buttonPin = 2; // the pin that the pushbutton is attached to 3 | const int ledPin = 13; // the pin that the LED is attached to 4 | 5 | // Variables will change: 6 | int buttonPushCounter = 0; // counter for the number of button presses 7 | int buttonState = 0; // current state of the button 8 | int lastButtonState = 0; // previous state of the button 9 | int ledState = 0; 10 | 11 | void setup() { 12 | // initialize the button pin as a input: 13 | pinMode(buttonPin, INPUT_PULLUP); //Resistencia de pullup interna 14 | // initialize the LED as an output: 15 | pinMode(ledPin, OUTPUT); 16 | // initialize serial communication: 17 | Serial.begin(9600); 18 | } 19 | 20 | 21 | void loop() { 22 | // read the pushbutton input pin: 23 | buttonState = digitalRead(buttonPin); 24 | 25 | // compare the buttonState to its previous state 26 | if (buttonState != lastButtonState) { 27 | // if the state has changed, increment the counter 28 | if (buttonState == HIGH) { 29 | // if the current state is HIGH then the button 30 | // wend from off to on: 31 | buttonPushCounter++; 32 | Serial.println("on"); 33 | Serial.print("number of button pushes: "); 34 | Serial.println(buttonPushCounter); 35 | 36 | digitalWrite(ledPin, !digitalRead(ledPin)); 37 | 38 | 39 | } 40 | else { 41 | // if the current state is LOW then the button 42 | // wend from on to off: 43 | Serial.println("off"); 44 | } 45 | 46 | // save the current state as the last state, 47 | //for next time through the loop 48 | lastButtonState = buttonState; 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Ejercicio10-tone/Ejercicio10-tone.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Melody 3 | 4 | Plays a melody 5 | 6 | circuit: 7 | * 8-ohm speaker on digital pin 8 8 | 9 | created 21 Jan 2010 10 | modified 30 Aug 2011 11 | by Tom Igoe 12 | 13 | This example code is in the public domain. 14 | 15 | http://arduino.cc/en/Tutorial/Tone 16 | 17 | */ 18 | #include "pitches.h" 19 | 20 | // notes in the melody: 21 | int melody[] = { 22 | NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; 23 | 24 | // note durations: 4 = quarter note, 8 = eighth note, etc.: 25 | int noteDurations[] = { 26 | 4, 8, 8, 4,4,4,4,4 }; 27 | 28 | void setup() { 29 | // iterate over the notes of the melody: 30 | for (int thisNote = 0; thisNote < 8; thisNote++) { 31 | 32 | // to calculate the note duration, take one second 33 | // divided by the note type. 34 | //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. 35 | int noteDuration = 1000/noteDurations[thisNote]; 36 | tone(8, melody[thisNote],noteDuration); 37 | 38 | // to distinguish the notes, set a minimum time between them. 39 | // the note's duration + 30% seems to work well: 40 | int pauseBetweenNotes = noteDuration * 1.30; 41 | delay(pauseBetweenNotes); 42 | // stop the tone playing: 43 | noTone(8); 44 | } 45 | } 46 | 47 | void loop() { 48 | // no need to repeat the melody. 49 | } 50 | -------------------------------------------------------------------------------- /Ejercicio10-tone/pitches.h: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | * Public Constants 3 | *************************************************/ 4 | 5 | #define NOTE_B0 31 6 | #define NOTE_C1 33 7 | #define NOTE_CS1 35 8 | #define NOTE_D1 37 9 | #define NOTE_DS1 39 10 | #define NOTE_E1 41 11 | #define NOTE_F1 44 12 | #define NOTE_FS1 46 13 | #define NOTE_G1 49 14 | #define NOTE_GS1 52 15 | #define NOTE_A1 55 16 | #define NOTE_AS1 58 17 | #define NOTE_B1 62 18 | #define NOTE_C2 65 19 | #define NOTE_CS2 69 20 | #define NOTE_D2 73 21 | #define NOTE_DS2 78 22 | #define NOTE_E2 82 23 | #define NOTE_F2 87 24 | #define NOTE_FS2 93 25 | #define NOTE_G2 98 26 | #define NOTE_GS2 104 27 | #define NOTE_A2 110 28 | #define NOTE_AS2 117 29 | #define NOTE_B2 123 30 | #define NOTE_C3 131 31 | #define NOTE_CS3 139 32 | #define NOTE_D3 147 33 | #define NOTE_DS3 156 34 | #define NOTE_E3 165 35 | #define NOTE_F3 175 36 | #define NOTE_FS3 185 37 | #define NOTE_G3 196 38 | #define NOTE_GS3 208 39 | #define NOTE_A3 220 40 | #define NOTE_AS3 233 41 | #define NOTE_B3 247 42 | #define NOTE_C4 262 43 | #define NOTE_CS4 277 44 | #define NOTE_D4 294 45 | #define NOTE_DS4 311 46 | #define NOTE_E4 330 47 | #define NOTE_F4 349 48 | #define NOTE_FS4 370 49 | #define NOTE_G4 392 50 | #define NOTE_GS4 415 51 | #define NOTE_A4 440 52 | #define NOTE_AS4 466 53 | #define NOTE_B4 494 54 | #define NOTE_C5 523 55 | #define NOTE_CS5 554 56 | #define NOTE_D5 587 57 | #define NOTE_DS5 622 58 | #define NOTE_E5 659 59 | #define NOTE_F5 698 60 | #define NOTE_FS5 740 61 | #define NOTE_G5 784 62 | #define NOTE_GS5 831 63 | #define NOTE_A5 880 64 | #define NOTE_AS5 932 65 | #define NOTE_B5 988 66 | #define NOTE_C6 1047 67 | #define NOTE_CS6 1109 68 | #define NOTE_D6 1175 69 | #define NOTE_DS6 1245 70 | #define NOTE_E6 1319 71 | #define NOTE_F6 1397 72 | #define NOTE_FS6 1480 73 | #define NOTE_G6 1568 74 | #define NOTE_GS6 1661 75 | #define NOTE_A6 1760 76 | #define NOTE_AS6 1865 77 | #define NOTE_B6 1976 78 | #define NOTE_C7 2093 79 | #define NOTE_CS7 2217 80 | #define NOTE_D7 2349 81 | #define NOTE_DS7 2489 82 | #define NOTE_E7 2637 83 | #define NOTE_F7 2794 84 | #define NOTE_FS7 2960 85 | #define NOTE_G7 3136 86 | #define NOTE_GS7 3322 87 | #define NOTE_A7 3520 88 | #define NOTE_AS7 3729 89 | #define NOTE_B7 3951 90 | #define NOTE_C8 4186 91 | #define NOTE_CS8 4435 92 | #define NOTE_D8 4699 93 | #define NOTE_DS8 4978 -------------------------------------------------------------------------------- /Ejercicio11-Analog/Ejercicio11-Analog.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Analog input, analog output, serial output 3 | 4 | Reads an analog input pin, maps the result to a range from 0 to 255 5 | and uses the result to set the pulsewidth modulation (PWM) of an output pin. 6 | Also prints the results to the serial monitor. 7 | 8 | The circuit: 9 | * potentiometer connected to analog pin 0. 10 | Center pin of the potentiometer goes to the analog pin. 11 | side pins of the potentiometer go to +5V and ground 12 | * LED connected from digital pin 9 to ground 13 | 14 | created 29 Dec. 2008 15 | modified 9 Apr 2012 16 | by Tom Igoe 17 | 18 | This example code is in the public domain. 19 | 20 | */ 21 | 22 | // These constants won't change. They're used to give names 23 | // to the pins used: 24 | const int analogInPin = A0; // Analog input pin that the potentiometer is attached to 25 | const int analogOutPin = 9; // Analog output pin that the LED is attached to 26 | 27 | int sensorValue = 0; // value read from the pot 28 | int outputValue = 0; // value output to the PWM (analog out) 29 | 30 | void setup() { 31 | // initialize serial communications at 9600 bps: 32 | Serial.begin(9600); 33 | } 34 | 35 | void loop() { 36 | // read the analog in value: 37 | sensorValue = analogRead(analogInPin); 38 | // map it to the range of the analog out: 39 | outputValue = map(sensorValue, 0, 1023, 0, 255); 40 | // change the analog out value: 41 | analogWrite(analogOutPin, outputValue); 42 | 43 | // print the results to the serial monitor: 44 | Serial.print("sensor = " ); 45 | Serial.print(sensorValue); 46 | Serial.print("\t output = "); 47 | Serial.print(outputValue); 48 | float voltage = sensorValue * (5.0 / 1023.0); 49 | Serial.print("\t voltage = "); 50 | Serial.println(voltage); 51 | 52 | // wait 2 milliseconds before the next loop 53 | // for the analog-to-digital converter to settle 54 | // after the last reading: 55 | delay(2); 56 | } -------------------------------------------------------------------------------- /Ejercicio14-Knock/Ejercicio14-Knock.ino: -------------------------------------------------------------------------------- 1 | /* Knock Sensor 2 | 3 | This sketch reads a piezo element to detect a knocking sound. 4 | It reads an analog pin and compares the result to a set threshold. 5 | If the result is greater than the threshold, it writes 6 | "knock" to the serial port, and toggles the LED on pin 13. 7 | 8 | The circuit: 9 | * + connection of the piezo attached to analog in 0 10 | * - connection of the piezo attached to ground 11 | * 1-megohm resistor attached from analog in 0 to ground 12 | 13 | http://www.arduino.cc/en/Tutorial/Knock 14 | 15 | created 25 Mar 2007 16 | by David Cuartielles 17 | modified 30 Aug 2011 18 | by Tom Igoe 19 | 20 | This example code is in the public domain. 21 | 22 | */ 23 | //Modified by 30/11/2014 24 | 25 | 26 | // these constants won't change: 27 | const int ledPin = 13; // led connected to digital pin 13 28 | const int knockSensor = A0; // the piezo is connected to analog pin 0 29 | const int threshold = 100; // threshold value to decide when the detected sound is a knock or not 30 | 31 | 32 | // these variables will change: 33 | int sensorReading = 0; // variable to store the value read from the sensor pin 34 | int ledState = LOW; // variable used to store the last LED status, to toggle the light 35 | 36 | void setup() { 37 | pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT 38 | Serial.begin(9600); // use the serial port 39 | } 40 | 41 | void loop() { 42 | // read the sensor and store it in the variable sensorReading: 43 | sensorReading = analogRead(knockSensor); 44 | 45 | // if the sensor reading is greater than the threshold: 46 | if (sensorReading >= threshold) { 47 | // toggle the status of the ledPin: 48 | ledState = !ledState; 49 | // update the LED pin itself: 50 | digitalWrite(ledPin, ledState); 51 | // send the string "Knock!" back to the computer, followed by newline 52 | Serial.println("Knock!"); 53 | } 54 | delay(100); // delay to avoid overloading the serial port buffer 55 | } 56 | -------------------------------------------------------------------------------- /Ejercicio15-Temperatura/Ejercicio15-Temperatura.ino: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jecrespo/Aprendiendo-Arduino/91fc9e97f379376ced35b1e9d1018852991ef112/Ejercicio15-Temperatura/Ejercicio15-Temperatura.ino -------------------------------------------------------------------------------- /Ejercicio15-Temperatura_con_histeresis/Ejercicio15-Temperatura_con_histeresis.ino: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jecrespo/Aprendiendo-Arduino/91fc9e97f379376ced35b1e9d1018852991ef112/Ejercicio15-Temperatura_con_histeresis/Ejercicio15-Temperatura_con_histeresis.ino -------------------------------------------------------------------------------- /Ejercicio16-TiltSensor/Ejercicio16-TiltSensor.ino: -------------------------------------------------------------------------------- 1 | /* Hagamos un sistema autonomo que detecte cambios 2 | de inclinación y emita un sonido el piezo. 3 | */ 4 | 5 | #define NOTE_C6 1047 6 | 7 | // these constants won't change: 8 | const int PinSensor = 2; // Sensor de inclinación en el pin 2 9 | const int soundOutput = A0; // the piezo is connected to analog pin 0 10 | 11 | void setup() { 12 | pinMode(PinSensor, INPUT_PULLUP); 13 | Serial.begin(9600); // use the serial port 14 | } 15 | 16 | void loop() { 17 | // read the sensor and store it in the variable sensorReading: 18 | if (digitalRead(PinSensor)){ 19 | Serial.println("Sonido!!!!!!!"); 20 | tone(soundOutput,NOTE_C6); 21 | } 22 | else 23 | noTone(soundOutput); 24 | 25 | delay(100); 26 | } 27 | -------------------------------------------------------------------------------- /Ejercicio17-CocheFantastico_1/Ejercicio17-CocheFantastico_1.ino: -------------------------------------------------------------------------------- 1 | /* Knight Rider 1 2 | * -------------- 3 | * 4 | * Basically an extension of Blink_LED. 5 | * 6 | * 7 | * (cleft) 2005 K3, Malmo University 8 | * @author: David Cuartielles 9 | * @hardware: David Cuartielles, Aaron Hallborg 10 | * modifed by: aprendiendoarduino 11 | */ 12 | 13 | int pin2 = 2; 14 | int pin3 = 3; 15 | int pin4 = 4; 16 | int pin5 = 5; 17 | int pin6 = 6; 18 | int timer = 100; 19 | 20 | void setup(){ 21 | Serial.begin(9600); 22 | pinMode(pin2, OUTPUT); 23 | pinMode(pin3, OUTPUT); 24 | pinMode(pin4, OUTPUT); 25 | pinMode(pin5, OUTPUT); 26 | pinMode(pin6, OUTPUT); 27 | } 28 | 29 | void loop() { 30 | 31 | //timer = analogRead(A0); //El valor leido por analog read es el temporizador 32 | 33 | digitalWrite(pin2, HIGH); 34 | Serial.println("Enciendo primer led"); 35 | delay(timer); 36 | digitalWrite(pin2, LOW); 37 | delay(timer); 38 | 39 | digitalWrite(pin3, HIGH); 40 | Serial.println("Enciendo segundo led"); 41 | delay(timer); 42 | digitalWrite(pin3, LOW); 43 | delay(timer); 44 | 45 | digitalWrite(pin4, HIGH); 46 | Serial.println("Enciendo tercer led"); 47 | delay(timer); 48 | digitalWrite(pin4, LOW); 49 | delay(timer); 50 | 51 | digitalWrite(pin5, HIGH); 52 | Serial.println("Enciendo cuarto led"); 53 | delay(timer); 54 | digitalWrite(pin5, LOW); 55 | delay(timer); 56 | 57 | digitalWrite(pin6, HIGH); 58 | Serial.println("Enciendo quinto led"); 59 | delay(timer); 60 | digitalWrite(pin6, LOW); 61 | delay(timer); 62 | 63 | digitalWrite(pin5, HIGH); 64 | Serial.println("Enciendo cuarto led"); 65 | delay(timer); 66 | digitalWrite(pin5, LOW); 67 | delay(timer); 68 | 69 | digitalWrite(pin4, HIGH); 70 | Serial.println("Enciendo tercer led"); 71 | delay(timer); 72 | digitalWrite(pin4, LOW); 73 | delay(timer); 74 | 75 | digitalWrite(pin3, HIGH); 76 | Serial.println("Enciendo segundo led"); 77 | delay(timer); 78 | digitalWrite(pin3, LOW); 79 | delay(timer); 80 | } 81 | -------------------------------------------------------------------------------- /Ejercicio17-CocheFantastico_2/Ejercicio17-CocheFantastico_2.ino: -------------------------------------------------------------------------------- 1 | /* Knight Rider 2 2 | -------------- 3 | 4 | Reducing the amount of code using for(;;). 5 | 6 | 7 | (cleft) 2005 K3, Malmo University 8 | @author: David Cuartielles 9 | @hardware: David Cuartielles, Aaron Hallborg 10 | */ 11 | 12 | int pinArray[] = {2, 3, 4, 5, 6, 7}; 13 | int count = 0; 14 | int timer = 100; 15 | 16 | void setup() { 17 | // we make all the declarations at once 18 | for (count = 0; count < 6; count++) { 19 | pinMode(pinArray[count], OUTPUT); 20 | } 21 | } 22 | 23 | void loop() { 24 | //timer = analogRead(A0); //El valor leido por analog read es el temporizador 25 | for (count = 0; count < 6; count++) { 26 | //timer = analogRead(A0); 27 | digitalWrite(pinArray[count], HIGH); 28 | delay(timer); 29 | digitalWrite(pinArray[count], LOW); 30 | delay(timer); 31 | } 32 | for (count = 5; count >= 0; count--) { 33 | //timer = analogRead(A0); 34 | digitalWrite(pinArray[count], HIGH); 35 | delay(timer); 36 | digitalWrite(pinArray[count], LOW); 37 | delay(timer); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Ejercicio17-CocheFantastico_3/Ejercicio17-CocheFantastico_3.ino: -------------------------------------------------------------------------------- 1 | /* Knight Rider 3 2 | -------------- 3 | 4 | This example concentrates on making the visuals fluid. 5 | 6 | 7 | (cleft) 2005 K3, Malmo University 8 | @author: David Cuartielles 9 | @hardware: David Cuartielles, Aaron Hallborg 10 | */ 11 | 12 | int pinArray[] = {2, 3, 4, 5, 6, 7}; 13 | int count = 0; 14 | int timer = 30; 15 | 16 | void setup() { 17 | for (count = 0; count < 6; count++) { 18 | pinMode(pinArray[count], OUTPUT); 19 | } 20 | } 21 | 22 | void loop() { 23 | for (count = 0; count < 5; count++) { 24 | digitalWrite(pinArray[count], HIGH); 25 | delay(timer); 26 | digitalWrite(pinArray[count + 1], HIGH); 27 | delay(timer); 28 | digitalWrite(pinArray[count], LOW); 29 | delay(timer * 2); 30 | //timer = analogRead(A0); //El valor leido por analog read es el temporizador 31 | } 32 | for (count = 5; count > 0; count--) { 33 | digitalWrite(pinArray[count], HIGH); 34 | delay(timer); 35 | digitalWrite(pinArray[count - 1], HIGH); 36 | delay(timer); 37 | digitalWrite(pinArray[count], LOW); 38 | delay(timer * 2); 39 | //timer = analogRead(A0); //El valor leido por analog read es el temporizador 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Ejercicio17-CocheFantastico_4/Ejercicio17-CocheFantastico_4.ino: -------------------------------------------------------------------------------- 1 | /* Knight Rider 4 2 | -------------- 3 | 4 | Based on Knight Rider 2 but change timer received by serial port 5 | 6 | */ 7 | 8 | int pinArray[] = {2, 3, 4, 5, 6, 7}; 9 | int count = 0; 10 | int timer = 100; 11 | 12 | void setup() { 13 | 14 | Serial.begin(9600); 15 | for (count = 0; count < 6; count++) { 16 | pinMode(pinArray[count], OUTPUT); 17 | } 18 | } 19 | 20 | void loop() { 21 | for (count = 0; count < 6; count++) { 22 | if (Serial.available()) timer = lee_timer(); 23 | Serial.println("Valor de timer = " + String(timer)); 24 | digitalWrite(pinArray[count], HIGH); 25 | Serial.println("Led " + String(count) + " ENCENDIDO"); 26 | delay(timer); 27 | digitalWrite(pinArray[count], LOW); 28 | Serial.println("Led " + String(count) + " APAGADO"); 29 | delay(timer); 30 | } 31 | for (count = 5; count >= 0; count--) { 32 | if (Serial.available()) timer = lee_timer(); 33 | Serial.println("Valor de timer = " + String(timer)); 34 | digitalWrite(pinArray[count], HIGH); 35 | Serial.println("Led " + String(count) + " ENCENDIDO"); 36 | delay(timer); 37 | digitalWrite(pinArray[count], LOW); 38 | Serial.println("Led " + String(count) + " APAGADO"); 39 | delay(timer); 40 | } 41 | } 42 | 43 | int lee_timer() { 44 | String cadena_Leida = ""; 45 | do { 46 | char caracter_leido = Serial.read(); 47 | cadena_Leida += caracter_leido; 48 | delay(5); 49 | } while (Serial.available() > 0); 50 | 51 | int nuevo_timer = cadena_Leida.toInt(); 52 | 53 | if (nuevo_timer >= 10 && nuevo_timer <= 500) { 54 | Serial.println("Nuevo timer establecido a " + cadena_Leida + "segundos"); 55 | return nuevo_timer; 56 | } 57 | else { 58 | Serial.println("El valor del timer debe estar entre 10 y 500 ms, introduce un valor correcto"); 59 | return timer; 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /Ejercicio18-strings/_1-StringConstructors/_1-StringConstructors.ino: -------------------------------------------------------------------------------- 1 | /* 2 | String constructors 3 | 4 | Examples of how to create strings from other data types 5 | 6 | created 27 July 2010 7 | modified 30 Aug 2011 8 | by Tom Igoe 9 | 10 | http://www.arduino.cc/en/Tutorial/StringConstructors 11 | 12 | This example code is in the public domain. 13 | */ 14 | 15 | void setup() { 16 | // Open serial communications and wait for port to open: 17 | Serial.begin(9600); 18 | while (!Serial) { 19 | ; // wait for serial port to connect. Needed for native USB port only 20 | } 21 | 22 | // send an intro: 23 | Serial.println("\n\nString Constructors:"); 24 | Serial.println(); 25 | } 26 | 27 | void loop() { 28 | // using a constant String: 29 | String stringOne = "Hello String"; 30 | Serial.println(stringOne); // prints "Hello String" 31 | 32 | // converting a constant char into a String: 33 | stringOne = String('a'); 34 | Serial.println(stringOne); // prints "a" 35 | 36 | // converting a constant string into a String object: 37 | String stringTwo = String("This is a string"); 38 | Serial.println(stringTwo); // prints "This is a string" 39 | 40 | // concatenating two strings: 41 | stringOne = String(stringTwo + " with more"); 42 | // prints "This is a string with more": 43 | Serial.println(stringOne); 44 | 45 | // using a constant integer: 46 | stringOne = String(13); 47 | Serial.println(stringOne); // prints "13" 48 | 49 | // using an int and a base: 50 | stringOne = String(analogRead(A0), DEC); 51 | // prints "453" or whatever the value of analogRead(A0) is 52 | Serial.println(stringOne); 53 | 54 | // using an int and a base (hexadecimal): 55 | stringOne = String(45, HEX); 56 | // prints "2d", which is the hexadecimal version of decimal 45: 57 | Serial.println(stringOne); 58 | 59 | // using an int and a base (binary) 60 | stringOne = String(255, BIN); 61 | // prints "11111111" which is the binary value of 255 62 | Serial.println(stringOne); 63 | 64 | // using a long and a base: 65 | stringOne = String(millis(), DEC); 66 | // prints "123456" or whatever the value of millis() is: 67 | Serial.println(stringOne); 68 | 69 | //using a float and the right decimal places: 70 | stringOne = String(5.698, 3); 71 | Serial.println(stringOne); 72 | 73 | //using a float and less decimal places to use rounding: 74 | stringOne = String(5.698, 2); 75 | Serial.println(stringOne); 76 | 77 | // do nothing while true: 78 | while (true); 79 | 80 | } 81 | -------------------------------------------------------------------------------- /Ejercicio18-strings/_2-StringCharacters/_2-StringCharacters.ino: -------------------------------------------------------------------------------- 1 | /* 2 | String charAt() and setCharAt() 3 | 4 | Examples of how to get and set characters of a String 5 | 6 | created 27 July 2010 7 | modified 2 Apr 2012 8 | by Tom Igoe 9 | 10 | http://www.arduino.cc/en/Tutorial/StringCharacters 11 | 12 | This example code is in the public domain. 13 | */ 14 | 15 | void setup() { 16 | // Open serial communications and wait for port to open: 17 | Serial.begin(9600); 18 | while (!Serial) { 19 | ; // wait for serial port to connect. Needed for native USB port only 20 | } 21 | 22 | Serial.println("\n\nString charAt() and setCharAt():"); 23 | } 24 | 25 | void loop() { 26 | // make a string to report a sensor reading: 27 | String reportString = "SensorReading: 456"; 28 | Serial.println(reportString); 29 | 30 | // the reading's most significant digit is at position 15 in the reportString: 31 | char mostSignificantDigit = reportString.charAt(15); 32 | 33 | String message = "Most significant digit of the sensor reading is: "; 34 | Serial.println(message + mostSignificantDigit); 35 | 36 | // add blank space: 37 | Serial.println(); 38 | 39 | // you can alo set the character of a string. Change the : to a = character 40 | reportString.setCharAt(13, '='); 41 | Serial.println(reportString); 42 | 43 | // do nothing while true: 44 | while (true); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Ejercicio18-strings/_4-StringCaseChanges/_4-StringCaseChanges.ino: -------------------------------------------------------------------------------- 1 | /* 2 | String Case changes 3 | 4 | Examples of how to change the case of a string 5 | 6 | created 27 July 2010 7 | modified 2 Apr 2012 8 | by Tom Igoe 9 | 10 | http://www.arduino.cc/en/Tutorial/StringCaseChanges 11 | 12 | This example code is in the public domain. 13 | */ 14 | 15 | void setup() { 16 | // Open serial communications and wait for port to open: 17 | Serial.begin(9600); 18 | while (!Serial) { 19 | ; // wait for serial port to connect. Needed for native USB port only 20 | } 21 | 22 | // send an intro: 23 | Serial.println("\n\nString case changes:"); 24 | Serial.println(); 25 | } 26 | 27 | void loop() { 28 | // toUpperCase() changes all letters to upper case: 29 | String stringOne = ""; 30 | Serial.println(stringOne); 31 | stringOne.toUpperCase(); 32 | Serial.println(stringOne); 33 | 34 | // toLowerCase() changes all letters to lower case: 35 | String stringTwo = ""; 36 | Serial.println(stringTwo); 37 | stringTwo.toLowerCase(); 38 | Serial.println(stringTwo); 39 | 40 | 41 | // do nothing while true: 42 | while (true); 43 | } 44 | -------------------------------------------------------------------------------- /Ejercicio18-strings/_5-String_vs_string/_5-String_vs_string.ino: -------------------------------------------------------------------------------- 1 | /* 2 | String vs string 3 | */ 4 | 5 | String stringOne, stringTwo; 6 | char cadenaOne[] = {'t', 'h', 'i', 's', '\0'}; 7 | char cadenaTwo[] = "that"; 8 | 9 | void setup() { 10 | Serial.begin(9600); 11 | stringOne = String("this"); 12 | stringTwo = String("that"); 13 | } 14 | 15 | void loop() { 16 | //Dos Strings Iguales// 17 | //Strings 18 | if (stringOne == "this") { 19 | Serial.println("StringOne == \"this\""); 20 | } 21 | 22 | //cadenas 23 | if (cadenaOne == "this") { //Al compilar de un warning 24 | Serial.println("cadenaOne == \"this\""); 25 | } 26 | else Serial.println("Esta comparacion no funciona: cadenaOne == \"this\""); 27 | 28 | if (cadenaTwo == "that") { 29 | Serial.println("cadenaTwo == \"that\""); 30 | } 31 | else Serial.println("Esta comparacion no funciona: cadenaTwo == \"that\""); 32 | 33 | const char cadena[5] = "this"; //probar a poner \0 al final 34 | if (!strcmp (cadenaOne, cadena)) { 35 | Serial.println("Esta comparacion si funciona con strcmp()"); 36 | } 37 | else Serial.println(strcmp (cadenaOne, cadena)); 38 | 39 | Serial.println(); 40 | 41 | //Dos Strings Distintos// 42 | //Strings 43 | if (stringOne != stringTwo) { 44 | Serial.println(stringOne + " =! " + stringTwo); 45 | } 46 | 47 | //cadenas 48 | if (cadenaOne != cadenaTwo) { //Ojo no estamos comparando las cadenas de caracteres sino los punteros 49 | // Serial.println(cadenaOne + " =! " + cadenaTwo); //Error de compilacion 50 | } 51 | 52 | if (strcmp (cadenaOne, cadenaTwo)) { 53 | Serial.print("Resultado de la comparacion de cadenas = "); 54 | Serial.println(strcmp (cadenaOne, cadenaTwo)); 55 | char resultado[30] = ""; 56 | strcat(resultado, cadenaOne); 57 | strcat(resultado, " =! ") ; 58 | strcat(resultado, cadenaTwo); 59 | Serial.println(resultado); 60 | } 61 | else { 62 | Serial.println("Las cadenas cadenaOne y cadenaTwo son iguales"); 63 | char resultado[30] = ""; 64 | strcat(resultado, cadenaOne); 65 | strcat(resultado, " = ") ; 66 | strcat(resultado, cadenaTwo); 67 | Serial.println(resultado); 68 | } 69 | 70 | 71 | while (true) { 72 | //no hago nada 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /Ejercicio18-strings/_6-interactivo/_6-interactivo.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Interactivo 3 | */ 4 | #define DEBUG 0 5 | 6 | String leido = ""; 7 | 8 | void setup() { 9 | Serial.begin(9600); 10 | Serial.println("Introduce una cadena de texto: "); 11 | while (Serial.available() == 0) { 12 | //Ho hago nada 13 | } 14 | 15 | do { 16 | char caracter_leido; 17 | delay(5); 18 | caracter_leido = Serial.read(); 19 | #if DEBUG 20 | Serial.print("Caracter leido: "); 21 | Serial.print(caracter_leido); 22 | Serial.print(" --> "); 23 | Serial.println(caracter_leido, DEC); 24 | Serial.print("Caracteres en buffer: "); 25 | Serial.println(Serial.available()); 26 | Serial.println(); 27 | #endif 28 | leido += caracter_leido; 29 | } while (Serial.available() > 0); 30 | 31 | Serial.println("He leido la cadena: \"" + leido + "\""); 32 | } 33 | 34 | void loop() { 35 | 36 | //no hago nada 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Ejercicio18-strings/_7-String_Numeros/_7-String_Numeros.ino: -------------------------------------------------------------------------------- 1 | /* 2 | String Numeros 3 | */ 4 | String leido = ""; 5 | int numeroLeido; 6 | 7 | void setup() { 8 | Serial.begin(9600); 9 | Serial.println("Introduce un numero entero:"); 10 | while (Serial.available() == 0) { 11 | //Ho hago nada 12 | } 13 | 14 | do { 15 | char caracter_leido; 16 | caracter_leido = Serial.read(); 17 | leido += caracter_leido; 18 | delay(5); 19 | } while (Serial.available() > 0); 20 | 21 | Serial.println("He leido la cadena: \"" + leido + "\""); 22 | 23 | Serial.println(leido.toInt()); 24 | 25 | if ((leido.toInt() != 0)||(leido =="0")) { 26 | numeroLeido = leido.toInt(); 27 | Serial.print("Tu numero multiplicado por 15 es: "); 28 | Serial.println(numeroLeido * 15); 29 | } 30 | else Serial.println("No has introducido un numero"); 31 | } 32 | 33 | void loop() { 34 | 35 | //no hago nada 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Ejercicio20-Debug/Ejercicio20-Debug.ino: -------------------------------------------------------------------------------- 1 | /* Version 0.2 */ 2 | ///// 3 | // Comprobar tama�o en flash al compilar. 4 | ///// 5 | 6 | #define DEBUG 0 //para hacer debug 7 | #define VERBOSE 0 //saca m�s informaci�n 8 | #define ERRORES 0 //Provoco errores 9 | unsigned long tiempo; 10 | 11 | #if DEBUG //Esto har� un consumo alto de memoria 12 | unsigned long datos[50]; 13 | #endif 14 | 15 | #if ERRORES 16 | serial.println(errores) 17 | ajs qweqwe; 18 | int variable = "error" 19 | #endif 20 | 21 | void setup(){ 22 | Serial.begin(9600); 23 | Serial.println(inicializa()); 24 | Serial.println(freeRam()); 25 | #if DEBUG 26 | for (int i=0; i<50; i++){ 27 | datos[i] = i; 28 | } 29 | Serial.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 30 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 31 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 32 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 33 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 34 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 35 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 36 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 37 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 38 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 39 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); //Ocupa memoria en flash 40 | #endif 41 | } 42 | void loop(){ 43 | Serial.println(freeRam()); 44 | #if VERBOSE 45 | Serial.print("Tiempo de loop: "); 46 | Serial.print(millis()-tiempo); 47 | Serial.println(" ms"); 48 | tiempo = millis(); 49 | #endif 50 | float temperatura = compruebaTemperatura(); 51 | #if DEBUG //Decido si saco errores por serie o no y retrasa el programa para que vaya m�s lento 52 | imprimeErrores(temperatura); 53 | retrasaPrograma(); 54 | #endif 55 | } 56 | 57 | String inicializa() { 58 | String inicio = "Inicializando..."; 59 | return inicio; 60 | } 61 | 62 | float compruebaTemperatura() { 63 | float temperatura = analogRead(A0)*5/1024; 64 | #if VERBOSE 65 | Serial.println("Tarea de lectura de temperatura finalizada..."); 66 | #endif 67 | return temperatura; 68 | } 69 | 70 | void imprimeErrores(float temp){ 71 | if ((temp > 4)||(temp <1)){ 72 | Serial.println("Error de temperatura!!!!!!!"); 73 | } 74 | } 75 | 76 | void retrasaPrograma(){ 77 | delay(1000); 78 | } 79 | 80 | int freeRam () 81 | { 82 | extern int __heap_start, *__brkval; 83 | int v; 84 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 85 | } 86 | 87 | -------------------------------------------------------------------------------- /Ejercicio21-MotorDC_1/Ejercicio21-MotorDC_1.ino: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jecrespo/Aprendiendo-Arduino/91fc9e97f379376ced35b1e9d1018852991ef112/Ejercicio21-MotorDC_1/Ejercicio21-MotorDC_1.ino -------------------------------------------------------------------------------- /Ejercicio21-MotorDC_1/Ejercicio21.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jecrespo/Aprendiendo-Arduino/91fc9e97f379376ced35b1e9d1018852991ef112/Ejercicio21-MotorDC_1/Ejercicio21.fzz -------------------------------------------------------------------------------- /Ejercicio21-MotorDC_1/Ejercicio21_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jecrespo/Aprendiendo-Arduino/91fc9e97f379376ced35b1e9d1018852991ef112/Ejercicio21-MotorDC_1/Ejercicio21_bb.png -------------------------------------------------------------------------------- /Ejercicio22-ChatSerie/Ejercicio22-ChatSerie.ino: -------------------------------------------------------------------------------- 1 | /* Ejercicio22-ChatSerie para el curso #aprendiendoarduino 2 | No todos los pines soportam interrupciones de cambio 3 | The circuit: 4 | RX is digital pin 10 (connect to TX of other device) 5 | TX is digital pin 11 (connect to RX of other device) 6 | */ 7 | 8 | // Ojo a las limitaciones y temas de interrupciones!!! 9 | // http://arduino.cc/en/Reference/SoftwareSerial 10 | // Basado en: http://www.instructables.com/id/Simple-Chat-Program-with-Arduino-Uno/?ALLSTEPS 11 | 12 | #include 13 | SoftwareSerial mySerial(10, 11); // RX, TX 14 | 15 | void setup() { 16 | Serial.begin(9600); 17 | Serial.println("Starting Chat Program..."); 18 | mySerial.begin (4800); //probar a poner velocidades diferentes en cada punto. 19 | delay(1000); 20 | mySerial.println("Hola, acabo de iniciar el chat"); 21 | } 22 | void loop() { //en cada loop leo un caracter si hay en alguno de los bufferes 23 | if (mySerial.available()) { 24 | Serial.print((char)mySerial.read()); 25 | } 26 | if (Serial.available()) { 27 | mySerial.print((char)Serial.read()); 28 | } 29 | } -------------------------------------------------------------------------------- /Ejercicio24-Simon/Tone/changelog.txt: -------------------------------------------------------------------------------- 1 | $Id: changelog.txt 120 2010-07-17 19:01:15Z bhagman@roguerobotics.com $ 2 | 3 | Tone Library 4 | 5 | Version Modified By Date Comments 6 | ------- ----------- -------- -------- 7 | 0001 B Hagman 09/08/02 Initial coding 8 | 0002 B Hagman 09/08/18 Fixed: Multiple pins. 9 | 0003 B Hagman 09/08/18 Fixed: Moved initialization from constructor to 10 | begin(). 11 | 0004 B Hagman 09/09/26 Fixed: Problems with ATmega8. 12 | 0005 B Hagman 09/11/23 Fixed: Scanned prescalars for best fit on 8 bit 13 | timers 14 | 09/11/25 Fixed: Pin toggle method to XOR. 15 | 09/11/25 Fixed: timer0 from being excluded. 16 | 0006 B Hagman 10/03/21 Fixed: License updates, minor fixes. 17 | B Hagman 10/07/17 Fixed: (more) problems with ATmega8 (thanks to Pete62) 18 | 0007 M Pilcher 12/2/20 Fixed: Arduino 1.0 complient -------------------------------------------------------------------------------- /Ejercicio24-Simon/Tone/examples/DTMFTest/DTMFTest.pde: -------------------------------------------------------------------------------- 1 | // DTMF (Dual Tone Multiple Frequency) Demonstration 2 | 3 | // http://en.wikipedia.org/wiki/Dual-tone_multi-frequency 4 | 5 | // To mix the output of the signals to output to a small speaker (i.e. 8 Ohms or higher), 6 | // simply use 1K Ohm resistors from each output pin and tie them together at the speaker. 7 | // Don't forget to connect the other side of the speaker to ground! 8 | 9 | #include 10 | 11 | Tone freq1; 12 | Tone freq2; 13 | 14 | const int DTMF_freq1[] = { 1336, 1209, 1336, 1477, 1209, 1336, 1477, 1209, 1336, 1477 }; 15 | const int DTMF_freq2[] = { 941, 697, 697, 697, 770, 770, 770, 852, 852, 852 }; 16 | 17 | void setup() 18 | { 19 | Serial.begin(9600); 20 | freq1.begin(11); 21 | freq2.begin(12); 22 | } 23 | 24 | void playDTMF(uint8_t number, long duration) 25 | { 26 | freq1.play(DTMF_freq1[number], duration); 27 | freq2.play(DTMF_freq2[number], duration); 28 | } 29 | 30 | 31 | void loop() 32 | { 33 | int i; 34 | uint8_t phone_number[] = { 8, 6, 7, 5, 3, 0 ,9 }; 35 | 36 | for(i = 0; i < sizeof(phone_number); i ++) 37 | { 38 | Serial.print(phone_number[i], 10); 39 | Serial.print(' '); 40 | playDTMF(phone_number[i], 500); 41 | delay(600); 42 | } 43 | 44 | Serial.println(); 45 | delay(4000); 46 | } 47 | -------------------------------------------------------------------------------- /Ejercicio24-Simon/Tone/examples/ToneTest/ToneTest.pde: -------------------------------------------------------------------------------- 1 | // Duelling Tones - Simultaneous tone generation. 2 | // To mix the output of the signals to output to a small speaker (i.e. 8 Ohms or higher), 3 | // simply use 1K Ohm resistors from each output pin and tie them together at the speaker. 4 | // Don't forget to connect the other side of the speaker to ground! 5 | 6 | // This example plays notes 'a' through 'g' sent over the Serial Monitor. 7 | // 's' stops the current playing tone. Use uppercase letters for the second. 8 | 9 | #include 10 | 11 | int notes[] = { NOTE_A3, 12 | NOTE_B3, 13 | NOTE_C4, 14 | NOTE_D4, 15 | NOTE_E4, 16 | NOTE_F4, 17 | NOTE_G4 }; 18 | 19 | // You can declare the tones as an array 20 | Tone notePlayer[2]; 21 | 22 | void setup(void) 23 | { 24 | Serial.begin(9600); 25 | notePlayer[0].begin(11); 26 | notePlayer[1].begin(12); 27 | } 28 | 29 | void loop(void) 30 | { 31 | char c; 32 | 33 | if(Serial.available()) 34 | { 35 | c = Serial.read(); 36 | 37 | switch(c) 38 | { 39 | case 'a'...'g': 40 | notePlayer[0].play(notes[c - 'a']); 41 | Serial.println(notes[c - 'a']); 42 | break; 43 | case 's': 44 | notePlayer[0].stop(); 45 | break; 46 | 47 | case 'A'...'G': 48 | notePlayer[1].play(notes[c - 'A']); 49 | Serial.println(notes[c - 'A']); 50 | break; 51 | case 'S': 52 | notePlayer[1].stop(); 53 | break; 54 | 55 | default: 56 | notePlayer[1].stop(); 57 | notePlayer[0].play(NOTE_B2); 58 | delay(300); 59 | notePlayer[0].stop(); 60 | delay(100); 61 | notePlayer[1].play(NOTE_B2); 62 | delay(300); 63 | notePlayer[1].stop(); 64 | break; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Ejercicio24-Simon/Tone/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Tone 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | Tone KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | play KEYWORD2 16 | stop KEYWORD2 17 | begin KEYWORD2 18 | isPlaying KEYWORD2 19 | 20 | ####################################### 21 | # Constants (LITERAL1) 22 | ####################################### 23 | 24 | NOTE_B0 LITERAL1 25 | NOTE_C1 LITERAL1 26 | NOTE_CS1 LITERAL1 27 | NOTE_D1 LITERAL1 28 | NOTE_DS1 LITERAL1 29 | NOTE_E1 LITERAL1 30 | NOTE_F1 LITERAL1 31 | NOTE_FS1 LITERAL1 32 | NOTE_G1 LITERAL1 33 | NOTE_GS1 LITERAL1 34 | NOTE_A1 LITERAL1 35 | NOTE_AS1 LITERAL1 36 | NOTE_B1 LITERAL1 37 | NOTE_C2 LITERAL1 38 | NOTE_CS2 LITERAL1 39 | NOTE_D2 LITERAL1 40 | NOTE_DS2 LITERAL1 41 | NOTE_E2 LITERAL1 42 | NOTE_F2 LITERAL1 43 | NOTE_FS2 LITERAL1 44 | NOTE_G2 LITERAL1 45 | NOTE_GS2 LITERAL1 46 | NOTE_A2 LITERAL1 47 | NOTE_AS2 LITERAL1 48 | NOTE_B2 LITERAL1 49 | NOTE_C3 LITERAL1 50 | NOTE_CS3 LITERAL1 51 | NOTE_D3 LITERAL1 52 | NOTE_DS3 LITERAL1 53 | NOTE_E3 LITERAL1 54 | NOTE_F3 LITERAL1 55 | NOTE_FS3 LITERAL1 56 | NOTE_G3 LITERAL1 57 | NOTE_GS3 LITERAL1 58 | NOTE_A3 LITERAL1 59 | NOTE_AS3 LITERAL1 60 | NOTE_B3 LITERAL1 61 | NOTE_C4 LITERAL1 62 | NOTE_CS4 LITERAL1 63 | NOTE_D4 LITERAL1 64 | NOTE_DS4 LITERAL1 65 | NOTE_E4 LITERAL1 66 | NOTE_F4 LITERAL1 67 | NOTE_FS4 LITERAL1 68 | NOTE_G4 LITERAL1 69 | NOTE_GS4 LITERAL1 70 | NOTE_A4 LITERAL1 71 | NOTE_AS4 LITERAL1 72 | NOTE_B4 LITERAL1 73 | NOTE_C5 LITERAL1 74 | NOTE_CS5 LITERAL1 75 | NOTE_D5 LITERAL1 76 | NOTE_DS5 LITERAL1 77 | NOTE_E5 LITERAL1 78 | NOTE_F5 LITERAL1 79 | NOTE_FS5 LITERAL1 80 | NOTE_G5 LITERAL1 81 | NOTE_GS5 LITERAL1 82 | NOTE_A5 LITERAL1 83 | NOTE_AS5 LITERAL1 84 | NOTE_B5 LITERAL1 85 | NOTE_C6 LITERAL1 86 | NOTE_CS6 LITERAL1 87 | NOTE_D6 LITERAL1 88 | NOTE_DS6 LITERAL1 89 | NOTE_E6 LITERAL1 90 | NOTE_F6 LITERAL1 91 | NOTE_FS6 LITERAL1 92 | NOTE_G6 LITERAL1 93 | NOTE_GS6 LITERAL1 94 | NOTE_A6 LITERAL1 95 | NOTE_AS6 LITERAL1 96 | NOTE_B6 LITERAL1 97 | NOTE_C7 LITERAL1 98 | NOTE_CS7 LITERAL1 99 | NOTE_D7 LITERAL1 100 | NOTE_DS7 LITERAL1 101 | NOTE_E7 LITERAL1 102 | NOTE_F7 LITERAL1 103 | NOTE_FS7 LITERAL1 104 | NOTE_G7 LITERAL1 105 | NOTE_GS7 LITERAL1 106 | NOTE_A7 LITERAL1 107 | NOTE_AS7 LITERAL1 108 | NOTE_B7 LITERAL1 109 | NOTE_C8 LITERAL1 110 | NOTE_CS8 LITERAL1 111 | NOTE_D8 LITERAL1 112 | NOTE_DS8 LITERAL1 113 | -------------------------------------------------------------------------------- /Ejercicio25-EthernetClient/Ejercicio25-EthernetClient.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | byte mac[] = { 6 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED 7 | }; 8 | byte ip[] = { 9 | 192, 168, 1, 179 10 | }; 11 | byte server[] = { 12 | 64, 233, 187, 99 13 | }; // Google 14 | byte DNS[] = { 15 | 8, 8, 8, 8 16 | }; 17 | byte gateway[] = { 18 | 192, 168, 1, 1 19 | }; 20 | byte subnet[] = { 21 | 255, 255, 255, 0 22 | }; 23 | byte aemet[] = { 24 | 172, 24, 34, 57 25 | }; 26 | byte google[] = { 64, 233, 187, 99 }; 27 | 28 | 29 | EthernetClient client; 30 | String webString = ""; 31 | char url[] = "aemet.es"; 32 | char api[] = "api.openweathermap.org"; 33 | 34 | void setup() 35 | { 36 | String cadenaLeida = ""; 37 | Ethernet.begin(mac, ip, DNS, gateway, subnet); 38 | Serial.begin(9600); 39 | 40 | Serial.println("inicializando red..."); 41 | 42 | delay(1000); 43 | Serial.println("MENU"); 44 | Serial.println("1 - google"); 45 | Serial.println("2 - API openweathermap"); 46 | Serial.println("3 - aemet"); 47 | while (Serial.available() == 0) { 48 | //nada 49 | } 50 | do { 51 | char caracter_leido = Serial.read(); 52 | cadenaLeida += caracter_leido; 53 | delay(5); 54 | } while (Serial.available() > 0); 55 | Serial.print("He Leido la cadena: "); 56 | Serial.println(cadenaLeida); 57 | 58 | int respuesta = cadenaLeida.toInt(); 59 | 60 | switch (respuesta) { 61 | case 1: 62 | if (client.connect(google, 80)) { 63 | Serial.println("connected"); 64 | client.println("GET / HTTP/1.0"); 65 | client.println(); 66 | } 67 | else { 68 | Serial.println("connection failed"); 69 | } 70 | 71 | break; 72 | case 2: 73 | if (client.connect(api, 80)) { 74 | Serial.println("connected"); 75 | client.println("GET /data/2.5/weather?q=London&mode=xml HTTP/1.1"); 76 | client.println(); 77 | } 78 | else { 79 | Serial.println("connection failed"); 80 | } 81 | break; 82 | case 3: 83 | if (client.connect(url, 80)) { 84 | Serial.println("connected"); 85 | client.println("GET /xml/municipios/localidad_26089.xml HTTP/1.1"); 86 | client.println(); 87 | } 88 | else { 89 | Serial.println("connection failed"); 90 | } 91 | break; 92 | default: 93 | Serial.println("Opcion incorrecta"); 94 | break; 95 | } 96 | 97 | } 98 | 99 | void loop() 100 | { 101 | if (client.available()) { 102 | char c = client.read(); 103 | webString += c; 104 | Serial.print(c); 105 | } 106 | 107 | if (!client.connected()) { 108 | Serial.println(); 109 | Serial.println("disconnecting."); 110 | client.stop(); 111 | //Serial.println(webString); 112 | for (;;) 113 | ; 114 | } 115 | } 116 | 117 | -------------------------------------------------------------------------------- /Ejercicio25-EthernetClient_DHCP/Ejercicio25-EthernetClient_DHCP.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | byte mac[] = { 6 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY //Sustituir YY por el numero de MAC correcto 7 | }; 8 | 9 | byte aemet[] = { 10 | 172, 24, 34, 57 11 | }; 12 | 13 | byte google[] = { 64, 233, 187, 99 }; 14 | 15 | 16 | EthernetClient client; 17 | String webString = ""; 18 | char url[] = "aemet.es"; 19 | char api[] = "api.openweathermap.org"; 20 | 21 | void setup() 22 | { 23 | Serial.begin(9600); 24 | String cadenaLeida = ""; 25 | // start the Ethernet connection: 26 | Serial.println("inicializando red..."); 27 | if (Ethernet.begin(mac) == 0) { 28 | Serial.println("Failed to configure Ethernet using DHCP"); 29 | for (;;) 30 | ; 31 | } 32 | else { 33 | Serial.print("IP asignada por DHCP: "); 34 | Serial.println(Ethernet.localIP()); 35 | } 36 | 37 | delay(1000); 38 | Serial.println("MENU"); 39 | Serial.println("1 - google"); 40 | Serial.println("2 - API openweathermap"); 41 | Serial.println("3 - aemet"); 42 | while (Serial.available() == 0) { 43 | //nada 44 | } 45 | do { 46 | char caracter_leido = Serial.read(); 47 | cadenaLeida += caracter_leido; 48 | delay(5); 49 | } while (Serial.available() > 0); 50 | Serial.print("He Leido la cadena: "); 51 | Serial.println(cadenaLeida); 52 | 53 | int respuesta = cadenaLeida.toInt(); 54 | 55 | switch (respuesta) { 56 | case 1: 57 | if (client.connect(google, 80)) { 58 | Serial.println("connected"); 59 | client.println("GET / HTTP/1.0"); 60 | client.println(); 61 | } 62 | else { 63 | Serial.println("connection failed"); 64 | } 65 | 66 | break; 67 | case 2: 68 | if (client.connect(api, 80)) { 69 | Serial.println("connected"); 70 | client.println("GET /data/2.5/weather?q=London&mode=xml HTTP/1.1"); 71 | client.println(); 72 | } 73 | else { 74 | Serial.println("connection failed"); 75 | } 76 | break; 77 | case 3: 78 | if (client.connect(url, 80)) { 79 | Serial.println("connected"); 80 | client.println("GET /xml/municipios/localidad_26089.xml HTTP/1.1"); 81 | client.println(); 82 | } 83 | else { 84 | Serial.println("connection failed"); 85 | } 86 | break; 87 | default: 88 | Serial.println("Opcion incorrecta"); 89 | break; 90 | } 91 | 92 | } 93 | 94 | void loop() 95 | { 96 | if (client.available()) { 97 | char c = client.read(); 98 | webString += c; 99 | Serial.print(c); 100 | } 101 | 102 | if (!client.connected()) { 103 | Serial.println(); 104 | Serial.println("disconnecting."); 105 | client.stop(); 106 | //Serial.println(webString); 107 | for (;;) 108 | ; 109 | } 110 | } 111 | 112 | -------------------------------------------------------------------------------- /Ejercicio26-EthernetServer/Ejercicio26-EthernetServer.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // network configuration. gateway and subnet are optional. 5 | 6 | // the media access control (ethernet hardware) address for the shield: 7 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 8 | //the IP address for the shield: 9 | byte ip[] = { 192, 168, 1, 179 }; 10 | // the router's gateway address: 11 | byte gateway[] = { 192, 168, 1, 1 }; 12 | // the subnet: 13 | byte subnet[] = { 255, 255, 255, 0 }; 14 | 15 | // telnet defaults to port 23 16 | EthernetServer server = EthernetServer(80); 17 | 18 | void setup() 19 | { 20 | Serial.begin(9600); 21 | // initialize the ethernet device 22 | Ethernet.begin(mac, ip, gateway, subnet); 23 | 24 | // start listening for clients 25 | server.begin(); 26 | } 27 | 28 | void loop() 29 | { 30 | // if an incoming client connects, there will be bytes available to read: 31 | EthernetClient client = server.available(); 32 | if (client > 0) { 33 | client.println("HTTP/1.0 200K"); 34 | client.println(); 35 | // read bytes from the incoming client and write them back 36 | // to any clients connected to the server: 37 | while (client.available() > 0){ 38 | char inChar = client.read(); 39 | client.write(inChar); 40 | Serial.print(inChar); 41 | //if (inChar == '\n') { 42 | //client.stop(); 43 | //} 44 | } 45 | client.print("He leido por el puerto analogico: "); 46 | client.println(analogRead(A0)); 47 | client.println(); 48 | client.stop(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Ejercicio26-EthernetServer_DHCP/Ejercicio26-EthernetServer_DHCP.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // network configuration. gateway and subnet are optional. 5 | 6 | // the media access control (ethernet hardware) address for the shield: 7 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY }; //Sustituir YY por el numero de MAC correcto 8 | 9 | // WEB defaults to port 80 10 | EthernetServer server = EthernetServer(80); 11 | 12 | void setup() 13 | { 14 | Serial.begin(9600); 15 | 16 | // initialize the ethernet device 17 | Serial.println("inicializando red..."); 18 | if (Ethernet.begin(mac) == 0) { 19 | Serial.println("Failed to configure Ethernet using DHCP"); 20 | for (;;) 21 | ; 22 | } 23 | else { 24 | Serial.print("IP asignada por DHCP: "); 25 | Serial.println(Ethernet.localIP()); 26 | } 27 | 28 | // start listening for clients 29 | server.begin(); 30 | } 31 | 32 | void loop() 33 | { 34 | // if an incoming client connects, there will be bytes available to read: 35 | EthernetClient client = server.available(); 36 | if (client > 0) { 37 | client.println("HTTP/1.0 200K"); 38 | client.println(); 39 | // read bytes from the incoming client and write them back 40 | // to any clients connected to the server: 41 | while (client.available() > 0) { 42 | char inChar = client.read(); 43 | client.write(inChar); 44 | Serial.print(inChar); 45 | //if (inChar == '\n') { 46 | //client.stop(); 47 | //} 48 | } 49 | client.print("He leido por el puerto analogico: "); 50 | client.println(analogRead(A0)); 51 | client.println(); 52 | client.stop(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Ejercicio27-Boton_Mejorado/Ejercicio27-Boton_Mejorado.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web impresa: 3 | 4 | 5 | 6 |

LED APAGADO

7 |
8 | 9 |
10 | 11 | 12 | */ 13 | 14 | #include 15 | #include 16 | 17 | //Configuración Ethernet 18 | byte mac[] = { 19 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA 20 | }; 21 | //the IP address for the shield: 22 | byte ip[] = { 23 | 10, 22, 72, 30 24 | }; 25 | // the router's gateway address: 26 | byte gateway[] = { 27 | 10, 22, 72, 1 28 | }; 29 | // the subnet: 30 | byte subnet[] = { 31 | 255, 255, 255, 192 32 | }; 33 | 34 | EthernetServer server = EthernetServer(80); 35 | 36 | boolean led_state = LOW; 37 | 38 | void setup() 39 | { 40 | Serial.begin(9600); 41 | //Inicializar Ethernet 42 | Ethernet.begin(mac, ip, gateway, subnet); 43 | 44 | pinMode(8, OUTPUT); 45 | digitalWrite(8, LOW); 46 | led_state = LOW; 47 | 48 | //Comienzo a escuchar cliente ethernet 49 | server.begin(); 50 | } 51 | 52 | void loop() 53 | { 54 | String recibido = ""; //Buffer 55 | 56 | //Si hay in cliente entrante, hay caracteres por leer 57 | EthernetClient client = server.available(); 58 | if (client) { 59 | while (client.available()) { 60 | recibido += char(client.read()); 61 | //Serial.println(client.read()); //Para hacer debug 62 | } 63 | Serial.println("He recibido por ethernet: "); 64 | Serial.println(recibido); 65 | 66 | if (recibido.startsWith("GET / HTTP/1.1")) { 67 | Serial.println("------> Muestro web"); 68 | muestraWeb(client); 69 | } 70 | else if (recibido.startsWith("POST / HTTP/1.1")) { 71 | Serial.println("------> Cambio estado LED"); 72 | led_state = !led_state; 73 | digitalWrite(8, led_state); //cambio el estado del led 74 | muestraWeb(client); 75 | } 76 | else { 77 | client.println("HTTP/1.0 200K"); 78 | client.println(); 79 | client.println(); 80 | client.stop(); 81 | client.flush(); 82 | } 83 | } 84 | } 85 | 86 | void muestraWeb(EthernetClient client) { 87 | client.println("HTTP/1.0 200K"); 88 | client.println(); 89 | client.println(""); 90 | client.println(""); 91 | client.println(""); 92 | if (led_state == HIGH) client.println("

LED ENCENDIDO

"); 93 | else client.println("

LED APAGADO

"); 94 | client.println("
"); 95 | if (led_state == HIGH) client.println(""); 96 | else client.println(""); 97 | client.println("
"); 98 | client.println(""); 99 | client.println(""); 100 | client.println(); 101 | client.stop(); 102 | client.flush(); 103 | } 104 | -------------------------------------------------------------------------------- /Ejercicio27-Boton_Mejorado2/Ejercicio27-Boton_Mejorado2.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web impresa: 3 | 4 | 5 | 6 |

LED APAGADO

7 |
8 | 9 | 10 |
11 | 12 | 13 | */ 14 | 15 | #include 16 | #include 17 | 18 | //Configuración Ethernet 19 | byte mac[] = { 20 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA 21 | }; 22 | //the IP address for the shield: 23 | byte ip[] = { 24 | 10, 22, 72, 30 25 | }; 26 | // the router's gateway address: 27 | byte gateway[] = { 28 | 10, 22, 72, 1 29 | }; 30 | // the subnet: 31 | byte subnet[] = { 32 | 255, 255, 255, 192 33 | }; 34 | 35 | EthernetServer server = EthernetServer(80); 36 | 37 | boolean led_state = LOW; 38 | 39 | void setup() 40 | { 41 | Serial.begin(250000); 42 | //Inicializar Ethernet 43 | Ethernet.begin(mac, ip, gateway, subnet); 44 | 45 | pinMode(8, OUTPUT); 46 | digitalWrite(8, LOW); 47 | led_state = LOW; 48 | 49 | //Comienzo a escuchar cliente ethernet 50 | server.begin(); 51 | } 52 | 53 | void loop() 54 | { 55 | String recibido = ""; //Buffer 56 | 57 | //Si hay in cliente entrante, hay caracteres por leer 58 | EthernetClient client = server.available(); 59 | if (client) { 60 | while (client.available()) { 61 | recibido += char(client.read()); 62 | } 63 | Serial.println("He recibido por ethernet: "); 64 | Serial.println(recibido); 65 | 66 | if (recibido.startsWith("GET / HTTP/1.1")) { 67 | Serial.println("------> Muestro web"); 68 | muestraWeb(client); 69 | } 70 | else if (recibido.startsWith("POST / HTTP/1.1")) { 71 | Serial.println("------> Cambio estado LED"); 72 | led_state = !led_state; 73 | digitalWrite(8, led_state); //cambio el estado del led 74 | muestraWeb(client); 75 | } 76 | else { 77 | client.println("HTTP/1.0 200K"); 78 | client.println(); 79 | client.stop(); 80 | } 81 | } 82 | } 83 | 84 | void muestraWeb(EthernetClient client) { 85 | client.println("HTTP/1.0 200K"); 86 | client.println("Content-Type:text/html"); 87 | client.println("Server:Arduino"); 88 | client.println(); 89 | client.println(""); 90 | client.println(""); 91 | client.println(""); 92 | if (led_state == HIGH) client.println("

LED ENCENDIDO

"); 93 | else client.println("

LED APAGADO

"); 94 | client.println("
"); 95 | if (led_state == HIGH)client.println(""); 96 | else client.println(""); 97 | if (led_state == HIGH) client.println(""); 98 | else client.println(""); 99 | client.println("
"); 100 | client.println(""); 101 | client.println(""); 102 | client.println(); 103 | client.stop(); 104 | } 105 | -------------------------------------------------------------------------------- /Ejercicio27-Boton_Mejorado2_DHCP/Ejercicio27-Boton_Mejorado2_DHCP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web impresa: 3 | 4 | 5 | 6 |

LED APAGADO

7 |
8 | 9 | 10 |
11 | 12 | 13 | */ 14 | 15 | #include 16 | #include 17 | 18 | //Configuración Ethernet 19 | byte mac[] = { 20 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY //Sustituir YY por el numero de MAC correcto 21 | }; 22 | 23 | EthernetServer server = EthernetServer(80); 24 | 25 | boolean led_state = LOW; 26 | 27 | void setup() 28 | { 29 | Serial.begin(9600); 30 | //Inicializar Ethernet 31 | Serial.println("inicializando red..."); 32 | if (Ethernet.begin(mac) == 0) { 33 | Serial.println("Failed to configure Ethernet using DHCP"); 34 | for (;;) 35 | ; 36 | } 37 | else { 38 | Serial.print("IP asignada por DHCP: "); 39 | Serial.println(Ethernet.localIP()); 40 | } 41 | 42 | pinMode(8, OUTPUT); 43 | digitalWrite(8, LOW); 44 | led_state = LOW; 45 | 46 | //Comienzo a escuchar cliente ethernet 47 | server.begin(); 48 | } 49 | 50 | void loop() 51 | { 52 | String recibido = ""; //Buffer 53 | 54 | //Si hay in cliente entrante, hay caracteres por leer 55 | EthernetClient client = server.available(); 56 | if (client) { 57 | while (client.available()) { 58 | recibido += char(client.read()); 59 | } 60 | Serial.println("He recibido por ethernet: "); 61 | Serial.println(recibido); 62 | 63 | if (recibido.startsWith("GET / HTTP/1.1")) { 64 | Serial.println("------> Muestro web"); 65 | muestraWeb(client); 66 | } 67 | else if (recibido.startsWith("POST / HTTP/1.1")) { 68 | Serial.println("------> Cambio estado LED"); 69 | led_state = !led_state; 70 | digitalWrite(8, led_state); //cambio el estado del led 71 | muestraWeb(client); 72 | } 73 | else { 74 | client.println("HTTP/1.0 200K"); 75 | client.println(); 76 | client.stop(); 77 | } 78 | } 79 | } 80 | 81 | void muestraWeb(EthernetClient client) { 82 | client.println("HTTP/1.0 200K"); 83 | client.println("Content-Type:text/html"); 84 | client.println("Server:Arduino"); 85 | client.println(); 86 | client.println(""); 87 | client.println(""); 88 | client.println(""); 89 | if (led_state == HIGH) client.println("

LED ENCENDIDO

"); 90 | else client.println("

LED APAGADO

"); 91 | client.println("
"); 92 | if (led_state == HIGH)client.println(""); 93 | else client.println(""); 94 | if (led_state == HIGH) client.println(""); 95 | else client.println(""); 96 | client.println("
"); 97 | client.println(""); 98 | client.println(""); 99 | client.println(); 100 | client.stop(); 101 | } 102 | -------------------------------------------------------------------------------- /Ejercicio27-Boton_Mejorado_DHCP/Ejercicio27-Boton_Mejorado_DHCP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web impresa: 3 | 4 | 5 | 6 |

LED APAGADO

7 |
8 | 9 |
10 | 11 | 12 | */ 13 | 14 | #include 15 | #include 16 | 17 | //Configuración Ethernet 18 | byte mac[] = { 19 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY //Sustituir YY por el numero de MAC correcto 20 | }; 21 | 22 | EthernetServer server = EthernetServer(80); 23 | 24 | boolean led_state = LOW; 25 | 26 | void setup() 27 | { 28 | Serial.begin(9600); 29 | //Inicializar Ethernet 30 | Serial.println("inicializando red..."); 31 | if (Ethernet.begin(mac) == 0) { 32 | Serial.println("Failed to configure Ethernet using DHCP"); 33 | for (;;) 34 | ; 35 | } 36 | else { 37 | Serial.print("IP asignada por DHCP: "); 38 | Serial.println(Ethernet.localIP()); 39 | } 40 | 41 | pinMode(8, OUTPUT); 42 | digitalWrite(8, LOW); 43 | led_state = LOW; 44 | 45 | //Comienzo a escuchar cliente ethernet 46 | server.begin(); 47 | } 48 | 49 | void loop() 50 | { 51 | String recibido = ""; //Buffer 52 | 53 | //Si hay in cliente entrante, hay caracteres por leer 54 | EthernetClient client = server.available(); 55 | if (client) { 56 | while (client.available()) { 57 | recibido += char(client.read()); 58 | //Serial.println(client.read()); //Para hacer debug 59 | } 60 | Serial.println("He recibido por ethernet: "); 61 | Serial.println(recibido); 62 | 63 | if (recibido.startsWith("GET / HTTP/1.1")) { 64 | Serial.println("------> Muestro web"); 65 | muestraWeb(client); 66 | } 67 | else if (recibido.startsWith("POST / HTTP/1.1")) { 68 | Serial.println("------> Cambio estado LED"); 69 | led_state = !led_state; 70 | digitalWrite(8, led_state); //cambio el estado del led 71 | muestraWeb(client); 72 | } 73 | else { 74 | client.println("HTTP/1.0 200K"); 75 | client.println(); 76 | client.println(); 77 | client.stop(); 78 | client.flush(); 79 | } 80 | } 81 | } 82 | 83 | void muestraWeb(EthernetClient client) { 84 | client.println("HTTP/1.0 200K"); 85 | client.println(); 86 | client.println(""); 87 | client.println(""); 88 | client.println(""); 89 | if (led_state == HIGH) client.println("

LED ENCENDIDO

"); 90 | else client.println("

LED APAGADO

"); 91 | client.println("
"); 92 | if (led_state == HIGH) client.println(""); 93 | else client.println(""); 94 | client.println("
"); 95 | client.println(""); 96 | client.println(""); 97 | client.println(); 98 | client.stop(); 99 | client.flush(); 100 | } 101 | -------------------------------------------------------------------------------- /Ejercicio27-Boton_Mejorado_UNO_WIFI/Ejercicio27-Boton_Mejorado_UNO_WIFI.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /* 4 | on your borwser, you type http:///arduino/webserver/ or http://.local/arduino/webserver/ 5 | 6 | http://labs.arduino.org/WebServer 7 | 8 | modified by www.aprendiendoarduino.com 9 | 10 | At least http:///arduino/web must be typed in browser for ESP send request to 328P and process 11 | */ 12 | 13 | boolean led_state = LOW; 14 | 15 | void setup() { 16 | Serial.begin(9600); 17 | Serial.println(F("Inicializando...")); 18 | pinMode(8, OUTPUT); 19 | digitalWrite(8, LOW); 20 | led_state = LOW; 21 | 22 | Wifi.begin(); 23 | Wifi.println(F("WebServer Server is up")); 24 | } 25 | void loop() { 26 | 27 | while (Wifi.available()) { 28 | process(Wifi); 29 | } 30 | delay(50); 31 | } 32 | void process(WifiData client) { 33 | // read the command 34 | String command = ""; 35 | 36 | while (Wifi.available()) { 37 | command = command + (char)client.read(); //read complete command to analyze 38 | } 39 | 40 | Serial.println(command); 41 | 42 | if (command == "/arduino/webserver/on") { 43 | digitalWrite(8, HIGH); 44 | led_state = HIGH; 45 | } 46 | if (command == "/arduino/webserver/off") { 47 | digitalWrite(8, LOW); 48 | led_state = LOW; 49 | } 50 | 51 | WebServer(client); //Always send web 52 | 53 | } 54 | void WebServer(WifiData client) { 55 | client.println(F("HTTP/1.1 200 OK")); 56 | client.println(F("Content-Type: text/html")); 57 | client.println(F("Connection: close")); 58 | client.println(); 59 | client.println(F("")); 60 | client.println(F("")); 61 | client.println(F("")); 65 | client.println(F("")); 66 | client.println(F("
")); 67 | if (led_state == HIGH) client.println(F("LED ON")); 68 | else client.println(F("LED OFF")); 69 | client.println(F("
")); 70 | if (led_state == HIGH) client.println(F("
")); 71 | else client.println(F("")); 72 | if (led_state == HIGH) client.println(F("")); 73 | else client.println(F("")); 74 | client.println(F("
")); 75 | client.println(F("")); 76 | client.println(F("")); 77 | client.print(DELIMITER); // very important to end the communication !!! 78 | } 79 | -------------------------------------------------------------------------------- /Ejercicio28-ChatEthernet/Ejercicio28-ChatEthernet_cliente/Ejercicio28-ChatEthernet_cliente.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | byte mac[] = { 4 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 5 | byte ip[] = { 6 | 192, 168, 1, 179 }; 7 | byte DNS[] = { 8 | 8,8,8,8}; 9 | byte gateway[] = { 10 | 192, 168, 1, 1}; 11 | byte subnet[] = { 12 | 255,255,255,0}; 13 | byte server[] = { 14 | 192, 168, 1, 180 }; 15 | 16 | EthernetClient client; 17 | 18 | void setup() { 19 | Serial.begin(9600); 20 | Ethernet.begin(mac, ip, DNS, gateway, subnet); 21 | delay(1000); 22 | Serial.println("connecting..."); 23 | if (client.connect(server, 80)) { 24 | Serial.println("connected"); 25 | client.println("Estoy conectado"); 26 | } else { 27 | Serial.println("connection failed"); 28 | } 29 | } 30 | void loop(){ 31 | if (client.available()){ 32 | Serial.print((char)client.read()); 33 | } 34 | if (Serial.available()){ 35 | client.print((char)Serial.read()); 36 | } 37 | } 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Ejercicio28-ChatEthernet/Ejercicio28-ChatEthernet_servidor/Ejercicio28-ChatEthernet_servidor.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | byte mac[] = { 4 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 5 | byte ip[] = { 6 | 192, 168, 1, 180 }; 7 | byte DNS[] = { 8 | 8,8,8,8}; 9 | byte gateway[] = { 10 | 192, 168, 1, 1}; 11 | byte subnet[] = { 12 | 255,255,255,0}; 13 | 14 | EthernetServer server = EthernetServer(80); 15 | 16 | void setup() { 17 | Serial.begin(9600); 18 | Ethernet.begin(mac, ip, DNS, gateway, subnet); 19 | server.begin(); 20 | delay(1000); 21 | } 22 | 23 | void loop(){ 24 | EthernetClient client = server.available(); 25 | if (client.available()){ 26 | Serial.print((char)client.read()); 27 | } 28 | if (Serial.available()){ 29 | client.print((char)Serial.read()); 30 | } 31 | } 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Ejercicio28-ChatEthernet_DHCP/Ejercicio28-ChatEthernet_cliente/Ejercicio28-ChatEthernet_cliente.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | byte mac[] = { 5 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY 6 | }; //Sustituir YY por el numero de MAC correcto 7 | byte server[] = { 8 | 192, 168, x, x 9 | }; //Poner IP asignada al server 10 | 11 | EthernetClient client; 12 | 13 | void setup() { 14 | Serial.begin(9600); 15 | 16 | Serial.println("inicializando red..."); 17 | if (Ethernet.begin(mac) == 0) { 18 | Serial.println("Failed to configure Ethernet using DHCP"); 19 | for (;;) 20 | ; 21 | } 22 | else { 23 | Serial.print("IP asignada por DHCP: "); 24 | Serial.println(Ethernet.localIP()); 25 | } 26 | 27 | delay(1000); 28 | 29 | Serial.println("connecting..."); 30 | if (client.connect(server, 80)) { 31 | Serial.println("connected"); 32 | client.println("Estoy conectado"); 33 | } else { 34 | Serial.println("connection failed"); 35 | } 36 | } 37 | void loop() { 38 | if (client.available()) { 39 | Serial.print((char)client.read()); 40 | } 41 | if (Serial.available()) { 42 | client.print((char)Serial.read()); 43 | } 44 | } 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Ejercicio28-ChatEthernet_DHCP/Ejercicio28-ChatEthernet_servidor/Ejercicio28-ChatEthernet_servidor.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | byte mac[] = { 4 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Sustituir YY por el numero de MAC correcto 5 | 6 | EthernetServer server = EthernetServer(80); 7 | 8 | void setup() { 9 | Serial.begin(9600); 10 | 11 | Serial.println("inicializando red..."); 12 | if (Ethernet.begin(mac) == 0) { 13 | Serial.println("Failed to configure Ethernet using DHCP"); 14 | for (;;) 15 | ; 16 | } 17 | else { 18 | Serial.print("IP asignada por DHCP: "); 19 | Serial.println(Ethernet.localIP()); 20 | } 21 | 22 | server.begin(); 23 | delay(1000); 24 | } 25 | 26 | void loop(){ 27 | EthernetClient client = server.available(); 28 | if (client.available()){ 29 | Serial.print((char)client.read()); 30 | } 31 | if (Serial.available()){ 32 | client.print((char)Serial.read()); 33 | } 34 | } 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Ejercicio29-Webduino/helloWord.txt: -------------------------------------------------------------------------------- 1 | /* Web_HelloWorld.pde - very simple Webduino example */ 2 | 3 | #include "SPI.h" 4 | #include "Ethernet.h" 5 | #include "WebServer.h" 6 | 7 | /* CHANGE THIS TO YOUR OWN UNIQUE VALUE. The MAC number should be 8 | * different from any other devices on your network or you'll have 9 | * problems receiving packets. */ 10 | static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 11 | 12 | 13 | /* CHANGE THIS TO MATCH YOUR HOST NETWORK. Most home networks are in 14 | * the 192.168.0.XXX or 192.168.1.XXX subrange. Pick an address 15 | * that's not in use and isn't going to be automatically allocated by 16 | * DHCP from your router. */ 17 | static uint8_t ip[] = { 192, 168, 1, 179 }; 18 | 19 | /* This creates an instance of the webserver. By specifying a prefix 20 | * of "", all pages will be at the root of the server. */ 21 | #define PREFIX "" 22 | WebServer webserver(PREFIX, 80); 23 | 24 | /* commands are functions that get called by the webserver framework 25 | * they can read any posted data from client, and they output to the 26 | * server to send data back to the web browser. */ 27 | void helloCmd(WebServer &server, WebServer::ConnectionType type, char *, bool) 28 | { 29 | /* this line sends the standard "we're all OK" headers back to the 30 | browser */ 31 | server.httpSuccess(); 32 | 33 | /* if we're handling a GET or POST, we can output our data here. 34 | For a HEAD request, we just stop after outputting headers. */ 35 | if (type != WebServer::HEAD) 36 | { 37 | /* this defines some HTML text in read-only memory aka PROGMEM. 38 | * This is needed to avoid having the string copied to our limited 39 | * amount of RAM. */ 40 | P(helloMsg) = "

Hello, World!

"; 41 | 42 | /* this is a special form of print that outputs from PROGMEM */ 43 | server.printP(helloMsg); 44 | } 45 | } 46 | 47 | void setup() 48 | { 49 | /* initialize the Ethernet adapter */ 50 | Ethernet.begin(mac, ip); 51 | 52 | /* setup our default command that will be run when the user accesses 53 | * the root page on the server */ 54 | webserver.setDefaultCommand(&helloCmd); 55 | 56 | /* run the same command if you try to load /index.html, a common 57 | * default page name */ 58 | webserver.addCommand("index.html", &helloCmd); 59 | 60 | /* start the webserver */ 61 | webserver.begin(); 62 | } 63 | 64 | void loop() 65 | { 66 | char buff[64]; 67 | int len = 64; 68 | 69 | /* process incoming connections one at a time forever */ 70 | webserver.processConnection(buff, &len); 71 | } -------------------------------------------------------------------------------- /Ejercicio30-SD/SDwrite/SDwrite.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int chipSelect = 4; 5 | 6 | //Texto a escribir 7 | char texto[] = "/r/n/r/n/r/nArduino SD Card Web Page/r/n/r/n/r/n

Hello from the Arduino SD Card!

/r/n

A web page from the Arduino SD card server.

/r/n/r/n"; 8 | //nombre fichero 9 | char filename[] = "index.htm"; 10 | 11 | void setup() 12 | { 13 | // Open serial communications and wait for port to open: 14 | Serial.begin(9600); 15 | while (!Serial) { 16 | ; // wait for serial port to connect. Needed for Leonardo only 17 | } 18 | 19 | 20 | Serial.print("Initializing SD card..."); 21 | 22 | // see if the card is present and can be initialized: 23 | if (!SD.begin(chipSelect)) { 24 | Serial.println("Card failed, or not present"); 25 | // don't do anything more: 26 | return; 27 | } 28 | Serial.println("card initialized."); 29 | 30 | Serial.println("Escribiendo en tarjeta SD..."); 31 | Serial.println("Fichero: " + (String)filename); 32 | Serial.println("Contenido: " + (String)texto); 33 | 34 | File dataFile = SD.open(filename, FILE_WRITE); 35 | 36 | if (dataFile) { 37 | dataFile.println(texto); 38 | dataFile.close(); 39 | } 40 | // if the file isn't open, pop up an error: 41 | else { 42 | Serial.println("error opening datalog.txt"); 43 | } 44 | } 45 | 46 | void loop() 47 | { 48 | //nada 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Ejercicio30-SD/dataLogger/dataLogger.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SD card datalogger 3 | 4 | This example shows how to log data from three analog sensors 5 | to an SD card using the SD library. 6 | 7 | The circuit: 8 | * analog sensors on analog ins 0, 1, and 2 9 | * SD card attached to SPI bus as follows: 10 | ** MOSI - pin 11 11 | ** MISO - pin 12 12 | ** CLK - pin 13 13 | ** CS - pin 4 14 | 15 | created 24 Nov 2010 16 | modified 9 Apr 2012 17 | by Tom Igoe 18 | 19 | This example code is in the public domain. 20 | 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | // On the Ethernet Shield, CS is pin 4. Note that even if it's not 27 | // used as the CS pin, the hardware CS pin (10 on most Arduino boards, 28 | // 53 on the Mega) must be left as an output or the SD library 29 | // functions will not work. 30 | const int chipSelect = 4; 31 | 32 | void setup() 33 | { 34 | // Open serial communications and wait for port to open: 35 | Serial.begin(9600); 36 | while (!Serial) { 37 | ; // wait for serial port to connect. Needed for Leonardo only 38 | } 39 | 40 | 41 | Serial.print("Initializing SD card..."); 42 | // make sure that the default chip select pin is set to 43 | // output, even if you don't use it: 44 | pinMode(10, OUTPUT); 45 | 46 | // see if the card is present and can be initialized: 47 | if (!SD.begin(chipSelect)) { 48 | Serial.println("Card failed, or not present"); 49 | // don't do anything more: 50 | return; 51 | } 52 | Serial.println("card initialized."); 53 | } 54 | 55 | void loop() 56 | { 57 | // make a string for assembling the data to log: 58 | String dataString = ""; 59 | 60 | // read three sensors and append to the string: 61 | for (int analogPin = 0; analogPin < 3; analogPin++) { 62 | int sensor = analogRead(analogPin); 63 | dataString += String(sensor); 64 | if (analogPin < 2) { 65 | dataString += ","; 66 | } 67 | } 68 | 69 | // open the file. note that only one file can be open at a time, 70 | // so you have to close this one before opening another. 71 | File dataFile = SD.open("datalog.txt", FILE_WRITE); 72 | 73 | // if the file is available, write to it: 74 | if (dataFile) { 75 | dataFile.println(dataString); 76 | dataFile.close(); 77 | // print to the serial port too: 78 | Serial.println(dataString); 79 | } 80 | // if the file isn't open, pop up an error: 81 | else { 82 | Serial.println("error opening datalog.txt"); 83 | } 84 | } -------------------------------------------------------------------------------- /Ejercicio30-SD/dumpFile/dumpFile.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SD card file dump 3 | 4 | This example shows how to read a file from the SD card using the 5 | SD library and send it over the serial port. 6 | 7 | The circuit: 8 | * SD card attached to SPI bus as follows: 9 | ** MOSI - pin 11 10 | ** MISO - pin 12 11 | ** CLK - pin 13 12 | ** CS - pin 4 13 | 14 | created 22 December 2010 15 | by Limor Fried 16 | modified 9 Apr 2012 17 | by Tom Igoe 18 | 19 | This example code is in the public domain. 20 | 21 | */ 22 | 23 | #include 24 | 25 | // On the Ethernet Shield, CS is pin 4. Note that even if it's not 26 | // used as the CS pin, the hardware CS pin (10 on most Arduino boards, 27 | // 53 on the Mega) must be left as an output or the SD library 28 | // functions will not work. 29 | const int chipSelect = 4; 30 | 31 | void setup() 32 | { 33 | // Open serial communications and wait for port to open: 34 | Serial.begin(9600); 35 | while (!Serial) { 36 | ; // wait for serial port to connect. Needed for Leonardo only 37 | } 38 | 39 | 40 | Serial.print("Initializing SD card..."); 41 | // make sure that the default chip select pin is set to 42 | // output, even if you don't use it: 43 | pinMode(10, OUTPUT); 44 | 45 | // see if the card is present and can be initialized: 46 | if (!SD.begin(chipSelect)) { 47 | Serial.println("Card failed, or not present"); 48 | // don't do anything more: 49 | return; 50 | } 51 | Serial.println("card initialized."); 52 | 53 | // open the file. note that only one file can be open at a time, 54 | // so you have to close this one before opening another. 55 | File dataFile = SD.open("datalog.txt"); 56 | 57 | // if the file is available, write to it: 58 | if (dataFile) { 59 | while (dataFile.available()) { 60 | Serial.write(dataFile.read()); 61 | } 62 | dataFile.close(); 63 | } 64 | // if the file isn't open, pop up an error: 65 | else { 66 | Serial.println("error opening datalog.txt"); 67 | } 68 | } 69 | 70 | void loop() 71 | { 72 | } -------------------------------------------------------------------------------- /Ejercicio30-SD/index.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Arduino SD Card Web Page 5 | 6 | 7 |

Hello from the Arduino SD Card!

8 |

A web page from the Arduino SD card server.

9 | 10 | -------------------------------------------------------------------------------- /Ejercicio32-EnviaDatos/Ejercicio32-EnviaDatos.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Timer.h" 4 | 5 | 6 | byte mac[] = { 7 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 8 | byte ip[] = { 9 | 192, 168, 1, 179 }; 10 | byte DNS[] = { 11 | 8,8,8,8}; 12 | byte gateway[] = { 13 | 192, 168, 1, 1}; 14 | byte subnet[] = { 15 | 255,255,255,0}; 16 | byte raspberry[] = { 17 | 192,168,1,169}; 18 | 19 | EthernetClient client; 20 | Timer t; 21 | String webString = ""; 22 | 23 | void setup() 24 | { 25 | Ethernet.begin(mac, ip, DNS, gateway, subnet); 26 | Serial.begin(9600); 27 | delay(1000); 28 | t.every(5000,grabaDatos); 29 | } 30 | 31 | void loop() 32 | { 33 | webString = ""; 34 | t.update(); 35 | if (client.available()) { 36 | Serial.println("Respuesta del Servidor---->"); 37 | while (client.available()){ 38 | char c = client.read(); 39 | webString += c; 40 | } 41 | Serial.println(webString); 42 | if (webString.endsWith("GRABADOS") == true) Serial.println("Datos guardados correctamente"); 43 | else Serial.println("Error al guardar los datos"); 44 | 45 | client.stop(); 46 | } 47 | } 48 | 49 | void grabaDatos(){ 50 | Serial.println("leyendo temperatura... "); 51 | int sensorVal = analogRead(A0); 52 | float voltage = (sensorVal/1024.0)*5.0; 53 | float temperature = (voltage - 0.5)*100; 54 | Serial.print("Leido: "); 55 | Serial.print(temperature); 56 | Serial.println(" grados"); 57 | 58 | Serial.println("connecting to server..."); 59 | if (client.connect(raspberry, 80)) { 60 | Serial.println("connected"); 61 | client.print("GET /grabaDatos.php?arduino=99&temperatura="); 62 | client.print(temperature); 63 | client.println(" HTTP/1.1"); 64 | client.println("Host: arduino"); 65 | client.println("Connection: close"); 66 | client.println(); 67 | } 68 | else { 69 | Serial.println("connection failed"); 70 | } 71 | } 72 | 73 | 74 | -------------------------------------------------------------------------------- /Ejercicio32-EnviaDatos/grabaDatos.php: -------------------------------------------------------------------------------- 1 | 42 | -------------------------------------------------------------------------------- /Ejercicio32-EnviaDatos_DHCP/Ejercicio32-EnviaDatos_DHCP.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Timer.h" 4 | 5 | 6 | byte mac[] = { 7 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY }; //Sustituir YY por el numero de MAC correcto 8 | 9 | byte raspberry[] = { 10 | 192,168,x,x}; //Poner la IP de Raspberry Pi 11 | 12 | EthernetClient client; 13 | Timer t; 14 | String webString = ""; 15 | 16 | void setup() 17 | { 18 | Serial.begin(9600); 19 | 20 | //Inicializar Ethernet 21 | Serial.println("inicializando red..."); 22 | if (Ethernet.begin(mac) == 0) { 23 | Serial.println("Failed to configure Ethernet using DHCP"); 24 | for (;;) 25 | ; 26 | } 27 | else { 28 | Serial.print("IP asignada por DHCP: "); 29 | Serial.println(Ethernet.localIP()); 30 | } 31 | 32 | delay(1000); 33 | t.every(5000,grabaDatos); 34 | } 35 | 36 | void loop() 37 | { 38 | webString = ""; 39 | t.update(); 40 | if (client.available()) { 41 | Serial.println("Respuesta del Servidor---->"); 42 | while (client.available()){ 43 | char c = client.read(); 44 | webString += c; 45 | } 46 | Serial.println(webString); 47 | if (webString.endsWith("GRABADOS") == true) Serial.println("Datos guardados correctamente"); 48 | else Serial.println("Error al guardar los datos"); 49 | 50 | client.stop(); 51 | } 52 | } 53 | 54 | void grabaDatos(){ 55 | Serial.println("leyendo temperatura... "); 56 | int sensorVal = analogRead(A0); 57 | float voltage = (sensorVal/1024.0)*5.0; 58 | float temperature = (voltage - 0.5)*100; 59 | Serial.print("Leido: "); 60 | Serial.print(temperature); 61 | Serial.println(" grados"); 62 | 63 | Serial.println("connecting to server..."); 64 | if (client.connect(raspberry, 80)) { 65 | Serial.println("connected"); 66 | client.print("GET /grabaDatos.php?arduino=99&temperatura="); 67 | client.print(temperature); 68 | client.println(" HTTP/1.1"); 69 | client.println("Host: arduino"); 70 | client.println("Connection: close"); 71 | client.println(); 72 | } 73 | else { 74 | Serial.println("connection failed"); 75 | } 76 | } 77 | 78 | 79 | -------------------------------------------------------------------------------- /Ejercicio32-EnviaDatos_DHCP/grabaDatos.php: -------------------------------------------------------------------------------- 1 | 42 | -------------------------------------------------------------------------------- /Ejercicio32-EnviaDatos_aprendiendoarduino/Ejercicio32-EnviaDatos_aprendiendoarduino.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Timer.h" 4 | 5 | 6 | byte mac[] = { 7 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 8 | byte ip[] = { 9 | 10, 22, 72, 30 }; 10 | byte DNS[] = { 11 | 8,8,8,8}; 12 | byte gateway[] = { 13 | 10, 22, 72, 1}; 14 | byte subnet[] = { 15 | 255,255,255,0}; 16 | char server[] = "www.aprendiendoarduino.com"; 17 | 18 | EthernetClient client; 19 | Timer t; 20 | String webString = ""; 21 | 22 | void setup() 23 | { 24 | Ethernet.begin(mac, ip, DNS, gateway, subnet); 25 | Serial.begin(9600); 26 | delay(1000); 27 | t.every(5000,grabaDatos); 28 | } 29 | 30 | void loop() 31 | { 32 | webString = ""; 33 | t.update(); 34 | if (client.available()) { 35 | Serial.println("Respuesta del Servidor---->"); 36 | while (client.available()){ 37 | char c = client.read(); 38 | webString += c; 39 | } 40 | Serial.println(webString); 41 | if (webString.endsWith("GRABADOS") == true) Serial.println("Datos guardados correctamente"); 42 | else Serial.println("Error al guardar los datos"); 43 | 44 | client.stop(); 45 | } 46 | } 47 | 48 | void grabaDatos(){ 49 | Serial.println("leyendo temperatura... "); 50 | int sensorVal = analogRead(A0); 51 | float voltage = (sensorVal/1024.0)*5.0; 52 | float temperature = (voltage - 0.5)*100; 53 | Serial.print("Leido: "); 54 | Serial.print(temperature); 55 | Serial.println(" grados"); 56 | 57 | Serial.println("connecting to server..."); 58 | if (client.connect(server, 80)) { 59 | Serial.println("connected"); 60 | client.print("GET /servicios/datos/grabaDatos.php?arduino=99&dato="); 61 | client.print(temperature); 62 | client.println(" HTTP/1.1"); 63 | client.println("Host: www.aprendiendoarduino.com"); 64 | client.println("Connection: close"); 65 | client.println(); 66 | } 67 | else { 68 | Serial.println("connection failed"); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Ejercicio32-EnviaDatos_aprendiendoarduino_DHCP/Ejercicio32-EnviaDatos_aprendiendoarduino_DHCP.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Timer.h" 4 | 5 | 6 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY}; //Sustituir YY por el numero de MAC correcto 7 | 8 | char server[] = "www.aprendiendoarduino.com"; 9 | 10 | EthernetClient client; 11 | Timer t; 12 | String webString = ""; 13 | 14 | void setup() 15 | { 16 | Serial.begin(9600); 17 | 18 | //Inicializar Ethernet 19 | Serial.println("inicializando red..."); 20 | if (Ethernet.begin(mac) == 0) { 21 | Serial.println("Failed to configure Ethernet using DHCP"); 22 | for (;;) 23 | ; 24 | } 25 | else { 26 | Serial.print("IP asignada por DHCP: "); 27 | Serial.println(Ethernet.localIP()); 28 | } 29 | 30 | delay(1000); 31 | t.every(5000, grabaDatos); 32 | } 33 | 34 | void loop() 35 | { 36 | webString = ""; 37 | t.update(); 38 | if (client.available()) { 39 | Serial.println("Respuesta del Servidor---->"); 40 | while (client.available()) { 41 | char c = client.read(); 42 | webString += c; 43 | } 44 | Serial.println(webString); 45 | client.stop(); 46 | } 47 | } 48 | 49 | void grabaDatos() { 50 | Serial.println("leyendo temperatura... "); 51 | int sensorVal = analogRead(A0); 52 | float voltage = (sensorVal / 1024.0) * 5.0; 53 | float temperature = (voltage - 0.5) * 100; 54 | Serial.print("Leido: "); 55 | Serial.print(temperature); 56 | Serial.println(" grados"); 57 | 58 | Serial.println("connecting to server..."); 59 | if (client.connect(server, 80)) { 60 | Serial.println("connected"); 61 | client.print("GET /servicios/datos/grabaDatos.php?arduino=99&dato="); 62 | client.print(temperature); 63 | client.println(" HTTP/1.1"); 64 | client.println("Host: www.aprendiendoarduino.com"); 65 | client.println("Connection: close"); 66 | client.println(); 67 | } 68 | else { 69 | Serial.println("connection failed"); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Ejercicio33-I2C/enviar_I2C/enviar_I2C.ino: -------------------------------------------------------------------------------- 1 | /*Tutorial Arduino -I2C 2 | Autor:Toni Ruiz Sastre 3 | www.electroensaimada.com 4 | */ 5 | #include 6 | 7 | void setup() 8 | { 9 | Wire.begin();//Iniciamos el bus I2C 10 | } 11 | 12 | void loop() 13 | { 14 | Wire.beginTransmission(0xF5); // Enviamos a la dirección 0xF5 en binario 111 0101 15 | Wire.write(0x52); // Enviamos el byte 0x52 16 | Wire.endTransmission(); // stop 17 | 18 | delay(100); 19 | } 20 | -------------------------------------------------------------------------------- /Ejercicio33-I2C/enviar_I2C_slave/enviar_I2C_slave.ino: -------------------------------------------------------------------------------- 1 | /*Tutorial Arduino -I2C 2 | Autor:Toni Ruiz Sastre 3 | www.electroensaimada.com 4 | */ 5 | 6 | 7 | #include 8 | 9 | unsigned char result=0; 10 | 11 | void setup() 12 | { 13 | Wire.begin(0xF5); // Address 0xf5 14 | Wire.onRequest(requestEvent); // Activamos evento de peticion 15 | Wire.onReceive(receiveEvent);//Activamos evento de lectura. 16 | } 17 | 18 | void loop() 19 | { 20 | delay(100); 21 | } 22 | 23 | //Evento de peticion se activa cuando un master nos pide que le enviemos algun dato 24 | void requestEvent() 25 | { 26 | Wire.write(result); // Enviamos el resultado del calculo 27 | 28 | } 29 | 30 | //Evento de recpcion 31 | void receiveEvent(int howMany) 32 | { 33 | unsigned char sum1,sum2; 34 | while( Wire.available()) //Leemos hasta que no haya datos. Teoricamente son 2. 35 | { 36 | sum1 = Wire.read(); //Leemos los dos bytes 37 | sum2 = Wire.read(); 38 | result=sum1+sum2;//Sumamos 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Ejercicio33-I2C/recibir_I2C/recibir_I2C.ino: -------------------------------------------------------------------------------- 1 | /*Tutorial Arduino -I2C 2 | Autor:Toni Ruiz Sastre 3 | www.electroensaimada.com 4 | */ 5 | 6 | 7 | #include 8 | 9 | void setup() 10 | { 11 | Wire.begin(0xF5); // Nos ponemos la direccion 0xF5 12 | Wire.onReceive(receiveEvent); // Configuramos la recepcion como una interrupcion/evento 13 | Serial.begin(9600); // Configuramos el serial 14 | } 15 | 16 | void loop() 17 | { 18 | delay(100); 19 | } 20 | 21 | 22 | //Interrupcion de dato recibido i2c 23 | void receiveEvent(int howMany) 24 | { 25 | while( Wire.available()>0) // Leemos todo el buffer 26 | { 27 | byte rec = Wire.read(); // almacenamos el byte 28 | Serial.print(rec,HEX); // Lo mostramos por pantalla 29 | } 30 | Serial.println(); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Ejercicio33-I2C/recibir_I2C_master/recibir_I2C_master.ino: -------------------------------------------------------------------------------- 1 | 2 | /*Tutorial Arduino -I2C 3 | Autor:Toni Ruiz Sastre 4 | www.electroensaimada.com 5 | */ 6 | 7 | #include 8 | 9 | void setup() 10 | { 11 | Wire.begin(); //Iniciamos el bus I2C 12 | Serial.begin(9600); //Iniciamos el Serial 13 | } 14 | 15 | void loop() 16 | { 17 | unsigned char result; 18 | 19 | 20 | Wire.beginTransmission(0xF5); // Enviamos a la dirección 0xF5 en binario 111 0101 21 | Wire.write(5); //Enviamos un 5 22 | Wire.write(7); //Enviamos un 7 23 | Wire.endTransmission(); //Terminamos la trama 24 | delayMicroseconds(100);//Para poder distinguir en el osciloscopio el envio y la recepcion 25 | Wire.requestFrom(0xF5, 1); // Pedimos 1 bytes al slave 0xf5 26 | 27 | while(Wire.available()) // Mientras tengamos datos. 28 | { 29 | result = Wire.read(); // Leemos el resultado 30 | 31 | } 32 | Serial.print("5 + 7 =");Serial.println(result); 33 | delay(500); 34 | } 35 | -------------------------------------------------------------------------------- /Ejercicio33-I2C/scanI2CBus/scanI2CBus.ino: -------------------------------------------------------------------------------- 1 | #include "Wire.h" 2 | extern "C" { 3 | #include "utility/twi.h" 4 | } 5 | void scanI2CBus(byte from_addr, byte to_addr, void(*callback)(byte address, byte result) ) 6 | { 7 | byte rc; 8 | byte data = 0; 9 | for( byte addr = from_addr; addr <= to_addr; addr++ ) { 10 | rc = twi_writeTo(addr, &data, 0, 1, 0); 11 | callback( addr, rc ); 12 | } 13 | } 14 | void scanFunc( byte addr, byte result ) { 15 | Serial.print("addr: "); 16 | Serial.print(addr,DEC); 17 | Serial.print( (result==0) ? " Encontrado!":" "); 18 | Serial.print( (addr%4) ? "\t":"\n"); 19 | } 20 | 21 | 22 | const byte start_address = 8; 23 | const byte end_address = 119; 24 | 25 | void setup() 26 | { 27 | Wire.begin(); 28 | Serial.begin(9600); 29 | Serial.print("Escaneando bus I2C..."); 30 | scanI2CBus( start_address, end_address, scanFunc ); 31 | Serial.println("\nTerminado"); 32 | } 33 | 34 | void loop() 35 | { 36 | delay(1000); 37 | } 38 | -------------------------------------------------------------------------------- /Ejercicio34-SPI/master/master.ino: -------------------------------------------------------------------------------- 1 | // Written by Nick Gammon 2 | // February 2011 3 | 4 | 5 | #include 6 | 7 | void setup (void) 8 | { 9 | 10 | digitalWrite(SS, HIGH); // ensure SS stays high for now 11 | 12 | // Put SCK, MOSI, SS pins into output mode 13 | // also put SCK, MOSI into LOW state, and SS into HIGH state. 14 | // Then put SPI hardware into Master mode and turn SPI on 15 | SPI.begin (); 16 | 17 | // Slow down the master a bit 18 | SPI.setClockDivider(SPI_CLOCK_DIV8); 19 | 20 | } // end of setup 21 | 22 | 23 | void loop (void) 24 | { 25 | 26 | char c; 27 | 28 | // enable Slave Select 29 | digitalWrite(SS, LOW); // SS is pin 10 30 | 31 | // send test string 32 | for (const char * p = "Hello, world!\n" ; c = *p; p++) 33 | SPI.transfer (c); 34 | 35 | // disable Slave Select 36 | digitalWrite(SS, HIGH); 37 | 38 | delay (1000); // 1 seconds delay 39 | } // end of loop -------------------------------------------------------------------------------- /Ejercicio34-SPI/master_2/master_2.ino: -------------------------------------------------------------------------------- 1 | // Written by Nick Gammon 2 | // April 2011 3 | 4 | 5 | #include 6 | #include "pins_arduino.h" 7 | 8 | void setup (void) 9 | { 10 | Serial.begin (115200); 11 | Serial.println (); 12 | 13 | digitalWrite(SS, HIGH); // ensure SS stays high for now 14 | 15 | // Put SCK, MOSI, SS pins into output mode 16 | // also put SCK, MOSI into LOW state, and SS into HIGH state. 17 | // Then put SPI hardware into Master mode and turn SPI on 18 | SPI.begin (); 19 | 20 | // Slow down the master a bit 21 | SPI.setClockDivider(SPI_CLOCK_DIV8); 22 | 23 | } // end of setup 24 | 25 | byte transferAndWait (const byte what) 26 | { 27 | byte a = SPI.transfer (what); 28 | delayMicroseconds (20); 29 | return a; 30 | } // end of transferAndWait 31 | 32 | void loop (void) 33 | { 34 | 35 | byte a, b, c, d; 36 | 37 | // enable Slave Select 38 | digitalWrite(SS, LOW); 39 | 40 | transferAndWait ('a'); // add command 41 | transferAndWait (10); 42 | a = transferAndWait (17); 43 | b = transferAndWait (33); 44 | c = transferAndWait (42); 45 | d = transferAndWait (0); 46 | 47 | // disable Slave Select 48 | digitalWrite(SS, HIGH); 49 | 50 | Serial.println ("Adding results:"); 51 | Serial.println (a, DEC); 52 | Serial.println (b, DEC); 53 | Serial.println (c, DEC); 54 | Serial.println (d, DEC); 55 | 56 | // enable Slave Select 57 | digitalWrite(SS, LOW); 58 | 59 | transferAndWait ('s'); // subtract command 60 | transferAndWait (10); 61 | a = transferAndWait (17); 62 | b = transferAndWait (33); 63 | c = transferAndWait (42); 64 | d = transferAndWait (0); 65 | 66 | // disable Slave Select 67 | digitalWrite(SS, HIGH); 68 | 69 | Serial.println ("Subtracting results:"); 70 | Serial.println (a, DEC); 71 | Serial.println (b, DEC); 72 | Serial.println (c, DEC); 73 | Serial.println (d, DEC); 74 | 75 | delay (1000); // 1 second delay 76 | } // end of loop -------------------------------------------------------------------------------- /Ejercicio34-SPI/slave/slave.ino: -------------------------------------------------------------------------------- 1 | // Written by Nick Gammon 2 | // February 2011 3 | 4 | 5 | #include 6 | 7 | char buf [100]; 8 | volatile byte pos; 9 | volatile boolean process_it; 10 | 11 | void setup (void) 12 | { 13 | Serial.begin (115200); // debugging 14 | 15 | // have to send on master in, *slave out* 16 | pinMode(MISO, OUTPUT); 17 | 18 | // turn on SPI in slave mode 19 | SPCR |= _BV(SPE); 20 | 21 | // get ready for an interrupt 22 | pos = 0; // buffer empty 23 | process_it = false; 24 | 25 | // now turn on interrupts 26 | SPI.attachInterrupt(); 27 | 28 | } // end of setup 29 | 30 | 31 | // SPI interrupt routine 32 | ISR (SPI_STC_vect) 33 | { 34 | byte c = SPDR; // grab byte from SPI Data Register 35 | 36 | // add to buffer if room 37 | if (pos < sizeof buf) 38 | { 39 | buf [pos++] = c; 40 | 41 | // example: newline means time to process buffer 42 | if (c == '\n') 43 | process_it = true; 44 | 45 | } // end of room available 46 | } // end of interrupt routine SPI_STC_vect 47 | 48 | // main loop - wait for flag set in interrupt routine 49 | void loop (void) 50 | { 51 | if (process_it) 52 | { 53 | buf [pos] = 0; 54 | Serial.println (buf); 55 | pos = 0; 56 | process_it = false; 57 | } // end of flag set 58 | 59 | } // end of loop -------------------------------------------------------------------------------- /Ejercicio34-SPI/slave_2/slave_2.ino: -------------------------------------------------------------------------------- 1 | // Written by Nick Gammon 2 | // April 2011 3 | 4 | #include "pins_arduino.h" 5 | 6 | // what to do with incoming data 7 | byte command = 0; 8 | 9 | void setup (void) 10 | { 11 | 12 | // have to send on master in, *slave out* 13 | pinMode(MISO, OUTPUT); 14 | 15 | // turn on SPI in slave mode 16 | SPCR |= _BV(SPE); 17 | 18 | // turn on interrupts 19 | SPCR |= _BV(SPIE); 20 | 21 | } // end of setup 22 | 23 | 24 | // SPI interrupt routine 25 | ISR (SPI_STC_vect) 26 | { 27 | byte c = SPDR; 28 | 29 | switch (command) 30 | { 31 | // no command? then this is the command 32 | case 0: 33 | command = c; 34 | SPDR = 0; 35 | break; 36 | 37 | // add to incoming byte, return result 38 | case 'a': 39 | SPDR = c + 15; // add 15 40 | break; 41 | 42 | // subtract from incoming byte, return result 43 | case 's': 44 | SPDR = c - 8; // subtract 8 45 | break; 46 | 47 | } // end of switch 48 | 49 | } // end of interrupt service routine (ISR) SPI_STC_vect 50 | 51 | void loop (void) 52 | { 53 | 54 | // if SPI not active, clear current command 55 | if (digitalRead (SS) == HIGH) 56 | command = 0; 57 | } // end of loop -------------------------------------------------------------------------------- /Ejercicio35-EEPROM/EEPROM_Dump/EEPROM_Dump.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup(){ 4 | Serial.begin(57600); 5 | 6 | e2reader(); 7 | } 8 | 9 | void loop(){ 10 | } 11 | 12 | void e2reader(){ 13 | char buffer[16]; 14 | char valuePrint[4]; 15 | byte value; 16 | unsigned int address; 17 | uint8_t trailingSpace = 2; 18 | 19 | Serial.print("Dumping "); Serial.print(E2END + 1); 20 | Serial.println(" bytes from EEPROM."); 21 | Serial.print("baseAddr "); 22 | for(int x = 0; x < 2; x++){ 23 | Serial.print(" "); 24 | for(int y = 0; y < 25; y++) 25 | Serial.print("="); 26 | } 27 | 28 | // E2END is a macro defined as the last EEPROM address 29 | // (1023 for ATMEGA328P) 30 | for(address = 0; address <= E2END; address++){ 31 | // read a byte from the current address of the EEPROM 32 | value = EEPROM.read(address); 33 | 34 | // add space between two sets of 8 bytes 35 | if(address % 8 == 0) 36 | Serial.print(" "); 37 | 38 | // newline and address for every 16 bytes 39 | if(address % 16 == 0){ 40 | //print the buffer 41 | if(address > 0 && address % 16 == 0) 42 | printASCII(buffer); 43 | 44 | sprintf(buffer, "\n 0x%05X: ", address); 45 | Serial.print(buffer); 46 | 47 | //clear the buffer for the next data block 48 | memset (buffer, 32, 16); 49 | } 50 | 51 | // save the value in temporary storage 52 | buffer[address%16] = value; 53 | 54 | // print the formatted value 55 | sprintf(valuePrint, " %02X", value); 56 | Serial.print(valuePrint); 57 | } 58 | 59 | if(address % 16 > 0){ 60 | if(address % 16 < 9) 61 | trailingSpace += 2; 62 | 63 | trailingSpace += (16 - address % 16) * 3; 64 | } 65 | 66 | for(int i = trailingSpace; i > 0; i--) 67 | Serial.print(" "); 68 | 69 | //last line of data and a new line 70 | printASCII(buffer); 71 | Serial.println(); 72 | } 73 | 74 | void printASCII(char * buffer){ 75 | for(int i = 0; i < 16; i++){ 76 | if(i == 8) 77 | Serial.print(" "); 78 | 79 | if(buffer[i] > 31 and buffer[i] < 127){ 80 | Serial.print(buffer[i]); 81 | }else{ 82 | Serial.print("."); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Ejercicio35-EEPROM/Ejercicio35-EEPROM.ino: -------------------------------------------------------------------------------- 1 | /* LoadAndSaveSettings 2 | * footswitch 2012-03-05, original code by Joghurt (2010) 3 | * Demonstrates how to load and save settings to the EEPROM 4 | * Modified for #aprendiendoarduino 5 | */ 6 | // Contains EEPROM.read() and EEPROM.write() 7 | #include 8 | 9 | // ID of the settings block 10 | #define CONFIG_VERSION "ls1" 11 | 12 | // Tell it where to store your config data in EEPROM 13 | #define CONFIG_START 32 14 | 15 | // Example settings structure 16 | struct StoreStruct { 17 | // The variables of your settings 18 | int a, b; 19 | char c; 20 | long d; 21 | int e[6]; 22 | // This is for mere detection if they are your settings 23 | char version_of_program[4]; // it is the last variable of the struct 24 | // so when settings are saved, they will only be validated if 25 | // they are stored completely. 26 | } settings = { 27 | // The default values 28 | HIGH, HIGH, 29 | 'c', 30 | 10000, 31 | {4, 5, 7, 8, 10, 12}, 32 | CONFIG_VERSION 33 | }; 34 | 35 | void loadConfig() { 36 | // To make sure there are settings, and they are YOURS! 37 | // If nothing is found it will use the default settings. 38 | if (//EEPROM.read(CONFIG_START + sizeof(settings) - 1) == settings.version_of_program[3] // this is '\0' 39 | EEPROM.read(CONFIG_START + sizeof(settings) - 2) == settings.version_of_program[2] && 40 | EEPROM.read(CONFIG_START + sizeof(settings) - 3) == settings.version_of_program[1] && 41 | EEPROM.read(CONFIG_START + sizeof(settings) - 4) == settings.version_of_program[0]) 42 | { // reads settings from EEPROM 43 | for (unsigned int t=0; t 10 | 11 | void setup() 12 | { 13 | // write a 0 to all 1024 bytes of the EEPROM 14 | for (int i = 0; i < 1024; i++) 15 | EEPROM.write(i, 0); 16 | 17 | // turn the LED on when we're done 18 | digitalWrite(13, HIGH); 19 | } 20 | 21 | void loop() 22 | { 23 | } -------------------------------------------------------------------------------- /Ejercicio35-EEPROM/ejemploAnything/EEPROMAnything.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include // for type definitions 3 | 4 | template int EEPROM_writeAnything(int ee, const T& value) 5 | { 6 | const byte* p = (const byte*)(const void*)&value; 7 | unsigned int i; 8 | for (i = 0; i < sizeof(value); i++) 9 | EEPROM.write(ee++, *p++); 10 | return i; 11 | } 12 | 13 | template int EEPROM_readAnything(int ee, T& value) 14 | { 15 | byte* p = (byte*)(void*)&value; 16 | unsigned int i; 17 | for (i = 0; i < sizeof(value); i++) 18 | *p++ = EEPROM.read(ee++); 19 | return i; 20 | } -------------------------------------------------------------------------------- /Ejercicio35-EEPROM/ejemploAnything/ejemploAnything.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "EEPROMAnything.h" 3 | 4 | struct config_t 5 | { 6 | long alarm; 7 | int mode; 8 | } configuration; 9 | 10 | void setup() 11 | { 12 | EEPROM_readAnything(0, configuration); 13 | // ... 14 | } 15 | void loop() 16 | { 17 | // let the user adjust their alarm settings 18 | // let the user adjust their mode settings 19 | // ... 20 | 21 | // if they push the "Save" button, save their configuration 22 | if (digitalRead(13) == HIGH) 23 | EEPROM_writeAnything(0, configuration); 24 | } -------------------------------------------------------------------------------- /Ejercicio35-EEPROM/leeEEPROM/leeEEPROM.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int a = 0; 4 | int value; 5 | 6 | void setup() 7 | { 8 | Serial.begin(9600); 9 | } 10 | 11 | void loop() 12 | { 13 | value = EEPROM.read(a); 14 | 15 | Serial.print(a); 16 | Serial.print("\t"); 17 | Serial.print(value,HEX); 18 | Serial.println(); 19 | 20 | a = a + 1; 21 | 22 | if (a == 1024) 23 | while(true){ 24 | } 25 | 26 | delay(50); 27 | } 28 | -------------------------------------------------------------------------------- /Ejercicio37-IoT/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jecrespo/Aprendiendo-Arduino/91fc9e97f379376ced35b1e9d1018852991ef112/Ejercicio37-IoT/readme.md -------------------------------------------------------------------------------- /Ejercicio38-Firmata/Entendiendo_Firmata/Entendiendo_Firmata.ino: -------------------------------------------------------------------------------- 1 | //Example based on https://www.arduino.cc/en/Reference/Firmata example 2 | //To decode the firmata protocol use Bray's terminal from https://sites.google.com/site/terminalbpp/ 3 | //More details in https://aprendiendoarduino.wordpress.com/ 4 | 5 | #include 6 | 7 | byte analogPin; 8 | 9 | void analogWriteCallback(byte pin, int value) 10 | { 11 | pinMode(pin, OUTPUT); 12 | analogWrite(pin, value); 13 | } 14 | 15 | void setup() 16 | { 17 | Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION); 18 | Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); 19 | Firmata.begin(); 20 | Serial.begin(57600); 21 | } 22 | 23 | void loop() 24 | { 25 | Serial.write(255); // send a byte with the value 255 en hexadecimal FF 26 | Serial.write(255); // FFFF means the loop init 27 | while (Firmata.available()) { 28 | Firmata.processInput(); 29 | } 30 | for (analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) { 31 | Firmata.sendAnalog(analogPin, analogRead(analogPin)); 32 | Serial.println(analogRead(analogPin)); 33 | delay(1000); 34 | } 35 | delay(5000); 36 | } 37 | -------------------------------------------------------------------------------- /Ejercicio38-Firmata/Python_Firmata/Medidor_Luz.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | ''' 3 | Created on 16/02/2016 4 | @author: jecrespo 5 | @version: 1.0 6 | @description: Medidor de luz mediante Firmata 7 | @url: http://www.aprendiendoarduino.com/ 8 | ''' 9 | 10 | from pyfirmata import Arduino, util 11 | import time 12 | board=Arduino('COM9') 13 | ledpin = board.get_pin('d:13:o') # d=digital 13=pin number o=output 14 | 15 | #testeo la conexión 16 | ledpin.write(1) 17 | time.sleep(1) 18 | ledpin.write(0) 19 | 20 | #ejecuto el programa 21 | 22 | # Pines 2, 3 y 4 23 | p1 = board.get_pin('d:2:o') 24 | p2 = board.get_pin('d:3:o') 25 | p3 = board.get_pin('d:4:o') 26 | 27 | #para leer de puertos analógicos inicio un iterador, en caso contrario el Arduino 28 | #sigue mandado datos hasta que llena el buffer del puerto serie 29 | it = util.Iterator(board) 30 | it.start() 31 | 32 | #fotoresistencia conectado a entrada analógica A0 33 | l = board.get_pin('a:0:i') 34 | l.enable_reporting() 35 | val = l.read() 36 | 37 | #Para parar el programa poner a masa pin A0 38 | while (val > 0): 39 | 40 | print (val) 41 | p1.write(0) 42 | p2.write(0) 43 | p3.write(0) 44 | if (val > 256): 45 | p1.write(1) 46 | print("Enciendo 1") 47 | if (val > 512): 48 | p2.write(1) 49 | print("Enciendo 2") 50 | if (val > 768): 51 | p3.write(1) 52 | print("Enciendo 3") 53 | 54 | val = l.read()*1000 55 | time.sleep(1) 56 | 57 | print("Fin de programa") 58 | board.exit() 59 | -------------------------------------------------------------------------------- /Ejercicio38-Firmata/Python_Firmata/blink.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | ''' 3 | Created on 16/02/2016 4 | @author: jecrespo 5 | @version: 1.0 6 | @description: Blink Arduino mediante Firmata 7 | @url: http://www.aprendiendoarduino.com/ 8 | ''' 9 | 10 | 11 | import time 12 | import sys 13 | from PyMata.pymata import PyMata 14 | 15 | BOARD_LED = 13 16 | port = input("Puerto Serie Arduino: ") 17 | 18 | try: 19 | arduino = PyMata(port, verbose=True) 20 | 21 | for x in range(10): 22 | print(x + 1) 23 | # Set the output to 1 = High 24 | arduino.digital_write(BOARD_LED, 1) 25 | # Wait a half second between toggles. 26 | time.sleep(.5) 27 | # Set the output to 0 = Low 28 | arduino.digital_write(BOARD_LED, 0) 29 | time.sleep(.5) 30 | 31 | # Close PyMata when we are done 32 | arduino.close() 33 | except: # catch *all* exceptions 34 | e = sys.exc_info()[0] 35 | print("Puerto no válido. %s" % e) 36 | -------------------------------------------------------------------------------- /Ejercicio39-NTP/readme.md: -------------------------------------------------------------------------------- 1 | # NTP Basic 2 | Obtener fecha y hora en Arduino sin necesidad de un RTC usando NTP 3 | NTP: https://es.wikipedia.org/wiki/Network_Time_Protocol -------------------------------------------------------------------------------- /Ejercicio39-NTP_DHCP/readme.md: -------------------------------------------------------------------------------- 1 | # NTP Basic 2 | Obtener fecha y hora en Arduino sin necesidad de un RTC usando NTP 3 | NTP: https://es.wikipedia.org/wiki/Network_Time_Protocol -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/CHANGES: -------------------------------------------------------------------------------- 1 | See VirtulWire.h for latest change log 2 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/LICENSE: -------------------------------------------------------------------------------- 1 | This software is Copyright (C) 2008 Mike McCauley. Use is subject to license 2 | conditions. The main licensing options available are GPL V2 or Commercial: 3 | 4 | Open Source Licensing GPL V2 5 | 6 | This is the appropriate option if you want to share the source code of your 7 | application with everyone you distribute it to, and you also want to give them 8 | the right to share who uses it. If you wish to use this software under Open 9 | Source Licensing, you must contribute all your source code to the open source 10 | community in accordance with the GPL Version 2 when your application is 11 | distributed. See http://www.gnu.org/copyleft/gpl.html 12 | 13 | Commercial Licensing 14 | 15 | This is the appropriate option if you are creating proprietary applications 16 | and you are not prepared to distribute and share the source code of your 17 | application. Contact info@open.com.au for details. 18 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/MANIFEST: -------------------------------------------------------------------------------- 1 | VirtualWire/doc 2 | VirtualWire/LICENSE 3 | VirtualWire/README 4 | VirtualWire/Makefile 5 | VirtualWire/VirtualWire.cpp 6 | VirtualWire/VirtualWire.h 7 | VirtualWire/CHANGES 8 | VirtualWire/MANIFEST 9 | VirtualWire/keywords.txt 10 | VirtualWire/util/crc16.h 11 | VirtualWire/examples/client/client.pde 12 | VirtualWire/examples/transmitter/transmitter.pde 13 | VirtualWire/examples/receiver/receiver.pde 14 | VirtualWire/examples/server/server.pde 15 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile 2 | # 3 | # Makefile for the Arduino VirtualWire project 4 | # 5 | # Author: Mike McCauley (mikem@airspayce.com) 6 | # Copyright (C) 2011 Mike McCauley 7 | # $Id: Makefile,v 1.1 2013/01/14 06:49:29 mikem Exp mikem $ 8 | 9 | PROJNAME = VirtualWire 10 | # Dont forget to also change the version at the top of RF22.h: 11 | DISTFILE = $(PROJNAME)-1.15.zip 12 | 13 | all: doxygen dist upload 14 | 15 | doxygen: 16 | doxygen project.cfg 17 | 18 | ci: 19 | (cd ..;ci -l `cat $(PROJNAME)/MANIFEST`) 20 | 21 | dist: 22 | (cd ..; zip $(PROJNAME)/$(DISTFILE) `cat $(PROJNAME)/MANIFEST`) 23 | 24 | upload: 25 | rsync -avz $(DISTFILE) doc/ www.airspayce.com:public_html/mikem/arduino/$(PROJNAME) 26 | rsync -avz ../../doc/VirtualWire.pdf doc/ www.airspayce.com:public_html/mikem/arduino/ 27 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/README: -------------------------------------------------------------------------------- 1 | Virtual Wire 2 | 3 | This is the VirtualWire library for Arduino 4 | It provides a simple message passing protocol for a range of inexpensive 5 | transmitter and receiver modules. 6 | 7 | See http://www.open.com.au/mikem/arduino/VirtualWire.pdf for full documentation. 8 | 9 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/examples/client/client.pde: -------------------------------------------------------------------------------- 1 | // client.pde 2 | // 3 | // Simple example of how to use VirtualWire to send and receive messages 4 | // with a DR3100 module. 5 | // Send a message to another arduino running the 'server' example, which 6 | // should send a reply, which we will check 7 | // 8 | // See VirtualWire.h for detailed API docs 9 | // Author: Mike McCauley (mikem@airspayce.com) 10 | // Copyright (C) 2008 Mike McCauley 11 | // $Id: client.pde,v 1.1 2008/04/20 09:24:17 mikem Exp $ 12 | 13 | #include 14 | 15 | void setup() 16 | { 17 | Serial.begin(9600); // Debugging only 18 | Serial.println("setup"); 19 | 20 | // Initialise the IO and ISR 21 | vw_set_ptt_inverted(true); // Required for DR3100 22 | vw_setup(2000); // Bits per sec 23 | vw_rx_start(); // Start the receiver PLL running 24 | } 25 | 26 | void loop() 27 | { 28 | const char *msg = "hello"; 29 | uint8_t buf[VW_MAX_MESSAGE_LEN]; 30 | uint8_t buflen = VW_MAX_MESSAGE_LEN; 31 | 32 | digitalWrite(13, true); // Flash a light to show transmitting 33 | vw_send((uint8_t *)msg, strlen(msg)); 34 | vw_wait_tx(); // Wait until the whole message is gone 35 | Serial.println("Sent"); 36 | digitalWrite(13, false); 37 | 38 | // Wait at most 200ms for a reply 39 | if (vw_wait_rx_max(200)) 40 | { 41 | if (vw_get_message(buf, &buflen)) // Non-blocking 42 | { 43 | int i; 44 | 45 | // Message with a good checksum received, dump it. 46 | Serial.print("Got: "); 47 | 48 | for (i = 0; i < buflen; i++) 49 | { 50 | Serial.print(buf[i], HEX); 51 | Serial.print(" "); 52 | } 53 | Serial.println(""); 54 | } 55 | } 56 | else 57 | Serial.println("Timout"); 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/examples/receiver/receiver.pde: -------------------------------------------------------------------------------- 1 | // receiver.pde 2 | // 3 | // Simple example of how to use VirtualWire to receive messages 4 | // Implements a simplex (one-way) receiver with an Rx-B1 module 5 | // 6 | // See VirtualWire.h for detailed API docs 7 | // Author: Mike McCauley (mikem@airspayce.com) 8 | // Copyright (C) 2008 Mike McCauley 9 | // $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $ 10 | 11 | #include 12 | 13 | const int led_pin = 13; 14 | const int transmit_pin = 12; 15 | const int receive_pin = 11; 16 | const int transmit_en_pin = 3; 17 | 18 | void setup() 19 | { 20 | delay(1000); 21 | Serial.begin(9600); // Debugging only 22 | Serial.println("setup"); 23 | 24 | // Initialise the IO and ISR 25 | vw_set_tx_pin(transmit_pin); 26 | vw_set_rx_pin(receive_pin); 27 | vw_set_ptt_pin(transmit_en_pin); 28 | vw_set_ptt_inverted(true); // Required for DR3100 29 | vw_setup(2000); // Bits per sec 30 | 31 | vw_rx_start(); // Start the receiver PLL running 32 | 33 | pinMode(led_pin, OUTPUT); 34 | } 35 | 36 | void loop() 37 | { 38 | uint8_t buf[VW_MAX_MESSAGE_LEN]; 39 | uint8_t buflen = VW_MAX_MESSAGE_LEN; 40 | 41 | if (vw_get_message(buf, &buflen)) // Non-blocking 42 | { 43 | int i; 44 | 45 | digitalWrite(led_pin, HIGH); // Flash a light to show received good message 46 | // Message with a good checksum received, dump it. 47 | Serial.print("Got: "); 48 | 49 | for (i = 0; i < buflen; i++) 50 | { 51 | Serial.print(buf[i], HEX); 52 | Serial.print(' '); 53 | } 54 | Serial.println(); 55 | digitalWrite(led_pin, LOW); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/examples/server/server.pde: -------------------------------------------------------------------------------- 1 | // server.pde 2 | // 3 | // Simple example of how to use VirtualWire to send and receive messages 4 | // with a DR3100 module. 5 | // Wait for a message from another arduino running the 'client' example, 6 | // and send a reply. 7 | // You can use this as the basis of a remote control/remote sensing system 8 | // 9 | // See VirtualWire.h for detailed API docs 10 | // Author: Mike McCauley (mikem@airspayce.com) 11 | // Copyright (C) 2008 Mike McCauley 12 | // $Id: server.pde,v 1.1 2008/04/20 09:24:17 mikem Exp $ 13 | 14 | #include 15 | 16 | void setup() 17 | { 18 | Serial.begin(9600); // Debugging only 19 | Serial.println("setup"); 20 | 21 | // Initialise the IO and ISR 22 | vw_set_ptt_inverted(true); // Required for DR3100 23 | vw_setup(2000); // Bits per sec 24 | vw_rx_start(); // Start the receiver PLL running 25 | } 26 | 27 | void loop() 28 | { 29 | const char *msg = "hello"; 30 | uint8_t buf[VW_MAX_MESSAGE_LEN]; 31 | uint8_t buflen = VW_MAX_MESSAGE_LEN; 32 | 33 | // Wait for a message 34 | vw_wait_rx(); 35 | if (vw_get_message(buf, &buflen)) // Non-blocking 36 | { 37 | int i; 38 | const char *msg = "goodbye"; 39 | 40 | digitalWrite(13, true); // Flash a light to show received good message 41 | // Message with a good checksum received, dump it. 42 | Serial.print("Got: "); 43 | 44 | for (i = 0; i < buflen; i++) 45 | { 46 | Serial.print(buf[i], HEX); 47 | Serial.print(" "); 48 | } 49 | Serial.println(""); 50 | 51 | // Send a reply 52 | vw_send((uint8_t *)msg, strlen(msg)); 53 | digitalWrite(13, false); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/examples/transmitter/transmitter.pde: -------------------------------------------------------------------------------- 1 | // transmitter.pde 2 | // 3 | // Simple example of how to use VirtualWire to transmit messages 4 | // Implements a simplex (one-way) transmitter with an TX-C1 module 5 | // 6 | // See VirtualWire.h for detailed API docs 7 | // Author: Mike McCauley (mikem@airspayce.com) 8 | // Copyright (C) 2008 Mike McCauley 9 | // $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $ 10 | 11 | #include 12 | 13 | const int led_pin = 11; 14 | const int transmit_pin = 12; 15 | const int receive_pin = 2; 16 | const int transmit_en_pin = 3; 17 | 18 | void setup() 19 | { 20 | // Initialise the IO and ISR 21 | vw_set_tx_pin(transmit_pin); 22 | vw_set_rx_pin(receive_pin); 23 | vw_set_ptt_pin(transmit_en_pin); 24 | vw_set_ptt_inverted(true); // Required for DR3100 25 | vw_setup(2000); // Bits per sec 26 | pinMode(led_pin, OUTPUT); 27 | } 28 | 29 | byte count = 1; 30 | 31 | void loop() 32 | { 33 | char msg[7] = {'h','e','l','l','o',' ','#'}; 34 | 35 | msg[6] = count; 36 | digitalWrite(led_pin, HIGH); // Flash a light to show transmitting 37 | vw_send((uint8_t *)msg, 7); 38 | vw_wait_tx(); // Wait until the whole message is gone 39 | digitalWrite(led_pin, LOW); 40 | delay(1000); 41 | count = count + 1; 42 | } 43 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/VirtualWire/keywords.txt: -------------------------------------------------------------------------------- 1 | VirtualWire KEYWORD1 2 | 3 | VW_MAX_MESSAGE_LEN LITERAL1 4 | VW_MAX_PAYLOAD LITERAL1 5 | 6 | vw_set_tx_pin KEYWORD1 7 | vw_set_rx_pin KEYWORD1 8 | vw_set_ptt_pin KEYWORD1 9 | vw_set_ptt_inverted KEYWORD1 10 | vw_setup KEYWORD1 11 | vw_rx_start KEYWORD1 12 | vw_rx_stop KEYWORD1 13 | vx_tx_active KEYWORD1 14 | vw_wait_tx KEYWORD1 15 | vw_wait_rx KEYWORD1 16 | vw_wait_rx_max KEYWORD1 17 | vw_send KEYWORD1 18 | vw_have_message KEYWORD1 19 | vw_get_message KEYWORD1 20 | 21 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/receiver/receiver.ino: -------------------------------------------------------------------------------- 1 | //simple Tx on pin D12 2 | //Written By : Mohannad Rawashdeh 3 | // 3:00pm , 13/6/2013 4 | //http://www.genotronex.com/ 5 | //.................................. 6 | #include 7 | void setup() 8 | { 9 | vw_set_ptt_inverted(true); // Required for DR3100 10 | vw_set_rx_pin(12); 11 | vw_setup(4000); // Bits per sec 12 | pinMode(13, OUTPUT); 13 | 14 | vw_rx_start(); // Start the receiver PLL running 15 | } 16 | void loop() 17 | { 18 | uint8_t buf[VW_MAX_MESSAGE_LEN]; 19 | uint8_t buflen = VW_MAX_MESSAGE_LEN; 20 | 21 | if (vw_get_message(buf, &buflen)) // Non-blocking 22 | { 23 | if (buf[0] == '1') { 24 | 25 | 26 | digitalWrite(13, 1); 27 | } 28 | if (buf[0] == '0') { 29 | digitalWrite(13, 0); 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Ejercicio40-Modulos_RF/transmitter/transmitter.ino: -------------------------------------------------------------------------------- 1 | //simple Tx on pin D7 2 | //Written By : Mohannad Rawashdeh 3 | // 3:00pm , 13/6/2013 4 | //http://www.genotronex.com/ 5 | //.................................. 6 | #include 7 | char *controller; 8 | void setup() { 9 | pinMode(13, OUTPUT); 10 | vw_set_ptt_inverted(true); // 11 | vw_set_tx_pin(7); 12 | vw_setup(4000);// speed of data transfer Kbps 13 | } 14 | 15 | void loop() { 16 | controller = "1" ; 17 | vw_send((uint8_t *)controller, strlen(controller)); 18 | vw_wait_tx(); // Wait until the whole message is gone 19 | digitalWrite(13, 1); 20 | delay(2000); 21 | controller = "0" ; 22 | vw_send((uint8_t *)controller, strlen(controller)); 23 | vw_wait_tx(); // Wait until the whole message is gone 24 | digitalWrite(13, 0); 25 | delay(2000); 26 | } 27 | -------------------------------------------------------------------------------- /Ejercicio41-Bluetooth/HC05/HC05.ino: -------------------------------------------------------------------------------- 1 | #include 2 | SoftwareSerial BT1(10, 11); // RX | TX 3 | void setup() 4 | { pinMode(8, OUTPUT); // Al poner en HIGH forzaremos el modo AT 5 | pinMode(9, OUTPUT); // cuando se alimente de aqui 6 | digitalWrite(9, HIGH); 7 | delay (500) ; // Espera antes de encender el modulo 8 | Serial.begin(9600); 9 | Serial.println("Levantando el modulo HC-05"); 10 | digitalWrite (8, HIGH); //Enciende el modulo 11 | Serial.println("Esperando comandos AT:"); 12 | //BT1.begin(38400); //MODO AT (PULSAR BOTON AL REINICIAR) 13 | BT1.begin(9600); //MODO BYTE STREAM 14 | } 15 | 16 | void loop() 17 | { if (BT1.available()) 18 | Serial.write(BT1.read()); 19 | if (Serial.available()) 20 | BT1.write(Serial.read()); 21 | } 22 | -------------------------------------------------------------------------------- /Ejercicio41-Bluetooth/basic_bluetooth/basic_bluetooth.ino: -------------------------------------------------------------------------------- 1 | /* Turn an LED on/off based on a command send via BlueTooth 2 | ** 3 | ** Credit: The following example was used as a reference 4 | ** Rui Santos: http://randomnerdtutorials.wordpress.com 5 | */ 6 | #include 7 | SoftwareSerial BT1(10, 11); // RX | TX 8 | 9 | int ledPin = 13; // use the built in LED on pin 13 of the Uno 10 | int state = 0; 11 | int flag = 0; // make sure that you return the state only once 12 | void setup() { 13 | // sets the pins as outputs: 14 | pinMode(ledPin, OUTPUT); 15 | digitalWrite(ledPin, LOW); 16 | Serial.begin(9600); 17 | BT1.begin(9600); // Default connection rate for my 18 | } 19 | void loop() { 20 | //if some data is sent, read it and save it in the state variable 21 | if(BT1.available() > 0){ 22 | state = BT1.read(); 23 | flag=0; 24 | } 25 | // if the state is 0 the led will turn off 26 | if (state == '0') { 27 | digitalWrite(ledPin, LOW); 28 | if(flag == 0){ 29 | Serial.println("LED: off"); 30 | BT1.println("LED: off"); 31 | flag = 1; 32 | } 33 | } 34 | // if the state is 1 the led will turn on 35 | else if (state == '1') { 36 | digitalWrite(ledPin, HIGH); 37 | if(flag == 0){ 38 | Serial.println("LED: on"); 39 | BT1.println("LED: on"); 40 | flag = 1; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Ejercicio43-RTC/Display_Hora/Display_Hora.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | SoftwareSerial mySerial(9, 8); 7 | 8 | void setup() { 9 | 10 | Serial.begin(9600); 11 | while (!Serial) ; // wait for Arduino Serial Monitor 12 | Serial.println("Ejemplo Reloj Display"); 13 | Serial.println("-------------------"); 14 | 15 | mySerial.begin(9600); 16 | delay(500); 17 | } 18 | 19 | void loop() { 20 | tmElements_t tm; 21 | 22 | mySerial.write(0xFE); //command flag 23 | mySerial.write(0x01); //clear command. 24 | 25 | if (RTC.read(tm)) { 26 | mySerial.write(0xFE); 27 | mySerial.write(128); //First Line 28 | mySerial.print((int)tm.Day); 29 | mySerial.print("/"); 30 | mySerial.print((int)tm.Month); 31 | mySerial.print("/"); 32 | mySerial.print((int)tmYearToCalendar(tm.Year)); 33 | 34 | mySerial.write(0xFE); 35 | mySerial.write(192); //Second Line 36 | print2digits(tm.Hour); 37 | mySerial.print(":"); 38 | print2digits(tm.Minute); 39 | mySerial.print(":"); 40 | print2digits(tm.Second); 41 | 42 | Serial.print("Time = "); 43 | Serial.print(tm.Hour); 44 | Serial.write(':'); 45 | Serial.print(tm.Minute); 46 | Serial.write(':'); 47 | Serial.print(tm.Second); 48 | Serial.print(", Date (D/M/Y) = "); 49 | Serial.print(tm.Day); 50 | Serial.write('/'); 51 | Serial.print(tm.Month); 52 | Serial.write('/'); 53 | Serial.print(tmYearToCalendar(tm.Year)); 54 | Serial.println(); 55 | 56 | Serial.println(millis()); 57 | Serial.println(now()); 58 | Serial.println(RTC.get()); 59 | Serial.println(makeTime(tm)); 60 | } else { 61 | if (RTC.chipPresent()) { 62 | Serial.println("The DS1307 is stopped. Please run the SetTime"); 63 | Serial.println("example to initialize the time and begin running."); 64 | Serial.println(); 65 | } else { 66 | Serial.println("DS1307 read error! Please check the circuitry."); 67 | Serial.println(); 68 | } 69 | delay(9000); 70 | } 71 | delay(1000); 72 | } 73 | 74 | void print2digits(int number) { 75 | if (number >= 0 && number < 10) { 76 | mySerial.print('0'); 77 | } 78 | mySerial.print(number); 79 | } 80 | -------------------------------------------------------------------------------- /Ejercicio44-Test_Library/Test/Test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Test.h - Test library for Arduino - implementation 3 | by aprendiendoarduino based on Test wiring library by John Doe. 4 | */ 5 | 6 | // include core Arduino API 7 | #include "Arduino.h" 8 | 9 | // include this library's description file 10 | #include "Test.h" 11 | 12 | // Constructor ///////////////////////////////////////////////////////////////// 13 | // Function that handles the creation and setup of instances 14 | 15 | Test::Test(int givenValue) 16 | { 17 | // initialize this instance's variables 18 | value = givenValue; 19 | 20 | // do whatever is required to initialize the library 21 | pinMode(13, OUTPUT); 22 | Serial.begin(9600); 23 | } 24 | 25 | // Public Methods ////////////////////////////////////////////////////////////// 26 | // Functions available in Wiring sketches, this library, and other libraries 27 | 28 | void Test::doSomething(void) 29 | { 30 | // eventhough this function is public, it can access 31 | // and modify this library's private variables 32 | Serial.print("value is "); 33 | Serial.println(value); 34 | 35 | // it can also call private functions of this library 36 | doSomethingSecret(); 37 | } 38 | 39 | // Private Methods ///////////////////////////////////////////////////////////// 40 | // Functions only available to other functions in this library 41 | 42 | void Test::doSomethingSecret(void) 43 | { 44 | digitalWrite(13, HIGH); 45 | delay(200); 46 | digitalWrite(13, LOW); 47 | delay(200); 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Ejercicio44-Test_Library/Test/Test.h: -------------------------------------------------------------------------------- 1 | /* 2 | Test.h - Test library for Arduino - header file 3 | by aprendiendoarduino based on Test wiring library by John Doe. 4 | */ 5 | 6 | // ensure this library description is only included once 7 | #ifndef Test_h 8 | #define Test_h 9 | 10 | // include types & constants of Arduino core API 11 | #include "Arduino.h" 12 | 13 | // library interface description 14 | class Test 15 | { 16 | // user-accessible "public" interface 17 | public: 18 | Test(int); //Contructor 19 | void doSomething(void); 20 | 21 | // library-accessible "private" interface 22 | private: 23 | int value; 24 | void doSomethingSecret(void); 25 | }; 26 | 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /Ejercicio44-Test_Library/Test/examples/doing_something/doing_something.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Doing Something 4 | // by aprendiendoarduino 5 | 6 | // Demostrates how to do something with the Test library 7 | 8 | Test myTest = Test(2); 9 | 10 | void setup() 11 | { 12 | } 13 | 14 | void loop() 15 | { 16 | myTest.doSomething(); 17 | delay(500); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Ejercicio44-Test_Library/Test/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Test 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | Test KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | doSomething KEYWORD2 16 | 17 | ####################################### 18 | # Instances (KEYWORD2) 19 | ####################################### 20 | 21 | ####################################### 22 | # Constants (LITERAL1) 23 | ####################################### 24 | 25 | -------------------------------------------------------------------------------- /Ejercicio44-Test_Library/Test/readme.txt: -------------------------------------------------------------------------------- 1 | This is an example C++ library for Arduino 0004+, based on one created by 2 | Nicholas Zambetti for Wiring 0006+ 3 | 4 | Installation 5 | -------------------------------------------------------------------------------- 6 | 7 | To install this library, just place this entire folder as a subfolder in your 8 | Arduino/lib/targets/libraries folder configured in Arduino IDE preferences 9 | 10 | When installed in Windows, this library should look like: 11 | 12 | C:\Users\user\Documents\libraries\Test (this library's folder) 13 | C:\Users\user\Documents\libraries\Test\Test.cpp (the library implementation file) 14 | C:\Users\user\Documents\libraries\Test\Test.h (the library description file) 15 | C:\Users\user\Documents\libraries\Test\keywords.txt (the syntax coloring file) 16 | C:\Users\user\Documents\libraries\Test\examples (the examples in the "open" menu) 17 | C:\Users\user\Documents\libraries\Test\readme.txt (this file) 18 | 19 | Building 20 | -------------------------------------------------------------------------------- 21 | 22 | After this library is installed, you just have to start the Arduino application. 23 | You may see a few warning messages as it's built. 24 | 25 | To use this library in a sketch, go to the Sketch | Import Library menu and 26 | select Test. This will add a corresponding line to the top of your sketch: 27 | #include 28 | 29 | To stop using this library, delete that line from your sketch. 30 | 31 | Geeky information: 32 | After a successful build of this library, a new file named "Test.o" will appear 33 | in "Arduino/lib/targets/libraries/Test". This file is the built/compiled library 34 | code. 35 | 36 | If you choose to modify the code for this library (i.e. "Test.cpp" or "Test.h"), 37 | then you must first 'unbuild' this library by deleting the "Test.o" file. The 38 | new "Test.o" with your code will appear after the next press of "verify" 39 | 40 | -------------------------------------------------------------------------------- /Ejercicio44-Test_Library/readme.md: -------------------------------------------------------------------------------- 1 | ###Test Library 2 | Example and template for Arduino libraries -------------------------------------------------------------------------------- /Ejercicio45-Serial_Plotter/analogica/analogica.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Serial Plotter Example 3 | */ 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | } 8 | 9 | void loop() { 10 | Serial.println(analogRead(A0)); 11 | delay(5); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Ejercicio45-Serial_Plotter/ecuacion/ecuacion.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Serial Plotter Example 3 | */ 4 | 5 | int x = -20; 6 | 7 | void setup() { 8 | Serial.begin(9600); 9 | } 10 | 11 | void loop() { 12 | Serial.println(x*x); 13 | x++; 14 | delay(5); 15 | if (x == 20) x = -20; 16 | } 17 | -------------------------------------------------------------------------------- /Ejercicio46-Estructuras_de_Control/Ejercicio46-Estructuras_de_Control.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Estructuras de Control 3 | Menu interactivo 4 | */ 5 | 6 | void setup() { 7 | Serial.begin(9600); 8 | String titulo = "Menu Interactivo con Arduino"; 9 | for (int i = 0; i < titulo.length(); i++) { 10 | Serial.print("-"); 11 | } 12 | Serial.print("\n" + titulo + "\n"); 13 | for (int i = 0; i < titulo.length(); i++) { 14 | Serial.print("-"); 15 | } 16 | Serial.println(); 17 | } 18 | 19 | void loop() { 20 | int respuesta; 21 | do { 22 | respuesta = menu(); 23 | switch (respuesta) { 24 | case 1: 25 | Serial.println("Ejecucion de Opcion 1"); 26 | delay(5000); 27 | break; 28 | case 2: 29 | Serial.println("Ejecucion de Opcion 2"); 30 | delay(5000); 31 | break; 32 | case 3: 33 | Serial.println("Ejecucion de Opcion 3"); 34 | delay(5000); 35 | break; 36 | case 4: 37 | Serial.println("Adios..."); 38 | while (true) { 39 | //se queda aqui el programa 40 | } 41 | break; 42 | default: 43 | Serial.println("Opcion de menu incorrecta!!!!!"); 44 | break; 45 | } 46 | limpia_terminal(); 47 | } while ((respuesta < 1) || (respuesta > 4)); 48 | } 49 | 50 | int menu() { 51 | String leido = ""; 52 | 53 | Serial.println("Elige opcion a ejecutar:"); 54 | Serial.println("1 - Multiplicar un numero por 15"); 55 | Serial.println("2 - Repetir 10 veces una cadena"); 56 | Serial.println("3 - Repetir \"hola\" hasta pulsar una tecla"); 57 | Serial.println("4 - Fin"); 58 | 59 | while (Serial.available() == 0) { 60 | //Ho hago nada 61 | } 62 | 63 | do { 64 | char caracter_leido; 65 | caracter_leido = Serial.read(); 66 | leido += caracter_leido; 67 | delay(5); 68 | } while (Serial.available() > 0); 69 | 70 | if (leido.toInt() != 0) 71 | return leido.toInt(); 72 | else return 0; 73 | 74 | } 75 | 76 | void limpia_terminal() { //Solo v�lido para usar con putty 77 | Serial.write(27); // ESC command 78 | Serial.print("[2J"); // clear screen command 79 | Serial.write(27); 80 | Serial.print("[H"); // cursor to home command 81 | } 82 | -------------------------------------------------------------------------------- /Ejercicio47-Motor_Shield/Ejercicio47-Motor_Shield.ino: -------------------------------------------------------------------------------- 1 | const int 2 | PWM_A = 3, 3 | DIR_A = 12, 4 | BRAKE_A = 9, 5 | SNS_A = A0; 6 | 7 | 8 | void setup() { 9 | // Configure the A output 10 | pinMode(BRAKE_A, OUTPUT); // Brake pin on channel A 11 | pinMode(DIR_A, OUTPUT); // Direction pin on channel A 12 | 13 | // Open Serial communication 14 | Serial.begin(9600); 15 | Serial.println("Motor shield DC motor Test:\n"); 16 | } 17 | 18 | void loop() { 19 | 20 | // Set the outputs to run the motor forward 21 | 22 | digitalWrite(BRAKE_A, LOW); // setting brake LOW disable motor brake 23 | digitalWrite(DIR_A, HIGH); // setting direction to HIGH the motor will spin forward 24 | 25 | analogWrite(PWM_A, 255); // Set the speed of the motor, 255 is the maximum value 26 | 27 | delay(5000); // hold the motor at full speed for 5 seconds 28 | Serial.print("current consumption at full speed: "); 29 | Serial.println(analogRead(SNS_A)); 30 | 31 | // Brake the motor 32 | 33 | Serial.println("Start braking\n"); 34 | // raising the brake pin the motor will stop faster than the stop by inertia 35 | digitalWrite(BRAKE_A, HIGH); // raise the brake 36 | delay(5000); 37 | 38 | // Set the outputs to run the motor backward 39 | 40 | Serial.println("Backward"); 41 | digitalWrite(BRAKE_A, LOW); // setting againg the brake LOW to disable motor brake 42 | digitalWrite(DIR_A, LOW); // now change the direction to backward setting LOW the DIR_A pin 43 | 44 | analogWrite(PWM_A, 255); // Set the speed of the motor 45 | 46 | delay(5000); 47 | Serial.print("current consumption backward: "); 48 | Serial.println(analogRead(SNS_A)); 49 | 50 | // now stop the motor by inertia, the motor will stop slower than with the brake function 51 | analogWrite(PWM_A, 0); // turn off power to the motor 52 | 53 | Serial.print("current brake: "); 54 | Serial.println(analogRead(A0)); 55 | Serial.println("End of the motor shield test with DC motors. Thank you!"); 56 | 57 | 58 | while(1); 59 | } -------------------------------------------------------------------------------- /Ejercicio48-Efectos_Led/Ejercicio48-Efectos_Led.ino: -------------------------------------------------------------------------------- 1 | /* Efectos Led 2 | -------------- 3 | @author: aprendiendoarduino 4 | 5 | */ 6 | 7 | int pinArray[] = {2, 3, 4, 5, 6, 7}; 8 | int count = 0; 9 | int timer = 100; 10 | 11 | void setup() { 12 | // we make all the declarations at once 13 | for (count = 0; count < 6; count++) { 14 | pinMode(pinArray[count], OUTPUT); 15 | } 16 | } 17 | 18 | void loop() { 19 | //timer = analogRead(A0); //El valor leido por analog read es el temporizador 20 | for (count = 0; count < 6; count++) { 21 | //timer = analogRead(A0); 22 | digitalWrite(pinArray[count], HIGH); 23 | delay(timer); 24 | } 25 | for (count = 0; count < 6; count++) { 26 | //timer = analogRead(A0); 27 | digitalWrite(pinArray[count], LOW); 28 | delay(timer); 29 | } 30 | for (count = 5; count >= 0; count--) { 31 | //timer = analogRead(A0); 32 | digitalWrite(pinArray[count], HIGH); 33 | delay(timer); 34 | } 35 | for (count = 5; count >= 0; count--) { 36 | //timer = analogRead(A0); 37 | digitalWrite(pinArray[count], LOW); 38 | delay(timer); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Ejercicio50-DAQ_SD/Ejercicio50-DAQ_SD.ino: -------------------------------------------------------------------------------- 1 | //Data Acquisition (DAQ) a tarjeta SD 2 | //Ejemplo para leer y guardar en una tarjeta SD cada 30 segundos la tempertura y humedad de una sonda DHT22 3 | //Los datos son guardados en un fichero llamado datalog.csv 4 | //Los datos también son mostrados por en una pantalla LCD modelo https://www.sparkfun.com/products/9395 5 | //Para leer los datos de SD usar sketck: https://www.arduino.cc/en/Tutorial/DumpFile 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | const int chipSelect = 4; 12 | const int dht_pin = 8; 13 | 14 | dht DHT; 15 | 16 | void setup() { 17 | // Open serial communications and wait for port to open: 18 | Serial.begin(9600); 19 | while (!Serial) { 20 | ; // wait for serial port to connect. Needed for native USB port only 21 | } 22 | 23 | MsTimer2::set(30000, saveData); // 30 sec period 24 | MsTimer2::start(); 25 | 26 | Serial.println("Initializing SD card..."); 27 | 28 | // see if the card is present and can be initialized: 29 | if (!SD.begin(chipSelect)) { 30 | Serial.println("Card failed, or not present"); 31 | // don't do anything more: 32 | return; 33 | } 34 | Serial.println("card initialized."); 35 | if (!SD.exists("datalog.csv")) { 36 | File dataFile = SD.open("datalog.csv", FILE_WRITE); 37 | dataFile.println("Time(sec),Temperature,Humidity"); 38 | Serial.println("Creo nuevo fichero"); 39 | dataFile.close(); 40 | } 41 | else Serial.println("Fichero existente"); 42 | } 43 | 44 | void loop() { 45 | //mando al display la información 46 | Serial.write(" "); // clear display 47 | Serial.write(" "); 48 | Serial.write(254); // move cursor to beginning of first line 49 | Serial.write(128); 50 | int chk = DHT.read22(dht_pin); 51 | if (chk == DHTLIB_OK) { 52 | Serial.print("Temp: " + String(DHT.temperature)); 53 | Serial.write(254); // move cursor to beginning of second line 54 | Serial.write(192); 55 | Serial.print("Humedad: " + String(DHT.humidity)); 56 | } 57 | else { 58 | Serial.print("Error sonda"); 59 | } 60 | delay(1000); 61 | } 62 | 63 | void saveData() 64 | { 65 | // make a string for assembling the data to log: 66 | String dataString = String(millis() / 1000) + ","; 67 | 68 | dataString = dataString + DHT.temperature + "," + DHT.humidity + "," ; 69 | 70 | // open the file. note that only one file can be open at a time, 71 | // so you have to close this one before opening another. 72 | File dataFile = SD.open("datalog.csv", FILE_WRITE); 73 | 74 | // if the file is available, write to it: 75 | if (dataFile) { 76 | dataFile.println(dataString); 77 | dataFile.close(); 78 | // print to the serial port too: 79 | Serial.println(dataString); 80 | } 81 | // if the file isn't open, pop up an error: 82 | else { 83 | Serial.println("error opening datalog.txt"); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Ejercicio50-DAQ_SD/README.md: -------------------------------------------------------------------------------- 1 | ##Data Acquisition (DAQ) a tarjeta SD 2 | 3 | **Función** Leer y guardar en una tarjeta SD cada 30 segundos la tempertura y humedad de una sonda DHT22. 4 | 5 | Los datos son guardados en un fichero llamado datalog.csv 6 | 7 | Los datos también son mostrados por en una pantalla LCD modelo https://www.sparkfun.com/products/9395 8 | 9 | Para leer los datos de la SD y sacarlos por el puerto serie, usar sketck: https://www.arduino.cc/en/Tutorial/DumpFile -------------------------------------------------------------------------------- /Ejercicio50-DAQ_SD/dumpFile/dumpFile.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SD card file dump 3 | 4 | This example shows how to read a file from the SD card using the 5 | SD library and send it over the serial port. 6 | 7 | The circuit: 8 | SD card attached to SPI bus as follows: 9 | ** MOSI - pin 11 10 | ** MISO - pin 12 11 | ** CLK - pin 13 12 | ** CS - pin 4 13 | 14 | created 22 December 2010 15 | by Limor Fried 16 | modified 9 Apr 2012 17 | by Tom Igoe 18 | 19 | This example code is in the public domain. 20 | 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | const int chipSelect = 4; 27 | 28 | void setup() { 29 | // Open serial communications and wait for port to open: 30 | Serial.begin(9600); 31 | while (!Serial) { 32 | ; // wait for serial port to connect. Needed for native USB port only 33 | } 34 | 35 | 36 | Serial.print("Initializing SD card..."); 37 | 38 | // see if the card is present and can be initialized: 39 | if (!SD.begin(chipSelect)) { 40 | Serial.println("Card failed, or not present"); 41 | // don't do anything more: 42 | return; 43 | } 44 | Serial.println("card initialized."); 45 | 46 | // open the file. note that only one file can be open at a time, 47 | // so you have to close this one before opening another. 48 | File dataFile = SD.open("datalog.txt"); 49 | 50 | // if the file is available, write to it: 51 | if (dataFile) { 52 | while (dataFile.available()) { 53 | Serial.write(dataFile.read()); 54 | } 55 | dataFile.close(); 56 | } 57 | // if the file isn't open, pop up an error: 58 | else { 59 | Serial.println("error opening datalog.txt"); 60 | } 61 | } 62 | 63 | void loop() { 64 | } 65 | -------------------------------------------------------------------------------- /Ejercicio52-Detectar_Alarma/Ejercicio52-Detectar_Alarma.ino: -------------------------------------------------------------------------------- 1 | const int analogPin = A0; // pin that the sensor is attached to 2 | const int ledPin = 13; // pin that the LED is attached to 3 | const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input 4 | boolean alarma; //1 = ALARMA, 0 = NO ALARMA 5 | 6 | void setup() { 7 | // initialize the LED pin as an output: 8 | pinMode(ledPin, OUTPUT); 9 | // initialize serial communications: 10 | Serial.begin(9600); 11 | int analogValue = analogRead(analogPin); //Inicializo el valor de la alarma 12 | if (analogValue > threshold) { 13 | alarma = 1; 14 | } 15 | else alarma = 0; 16 | } 17 | 18 | void loop() { 19 | // read the value of the potentiometer: 20 | int analogValue = analogRead(analogPin); //En cada loop compruebo el valor de la entrada analógica 21 | 22 | // if the analog value is high enough, turn on the LED: 23 | if (analogValue > threshold) { 24 | if (alarma == 0) { 25 | digitalWrite(ledPin, HIGH); 26 | Serial.println("ALARMA!!!!!!!!!!!!!!"); 27 | alarma = 1; 28 | } 29 | } else { 30 | if (alarma == 1){ 31 | digitalWrite(ledPin, LOW); 32 | Serial.println("ALARMA RECUPERADA"); 33 | alarma = 0; 34 | } 35 | } 36 | 37 | // print the analog value: 38 | Serial.println(analogValue); 39 | delay(500); // delay para leer mejor la salida 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Ejercicio52-Detectar_Alarma_Histeresis/Ejercicio52-Detectar_Alarma_Histeresis.ino: -------------------------------------------------------------------------------- 1 | const int analogPin = A0; // pin that the sensor is attached to 2 | const int ledPin = 13; // pin that the LED is attached to 3 | const int threshold_base; 4 | int threshold; // an arbitrary threshold level that's in the range of the analog input 5 | const int histeresis = 10; //Valor de la histeresis 6 | boolean alarma; //1 = ALARMA, 0 = NO ALARMA 7 | 8 | void setup() { 9 | // initialize the LED pin as an output: 10 | pinMode(ledPin, OUTPUT); 11 | // initialize serial communications: 12 | Serial.begin(9600); 13 | int analogValue = analogRead(analogPin); //Inicializo el valor de la alarma 14 | if (analogValue > threshold_base) { 15 | alarma = 1; 16 | threshold = threshold_base - histeresis; 17 | } 18 | else { 19 | alarma = 0; 20 | threshold = threshold_base + histeresis; 21 | } 22 | } 23 | 24 | void loop() { 25 | // read the value of the potentiometer: 26 | int analogValue = analogRead(analogPin); //En cada loop compruebo el valor de la entrada analógica 27 | 28 | // if the analog value is high enough, turn on the LED: 29 | if (analogValue > threshold) { 30 | if (alarma == 0) { 31 | digitalWrite(ledPin, HIGH); 32 | Serial.println("ALARMA!!!!!!!!!!!!!!"); 33 | alarma = 1; 34 | threshold = threshold_base - histeresis; 35 | } 36 | } else { 37 | if (alarma == 1){ 38 | digitalWrite(ledPin, LOW); 39 | Serial.println("ALARMA RECUPERADA"); 40 | alarma = 0; 41 | threshold = threshold_base + histeresis; 42 | } 43 | } 44 | 45 | // print the analog value: 46 | Serial.println(analogValue); 47 | delay(500); // delay para leer mejor la salida 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Ejercicio53-Watchdog/Ejercicio53-Watchdog.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int loops = 0; 4 | 5 | void setup() 6 | { 7 | Serial.begin(9600); 8 | wdt_disable(); 9 | wdt_enable(WDTO_8S); 10 | Serial.println("iniciando"); 11 | delay(1000); 12 | } 13 | 14 | void loop() 15 | { 16 | Serial.println("hola " + String(loops)); 17 | delay(1000); 18 | if (loops > 20) { 19 | Serial.println("entro en bucle largo"); 20 | for (int i = 0; i < 100; i++) { 21 | Serial.println(i); 22 | delay(1000); 23 | } 24 | } 25 | wdt_reset(); 26 | loops++; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Ejercicio54-MotorDC_Basico/Ejercicio54-MotorDC_Basico.ino: -------------------------------------------------------------------------------- 1 | /* 2 | www.diymakers.es 3 | by A.García 4 | Control velocidad y sentido motor DC 5 | Tutorial en: http://diymakers.es/control-velocidad-y-sentido-de-motor-dc/ 6 | */ 7 | 8 | int pin2=9; //Entrada 2 del L293D 9 | int pin7=10; //Entrada 7 del L293D 10 | int pote=A0; //Potenciómetro 11 | 12 | int valorpote; //Variable que recoge el valor del potenciómetro 13 | int pwm1; //Variable del PWM 1 14 | int pwm2; //Variable del PWM 2 15 | 16 | void setup() 17 | { 18 | //Inicializamos los pins de salida 19 | pinMode(pin2,OUTPUT); 20 | pinMode(pin7, OUTPUT); 21 | } 22 | 23 | void loop() 24 | { 25 | 26 | //Almacenamos el valor del potenciómetro en la variable 27 | valorpote=analogRead(pote); 28 | 29 | //Como la entrada analógica del Arduino es de 10 bits, el rango va de 0 a 1023. 30 | //En cambio, la salidas del Arduio son de 8 bits, quiere decir, rango entre 0 a 255. 31 | //Por esta razón tenemos que mapear el número de un rango a otro usando este código. 32 | pwm1 = map(valorpote, 0, 1023, 0, 255); 33 | pwm2 = map(valorpote, 0, 1023, 255, 0); //El PWM 2 esta invertido respecto al PWM 1 34 | 35 | //Sacamos el PWM de las dos salidas usando analogWrite(pin,valor) 36 | analogWrite(pin2,pwm1); 37 | analogWrite(pin7,pwm2); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Ejercicio55-Servo/Knob/Knob.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Controlling a servo position using a potentiometer (variable resistor) 3 | by Michal Rinott 4 | 5 | modified on 8 Nov 2013 6 | by Scott Fitzgerald 7 | http://www.arduino.cc/en/Tutorial/Knob 8 | */ 9 | 10 | #include 11 | 12 | Servo myservo; // create servo object to control a servo 13 | 14 | int potpin = 0; // analog pin used to connect the potentiometer 15 | int val; // variable to read the value from the analog pin 16 | 17 | void setup() { 18 | myservo.attach(9); // attaches the servo on pin 9 to the servo object 19 | } 20 | 21 | void loop() { 22 | val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) 23 | val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) 24 | myservo.write(val); // sets the servo position according to the scaled value 25 | delay(15); // waits for the servo to get there 26 | } 27 | -------------------------------------------------------------------------------- /Ejercicio55-Servo/Knob_Serial/Knob_Serial.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int posicion = 0; 4 | 5 | Servo myservo; // create servo object to control a servo 6 | // twelve servo objects can be created on most boards 7 | 8 | int pos = 0; // variable to store the servo position 9 | 10 | void setup() { 11 | Serial.begin(9600); 12 | myservo.attach(9); // attaches the servo on pin 9 to the servo object 13 | } 14 | 15 | void loop() { 16 | 17 | if (Serial.available() > 0) { 18 | String grados = ""; 19 | do { 20 | grados = grados + (char)Serial.read(); 21 | Serial.println(grados); 22 | delay(5); 23 | } 24 | while (Serial.available() > 0); 25 | Serial.println(grados); 26 | posicion = grados.toInt(); 27 | myservo.write(posicion); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Ejercicio55-Servo/Sweep/Sweep.ino: -------------------------------------------------------------------------------- 1 | /* Sweep 2 | by BARRAGAN 3 | This example code is in the public domain. 4 | 5 | modified 8 Nov 2013 6 | by Scott Fitzgerald 7 | http://www.arduino.cc/en/Tutorial/Sweep 8 | */ 9 | 10 | #include 11 | 12 | Servo myservo; // create servo object to control a servo 13 | // twelve servo objects can be created on most boards 14 | 15 | int pos = 0; // variable to store the servo position 16 | 17 | void setup() { 18 | myservo.attach(9); // attaches the servo on pin 9 to the servo object 19 | } 20 | 21 | void loop() { 22 | for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees 23 | // in steps of 1 degree 24 | myservo.write(pos); // tell servo to go to position in variable 'pos' 25 | delay(15); // waits 15ms for the servo to reach the position 26 | } 27 | for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees 28 | myservo.write(pos); // tell servo to go to position in variable 'pos' 29 | delay(15); // waits 15ms for the servo to reach the position 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Ejercicio56-Libreria_Morse/Morse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Morse.cpp - Library for flashing Morse code. 3 | Created by David A. Mellis, November 2, 2007. 4 | Released into the public domain. 5 | */ 6 | 7 | #include "Arduino.h" 8 | #include "Morse.h" 9 | 10 | Morse::Morse(int pin) 11 | { 12 | pinMode(pin, OUTPUT); 13 | _pin = pin; 14 | } 15 | 16 | void Morse::dot() 17 | { 18 | digitalWrite(_pin, HIGH); 19 | delay(250); 20 | digitalWrite(_pin, LOW); 21 | delay(250); 22 | } 23 | 24 | void Morse::dash() 25 | { 26 | digitalWrite(_pin, HIGH); 27 | delay(1000); 28 | digitalWrite(_pin, LOW); 29 | delay(250); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Ejercicio56-Libreria_Morse/Morse.h: -------------------------------------------------------------------------------- 1 | /* 2 | Morse.h - Library for flashing Morse code. 3 | Created by David A. Mellis, November 2, 2007. 4 | Released into the public domain. 5 | */ 6 | 7 | #ifndef Morse_h 8 | #define Morse_h 9 | 10 | #include "Arduino.h" 11 | 12 | class Morse 13 | { 14 | public: 15 | Morse(int pin); 16 | void dot(); 17 | void dash(); 18 | private: 19 | int _pin; 20 | }; 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /Ejercicio56-Libreria_Morse/examples/SOS/SOS.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | Morse morse(13); 4 | 5 | void setup() 6 | { 7 | } 8 | 9 | void loop() 10 | { 11 | morse.dot(); morse.dot(); morse.dot(); 12 | morse.dash(); morse.dash(); morse.dash(); 13 | morse.dot(); morse.dot(); morse.dot(); 14 | delay(3000); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Ejercicio56-Libreria_Morse/keywords.txt: -------------------------------------------------------------------------------- 1 | Morse KEYWORD1 2 | dash KEYWORD2 3 | dot KEYWORD2 4 | -------------------------------------------------------------------------------- /Ejercicio58-Manipular_Registros/InternalTemperature/InternalTemperature.ino: -------------------------------------------------------------------------------- 1 | // Internal Temperature Sensor 2 | // Example sketch for ATmega328 types. 3 | // 4 | // April 2012, Arduino 1.0 5 | 6 | void setup() 7 | { 8 | Serial.begin(9600); 9 | 10 | Serial.println(F("Internal Temperature Sensor")); 11 | } 12 | 13 | void loop() 14 | { 15 | // Show the temperature in degrees Celsius. 16 | Serial.println(GetTemp(), 1); 17 | delay(1000); 18 | } 19 | 20 | double GetTemp(void) 21 | { 22 | unsigned int wADC; 23 | double t; 24 | 25 | // The internal temperature has to be used 26 | // with the internal reference of 1.1V. 27 | // Channel 8 can not be selected with 28 | // the analogRead function yet. 29 | 30 | // Set the internal reference and mux. 31 | ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3)); 32 | ADCSRA |= _BV(ADEN); // enable the ADC 33 | 34 | delay(20); // wait for voltages to become stable. 35 | 36 | ADCSRA |= _BV(ADSC); // Start the ADC 37 | 38 | // Detect end-of-conversion 39 | while (bit_is_set(ADCSRA, ADSC)); 40 | 41 | // Reading register "ADCW" takes care of how to read ADCL and ADCH. 42 | wADC = ADCW; 43 | 44 | // The offset of 324.31 could be wrong. It is just an indication. 45 | t = (wADC - 324.31 ) / 1.22; 46 | 47 | // The returned temperature is in degrees Celsius. 48 | return (t); 49 | } 50 | -------------------------------------------------------------------------------- /Ejercicio59-Puertos_Digitales/Leer_pin/Leer_pin.ino: -------------------------------------------------------------------------------- 1 | void setup() { 2 | Serial.begin(9600); 3 | DDRB = B11111101; //Configura pin 1 de PORTB como entrada y el resto salida PIN 9 4 | PORTB = B00000110; // pongo a pullup el pin 9 y escribo a HIGH el pin 10 5 | } 6 | 7 | void loop() { 8 | boolean my_var = 0; //Variable para guardar la información leída en PORTD 9 | uint8_t my_var_int = 0; 10 | my_var = (PINB & (1 << PB1)); /* Lee el pin 1 de PORTD y lo coloca en la variable. */ 11 | my_var_int = (PINB & (1 << PB1)); /* Lee el pin 1 de PORTD y lo coloca en la variable. */ 12 | Serial.println("Selecionar pines:"); 13 | Serial.println(1 << PB1, BIN); //Pin 9 14 | Serial.println(1 << PB2, BIN); //Pin 10 15 | Serial.println("Estado puerto B:"); 16 | Serial.println(PINB, BIN); 17 | Serial.println("Valor del pin seleccionado en boolean y entero"); 18 | Serial.println(my_var, BIN); 19 | Serial.println(my_var_int, BIN); 20 | Serial.println("-------"); 21 | delay(1000); 22 | } 23 | -------------------------------------------------------------------------------- /Ejercicio59-Puertos_Digitales/Registos_PORT_simple/Registos_PORT_simple.ino: -------------------------------------------------------------------------------- 1 | void setup() { 2 | //PORTD maps to Arduino digital pins 0 to 7 3 | pinMode(2, INPUT_PULLUP); 4 | pinMode(3, OUTPUT); 5 | digitalWrite(3, HIGH); 6 | pinMode(4, INPUT_PULLUP); 7 | pinMode(5, OUTPUT); 8 | digitalWrite(5, HIGH); 9 | pinMode(6, INPUT_PULLUP); 10 | pinMode(7, OUTPUT); 11 | digitalWrite(7, HIGH); 12 | Serial.begin(9600); 13 | } 14 | 15 | void loop() { 16 | Serial.println("PIND - The Port D Input Pins Register. Lectura INPUT"); 17 | Serial.println(PIND, DEC); 18 | Serial.println(PIND, HEX); 19 | Serial.println(PIND, BIN); 20 | Serial.println("DDRD - The Port D Data Direction Register. DDRX 1 OUTPUT 0 INPUT"); 21 | Serial.println(DDRD, DEC); 22 | Serial.println(DDRD, HEX); 23 | Serial.println(DDRD, BIN); 24 | Serial.println("PORTD - The Port D Data Register. Escritura OUTPUT"); 25 | Serial.println(PORTD, DEC); 26 | Serial.println(PORTD, HEX); 27 | Serial.println(PORTD, BIN); 28 | delay(10000); 29 | } 30 | -------------------------------------------------------------------------------- /Ejercicio59-Puertos_Digitales/Velocidad_Read/Velocidad_Read.ino: -------------------------------------------------------------------------------- 1 | #define LECTURAS 100000L 2 | 3 | unsigned long tiempo = 0; 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | pinMode(9, INPUT_PULLUP); 8 | } 9 | 10 | void loop() { 11 | long lecturas = 0; 12 | tiempo = micros(); 13 | for (long i = 0; i < LECTURAS; i++) { 14 | lecturas += digitalRead(9); 15 | } 16 | tiempo = micros() - tiempo; 17 | Serial.println("Usando digitalRead() -->"); 18 | Serial.println("Se han leido un total de " + (String)lecturas + " HIGH"); 19 | Serial.println("La lectura ha costado " + (String)tiempo + " us"); 20 | 21 | lecturas = 0; 22 | tiempo = micros(); 23 | for (long i = 0; i < LECTURAS; i++) { 24 | lecturas += (boolean)(PINB & (1 << PB1)); 25 | } 26 | tiempo = micros() - tiempo; 27 | Serial.println("Usando PINx -->"); 28 | Serial.println("Se han leido un total de " + (String)lecturas + " HIGH"); 29 | Serial.println("La lectura ha costado " + (String)tiempo + " us"); 30 | 31 | delay(5000); 32 | } 33 | -------------------------------------------------------------------------------- /Ejercicio61-ESP8266/conectar_wifi/conectar_wifi.ino: -------------------------------------------------------------------------------- 1 | #include 2 | SoftwareSerial BT1(3, 2); // RX | TX 3 | 4 | void setup() 5 | { Serial.begin(9600); 6 | BT1.begin(9600); 7 | //Serial.println("Version"); 8 | //BT1.println("AT+GMR"); 9 | //respuesta(); 10 | //delay(500); 11 | //Serial.println("Factory reset"); 12 | //BT1.println("AT+RESTORE"); 13 | //respuesta(); 14 | //delay(500); 15 | //Serial.println("Velocidad 9600"); 16 | //BT1.println("AT+UART_DEF=9600,8,1,0,0"); 17 | //respuesta(); 18 | //delay(500); 19 | //BT1.begin(9600); 20 | Serial.println("modo cliente"); 21 | BT1.println("AT+CWMODE_DEF=1"); 22 | respuesta(); 23 | delay(500); 24 | Serial.println("conectar wifi"); 25 | BT1.println("AT+CWJAP=\"AndroidAP4628\",\"esp8266wifi\""); 26 | respuesta(); 27 | delay(500); 28 | Serial.println("comprobar la IP asignada"); 29 | BT1.println("AT+CIFSR"); 30 | respuesta(); 31 | delay(500); 32 | } 33 | 34 | void loop() 35 | { 36 | 37 | } 38 | 39 | void respuesta() { 40 | String respuesta = ""; 41 | boolean ok = 1; 42 | while (BT1.available() == 0) { 43 | //nada 44 | } 45 | do { 46 | if (BT1.available() > 0) { 47 | char caracter_leido = BT1.read(); 48 | respuesta = respuesta + caracter_leido; 49 | Serial.print(caracter_leido); 50 | if (respuesta.endsWith("OK")) ok = 0; 51 | } 52 | } while (ok || (BT1.available() == 0)); 53 | Serial.println(); 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Ejercicio61-ESP8266/conexion_serial/conexion_serial.ino: -------------------------------------------------------------------------------- 1 | #include 2 | SoftwareSerial BT1(3, 2); // RX | TX 3 | 4 | void setup() 5 | { Serial.begin(9600); 6 | BT1.begin(9600); 7 | } 8 | 9 | void loop() 10 | { 11 | if (BT1.available()) 12 | { char c = BT1.read(); 13 | Serial.print(c); 14 | } 15 | if (Serial.available()) 16 | { char c = Serial.read(); 17 | BT1.print(c); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Ejercicio62-MPU6050/raw_values/raw_values.ino: -------------------------------------------------------------------------------- 1 | // MPU-6050 Short Example Sketch 2 | // By Arduino User JohnChi 3 | // August 17, 2014 4 | // Public Domain 5 | #include 6 | const int MPU_addr=0x68; // I2C address of the MPU-6050 7 | int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; 8 | void setup(){ 9 | Wire.begin(); 10 | Wire.beginTransmission(MPU_addr); 11 | Wire.write(0x6B); // PWR_MGMT_1 register 12 | Wire.write(0); // set to zero (wakes up the MPU-6050) 13 | Wire.endTransmission(true); 14 | Serial.begin(9600); 15 | } 16 | void loop(){ 17 | Wire.beginTransmission(MPU_addr); 18 | Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) 19 | Wire.endTransmission(false); 20 | Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers 21 | AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) 22 | AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) 23 | AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L) 24 | Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L) 25 | GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L) 26 | GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L) 27 | GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L) 28 | Serial.print("AcX = "); Serial.print(AcX); 29 | Serial.print(" | AcY = "); Serial.print(AcY); 30 | Serial.print(" | AcZ = "); Serial.print(AcZ); 31 | Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet 32 | Serial.print(" | GyX = "); Serial.print(GyX); 33 | Serial.print(" | GyY = "); Serial.print(GyY); 34 | Serial.print(" | GyZ = "); Serial.println(GyZ); 35 | delay(333); 36 | } 37 | -------------------------------------------------------------------------------- /Ejercicio63-Uso_Libreria_NTP/Ejercicio63-Uso_Libreria_NTP.ino: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------* 2 | * simple-NTP library example sketch. * 3 | * Self-adjusting clock from NTP server * 4 | * aprendiendoarduino by jecrespo May 2016 * 5 | * * 6 | * This work is licensed under the Creative Commons Attribution- * 7 | * ShareAlike 3.0 Unported License. To view a copy of this license, * 8 | * visit http://creativecommons.org/licenses/by-sa/3.0/ or send a * 9 | * letter to Creative Commons, 171 Second Street, Suite 300, * 10 | * San Francisco, California, 94105, USA. * 11 | *----------------------------------------------------------------------*/ 12 | 13 | #include //http://www.arduino.cc/playground/Code/Time 14 | #include 15 | #include 16 | #include 17 | 18 | simpleNTP ntp; //simpleNTP object 19 | byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0x70, 0xEF}; 20 | byte ip[] = {192, 168, 1, 179}; 21 | 22 | void setup() 23 | { 24 | Ethernet.begin(mac, ip); 25 | delay(500); 26 | Serial.begin(9600); 27 | Serial.println("Requesting Time..."); 28 | unsigned long utc = ntp.readNTP(); //UTC time 29 | setTime(utc); 30 | Serial.println(utc); 31 | } 32 | 33 | void loop() 34 | { 35 | Serial.println(millis()); 36 | Serial.println(now()); 37 | digitalClockDisplay(); 38 | delay(1000); 39 | } 40 | 41 | void digitalClockDisplay(){ 42 | // digital clock display of the time 43 | Serial.print(year()); 44 | Serial.print("-"); 45 | Serial.print(month()); 46 | Serial.print("-"); 47 | Serial.print(day()); 48 | Serial.print(" "); 49 | Serial.print(hour()); 50 | printDigits(minute()); 51 | printDigits(second()); 52 | Serial.println(); 53 | } 54 | 55 | void printDigits(int digits){ 56 | // utility for digital clock display: prints preceding colon and leading 0 57 | Serial.print(":"); 58 | if(digits < 10) 59 | Serial.print('0'); 60 | Serial.print(digits); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /Ejercicio64-Interrupciones/Ejercicio64-Interrupciones.ino: -------------------------------------------------------------------------------- 1 | const int LEDPin = 13; 2 | const int intPin = 2; 3 | volatile int frecuencia = 1000; 4 | volatile unsigned long tiempo_interrupcion = 0; 5 | 6 | void setup() { 7 | Serial.begin(9600); 8 | pinMode(LEDPin, OUTPUT); 9 | pinMode(intPin, INPUT_PULLUP); 10 | attachInterrupt(digitalPinToInterrupt(intPin), blink, RISING); 11 | } 12 | 13 | void loop() { 14 | //esta parte es para emular la salida 15 | digitalWrite(LEDPin, HIGH); 16 | delay(frecuencia); 17 | digitalWrite(LEDPin, LOW); 18 | delay(frecuencia); 19 | 20 | if (tiempo_interrupcion > 0) { 21 | Serial.println("interrupcion lanzada"); 22 | if ((millis() - tiempo_interrupcion) > 5000) { 23 | frecuencia = 1000; 24 | tiempo_interrupcion = 0; 25 | Serial.println("Vuelvo a estado normal"); 26 | } 27 | } 28 | 29 | } 30 | 31 | void blink() { 32 | frecuencia = 100; 33 | tiempo_interrupcion = millis(); 34 | } 35 | -------------------------------------------------------------------------------- /Ejercicio65-Thingspeak/Ejercicio65-Thingspeak.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Timer.h" 4 | 5 | #define NUM_ARDUINO X //Sustituir X por el numero de Arduino correcto 6 | 7 | byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0x70, 0xYY}; //Sustituir YY por el numero de MAC correcto 8 | 9 | byte ip[] = { 10 | 192, 168, 1, 10 11 | }; 12 | byte DNS[] = { 13 | 8, 8, 8, 8 14 | }; 15 | byte gateway[] = { 16 | 192, 168, 1, 1 17 | }; 18 | byte subnet[] = { 19 | 255, 255, 255, 0 20 | }; 21 | char url[] = "api.thingspeak.com"; 22 | 23 | EthernetClient client; 24 | Timer t; 25 | String webString = ""; 26 | 27 | void setup() 28 | { 29 | Ethernet.begin(mac, ip, DNS, gateway, subnet); 30 | Serial.begin(9600); 31 | Serial.println("Iniciando datalogger..."); 32 | delay(1000); 33 | t.every(5000, grabaDatos); 34 | } 35 | 36 | void loop() 37 | { 38 | webString = ""; 39 | t.update(); 40 | if (client.available()) { 41 | Serial.println("Respuesta del Servidor---->"); 42 | while (client.available()) { 43 | char c = client.read(); 44 | webString += c; 45 | } 46 | Serial.println(webString); 47 | client.stop(); 48 | } 49 | } 50 | 51 | void grabaDatos() { 52 | Serial.println("leyendo temperatura... "); 53 | int sensorVal = analogRead(A0); 54 | float voltage = (sensorVal / 1024.0) * 5.0; 55 | float temperature = (voltage - 0.5) * 100; //Sonda temperatura kit Arduino 56 | Serial.print("Temperatura Leida: "); 57 | Serial.println(temperature); 58 | 59 | Serial.println("connecting to server..."); 60 | if (client.connect(url, 80)) { 61 | Serial.println("connected"); 62 | client.print("POST /update.json?api_key=DRRINT6CZN93DPJW&field1="); 63 | client.print(temperature); 64 | client.println(" HTTP/1.1"); 65 | client.println("Host: api.thingspeak.com"); 66 | client.println("Connection: close"); 67 | client.println(); 68 | } 69 | else { 70 | Serial.println("connection failed"); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Ejercicio65-Thingspeak_DHCP/Ejercicio65-Thingspeak.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Timer.h" 4 | 5 | #define NUM_ARDUINO 5 6 | 7 | byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0x70, 0xYY}; //Sustituir YY por el numero de MAC correcto 8 | 9 | char url[] = "api.thingspeak.com"; 10 | 11 | EthernetClient client; 12 | Timer t; 13 | String webString = ""; 14 | 15 | void setup() 16 | { 17 | Serial.begin(9600); 18 | Serial.println("inicializando red..."); 19 | if (Ethernet.begin(mac) == 0) { 20 | Serial.println("Failed to configure Ethernet using DHCP"); 21 | for (;;) 22 | ; 23 | } 24 | else { 25 | Serial.print("IP asignada por DHCP: "); 26 | Serial.println(Ethernet.localIP()); 27 | } 28 | 29 | Serial.println("Iniciando datalogger..."); 30 | delay(1000); 31 | t.every(5000, grabaDatos); 32 | } 33 | 34 | void loop() 35 | { 36 | webString = ""; 37 | t.update(); 38 | if (client.available()) { 39 | Serial.println("Respuesta del Servidor---->"); 40 | while (client.available()) { 41 | char c = client.read(); 42 | webString += c; 43 | } 44 | Serial.println(webString); 45 | client.stop(); 46 | } 47 | } 48 | 49 | void grabaDatos() { 50 | Serial.println("leyendo temperatura... "); 51 | int sensorVal = analogRead(A0); 52 | float voltage = (sensorVal / 1024.0) * 5.0; 53 | float temperature = (voltage - 0.5) * 100; //Sonda temperatura kit Arduino 54 | Serial.print("Temperatura Leida: "); 55 | Serial.println(temperature); 56 | 57 | Serial.println("connecting to server..."); 58 | if (client.connect(url, 80)) { 59 | Serial.println("connected"); 60 | client.print("POST /update.json?api_key=DRRINT6CZN93DPJW&field1="); 61 | client.print(temperature); 62 | client.println(" HTTP/1.1"); 63 | client.println("Host: api.thingspeak.com"); 64 | client.println("Connection: close"); 65 | client.println(); 66 | } 67 | else { 68 | Serial.println("connection failed"); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Ejercicio67-operacionesBitwise/Ejercicio67-operacionesBitwise.ino: -------------------------------------------------------------------------------- 1 | //Pin connected to ST_CP of 74HC595 2 | int latchPin = 7; 3 | //Pin connected to SH_CP of 74HC595 4 | int clockPin = 6; 5 | //Pin connected to DS of 74HC595 6 | int dataPin = 5; 7 | //Pin connected to Compressor 1 8 | int comp1Pin = 2; 9 | //Pin connected to Compressor 2 10 | int comp2Pin = 3; 11 | 12 | byte numberToDisplay = B00000001; 13 | 14 | boolean sentido = 0; 15 | 16 | void setup() { 17 | Serial.begin(9600); 18 | //set pins to output so you can control the shift register 19 | pinMode(latchPin, OUTPUT); 20 | pinMode(clockPin, OUTPUT); 21 | pinMode(dataPin, OUTPUT); 22 | pinMode(comp1Pin, INPUT); 23 | pinMode(comp2Pin, INPUT); 24 | 25 | } 26 | 27 | void loop() { 28 | Serial.print("Rele encendido: "); 29 | Serial.println(numberToDisplay, BIN); 30 | digitalWrite(latchPin, LOW); 31 | shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay); 32 | digitalWrite(latchPin, HIGH); 33 | Serial.println("Estado compresor 1: " + (String)digitalRead(comp1Pin) + " | Estado compresor 2: " + (String)digitalRead(comp2Pin)); 34 | delay(1000); 35 | if (sentido) { 36 | numberToDisplay = numberToDisplay >> 1 ; 37 | if (numberToDisplay == B00000001) sentido = 0; 38 | } 39 | else { 40 | numberToDisplay = numberToDisplay << 1 ; 41 | if (numberToDisplay == B00001000) sentido = 1; 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Ejercicio68-toCharArray/Ejercicio68-toCharArray.ino: -------------------------------------------------------------------------------- 1 | /* this library is writing by Cristian Steib. 2 | steibkhriz@gmail.com 3 | Designed to work with the GSM Sim800l,maybe work with SIM900L 4 | 5 | This library use SoftwareSerial, you can define de RX and TX pin 6 | in the header "Sim800l.h" ,by default the pin is RX=10 TX=11.. 7 | be sure that gnd is attached to arduino too. 8 | You can also change the other preferred RESET_PIN 9 | 10 | Esta libreria usa SoftwareSerial, se pueden cambiar los pines de RX y TX 11 | en el archivo header, "Sim800l.h", por defecto los pines vienen configurado en 12 | RX=10 TX=11. 13 | Tambien se puede cambiar el RESET_PIN por otro que prefiera 14 | 15 | PINOUT: 16 | _____________________________ 17 | | ARDUINO UNO >>> SIM800L | 18 | ----------------------------- 19 | GND >>> GND 20 | RX 10 >>> TX 21 | TX 11 >>> RX 22 | RESET 2 >>> RST 23 | 24 | POWER SOURCE 4.2V >>> VCC 25 | 26 | Created on: April 20, 2016 27 | Author: Cristian Steib 28 | 29 | 30 | */ 31 | 32 | #include 33 | #include //is necesary for the library!! 34 | Sim800L Sim800l; //to declare the library 35 | String telefono; 36 | String Mensaje; 37 | 38 | bool error; //to catch the response of sendSms 39 | 40 | 41 | void setup() { 42 | Serial.begin(9600); 43 | Serial.println("Formato telefono+-+texto del mensaje+!"); 44 | Sim800l.begin(); // initializate the library. 45 | } 46 | 47 | void loop() { 48 | 49 | if (Serial.available()) 50 | { 51 | String data = Serial.readStringUntil('\n'); 52 | int guion = data.indexOf("-"); 53 | int admiracion = data.indexOf("!"); 54 | telefono = data.substring(0, guion); 55 | Mensaje = data.substring((guion + 1), admiracion); 56 | Serial.println("Telefono leido: " + telefono); 57 | Serial.println("Mensaje leido: " + Mensaje); 58 | char number[telefono.length()+1]; 59 | char text[Mensaje.length()+1]; 60 | 61 | telefono.toCharArray(number, telefono.length()+1); 62 | Mensaje.toCharArray(text, Mensaje.length()+1); 63 | Serial.print("Telefono leido convertido: " ); 64 | Serial.println(number); 65 | Serial.print("Mensaje leido convertido: " ); 66 | Serial.println(text); 67 | 68 | error = Sim800l.sendSms(number, text); 69 | Serial.println("Respuesta del envio de SMS: " + (String(error))); 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Ejercicio69-Split_String/Ejercicio69-Split_String.ino: -------------------------------------------------------------------------------- 1 | String strTest = "123,456,789,abc"; 2 | 3 | void setup() { 4 | Serial.begin(9600); 5 | Serial.println("Longitud del String: " + (String)strTest.length()); 6 | int i = num_ocurrencias(strTest, ","); 7 | Serial.println("Número de ocurrencias: " + (String)i); 8 | String dividido[i + 1]; 9 | split(strTest, ",", dividido); 10 | Serial.println("Resultado de la división es: "); 11 | for (int j = 0; j < i + 1 ; j++) { 12 | Serial.println(dividido[j]); 13 | } 14 | } 15 | 16 | void loop() { 17 | 18 | } 19 | 20 | void split (String str, String delimitador, String* resultado) { 21 | int tam = num_ocurrencias(str, delimitador); 22 | Serial.println("Se va a dividir en " + (String)(tam + 1) + " trozos"); 23 | int indice = 0; 24 | for (int i = 0; i < tam + 1; i++) { 25 | resultado[i] = str.substring(indice, str.indexOf(delimitador, indice + 1)); 26 | Serial.println("Trozo " + (String)(i + 1) + " desde " + indice + " hasta " + String(str.indexOf(delimitador, indice + 1)) + " es " + resultado[i]); 27 | indice = str.indexOf(delimitador, indice + 1) + 1; 28 | } 29 | } 30 | 31 | int num_ocurrencias (String str, String delimitador) { 32 | int ocurrencias = 0; 33 | int indice = str.indexOf(delimitador); 34 | while (indice >= 0) { 35 | ocurrencias++; 36 | Serial.println("Ocurrencia " + (String)ocurrencias + " en la posición " + (String)indice); 37 | indice = str.indexOf(delimitador, indice + 1); 38 | } 39 | return ocurrencias; 40 | } 41 | -------------------------------------------------------------------------------- /Ejercicio70-TMP36_Histeresis/Ejercicio70-TMP36_Histeresis.ino: -------------------------------------------------------------------------------- 1 | const int sensorPin = A0; 2 | const float baselineTemp = 20.0; 3 | const int ledPin = 13; // pin that the LED is attached to 4 | float threshold; 5 | const float histeresis = 0.5; //Valor de la histeresis 6 | boolean alarma; //1 = ALARMA, 0 = NO ALARMA 7 | 8 | void setup() { 9 | // initialize the LED pin as an output: 10 | pinMode(ledPin, OUTPUT); 11 | // initialize serial communications: 12 | Serial.begin(9600); 13 | int analogValue = analogRead(sensorPin); //Inicializo el valor de la alarma 14 | float voltage = (analogValue/1024.0)*5.0; 15 | float temperature = (voltage - 0.5)*100; 16 | 17 | if (temperature > baselineTemp) { 18 | alarma = 1; 19 | threshold = baselineTemp - histeresis; 20 | digitalWrite(ledPin, HIGH); 21 | } 22 | else { 23 | alarma = 0; 24 | threshold = baselineTemp + histeresis; 25 | digitalWrite(ledPin, LOW); 26 | } 27 | } 28 | 29 | void loop() { 30 | // read the value of the potentiometer: 31 | int analogValue = analogRead(sensorPin); //En cada loop compruebo el valor de la entrada analógica 32 | float voltage = (analogValue/1024.0)*5.0; 33 | float temperature = (voltage - 0.5)*100; 34 | 35 | // if the analog value is high enough, turn on the LED: 36 | if (temperature > threshold) { 37 | if (alarma == 0) { 38 | digitalWrite(ledPin, HIGH); 39 | Serial.println("ALARMA!!!!!!!!!!!!!!"); 40 | alarma = 1; 41 | threshold = baselineTemp - histeresis; 42 | } 43 | } else { 44 | if (alarma == 1) { 45 | digitalWrite(ledPin, LOW); 46 | Serial.println("ALARMA RECUPERADA"); 47 | alarma = 0; 48 | threshold = baselineTemp + histeresis; 49 | } 50 | } 51 | 52 | // print values 53 | Serial.print(temperature); 54 | Serial.print(" "); 55 | Serial.print(threshold); 56 | Serial.print(" "); 57 | Serial.println(alarma * 1024); 58 | delay(50); 59 | delay(50); 60 | } 61 | -------------------------------------------------------------------------------- /Ejercicio71-DHT11_Histeresis/Ejercicio71-DHT11_Histeresis.ino: -------------------------------------------------------------------------------- 1 | #include "DHT.h" 2 | 3 | #define DHTPIN 4 4 | #define DHTTYPE DHT11 // DHT 11 5 | 6 | // for DHT11, 7 | // VCC: 5V or 3V 8 | // GND: GND 9 | // DATA: 4 10 | 11 | DHT dht(DHTPIN, DHTTYPE); 12 | 13 | const float baselineTemp = 20.0; 14 | const int ledPin = 13; // pin that the LED is attached to 15 | float threshold; 16 | const float histeresis = 0.5; //Valor de la histeresis 17 | boolean alarma; //1 = ALARMA, 0 = NO ALARMA 18 | 19 | void setup() { 20 | // initialize the LED pin as an output: 21 | pinMode(ledPin, OUTPUT); 22 | // initialize serial communications: 23 | Serial.begin(9600); 24 | 25 | dht.begin(); 26 | 27 | float temperature = dht.readTemperature(); 28 | 29 | if (temperature > baselineTemp) { 30 | alarma = 1; 31 | threshold = baselineTemp - histeresis; 32 | digitalWrite(ledPin, HIGH); 33 | } 34 | else { 35 | alarma = 0; 36 | threshold = baselineTemp + histeresis; 37 | digitalWrite(ledPin, LOW); 38 | } 39 | } 40 | 41 | void loop() { 42 | // read the value of the potentiometer: 43 | float temperature = dht.readTemperature(); 44 | 45 | // if the analog value is high enough, turn on the LED: 46 | if (temperature > threshold) { 47 | if (alarma == 0) { 48 | digitalWrite(ledPin, HIGH); 49 | Serial.println("ALARMA!!!!!!!!!!!!!!"); 50 | alarma = 1; 51 | threshold = baselineTemp - histeresis; 52 | } 53 | } else { 54 | if (alarma == 1) { 55 | digitalWrite(ledPin, LOW); 56 | Serial.println("ALARMA RECUPERADA"); 57 | alarma = 0; 58 | threshold = baselineTemp + histeresis; 59 | } 60 | } 61 | 62 | // print values 63 | Serial.print(temperature); 64 | Serial.print(" "); 65 | Serial.print(threshold); 66 | Serial.print(" "); 67 | Serial.println(alarma * 20); 68 | delay(50); 69 | delay(50); 70 | } 71 | -------------------------------------------------------------------------------- /Otros/ArduinoExplorer/ToneNotes.h: -------------------------------------------------------------------------------- 1 | 2 | /************************************************* 3 | * Public Constants 4 | *************************************************/ 5 | 6 | // Lowest for ck/1024 7 | #define NOTE_B0 31 8 | #define NOTE_C1 33 9 | #define NOTE_CS1 35 10 | #define NOTE_D1 37 11 | #define NOTE_DS1 39 12 | #define NOTE_E1 41 13 | #define NOTE_F1 44 14 | #define NOTE_FS1 46 15 | #define NOTE_G1 49 16 | #define NOTE_GS1 52 17 | #define NOTE_A1 55 18 | #define NOTE_AS1 58 19 | #define NOTE_B1 62 20 | #define NOTE_C2 65 21 | #define NOTE_CS2 69 22 | #define NOTE_D2 73 23 | #define NOTE_DS2 78 24 | #define NOTE_E2 82 25 | #define NOTE_F2 87 26 | #define NOTE_FS2 93 27 | #define NOTE_G2 98 28 | #define NOTE_GS2 104 29 | #define NOTE_A2 110 30 | #define NOTE_AS2 117 31 | // Lowest for ck/256 32 | #define NOTE_B2 123 33 | #define NOTE_C3 131 34 | #define NOTE_CS3 139 35 | #define NOTE_D3 147 36 | #define NOTE_DS3 156 37 | #define NOTE_E3 165 38 | #define NOTE_F3 175 39 | #define NOTE_FS3 185 40 | #define NOTE_G3 196 41 | #define NOTE_GS3 208 42 | #define NOTE_A3 220 43 | #define NOTE_AS3 233 44 | // Lowest for ck/128 45 | #define NOTE_B3 247 46 | #define NOTE_C4 262 47 | #define NOTE_CS4 277 48 | #define NOTE_D4 294 49 | #define NOTE_DS4 311 50 | #define NOTE_E4 330 51 | #define NOTE_F4 349 52 | #define NOTE_FS4 370 53 | #define NOTE_G4 392 54 | #define NOTE_GS4 415 55 | #define NOTE_A4 440 56 | #define NOTE_AS4 466 57 | #define NOTE_B4 494 58 | #define NOTE_C5 523 59 | #define NOTE_CS5 554 60 | #define NOTE_D5 587 61 | #define NOTE_DS5 622 62 | #define NOTE_E5 659 63 | #define NOTE_F5 698 64 | #define NOTE_FS5 740 65 | #define NOTE_G5 784 66 | #define NOTE_GS5 831 67 | #define NOTE_A5 880 68 | #define NOTE_AS5 932 69 | #define NOTE_B5 988 70 | #define NOTE_C6 1047 71 | #define NOTE_CS6 1109 72 | #define NOTE_D6 1175 73 | #define NOTE_DS6 1245 74 | #define NOTE_E6 1319 75 | #define NOTE_F6 1397 76 | #define NOTE_FS6 1480 77 | #define NOTE_G6 1568 78 | #define NOTE_GS6 1661 79 | #define NOTE_A6 1760 80 | #define NOTE_AS6 1865 81 | #define NOTE_B6 1976 82 | #define NOTE_C7 2093 83 | #define NOTE_CS7 2217 84 | #define NOTE_D7 2349 85 | #define NOTE_DS7 2489 86 | #define NOTE_E7 2637 87 | #define NOTE_F7 2794 88 | #define NOTE_FS7 2960 89 | #define NOTE_G7 3136 90 | #define NOTE_GS7 3322 91 | #define NOTE_A7 3520 92 | #define NOTE_AS7 3729 93 | #define NOTE_B7 3951 94 | #define NOTE_C8 4186 95 | #define NOTE_CS8 4435 96 | #define NOTE_D8 4699 97 | #define NOTE_DS8 4978 98 | 99 | 100 | -------------------------------------------------------------------------------- /Otros/DHCP_Arduino/DHCP_Arduino.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY }; //Sustituir YY por el numero de MAC correcto 5 | 6 | EthernetClient client; 7 | 8 | void setup() { 9 | // start the serial library: 10 | Serial.begin(9600); 11 | // start the Ethernet connection: 12 | if (Ethernet.begin(mac) == 0) { 13 | Serial.println("Failed to configure Ethernet using DHCP"); 14 | // no point in carrying on, so do nothing forevermore: 15 | for(;;) 16 | ; 17 | } 18 | // print your local IP address: 19 | Serial.println(Ethernet.localIP()); 20 | } 21 | 22 | void loop() { 23 | Serial.println("Now, you can connect to Internet"); 24 | while(true){ 25 | //nada 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Otros/Ethernet Modificada/EthernetClient.h: -------------------------------------------------------------------------------- 1 | #ifndef ethernetclient_h 2 | #define ethernetclient_h 3 | #include "Arduino.h" 4 | #include "Print.h" 5 | #include "Client.h" 6 | #include "IPAddress.h" 7 | 8 | class EthernetClient : public Client { 9 | 10 | public: 11 | EthernetClient(); 12 | EthernetClient(uint8_t sock); 13 | 14 | uint8_t status(); 15 | virtual int connect(IPAddress ip, uint16_t port); 16 | virtual int connect(const char *host, uint16_t port); 17 | virtual size_t write(uint8_t); 18 | virtual size_t write(const uint8_t *buf, size_t size); 19 | virtual int available(); 20 | virtual int read(); 21 | virtual int read(uint8_t *buf, size_t size); 22 | virtual int peek(); 23 | virtual void flush(); 24 | virtual void stop(); 25 | // Añadido por ecrespo 26 | uint8_t *getRemoteIP(uint8_t remoteIP[]);//adds remote ip address 27 | uint8_t *getRemoteMAC(uint8_t remoteMAC[]); 28 | uint16_t getRemotePort(); 29 | 30 | // 31 | virtual uint8_t connected(); 32 | virtual operator bool(); 33 | 34 | friend class EthernetServer; 35 | 36 | using Print::write; 37 | 38 | private: 39 | static uint16_t _srcport; 40 | uint8_t _sock; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Otros/Flaco_Multitarea/Flaco_Multitarea.ino: -------------------------------------------------------------------------------- 1 | #define TIEMPO_ENCENDIDO 5000 2 | 3 | boolean estado_alarma = 0; 4 | unsigned long tiempo_flanco = 0; 5 | 6 | void setup() { 7 | pinMode(13, OUTPUT); 8 | pinMode(7, INPUT_PULLUP); 9 | Serial.begin(9600); 10 | } 11 | 12 | void loop() { 13 | if (tiempo_flanco > 0){ 14 | if ((millis() - tiempo_flanco) > TIEMPO_ENCENDIDO){ 15 | digitalWrite(13, LOW); 16 | tiempo_flanco = 0; 17 | Serial.println("Apago Led"); 18 | } 19 | } 20 | 21 | int lectura = digitalRead(7); 22 | if (lectura == 0) { 23 | if (estado_alarma == 0) { 24 | Serial.println("ALARMA!!!!!!"); 25 | digitalWrite(13, HIGH); 26 | //tiempo_flanco = millis(); //Opcion donde reinicio el contador de de tiempo de encendido 27 | if (tiempo_flanco == 0) tiempo_flanco = millis(); 28 | estado_alarma = 1; 29 | } 30 | } 31 | else { 32 | if (estado_alarma == 1) { 33 | estado_alarma = 0; 34 | Serial.println("ALARMA RECUPERADA"); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Otros/Histeresis/Histeresis.ino: -------------------------------------------------------------------------------- 1 | const int sensorPin=A0; 2 | const float baselineTemp=19.0; 3 | const float paso=2.0; 4 | float histe1=0,histe2=0,histe3=0; 5 | float histeresis=1.0; 6 | 7 | 8 | void setup(){ 9 | 10 | Serial.begin(9600); 11 | 12 | for (int pinNumber=2;pinNumber<5;pinNumber++){ 13 | pinMode(pinNumber,OUTPUT); 14 | digitalWrite(pinNumber,LOW); 15 | } 16 | 17 | } 18 | 19 | void loop(){ 20 | 21 | int sensorVal = analogRead(sensorPin); 22 | 23 | Serial.print("Sensor Value:"); 24 | Serial.print(sensorVal); 25 | 26 | float voltage = (sensorVal/1024.0)*5.0; 27 | 28 | Serial.print(", Volts: "); 29 | Serial.print(voltage); 30 | 31 | Serial.print(", degree C: "); 32 | 33 | float temperature = (voltage - .5)*100; 34 | Serial.print(temperature); 35 | Serial.print(" Histe actual:"); 36 | 37 | if (temperature < baselineTemp+histe1) 38 | { 39 | histe1=0; 40 | digitalWrite(2,LOW); 41 | digitalWrite(3,LOW); 42 | digitalWrite(4,LOW); 43 | Serial.println(histe1); 44 | 45 | } 46 | else if (temperature>=(baselineTemp+paso+histe1) && temperature<(baselineTemp+(paso*2)+histe2)) 47 | { 48 | histe1=-histeresis; 49 | histe2=0; 50 | digitalWrite(2,HIGH); 51 | digitalWrite(3,LOW); 52 | digitalWrite(4,LOW); 53 | Serial.println(baselineTemp+paso+histe1); 54 | 55 | } 56 | else if (temperature>=(baselineTemp+(paso*2)+histe2) && temperature<(baselineTemp+(paso*3))+histe3) 57 | { 58 | histe1=0; 59 | histe2=-histeresis; 60 | histe3=0; 61 | digitalWrite(2,HIGH); 62 | digitalWrite(3,HIGH); 63 | digitalWrite(4,LOW); 64 | Serial.println(baselineTemp+(paso*2)+histe2); 65 | 66 | } 67 | else if(temperature >=(baselineTemp+(paso*3))+histe3) 68 | { 69 | histe2=0; 70 | histe3=-histeresis; 71 | digitalWrite(2,HIGH); 72 | digitalWrite(3,HIGH); 73 | digitalWrite(4,HIGH); 74 | Serial.println(baselineTemp+(paso*3)+histe3); 75 | } 76 | delay(1); 77 | 78 | } -------------------------------------------------------------------------------- /Otros/IP_fija_Arduino/IP_fija_Arduino.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xYY }; //Sustituir YY por el numero de MAC correcto 5 | byte ip[] = {192, 168, 1, 10}; //the IP 6 | byte gateway[] = {192, 168, 1, 1}; // the router's gateway address 7 | byte subnet[] = {255, 255, 255, 0}; // the subnet 8 | 9 | EthernetClient client; 10 | 11 | void setup() { 12 | // start the serial library 13 | Serial.begin(9600); 14 | // start the Ethernet connection 15 | Ethernet.begin(mac, ip, gateway, subnet); 16 | 17 | // print your local IP address: 18 | Serial.println(Ethernet.localIP()); 19 | } 20 | 21 | void loop() { 22 | Serial.println("Now, you can connect to Internet"); 23 | while (true) { 24 | //nada 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Otros/Metodologia_Sketch/paso1/paso1.ino: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jecrespo/Aprendiendo-Arduino/91fc9e97f379376ced35b1e9d1018852991ef112/Otros/Metodologia_Sketch/paso1/paso1.ino -------------------------------------------------------------------------------- /Otros/Metodologia_Sketch/paso2/paso2.ino: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jecrespo/Aprendiendo-Arduino/91fc9e97f379376ced35b1e9d1018852991ef112/Otros/Metodologia_Sketch/paso2/paso2.ino -------------------------------------------------------------------------------- /Otros/README.md: -------------------------------------------------------------------------------- 1 | # Otros sketchs 2 | 3 | Otros sketchs de prueba para convertir a ejercicios -------------------------------------------------------------------------------- /Otros/Test_L298_Mario/Test_L298_Mario.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Test simple para comprobar funcionamiento de un coche con 3 | dos motores DC y un driver L298 4 | */ 5 | 6 | #define PIN_IN1 11 7 | #define PIN_IN2 10 8 | #define PIN_IN3 9 9 | #define PIN_IN4 6 10 | 11 | void setup() { 12 | Serial.begin(9600); 13 | pinMode(PIN_IN1, OUTPUT); 14 | pinMode(PIN_IN2, OUTPUT); 15 | pinMode(PIN_IN3, OUTPUT); 16 | pinMode(PIN_IN4, OUTPUT); 17 | } 18 | 19 | void loop() { 20 | char comando = '0'; 21 | 22 | Serial.println(F("\n------------------------")); 23 | Serial.println(F("MENU")); 24 | Serial.println(F("Selecciona acción ejecutar")); 25 | Serial.println(F("F - Forward")); 26 | Serial.println(F("B - Backward")); 27 | Serial.println(F("R - Right")); 28 | Serial.println(F("L - Left")); 29 | Serial.println(F("S - Stop")); 30 | 31 | while (Serial.available() == 0) { 32 | //Nada 33 | } 34 | 35 | if (Serial.available() > 0) { 36 | comando = Serial.read(); 37 | Serial.print("\nHe Leido la instrucción: " + (String)comando); 38 | } 39 | 40 | if (comando == 'F') { //Forward 41 | Serial.println(comando); 42 | digitalWrite(PIN_IN1, LOW); 43 | digitalWrite(PIN_IN2, HIGH); 44 | digitalWrite(PIN_IN3, HIGH); 45 | digitalWrite(PIN_IN4, LOW); 46 | } 47 | else if (comando == 'B') { //Backward 48 | Serial.println(comando); 49 | digitalWrite(PIN_IN1, HIGH); 50 | digitalWrite(PIN_IN2, LOW); 51 | digitalWrite(PIN_IN3, LOW); 52 | digitalWrite(PIN_IN4, HIGH); 53 | } 54 | else if (comando == 'R') { //Right 55 | Serial.println(comando); 56 | digitalWrite(PIN_IN1, LOW); 57 | digitalWrite(PIN_IN2, LOW); 58 | digitalWrite(PIN_IN3, HIGH); 59 | digitalWrite(PIN_IN4, LOW); 60 | } 61 | else if (comando == 'L') { //Left 62 | Serial.println(comando); 63 | digitalWrite(PIN_IN1, LOW); 64 | digitalWrite(PIN_IN2, HIGH); 65 | digitalWrite(PIN_IN3, LOW); 66 | digitalWrite(PIN_IN4, LOW); 67 | } 68 | else if (comando == 'S') { //Stop 69 | Serial.println(comando); 70 | digitalWrite(PIN_IN1, LOW); 71 | digitalWrite(PIN_IN2, LOW); 72 | digitalWrite(PIN_IN3, LOW); 73 | digitalWrite(PIN_IN4, LOW); 74 | } 75 | else 76 | Serial.println("Comando incorrecto!!!"); 77 | } 78 | -------------------------------------------------------------------------------- /Otros/compara_timers/compara_timers.ino: -------------------------------------------------------------------------------- 1 | //Demostración retrasos de librería timer vs mstimer2 2 | // 3 | //by aprendiendoarduino 2016 4 | // 5 | //Beerware license: Free for any and all purposes, but if you find it 6 | 7 | #include "Timer.h" //http://github.com/JChristensen/Timer 8 | #include 9 | Timer t; //instantiate the timer object 10 | 11 | void setup() 12 | { 13 | t.every(500, funcion1); 14 | MsTimer2::set(500, funcion2); // 500ms period 15 | MsTimer2::start(); 16 | Serial.begin(250000); 17 | } 18 | 19 | void loop(void) 20 | { 21 | t.update(); 22 | Serial.println("************************ LOOP **********************"); 23 | Serial.print("Tiempo de loop: "); 24 | Serial.println(millis()); 25 | delay (2000); 26 | } 27 | 28 | void funcion1() { 29 | Serial.print("++++++++++ejecuto función1 cada 500ms y es el tiempo "); 30 | Serial.println(millis()); 31 | delay(100); //simulo que la función tarda 100 ms 32 | //Esta función acumula un retraso de 100ms cada vez que se ejecuta 33 | } 34 | 35 | void funcion2() { 36 | Serial.print("----------ejecuto función2 cada 500ms y es el tiempo "); 37 | Serial.println(millis()); 38 | delay(200); //simulo que la función tarda 200 ms, pero no se ejecuta ¿porque? https://www.arduino.cc/en/Reference/AttachInterrupt 39 | } 40 | -------------------------------------------------------------------------------- /Otros/ejemplo_modbus/ejemplo_modbus.ino: -------------------------------------------------------------------------------- 1 | int numero = 0; 2 | byte respuesta [8]; 3 | 4 | void setup() { 5 | Serial.begin(9600); 6 | Serial1.begin(9600); 7 | pinMode(LED_BUILTIN, OUTPUT); 8 | } 9 | 10 | void loop() { 11 | // read from port 1 (mod bus), send to port 0 (serial monitor) 12 | String trama = "Recibido ---> "; 13 | //cuando recibo algo del master 14 | if (Serial1.available()) { 15 | int i = 0; 16 | //leo toda la trama y la guardo en respuesta 17 | while (Serial1.available()) { 18 | byte inByte = Serial1.read(); 19 | respuesta[i] = inByte; //La respuesta es un echo a la petición para función 0x06 20 | trama = trama + String(inByte, HEX) + "-"; //Trama completa para imprimir por pantalla 21 | i++; 22 | delay(30); //Para dar tiempo a que llegue toda la trama 23 | } 24 | numero++; //Numero de petición 25 | Serial.print("---------- "); 26 | Serial.print(numero); 27 | Serial.println(" ----------"); 28 | Serial.println(trama); 29 | if (respuesta[5] == 0) { 30 | Serial.println("Apago Led"); 31 | digitalWrite(LED_BUILTIN, LOW); 32 | } 33 | else { 34 | Serial.println("Enciendo Led"); 35 | digitalWrite(LED_BUILTIN, HIGH); 36 | } 37 | for (int i = 0; i < sizeof(respuesta); i++) { //repuesta mandada a master 38 | Serial1.write(respuesta[i]); 39 | Serial.print(respuesta[i], HEX); 40 | } 41 | Serial.println(" ---> Respuesta Enviada"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Otros/ejemplo_sleep_arduino/ejemplo_sleep_arduino.ino: -------------------------------------------------------------------------------- 1 | #include //https://github.com/rocketscream/Low-Power 2 | 3 | // Usar pin 2 como pin para despertar y dormir 4 | const int wakeUpPin = 2; 5 | 6 | void setup() { 7 | pinMode(wakeUpPin, INPUT_PULLUP); //uso resistencia interna pullup 8 | Serial.begin(9600); 9 | 10 | } 11 | 12 | void loop() { 13 | Serial.println("loop " + String(millis())); 14 | 15 | if (digitalRead(wakeUpPin) == LOW) { //detecta boton pulsado, conectado a input_pullup 16 | int tiempo_rebote = 0; 17 | while (digitalRead(wakeUpPin) == LOW) { //pequeño bucle para evitar rebotes espero al menos 150 ms pulsado para entrar en modo sleep 18 | delay(10); 19 | tiempo_rebote += 10; 20 | if (digitalRead(wakeUpPin) == HIGH) { //al liberar el botón 21 | break; 22 | } 23 | } 24 | 25 | if (tiempo_rebote > 150) { //Solo entro si el pin está a HIGH y ha estado es ese estado más de 150 ms para evitar rebotes botón 26 | delay(100); 27 | Serial.println("Entro en modo sleep"); 28 | delay(100); 29 | // Permite despertar al poner el pin en LOW 30 | attachInterrupt(0, wakeUp, LOW); 31 | 32 | // ENTRO EN MODO SLEEP CON MODULOS DE ANALOGICO Y BROWN OUT DESHABILITADOS 33 | LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 34 | 35 | // deshabilito la interrupcion externa en el pin de wakeup 36 | detachInterrupt(0); 37 | Serial.println("Salgo modo sleep"); 38 | } 39 | } 40 | } 41 | 42 | void wakeUp() 43 | { 44 | // función para manejar la interrupción del wakeup 45 | } 46 | -------------------------------------------------------------------------------- /Otros/entenderSerial/entenderSerial.ino: -------------------------------------------------------------------------------- 1 | 2 | void setup(){ 3 | Serial.begin(9600); 4 | Serial.write(0xFE); 5 | Serial.write(0x00); 6 | Serial.print("Inicio"); 7 | delay(1000); 8 | } 9 | 10 | 11 | void loop() { 12 | Serial.write(0xFE); 13 | Serial.write(0x00); 14 | Serial.print("Tiempo: "); 15 | Serial.write(0xFE); 16 | Serial.write(192); 17 | Serial.print(millis()); 18 | delay(1000); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Otros/example_time_edison/example_time_edison.ino: -------------------------------------------------------------------------------- 1 | time_t rawtime; 2 | struct tm *timeinfo; 3 | int year, month , day, hour, minute, second; 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | do { 8 | Serial.println("Enter Year (YYYY): "); 9 | year = readData (); 10 | } while ((year > 2000) && (year < 2050)); 11 | 12 | do { 13 | Serial.println("Enter Month: "); 14 | month = readData (); 15 | } while ((month > 0) && (month < 13)); 16 | 17 | do { 18 | Serial.println("Enter Day: "); 19 | day = readData (); 20 | } while ((day > 0) && (day < 32)); 21 | 22 | do { 23 | Serial.println("Enter Hour: "); 24 | hour = readData (); 25 | } while ((hour >= 0) && (hour <= 60)); 26 | 27 | do { 28 | Serial.println("Enter Minute: "); 29 | minute = readData (); 30 | } while ((minute >= 0) && (minute <= 60)); 31 | 32 | do { 33 | Serial.println("Enter Second: "); 34 | second = readData (); 35 | } while ((second >= 0) && (second <= 60)); 36 | 37 | timeinfo->tm_year = year; 38 | timeinfo->tm_mon = month; 39 | timeinfo->tm_mday = day; 40 | timeinfo->tm_hour = hour; 41 | timeinfo->tm_min = minute; 42 | timeinfo->tm_sec = second; 43 | 44 | rawtime = mktime(timeinfo); 45 | stime(&rawtime); //Set system time 46 | } 47 | 48 | void loop() { 49 | Serial.println(time(&rawtime)); 50 | delay(1000); 51 | } 52 | 53 | int readData () { 54 | String data = ""; 55 | while (Serial.available() == 0) { 56 | //Wait enter data 57 | } 58 | 59 | do { 60 | char character; 61 | character = Serial.read(); 62 | data += character; 63 | delay(5); 64 | } while (Serial.available() > 0); 65 | 66 | return (data.toInt()); 67 | } 68 | -------------------------------------------------------------------------------- /Otros/llenarMemoria/README.md: -------------------------------------------------------------------------------- 1 | Opciones de compilación: 2 | - Opción todo a 0: solo ocupa la memoria de las variables globales definidas pero no inicializadas. 822 bytes libres. 3 | - Opción todo a 0 y eliminar el array de 100 Strings global: Dispongo de toda la memoria. 4 | - Opción LOCAL = 1: defino una nueva variable local en el loop pero al inicializarla en cada loop no aumenta la memoria. 5 | - Opción STRINGS = 1: tengo un array de 100 objetos Strings y a medida que los voy inicializando con 10 bytes lleno el heap y acabo llenando la memoria 6 | - Opción GLOBAL = 1: en el momento que inicalizo el array de 1000 long asignando un valor me ocupa 4000 bytes en la RAM y ya de un error de compilación. 7 | -------------------------------------------------------------------------------- /Otros/llenarMemoria/llenarMemoria.ino: -------------------------------------------------------------------------------- 1 | //Selecciona la opcion a probar 2 | #define GLOBAL 1 3 | #define LOCAL 0 4 | #define STRINGS 0 5 | 6 | long numeros [1000]; //un long ocupa 4 bytes 7 | String frase[100]; 8 | 9 | int freeRam () 10 | { 11 | extern int __heap_start, *__brkval; 12 | int v; 13 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 14 | } 15 | 16 | void setup(){ 17 | Serial.begin(9600); 18 | Serial.println("Empiezo"); 19 | Serial.println(freeRam()); 20 | } 21 | 22 | void loop(){ 23 | #if LOCAL 24 | long numeros2 [1000]; //un long ocupa 4 bytes 25 | #endif 26 | for (int i=0; i < 5000 ; i++){ 27 | #if GLOBAL 28 | numeros[i] = i; 29 | #endif 30 | #if LOCAL 31 | numeros2[i] = i; 32 | #endif 33 | #if STRINGS 34 | frase[i] = "aaaaaaaaaa"; //10 bytes 35 | #endif 36 | Serial.print("Memoria libre: "); 37 | Serial.println(freeRam()); 38 | Serial.println(i); 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Otros/timerlocalblink/Event.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software; you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation; either version 2 of the License, or 5 | * (at your option) any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program; if not, write to the Free Software 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 15 | * MA 02110-1301, USA. 16 | */ 17 | 18 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * 19 | Code by Simon Monk 20 | http://www.simonmonk.org 21 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 22 | 23 | // For Arduino 1.0 and earlier 24 | #if defined(ARDUINO) && ARDUINO >= 100 25 | #include "Arduino.h" 26 | #else 27 | #include "WProgram.h" 28 | #endif 29 | 30 | #include "Event.h" 31 | 32 | Event::Event(void) 33 | { 34 | eventType = EVENT_NONE; 35 | } 36 | 37 | void Event::update(void) 38 | { 39 | unsigned long now = millis(); 40 | update(now); 41 | } 42 | 43 | void Event::update(unsigned long now) 44 | { 45 | if (now - lastEventTime >= period) 46 | { 47 | switch (eventType) 48 | { 49 | case EVENT_EVERY: 50 | (*callback)(); 51 | break; 52 | 53 | case EVENT_OSCILLATE: 54 | pinState = ! pinState; 55 | digitalWrite(pin, pinState); 56 | break; 57 | } 58 | lastEventTime = now; 59 | count++; 60 | } 61 | if (repeatCount > -1 && count >= repeatCount) 62 | { 63 | eventType = EVENT_NONE; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Otros/timerlocalblink/Event.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software; you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation; either version 2 of the License, or 5 | * (at your option) any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program; if not, write to the Free Software 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 15 | * MA 02110-1301, USA. 16 | */ 17 | 18 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * 19 | Code by Simon Monk 20 | http://www.simonmonk.org 21 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 22 | 23 | #ifndef Event_h 24 | #define Event_h 25 | 26 | #include 27 | 28 | #define EVENT_NONE 0 29 | #define EVENT_EVERY 1 30 | #define EVENT_OSCILLATE 2 31 | 32 | class Event 33 | { 34 | 35 | public: 36 | Event(void); 37 | void update(void); 38 | void update(unsigned long now); 39 | int8_t eventType; 40 | unsigned long period; 41 | int repeatCount; 42 | uint8_t pin; 43 | uint8_t pinState; 44 | void (*callback)(void); 45 | unsigned long lastEventTime; 46 | int count; 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /Otros/timerlocalblink/Timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software; you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation; either version 2 of the License, or 5 | * (at your option) any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program; if not, write to the Free Software 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 15 | * MA 02110-1301, USA. 16 | */ 17 | 18 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * 19 | Code by Simon Monk 20 | http://www.simonmonk.org 21 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 22 | 23 | #ifndef Timer_h 24 | #define Timer_h 25 | 26 | #include 27 | #include "Event.h" 28 | 29 | #define MAX_NUMBER_OF_EVENTS (10) 30 | 31 | #define TIMER_NOT_AN_EVENT (-2) 32 | #define NO_TIMER_AVAILABLE (-1) 33 | 34 | class Timer 35 | { 36 | 37 | public: 38 | Timer(void); 39 | 40 | int8_t every(unsigned long period, void (*callback)(void)); 41 | int8_t every(unsigned long period, void (*callback)(void), int repeatCount); 42 | int8_t after(unsigned long duration, void (*callback)(void)); 43 | int8_t oscillate(uint8_t pin, unsigned long period, uint8_t startingValue); 44 | int8_t oscillate(uint8_t pin, unsigned long period, uint8_t startingValue, int repeatCount); 45 | 46 | /** 47 | * This method will generate a pulse of !startingValue, occuring period after the 48 | * call of this method and lasting for period. The Pin will be left in !startingValue. 49 | */ 50 | int8_t pulse(uint8_t pin, unsigned long period, uint8_t startingValue); 51 | 52 | /** 53 | * This method will generate a pulse of pulseValue, starting immediately and of 54 | * length period. The pin will be left in the !pulseValue state 55 | */ 56 | int8_t pulseImmediate(uint8_t pin, unsigned long period, uint8_t pulseValue); 57 | void stop(int8_t id); 58 | void update(void); 59 | void update(unsigned long now); 60 | 61 | protected: 62 | Event _events[MAX_NUMBER_OF_EVENTS]; 63 | int8_t findFreeEventIndex(void); 64 | 65 | }; 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /Otros/timerlocalblink/timerlocalblink.ino: -------------------------------------------------------------------------------- 1 | //Flash two LEDs at different rates using Simon Monk's Timer library 2 | //http://www.doctormonk.com/2012/01/arduino-timer-library.html 3 | // 4 | //Jack Christensen 30Sep2013 5 | // 6 | //Beerware license: Free for any and all purposes, but if you find it 7 | //useful AND we actually meet someday, you can buy me a beer! 8 | 9 | #include //http://github.com/JChristensen/Timer 10 | 11 | const int LED1 = 8; //connect one LED to this pin (with appropriate current-limiting resistor of course) 12 | const int LED2 = 9; //connect another LED to this pin (don't forget the resistor) 13 | const unsigned long PERIOD1 = 1000; //one second 14 | const unsigned long PERIOD2 = 10000; //ten seconds 15 | Timer t; //instantiate the timer object 16 | 17 | void setup(void) 18 | { 19 | pinMode(LED1, OUTPUT); 20 | pinMode(LED2, OUTPUT); 21 | t.oscillate(LED1, PERIOD1, HIGH); 22 | t.oscillate(LED2, PERIOD2, HIGH); 23 | } 24 | 25 | void loop(void) 26 | { 27 | t.update(); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Aprendiendo-Arduino 2 | =================== 3 | 4 | [![Join the chat at https://gitter.im/jecrespo/Aprendiendo-Arduino](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jecrespo/Aprendiendo-Arduino?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 5 | [![Stories in Ready](https://badge.waffle.io/jecrespo/Aprendiendo-Arduino.svg?label=ready&title=Ready)](http://waffle.io/jecrespo/Aprendiendo-Arduino) 6 | 7 | Ejercicios del curso #aprendiendoarduino 8 | 9 | Todo el curso en: https://aprendiendoarduino.wordpress.com/ 10 | --------------------------------------------------------------------------------