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.
// ARDUINO NANO
// 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
pinMode( LED_BUILTIN, OUTPUT );
}
//------------------------------------------------------------------------------------------------------------
// 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;
}