Perdoname que te moleste, pero me surgió esta inquietud.
Cuando realizo algún Programa. Me ha ocurrido que lo hice (por Ejemplo) para un Arduino NANO y tiempo después lo instalé en un Arduino UNO teniendo que realizar alguna modificaciones.
Arduino Nano tiene la posibilidad de permitirme (Analógicos) A0... hasta A7
Pero un nHduino o Arduino UNO solo tiene (Analógicos) A0... A5
Mi pregunta es:
¿Podemos (de algún modo) saber que placa es para preverlo en nuestro Sketch y de este modo configurar esos puertos de acuerdo a la placa que sea corrido nuestro programa en ese momento?
Dicho de otro modo ¿poseen algún Nro. Interno de serie el cual podamos verlo y consultarlo; como es el caso de los módulos Bluetooth HC-05?
Recuerdo que a la hora de hacer una librería yo consulto si es un arduino en el encabezado de la librería. Pero en sentido global.
Te agradezco cualquier comentario o link donde me pueda orientar a encontrar la respuesta.
NO entiendo para que quieres saber eso si tienes que subir el Sketch y en ese momento lo tienes delante.
Entiendo tu duda para alguien que no sabe nada de Arduino y le pones un NANO y un UNO y dice que es esto?
Le subo el mismo programa?
Si no sabe nada pensará que es posible pero no veo otra situación en la que alguien se pregunte eso.
Ahora pensarás que evadí tu respuesta, vamos a como identificarlos:
Cuando tu pones un arduino y se detecta el puerto COM en el IDE tienes una opción que dice
Obten información de la placa
Con esta información supongo que algo podrias hacer.
Concretamente eso me paso en el proyecto en el que estoy "nRF24L01"
Estaba en el desarrollo de Comunicación entre 2 Arduinos. Los cuales 1 es NANO y otro UNO.
Ambos tienen el mismo Sketch, pero NANO tiene ocupa los analógicos A6 y A7, pero grabo el Sketch sobre el UNO y debo corregirlo porque el UNO tiene A4 y A5.
entonces mi necesidad es escribirle dentro: Por Ej.
if (Modelo == "NANO"
if (analogRead(A7) >127) { tal cosa }
goto COMIENZO:
}
if (Modelo == "UNO"
if (analogRead(A5) >127) { la misma cosa }
goto COMIENZO:
}
esto sería en concreto. Si sé en que placa corre el mismo sabrá por que analógico pregunta. Sin que yo tenga que estar modificando su Sketch antes de grabarlo.
Puede que en el caso de @Humberto02 se trate de un Arduino Nano con un ATmega168 con lo que la solución sea simplemente ver si está definido
__AVR_ATmega328P__
para saber que se trata del Arduino UNO.
Pero si se trata de diferenciar únicamente entre un Arduino UNO y un Arduino Nano con el mismo micro entonces sí que no nos vale ver si está definido
__AVR_ATmega328P__
ya que lo está para ambos. Para este caso en particular nos valdría con poner lo siguiente:
#include <pins_arduino.h>
#if defined(NUM_ANALOG_INPUTS) && NUM_ANALOG_INPUTS == 6
// Definiciones para el arduino UNO
#define PIN_ANALOGICO A5
#else
// Definiciones para el arduino Nano
#define PIN_ANALOGICO A7
#endif
void setup() {
}
void loop() {
if (analogRead(PIN_ANALOGICO) >127) {
// Hacer lo que se quiera
}
}
Esto ha de funcionar ya que para el Arduino Nano con un ATmega328P la constante
Creo que la confusión viene de que en versiones más modernas del IDE los han distinguido.
En el siguiente código, sacado de la librería ArduinoBoardManager, se pude ver las directivas de compilación definidas.
UNO AVR_ATmega328P
NANO AVR_ATmega328_
#if defined(__AVR_ATmega328P__) // uno, fio
static const uint8_t BOARD = 0x01; /**< UNO board */
static const uint8_t NUM_BITS = 8; /**< 8-bit processor */
static const uint16_t CPU = __AVR_ATmega328P__; /**< 16Mhz */
static const unsigned long SRAM_SIZE = 2000; /**< 2kb of sram */
static const unsigned long EEPROM_SIZE = 1000; /**< 1kb eeprom */
static const unsigned long FLASH_SIZE = 32000; /**< 32k flash storage */
#elif defined(__AVR_ATSAMD21G18A__) // zero
static const uint8_t BOARD = 0x02
static const uint8_t NUM_BITS = 8;
static const uint16_t CPU = __AVR_ATSAMD21G18A__;
static const unsigned long SRAM_SIZE = 32000;
static const unsigned long EEPROM_SIZE = 16000;
static const unsigned long FLASH_SIZE = 256000;
#elif defined(__AVR_ATSAM3X8E__) // Due
static const uint8_t BOARD = 0x03;
static const uint8_t NUM_BITS = 8;
static const uint16_t CPU = __AVR_ATSAMD21G18A__;
static const unsigned long SRAM_SIZE = 96000;
static const unsigned long EEPROM_SIZE = 0;
static const unsigned long FLASH_SIZE = 512000;
#elif defined(__AVR_Atmega32U4__) // Yun 16Mhz, Micro, Leonardo, Esplora
static const uint8_t BOARD = 0x04;
static const uint8_t NUM_BITS = 8;
static const uint16_t CPU = __AVR_Atmega32U4__;
static const unsigned long SRAM_SIZE = 2500;
static const unsigned long EEPROM_SIZE = 1000;
static const unsigned long FLASH_SIZE = 32000;
#elif defined(_AVR_AR9331__) // Yun 400Mhz
static const uint8_t BOARD = 0x05;
static const uint8_t NUM_BITS = 8;
static const uint16_t CPU = _AVR_AR9331__;
static const unsigned long SRAM_SIZE = 64000000;
static const unsigned long EEPROM_SIZE = 0;
static const unsigned long FLASH_SIZE = 16000000;
#elif defined(__AVR_ATmega16U4__) // leonardo
static const uint8_t BOARD = 0x06;
static const uint8_t NUM_BITS = 8;
static const uint16_t CPU = __AVR_ATmega16U4__;
static const unsigned long SRAM_SIZE = 2560;
static const unsigned long EEPROM_SIZE = 1000;
static const unsigned long FLASH_SIZE = 32000;
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // mega, Mega ADK
static const uint8_t BOARD = 0x07;
static const uint8_t NUM_BITS = 8;
static const uint16_t CPU = __AVR_ATmega1280__;
static const unsigned long SRAM_SIZE = 8000;
static const unsigned long EEPROM_SIZE = 4000;
static const unsigned long FLASH_SIZE = 256000;
#elif defined(_AVR_ATmega328__) // Nano >= v3.0 or Arduino Pro, pro328, ethernet
static const uint8_t BOARD = 0x08;
static const uint8_t NUM_BITS = 8;
static const uint16_t CPU = _AVR_ATmega328__;
static const unsigned long SRAM_SIZE = 2000;
static const unsigned long EEPROM_SIZE = 1000;
static const unsigned long FLASH_SIZE = 32000;
#elif defined(_AVR_ATmega168__) // Nano < v3.0 or uno, pro
static const uint8_t BOARD = 0x09;
static const uint8_t NUM_BITS = 8;
static const uint16_t CPU = _AVR_ATmega168;
static const unsigned long SRAM_SIZE = 1000;
static const unsigned long EEPROM_SIZE = 500;
static const unsigned long FLASH_SIZE = 16000;
#elif defined(_AVR_ATmega168V__) // LilyPad
static const uint8_t BOARD = 0x0a;
static const uint8_t CPU = _AVR_ATmega168V__;
static const unsigned int NUM_BITS = 8;
static const unsigned long SRAM_SIZE = 1000;
static const unsigned long EEPROM_SIZE = 500;
static const unsigned long FLASH_SIZE = 14000
#elif defined(_AVR_ATmega328V__) // LilyPad 2
static const uint8_t BOARD = 0x0b;
static const uint8_t CPU = _AVR_ATmega328V__;
static const unsigned int NUM_BITS = 8;
static const unsigned long SRAM_SIZE = 1000;
static const unsigned long EEPROM_SIZE = 500;
static const unsigned long FLASH_SIZE = 14000
#elif defined(_AVR_ATTiny85__) // trinket
static const uint8_t BOARD = 0x0c;
static const uint8_t NUM_BITS = 8;
static const uint16_t CPU = _AVR_ATTiny85__;
static const unsigned long SRAM_SIZE = 500;
static const unsigned long EEPROM_SIZE = 500;
static const unsigned long FLASH_SIZE = 2500;
#elif defined(__AVR_ARCv2EM__) || (__CURIE_FACTORY_DATA_H_) // Intel Curie/101
static const uint8_t BOARD = 0x0d;
static const uint8_t NUM_BITS = 32;
static const uint16_t CPU = __AVR_ARCv2EM__;
static const unsigned long SRAM_SIZE = 24000; // might be 80k?
static const unsigned long EEPROM_SIZE = 0;
static const unsigned long FLASH_SIZE = 384000;
#else
static const uint8_t BOARD = 0x00;
static const uint8_t NUM_BITS = 0;
static const uint16_t CPU = 0;
static const unsigned long SRAM_SIZE = 0;
static const unsigned long EEPROM_SIZE = 0;
static const unsigned long FLASH_SIZE = 0;
#endif