Uno R3 sketch Not Working with Uno R4 Minima

Hello All,

I just purchased an Arduino Uno R4 Minima with plans to use it as controller for a DIY motion simulator. The code I have is written for Uno R3. Needless to say, I found out that the R3 sketch does not compile on the R4 likely due to the AVR specific portions of the code. Before I give up on the R4, I was wondering if anyone can help with this error?

Compilation error: 'PORTD' was not declared in this scope

Any suggestions on how to fix this? There may be many more issues that I don't know about, but figured I would start with this since this is the first error I get when compiling.

Any help or insight is greatly appreciated.

Thanks,
Lee

Please post the entire sketch in code tags, and also copy and paste the entire error message in a new post.

I think your realization that it's a different processor has already suggested to you the explanation.

If you can re-write your sketch to use "digitalWriteFast()" instead of direct port access, that would be a big step toward making it more portable. Being "more portable" is a good thing; much better than re-writing it for each new architecture that comes along.

Some discussion of the R4 "direct IO" stuff is here: digitalWriteFast with UNO R4

Or, the short version, if you can re-write your sketch. Since you wrote it, you must have a fairly good familiarity with direct access to hardware. The R4 hardware must differ only in form... did you investigate?

As others have mentioned, posting the code helps others to help you.

By coincidence, earlier today I started a thread:
AVR Compatibility: PORT and PIN registers? - UNO R4 / UNO R4 WiFi - Arduino Forum
To discuss the issues associated with using the AVR port registers.

For some board types there are some c++ emulation classes for the different port registers. I knew that we have some of them defined for the Teensy (PJRC) board types. @ptillisch posted there, that there as are similar emulation classes for some of the other Arduino boards as well. But hard to say, without additional information, what approach would work in your case.

Good luck

Hi Guys,

Thank you for the responses. To be clear, I did not write the sketch. I'm brand new to Arduino. I'm trying to use a sketch written for the Uno R3 by others. Because I'm new to the forum I cannot upload. So I have copied and pasted a portion of the code up to the error line into the window. I don't know of a better way and pasting the entire code was obnoxious. The first error starts on line 145. If someone can suggest a better way, or if I should paste the entire code, just let me know.

Line 145 int OutputPort =PORTD; // read the current port D bit mask

Error Code
Compilation error: 'PORTD' was not declared in this scope

Thanks again,
Lee

//****************************************************************************************************************
// SMC3.ino is basic Motor PID driver designed for motion simulators with upto 3 motors (written for UNO R3)
// Sabertooth Mode 3 upgrade to packet serial mode with serial timeout by BlazinH
// Softstart by BlazinH
//****************************************************************************************************************
//****************************************************************************************************************
//SET SOFTSTART SPEED
const int SSloop = 30;
//****************************************************************************************************************

// Set to MODE1 for use with a typical H-Bride that requires PWM and 1 or 2 direction inputs
// Set to MODE2 for a 43A "Chinese" IBT-2 H-Bridge from e-bay or equiv
// Set to MODE3 for a Sabretooth setup in simple serial mode

#define MODE3

// Uncomment the following line to reverse the direction of Motor 1.
// #define REVERSE_MOTOR1

// Uncomment ONE of the following lines to enable analogue input AN5 as a scaler for the motion values.
// #define ENABLE_POT_SCALING
// #define ENABLE_NON_LINEAR_POT_SCALING

// Uncomment the following line to enable the second software serial port.
// NOTE: This is currently not working - leave commented out until fixed!!!
// #define SECOND_SERIAL

// COMMAND SET:
//
// [] Drive all motors to defined stop positions and hold there
// [Axx],[Bxx],[Cxx] Send position updates for Motor 1,2,3 where xx is the binary position limitted to range 0-1024
// [Dxx],[Exx],[Fxx] Send the Kp parameter for motor 1,2,3 where xx is the Kp value multiplied by 100
// [Gxx],[Hxx],[Ixx] Send the Ki parameter for motor 1,2,3 where xx is the Ki value multiplied by 100
// [Jxx],[Kxx],[Lxx] Send the Kd parameter for motor 1,2,3 where xx is the Kd value multiplied by 100
// [Mxx],[Nxx],[Oxx] Send the Ks parameter for motor 1,2,3 where xx is the Ks value
// [Pxy],[Qxy],[Rxy] Send the PWMmin and PWMmax values x is the PWMmin and y is PWMmax each being in range 0-255
// [Sxy],[Txy],[Uxy] Send the Motor Min/Max Limits (x) and Input Min/Max Limits (y) (Note same value used for Min and Max)
// [Vxy],[Wxy],[Xxy] Send the Feedback dead zone (x) and the PWM reverse duty (y) for each motor
// [rdx] Read a value from the controller where x is the code for the parameter to read
// [ena] Enable all motors
// [sav] Save parameters to non-volatile memory
// [ver] Send the SMC3 software version
//

// Arduino UNO Pinouts Used
//
// 9 - Motor 1 PWM
// 10 - Motor 2 PWM
// 11 - Motor 3 PWM
// 2 - Motor 1 H-Bridge ENA
// 3 - Motor 1 H-Bridge ENB
// 4 - Motor 2 H-Bridge ENA
// 5 - Motor 2 H-Bridge ENB
// 6 - Motor 3 H-Bridge ENA
// 7 - Motor 3 H-Bridge ENB
// A0 - Motor 1 Feedback
// A1 - Motor 2 Feedback
// A2 - Motor 3 Feedback
// A5 - Motion scaler pot input

// BOF preprocessor bug prevention - leave me at the top of the arduino-code
#if 1
__asm volatile ("nop");
#endif

#include <EEPROM.h>
#include <SoftwareSerial.h>

// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

#define COM0 0 // hardware Serial Port
#define COM1 1 // Software Serial Port
#define START_BYTE '[' // Start Byte for serial commands
#define END_BYTE ']' // End Byte for serial commands
#define PROCESS_PERIOD_uS 1000 // 244 -> 4096 per second approx
#define TIMER_UPPER_LIMIT 4294966000 // Used to check for timer wraparound
#define TIMER_LOWER_LIMIT 1000 // Used to check for timer wraparound

int holdmax1;
int holdmax2;
int holdmax3;
int softstart = 0;
int counter = 0;

unsigned long TimesUp=0; // Counter used to see if it's time to calculate next PID update

int Feedback1 = 512;
int Feedback2 = 512;
int Feedback3 = 512;
int Target1 = 512;
int Target2 = 512;
int Target3 = 512;
int PotInput = 512;
int OffsetAdjust=0;

unsigned int RxByte[2]={0}; // Current byte received from each of the two comm ports
int BufferEnd[2]={-1}; // Rx Buffer end index for each of the two comm ports
unsigned int RxBuffer[5][2]={0}; // 5 byte Rx Command Buffer for each of the two comm ports
unsigned long LoopCount = 0; // unsigned 32bit, 0 to 4,294,967,295 to count times round loop
unsigned long LastCount = 0; // loop counter the last time it was read so can calc delta
byte errorcount = 0; // serial receive error detected by invalid packet start/end bytes
unsigned int CommsTimeout = 0; // used to reduce motor power if there has been no comms for a while
byte PowerScale = 7; // used to divide(shift) PID result changes when in low power

//
//
// +----+ +------+
// +-|PWR |--------------| USB |-----+
// | | | | | |
// | +----+ +------+ o|
// | o|
// | o|AREF
// | o|GND
// NC|o o|13 TTL (software) Serial
// IOREF|o +----+ o|12 TTL (software) Serial
// RESET|o | | o|11~ PWM Motor 3
// 3.3V|o | | o|10~ PWM Motor 2
// 5V|o | | Arduino o|9~ PWM Motor 1
// GND|o | | UNO o|8
// GND|o | | |
// VIN|o | | o|7 Motor 3 ENB
// | | | o|6~ Motor 3 ENA
// Motor 1 Pot A0|o | | o|5~ Motor 2 ENB
// Motor 2 Pot A1|o | | o|4 Motor 2 ENA
// Motor 3 Pot A2|o | | o|3~ Motor 1 ENB
// A3|o | | o|2 Motor 1 ENA
// A4|o | | o|1
// Motion Scaler A5|o +-/-+ o|0
// +-\ /---------+
// --------------------/
//
//

int OutputPort =PORTD; // read the current port D bit mask

There is much written in that code which is specific to the AT328 chip used in the UnoR3. As a beginner I doubt you will be successful in converting the sketch. I suggest that you buy an UnoR3 if you want to use that code.

Ok, thank you for the feedback. That was my fallback position but figured I should give it the ole college try first.

Guess R3 is up next.

Lee