Translating Code from Nano to Uno - pin in0 vs. A0?

Yes I am a newb. This is a project that is using an Arduino Nano to read a potentiometer and send that value over serial uart to an FPGA based synthesizer board called "XFM2". The code was provided by the creator of the XFM2 and written for Nano, but I want to use my Uno R3. My problem is that I am a newb and don't understand how to translate the code from Nano to Uno regarding the defining of the analog pins.

For example, I don't really understand how the pins are being defined to read the analog pin. On the Uno, I would expect maybe something like "int analogPin = A0" towards the beginning of the code.

I think the code below is defining "in0" as the analog pin on the Nano, but after that it gets converted to other things and I get lost. What is really confusing is that he created an "int a0" a bit later. Is that just a coincidence or is it referencing "analog pin 0"?

How would I make this work on the Uno?

Your help is most appreciated.

PS - the //notes are not my notes to you. They are notes the programmer wrote to be helpful.
P.S.S here is the youtube link where I got the code. It shows what this is accomplishing.
Arduino Nano controlling XFM2

// This example sketch shows how to command XFM2 using the UART port
// All parameters in XFM2 can be changed by:
// 1- USB serial, using any USB serial host device (i.e. a computer)
// 2- MIDI CC (most common controls have a direct CC mapping, and there are 4 user-definable CC slots)
// 3- MIDI System Exclusive messages (sysex), of the form F0 43 UU P1 P2 V1 V2 F7.
// 4- External UART
// When building an User Interface for XFM2, the most flexible option is #4:
// - UART is very fast, works at 500kbps (16x faster than MIDI)
// - It's 8-bit, so a single set_parameter message is 3 or 4 bytes instead of 8 for sysex
// - It works *in parallel* to the MIDI stream, so both streams are processed simultaneously.
// So the external UART approach is about 32x times faster than MIDI.

// The nano has 8 analog inputs. This means, up to eight direct controls can be added just by replicating
// the code below. Also, switches connected to the digital inputs could be used to change 'control pages',
// or to drive an external multiplexor as to have any number of controls beyond 8.

// XFM2 parameter numbers
#include "XFM2_params.h"

// set_unit(), set_parameter()
void set_unit( int unit ) {
  // Default to unit 1
  Serial.write( unit == 2 ? '2' : '1' );

void set_parameter( int param, int value ) {
  Serial.write( 's' ); // 's' = Set Parameter

  if ( param > 255 ) {
    // Parameters above 255 have a two-byte format: b1 = 255, b2 = x-256
    Serial.write( 255 );
    Serial.write( param - 256  );
    Serial.write( value );
  else {
    Serial.write( param );
    Serial.write( value );

// setup()
void setup() {
  // Set COM to transmit at XFM2 speed (500kpbs)
  Serial.begin( 500000 );

  // Built-in LED in the Arduino nano

// loop()
void loop() {

  // Statics
  static int in0;
  static int v0;
  static int avg0;
  static int last0 = 2000;

  // Read pot value from analog input 0
  v0 = analogRead( in0 );

  // Smooth value cheaper than a cap
  avg0 += ( v0 - avg0 ) / 2;

  // 10-bit -> 8-bit
  int a0 = avg0 >> 2;

  // New value? send it to XFM2
  if ( a0 != last0 ) {

    // Set this message to be sent to XFM2 unit 1 (change to '2' to send to the second unit)
    set_unit( 1 );

    // Send parameter AM DEPTH (#332) with value 255
    // NOTE: This first message is just needed to ensure the next parameter effect is heard
    //set_parameter( PRM_AM_DEPTH, 255 );
    set_parameter( PRM_OUTPUT_LEVEL, 255 );

    // Send parameter AM SPEED RANGE (#331) with the read value
    set_parameter( PRM_AM_RANGE, a0 );

    // Flash some light as to assert dominance
    digitalWrite( LED_BUILTIN, HIGH );
    delay( 20 );
    digitalWrite( LED_BUILTIN, LOW );
    delay( 20 );

  last0 = a0;

The Uno and the Nano use the same ATMEGA328P chip. There is one notable difference - The Nano gives you access to 2 additional analog pins A6 and A7. These are not accessible on the Uno.

Your code will compile just fine for either board.

The "pinMode" is set in the analogRead() function. That function knows the mode to put the pin into. The same with the analogWrite() function. The functions not only set up the pin, they also configure the ADC or set PWM default parameters.

There should not be any changes to the code, Uno to Nano. The processor is the same. The analog inputs are referred to by A0, A1, A2, ... for both the Uno and Nano.

That code is a bit odd....

It declares a variable in0 and since it is declared as static, you are guaranteed it will be initialized to 0. Then, later on, it is used as the pin for analogRead() which reads A0. [analogRead() is smart enough to figure out the correct pin if you say 0 or A0]

A more typical bit of code would be

const int analogPin = A0;
v0 = analogRead(analogPin);

Thank you.

So in this code:

From what I understand there is no pin on Nano called "in0". That's what confuses me, I don't see any reference anywhere to an actual pin on the Nano. Wouldn't you do "analogRead( A0 )" ?

How is it reading the analog pin (in0) when there is no such pin?

I apologize for my lack of knowledge.


That is what confused me. I assumed the programmer was using some trickery, but I just couldn't figure it out.

Thank you.

in0 is just a variable name, just like A0 or A3.... Don't confuse a variable name with the value it holds.

If they wanted their code to be more readable:

  const byte in0 = A0;
  static int v0 = 0;
  static int avg0 = 0;

Changing the name "in0" to "analogInPin0" would be even better.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.