I have written a simple filter function to lowpass filter on voltage. I noticed it was running slower on the nano every vs the regular nano, in either native 4809 or emulated ATMEGA328. I don't think it's a code issue as the code is identical and loaded on both, but someone here may point out how to optimize it better or that I've done something wrong... I have an unfiltered analog read on pin A6 to take voltage. Then I print that to a display, and pass the value into two filters for additional output. All the outputs to the display differ in speed between the two boards, though they shouldn't. I have the boards both powered from the same source, and both getting a feed from the same battery in parallel to measure. Current and voltage supply is not a problem. I am using a pot to adjust the voltage for testing to both boards simultaneously.
Voltage read is fluctuating on the nano every up and down, a tenth or so, in a slow sine wave like fashion, and that same input voltage is completely locked and stable on the classic nano. That's the first issue.
The second is I don't think that SPI is working correctly on the Every, as it is very slow in both processor modes. The updates to the screen trail the classic by quite a bit regarding the first and second filtered outputs. And the raw read output is very choppy as it passes through its range on the every.
Here's the code:
//Arduino Nano vs Nano Every comparison with identical code
#include <Adafruit_SSD1331.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
//********************NANO OLED DEFINITIONS for use when uploading NANO Code
#define SCL 13
#define SDA 11
#define CS 10
#define RST 9
#define DC 8
//_______________________________________
#define VOLTPIN A6
// ---------------------------------------------Display Initializers
// -----------------------------------------------Color definitions
#define YELLOW 0xFFE0
#define BLACK 0x0000
//Adafruit_SSD1331 display = Adafruit_SSD1331(CS, DC,SDA, SCL, RST); // for non -spi on worse boards
Adafruit_SSD1331 display = Adafruit_SSD1331(&SPI, CS, DC, RST);
//SPISettings settingsA(2000000, MSBFIRST, SPI_MODE1); // use lower MB values for worse boards to reduce impedance issues
char voltage_str [4] = "";
float batteryVoltageInput = 0;
float voltageFilter (float rawValue, byte factor) {
static float filteredVal1 = 0, filteredVal2 = 0;
if (factor ==1) {
filteredVal1 = 0.80 *filteredVal1 + 0.20 * rawValue;
return filteredVal1;
}
else
{
filteredVal2 = 0.946*filteredVal2 + 0.054 * rawValue;
return filteredVal2;
}
}
void setup() {
pinMode(10, OUTPUT);
pinMode(VOLTPIN, INPUT);
// analogRead(INTERNAL4V3); // Try to see if better next?
SPI.begin();
display.begin(); // initialize with the I2C addr 0x3D (for the 128x64)
display.fillScreen(BLACK);
delay(200);
display.setTextSize(1);
display.setTextColor(YELLOW,BLACK);
}
// ------------------------------------------------- V O I D L O O P ------------------------------------------
void loop() {
static float filtered_voltage = 0;
batteryVoltageInput = (float)analogRead(VOLTPIN)*5/1023; // Read the raw analog value and convert it
dtostrf(batteryVoltageInput,4,2,voltage_str);
display.setCursor(0,8);
display.write(voltage_str); //unfiltered voltage
filtered_voltage = voltageFilter(batteryVoltageInput,1);
dtostrf(filtered_voltage,4,2,voltage_str);
display.setCursor(0,22);
display.write(voltage_str); //filtered with first filter function
filtered_voltage = voltageFilter(batteryVoltageInput,2);
dtostrf(filtered_voltage,4,2,voltage_str);
display.setCursor(0,33);
display.write(voltage_str); //Filtered with 2nd filter function
}
The output is to an oled with 1331 driver.