An error that I can't identify and correct

This is the code:

[code]
/* ////////////////////////////////////////////////////////////////// 
  
ARDUINO/Genuino Project 
 
////////////////////////////////////////////////////////////////// */
 
 
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
  
// CONNECTIONS OLED
#define OLED_MOSI 11
#define OLED_CLK 13
#define OLED_DC 4
#define OLED_CS 3
#define OLED_RESET 5
 
// THE RANGES
int Range = 1 ;
// Range 1 uses 91 R
// Range 2 uses 910 R
// Range 3 uses 9k1
// Range 4 uses 91k
// Range 0 uses 910k, THIS IS ALWAYS "ON"
const int Range1Pin = 10 ;
const int Range2Pin = 9 ;
const int Range3Pin = 8 ;
const int Range4Pin = 7 ;
const int DisChargePin = 12 ;
const int EnableChargPin = 6 ;
 
// COUNTER
const int Gate = A0 ;
const int Reset = A2 ;
const int OVR0 = A6 ;
const int OVR1 = A3 ;
const int OVR2 = A7 ;
const byte AdrLoBytes = 0x27 ;    // MCP 23017
const byte AdrHiBytes = 0x20 ;    // MCP 23017
 
unsigned long Counts = 0 ;
double Capacity = 0.0 ;
int CapUnit = 3 ;  // THIS IS ABOVE pF, i.e. 3 -> nF, 6 -> uF, ...
 
 
Adafruit_SH1106 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
 
#if (SH1106_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SH1106.h!");
#endif
 
 
boolean debug = false ;
 
// ZERO OFFSET COMPENSATION VALUES
float ZERO[5] = { 0.0, 0.0, 0.0, 0.0, 0.0 } ;
const int ZEROPin = 2 ;
volatile bool NeedZERO = false ;
 
 
// /////////////////////////////////////////////////////////////////////
// SUBROUTINES  
// /////////////////////////////////////////////////////////////////////
 
float Reff = 1.0 ;
 
void SetR()
{
  if(Range < 0 ) Range = 0 ;
  if(Range > 4 ) Range = 4 ;
  switch(Range)
  {
    case 0:
    digitalWrite(Range1Pin, LOW) ;
    digitalWrite(Range2Pin, LOW) ;
    digitalWrite(Range3Pin, LOW) ;
    digitalWrite(Range4Pin, LOW) ;
    digitalWrite(DisChargePin, LOW) ;
    digitalWrite(EnableChargPin, HIGH) ;
    Reff = 910000 ;   // 910k // nüüt
    break ;
 
    case 1:
    digitalWrite(Range1Pin, HIGH) ;
    digitalWrite(Range2Pin, LOW) ;
    digitalWrite(Range3Pin, LOW) ;
    digitalWrite(Range4Pin, LOW) ;
    digitalWrite(DisChargePin, LOW) ;
    digitalWrite(EnableChargPin, HIGH) ;
    Reff = 82727.27 ;   // 910k // 91kR
    break ;
 
    case 2:
    digitalWrite(Range1Pin, LOW) ;
    digitalWrite(Range2Pin, HIGH) ;
    digitalWrite(Range3Pin, LOW) ;
    digitalWrite(Range4Pin, LOW) ;
    digitalWrite(DisChargePin, LOW) ;
    digitalWrite(EnableChargPin, HIGH) ;
    Reff = 9009.90 ;   // 910k // 9k1
    break ;
 
    case 3:
    digitalWrite(Range1Pin, LOW) ;
    digitalWrite(Range2Pin, LOW) ;
    digitalWrite(Range3Pin, HIGH) ;
    digitalWrite(Range4Pin, LOW) ;
    digitalWrite(DisChargePin, LOW) ;
    digitalWrite(EnableChargPin, HIGH) ;
    Reff = 909.09 ;   // 910k // 910R
    break ;
 
    case 4:
    digitalWrite(Range1Pin, LOW) ;
    digitalWrite(Range2Pin, LOW) ;
    digitalWrite(Range3Pin, LOW) ;
    digitalWrite(Range4Pin, HIGH) ;
    digitalWrite(DisChargePin, LOW) ;
    digitalWrite(EnableChargPin, HIGH) ;
    Reff = 90.99 ;   // 910k // 91R
    break ;  
  }
}
 
 
void UpDateCounts()
{
  byte aux = 0x00 ;
  Counts = 0x00000000 ;
  Wire.beginTransmission(AdrHiBytes) ;  // PORT B COUNTS @ X5     
  Wire.write(0x13) ;                   
  Wire.endTransmission() ;
  Wire.requestFrom(AdrHiBytes, 1) ;           
  if (Wire.available()) aux = Wire.read() ;      
  Counts = aux ;
  Counts = Counts << 8 ;
  Wire.beginTransmission(AdrHiBytes) ;  // PORT A COUNTS @ X5         
  Wire.write(0x12) ;                   
  Wire.endTransmission() ;
  Wire.requestFrom(AdrHiBytes, 1) ;           
  if (Wire.available()) aux = Wire.read() ;      
  Counts |= aux ;
  Counts = Counts << 8 ;
  Wire.beginTransmission(AdrLoBytes) ;  // PORT B COUNTS @ X4         
  Wire.write(0x13) ;                   
  Wire.endTransmission() ;
  Wire.requestFrom(AdrLoBytes, 1) ;           
  if (Wire.available()) aux = Wire.read() ;      
  Counts |= aux ;
  Counts = Counts << 8 ;
  Wire.beginTransmission(AdrLoBytes) ;  // PORT A COUNTS @ X4         
  Wire.write(0x12) ;                   
  Wire.endTransmission() ;
  Wire.requestFrom(AdrLoBytes, 1) ;           
  if (Wire.available()) aux = Wire.read() ;      
  Counts |= aux ;
  if(debug) Serial.println(Counts,DEC) ;
}
 
 
void ResetCounter()
{
  digitalWrite(Reset, HIGH) ;
  delay(10) ;
  digitalWrite(Reset, LOW) ;
  delay(10) ;
}
 
 
void UpdateCapacity()
{
  //      ln(2) * tau = counts * 1 / Fosc
  //      tau = counts  / ( Fosc * ln(2))
  //      R * C = counts / ( Fosc * ln(2))
  //      C = counts / ( Fosc * ln(2) * R )
  //      Fosc = 64 MHz, ln(2) = 0.69314718056
  //      C = counts / ( 44361419.5558 * R )
  //      C[pF] = 1E12 * counts / ( 44361419.5558 * R )
  //      C[pF] = counts * 0.02477154946, when using 910kR
   
  switch(Range)
  {
    case 0 :    // 910kR, pF
    Capacity = Counts * 24.77154946 / 1000.0 ;
    Capacity -= ZERO[0] ;
    CapUnit = 0 ;   
    break;
    case 1 :    // 91kR, nF
    Capacity = Counts * 2.7248704412 / 1000.0 ;
    Capacity -= ZERO[1] ;
    CapUnit = 3 ;   
    break;
    case 2 :    // 9k1, nF
    Capacity = Counts * 2.50192649605 / 1000.0 ;
    Capacity -= ZERO[2] ;
    CapUnit = 3 ;   
    break;
    case 3 :    // 910R, nF
    Capacity = Counts * 24.79632101528 / 1000.0 ;
    Capacity -= ZERO[3] ;
    CapUnit = 3 ;   
    break;
    case 4 :    // 91R, uF
    Capacity = Counts * 247.74026620760 / 1000000.0 ;
    Capacity -= ZERO[4] ;
    CapUnit = 6 ;   
    break;
  }  
  // if(debug) Serial.println(Capacity,3) ;
}
 
 
void DisCharge(unsigned long HowLong) 
{
  digitalWrite(DisChargePin, HIGH) ;
  digitalWrite(EnableChargPin, LOW) ;
  delay(HowLong) ;
}
 
 
void CheckRange()
{
  const long GearUp = 9999 ;   // Counted Pulses
  const long GearDown = 120000 ; // Counted Pulses
  if(Counts < GearUp)
  {
    Range -= 1 ;  // INCREASE RESISTANCE
    SetR() ;
  }
  if(Counts > GearDown)
  {
    Range += 1 ;  // DECREASE RESISTANCE
    SetR() ;
  }
  if(Counts == 0)
  {
    Range = 4 ;   // MAXIMUM : SHORT OR LARGE
    SetR() ;
  }
}
 
 
void WaitGateFalling(unsigned long timeout)
{
  unsigned long StopTime = millis() + timeout ;
  int StatusGate = digitalRead(Gate) ;
  int LastStatusGate = 0 ;
  bool Flag = true ;
  while(Flag)
  {
    LastStatusGate = StatusGate ;
    StatusGate = digitalRead(Gate) ;     
    // CHECK FOR TIMEOUT
    if(millis() > StopTime) Flag = false ;
    // CHECK FOR TRANSITION
    if((StatusGate == 0)&(LastStatusGate == 1)) Flag = false ;
    delay(9) ;
  }
}
 
 
// /////////////////////////////////////////////////////////////////////
// SUBROUTINES DISPLAY.
// /////////////////////////////////////////////////////////////////////
  
void UpDateDisplay()
{
  int PCD = 1 ; // Pre Comma Digits
  // LOG10 NEEDS MATH LIBRARY ...
  if(Capacity >= 10.0000) PCD = 2 ;
  if(Capacity >= 100.000) PCD = 3 ;
  if(Capacity >= 1000.00) PCD = 4 ;
  if(Capacity >= 10000.0) PCD = 5 ;
  int ACD = 5 - PCD ;
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(1,0);
  display.println("***  IPAMOD 2022  ***");
  display.drawLine(0, 12, 128, 12, WHITE);
  display.setTextSize(2);
  // NUMERICAL VALUE
  display.setCursor(15,25);
  if(Capacity < 0.0) Capacity = 0.0001 ;
  display.print(Capacity,ACD);
  Serial.println(Capacity, ACD) ;
  // UNIT
  switch(CapUnit)
  {
    case 0:
      display.setCursor(100,25); display.print("p");
      display.setCursor(115,25); display.print("F");
      break ;
    case 3:
      display.setCursor(100,25); display.print("n");
      display.setCursor(115,25); display.print("F");
      break ;
    case 6:
      display.setCursor(100,25); display.print("u");
      display.setCursor(115,25); display.print("F");
      display.drawLine(100, 37, 98, 42, WHITE);
      display.drawLine(101, 37, 99, 42, WHITE);
      break ;
    case 9:
      display.setCursor(100,25); display.print("m");
      display.setCursor(115,25); display.print("F");
      break ;    
  }
  display.drawLine(0, 52, 128, 52, WHITE);
  // DISPLAY RESISTANCE USED
  display.setCursor(0,56);
  display.setTextSize(1);
  display.print("RANGE = ");
  display.print(Range,DEC);
  // DISPLAY COUNTS
  display.setCursor(69,56);
  display.print(Counts,DEC);
   
  display.display() ;
}
 
 
// /////////////////////////////////////////////////////////////
// ZEROing ROUTINES
// /////////////////////////////////////////////////////////////
 
void ZeroDisplay()
{
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(1,0);
  display.println("***  IPAMOD 2022  ***");
  display.drawLine(0, 12, 128, 12, WHITE);
  display.setTextSize(2);
  display.setCursor(15,25);
  display.print("ZERO ...");
  display.drawLine(0, 52, 128, 52, WHITE);
  // DISPLAY RESISTANCE USED
  display.setCursor(0,56);
  display.setTextSize(1);
  display.print("RANGE = ");
  display.print(Range,DEC);
  // DISPLAY COUNTS
  display.setCursor(69,56);
  display.print(Counts,DEC); 
 
  display.display() ;
}
 
 
void ZEROing()
{
  // MEASURE ALL RANGES AND STORE CAPACITY
  for(int i=0 ; i<4; i++)
  {
    Range = i ;
    ZeroDisplay() ; 
    DisCharge(499) ;  
    ResetCounter() ;
    SetR() ;       
    WaitGateFalling(999) ;
    UpDateCounts() ;
    UpdateCapacity() ;
    ZERO[i] = Capacity ;
    delay(999) ;
  }
  NeedZERO = false ;
}
 
 
void setup() 
{
  Serial.begin(115200) ;
 
  Wire.begin() ;
 
  // INIT PINS FOR REED RELAYS
  pinMode(Range1Pin, OUTPUT) ; 
  pinMode(Range2Pin, OUTPUT) ; 
  pinMode(Range3Pin, OUTPUT) ; 
  pinMode(Range4Pin, OUTPUT) ; 
  // DISCONNECT SOURCE    
  pinMode(EnableChargPin, OUTPUT) ;
  digitalWrite(EnableChargPin, LOW) ;   
  // DISCHARGE ANYTHING
  pinMode(DisChargePin, OUTPUT) ;
  digitalWrite(DisChargePin, HIGH) ;
  // ZERO
  pinMode(ZEROPin, INPUT_PULLUP) ;
 
  // COUNTER / COMPARATOR
  // HOUSEKEEPING
  pinMode(Gate, INPUT) ;
  pinMode(Reset, OUTPUT) ;
  pinMode(OVR0, INPUT) ;
  pinMode(OVR1, INPUT) ;
  pinMode(OVR2, INPUT) ;
 
  // INIT OLED
  display.begin(SH1106_SWITCHCAPVCC);
  // SHOW STARTUP SCREEN
  
  attachInterrupt(digitalPinToInterrupt(ZEROPin), ISRzero, FALLING) ;
   
  Range = 1 ;
  SetR() ;    
  DisCharge(5500) ;
  UpDateDisplay() ;
 }
 
 
   
void loop() 
{
  DisCharge(499) ;  
  ResetCounter() ;
  SetR() ;       
  WaitGateFalling(999) ; 
  UpDateCounts() ;
  UpdateCapacity() ;
  UpDateDisplay() ;
  CheckRange() ;
  delay(999) ;
  if(NeedZERO) ZEROing() ;
}
 
 
// /////////////////////////////////////////////////////////////
// INTERRUPT SERVICE ROUTINES
// /////////////////////////////////////////////////////////////
 
void ISRzero()
{
  NeedZERO = true ;
}
 
 
// ///////////////////////////////////////////////////////////// 
// END OF FILE.
// ///////////////////////////////////////////////////////////// 
[/code]

Thank you UKHeliBob

Please post the sketch that you are trying to compile, using code tags when you do

I assume that you are compiling for a Nano

please post the error, describe the error?

OK UKHeliBob, thank you again.
I clearly compiled the sketch for Arduino Nano via Arduino 1.8.13 and yet the errors were these:

Arduino:1.8.13 (Windows 10), Scheda:"Arduino Nano, ATmega328P"

In file included from C:\Users\lenovo\Documents\IZ5FCY PROJECTS\PrecCapMeter3\PreCapMeter\PreCapMeter.ino:8:0:

C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h: In function 'void UpDateCounts()':

C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:68:13: note: candidate 1: uint8_t TwoWire::requestFrom(int, int)

     uint8_t requestFrom(int, int);

             ^~~~~~~~~~~

C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:65:13: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)

     uint8_t requestFrom(uint8_t, uint8_t);

             ^~~~~~~~~~~

C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:68:13: note: candidate 1: uint8_t TwoWire::requestFrom(int, int)

     uint8_t requestFrom(int, int);

             ^~~~~~~~~~~

C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:65:13: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)

     uint8_t requestFrom(uint8_t, uint8_t);

             ^~~~~~~~~~~

C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:68:13: note: candidate 1: uint8_t TwoWire::requestFrom(int, int)

     uint8_t requestFrom(int, int);

             ^~~~~~~~~~~

C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:65:13: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)

     uint8_t requestFrom(uint8_t, uint8_t);

             ^~~~~~~~~~~

C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:68:13: note: candidate 1: uint8_t TwoWire::requestFrom(int, int)

     uint8_t requestFrom(int, int);

             ^~~~~~~~~~~

C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:65:13: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)

     uint8_t requestFrom(uint8_t, uint8_t);

             ^~~~~~~~~~~

In file included from c:\users\lenovo\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\io.h:272:0,

                 from c:\users\lenovo\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\pgmspace.h:90,

                 from C:\Users\lenovo\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino/Arduino.h:28,

                 from sketch\PreCapMeter.ino.cpp:1:

C:\Users\lenovo\Documents\IZ5FCY PROJECTS\PrecCapMeter3\PreCapMeter\PreCapMeter.ino: In function 'void UpDateDisplay()':

PreCapMeter:275:7: error: expected unqualified-id before numeric constant

   int ACD = 5 - PCD ;

       ^

exit status 1

expected unqualified-id before numeric constant


try making them integers

// const byte AdrLoBytes = 0x27 ;    // MCP 23017
// const byte AdrHiBytes = 0x20 ;    // MCP 23017
const int AdrLoBytes = 0x27 ;    // MCP 23017
const int AdrHiBytes = 0x20 ;    // MCP 23017

The problem is --

The Compiler doesn't like 'ACD'.

int unk = 5 - PCD;
Compiles.

Many thanks for your help.
IZ5FCY Roberto

Dear friends,
thank you for your valuable suggestions which have enabled me to make many steps forward.
Now I have another problem.
I attach the initial part of the code:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>


// DISPLAY

#define OLED_MOSI  8
#define OLED_CLK   9
#define OLED_DC    6
#define OLED_CS    5
#define OLED_RESET 7

int MAX_BAR_LENGTH = 127 ;
int MIN_BAR_LENGTH = 0 ;
int BARY = 45 ;


Adafruit_SH1106 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

From the wiring diagram it appears that an Arduino Nano Rev.3 processor is adopted.
If I perform the compilation for Aruino Nano, I get the following errors.

Arduino:1.8.13 (Windows 10), Board: "Arduino Nano, ATmega328P"

The sketch uses 27318 bytes (88%) of the space available for programs. Maximum is 30720 bytes.data section exceeds available space in board

Global variables use 3152 bytes (153%) of dynamic memory, leaving another -1104 bytes free for local variables. The maximum is 2048 bytes.
Memory exhausted!!!
Error during compilation for Arduino Nano board.

If I compile for an Arduino Nano Every, instead I get:

Arduino:1.8.13 (Windows 10), Board: "Arduino Nano Every, ATMEGA328"

C:\Users\lenovo\Documents\Arduino\libraries\Adafruit_SH1106-master\Adafruit_SH1106.cpp: In member function 'void Adafruit_SH1106::display()':
C:\Users\lenovo\Documents\Arduino\libraries\Adafruit_SH1106-master\Adafruit_SH1106.cpp:551:28: 
error: 'TWBR' was not declared in this scope
       uint8_t twbrbackup = TWBR;
                                           ^~~~
C:\Users\lenovo\Documents\Arduino\libraries\Adafruit_SH1106-master\Adafruit_SH1106.cpp: 551:28: note: 
suggested alternative: 'TWI0'
       uint8_t twbrbackup = TWBR;
                                           ^~~~
                                          TWI0
exit status 1
Error during compilation for Arduino Nano Every board.

In your opinion what can be the solution to this problem ?
I have searched but have not found an equivalent to SH1106.. so as not to upset the hardware.
Thank you for your cooperation.
IZ5FCY Roberto

@iz5fcy_robymak
Please show your revised code in full

Hi B707,
you can see the entire sketch at the beginning of this discussion.
Thank you

If I understand correct, the code at the beginning didn't compile. Later you fix the errors and now the code compiles fine.
So please show your new code.

My doubt is that the SH1106 display is not compatible with the processor of the Arduino Nano Every... :thinking:

But if it is incompatible, what can I replace it with?

From ETH Quantumoptics

it is definitely is not true. I am sure that SH1106 display can be used with Nano Every.

But this specific code that you try to use - probably not compatible with the board.

@iz5fcy_robymak
I asked for your final code because in the code from the beginning I don't see anyjthing that could require

27318 bytes (88%) of the program space 
3152 bytes (153%) of dynamic memory

I think that you show only a part of your code..

If you not going to share a full program - it is your decision.
In that case, I don't see how can we help you.

No intention of hiding anything :

/* //////////////////////////////////////////////////////////////////

  ARDUINO Broadband RF Power Meter

////////////////////////////////////////////////////////////////// */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>


// DISPLAY

#define OLED_MOSI  8
#define OLED_CLK   9
#define OLED_DC    6
#define OLED_CS    5
#define OLED_RESET 7

int MAX_BAR_LENGTH = 127 ;
int MIN_BAR_LENGTH = 0 ;
int BARY = 45 ;


// ROTARY ENCODER
const int RotaryEncoder1 = 4 ;   // PRESSED
const int RotaryEncoder2 = 2 ;
const int RotaryEncoder3 = 3 ;
volatile boolean LEFT = false ;
volatile boolean RIGHT = false ;
volatile boolean READY = true ;


Adafruit_SH1106 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

#if (SH1106_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SH1106.h!");
#endif

unsigned int SENSOR_TYPE = 0x0000 ;  
unsigned int SENSOR_TYPE_OLD = 0x0000 ;
unsigned int SERIAL_SENSOR = 0x00 ;

int RSSI_ARD_PIN = A0 ;
int SUPPLY_ARD_PIN = A7 ;
float SUPPLY_ADC = 5.024 ;
unsigned int RSSI_ARD_RAW = 0 ;
unsigned int MIN_RSSI_ARD_RAW = 20 ;  // BELOW : NO SENSOR, MEASURED 70 mV
float RSSI_ARD = -19.9 ;
float RSSI_ADC = -19.9 ;

// FREQUENCIES IN MHz
float F_LOW = 1.0 ;     // FROM EEPROM
float F_HIGH = 500.0 ;  // FROM EEPROM
// CALCULATED VALUES
float F_ARRAY[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13} ;

// LEVELS IN dBm
float L_LOW = -70.0 ;  // FROM EEPROM, LOWEST VALUE
float L_HIGH = 10.0 ;  // FROM EEPROM, HIGHEST VALUE

float SLOPE[13] = {1,1,1,1,1,1,1,1,1,1,1,1,1} ; 
float INTERCEPT[13] = {1,1,1,1,1,1,1,1,1,1,1,1,1} ; 

// POINTER TO APPLIED DATASET
int F_POINTER = 6 ;


// ///////////////////////////////////////////////////////////// 
// Prints the title block when program first starts.
// ///////////////////////////////////////////////////////////// 

const float Version = 22.06 ;

void print_title()
{
  Serial.println("Welcome to Broadband RF Power Meter");  
 
  Serial.println("======================================");   
}


// ///////////////////////////////////////////////////////////// 
// Prints a list of Options
// ///////////////////////////////////////////////////////////// 

void print_options()
{
  Serial.println("\n>>> WHAT ARE YOU DOING ??");
  Serial.println("----- SENSOR INFORMATION --------------");
  Serial.println("[t] Write Sensor Type");
  Serial.println("[n] Write Serial Number");
  Serial.println("[r] Write Reference Voltage");
  Serial.println("----- SENSOR CALIBRATION VALUES -------");
  Serial.println("[f] Write minimum Frequency");
  Serial.println("[g] Write maximum Frequency");
  Serial.println("[i] Write lower Level");
  Serial.println("[j] Write upper Level");
  Serial.println("[p] Change Pointer to Dataset.");
  Serial.print("[l] Write Slope ["); Serial.print(F_POINTER,DEC); Serial.println("]");
  Serial.print("[m] Write Intercept ["); Serial.print(F_POINTER,DEC); Serial.println("]");
  Serial.println("----- OTHER FUNCTIONS -----------------");
  Serial.println("[s] Init Sensor");
  Serial.println("[h] Hex Dump Eeprom");
  Serial.println("[d] Display Eeprom Contents");
  Serial.println("---------------------------------------");
}


// /////////////////////////////////////////////////////////////
// Serial Communication Routines : USER AND EEPROM
// /////////////////////////////////////////////////////////////

#define EEPROM_24C01_I2CADDR 0x50

char Buffy[20] ;  // holds User Input from Serial
byte byteRead ;


// /////////////////////////////////////////////////////////////
void InitBuffy()
// /////////////////////////////////////////////////////////////

{
  for (int i=0; i<20; i++) Buffy[i] = 32 ;   // SPACE
}


void ShowBuffy()
{
  for (int i=0; i<19; i++) Serial.print(char(Buffy[i]));
}


// /////////////////////////////////////////////////////////////
void ReadUserInput()
// /////////////////////////////////////////////////////////////

{
  int pointer = 0 ;
  if(Serial.available() > 0)
  {
    while(Serial.available())
    {
    Buffy[pointer] = Serial.read();
    if ( pointer < 19 ) pointer++ ;
    }
  }
}


// /////////////////////////////////////////////////////////////
void WaitUserInput()
// /////////////////////////////////////////////////////////////

{
  int pointer = 0 ;
  boolean ende = false ;
  while (!ende)
  {
    if(Serial.available())
    {
    Buffy[pointer] = Serial.read();
    if(Buffy[pointer] == 10) ende = true ;
    if(Buffy[pointer] == 13) ende = true ;
    if ( pointer < 19 ) pointer++ ;
    }
    delay(5) ;
  }
}

// /////////////////////////////////////////////////////////////
int ReadInteger(int WoStehtDasImEeprom)
// /////////////////////////////////////////////////////////////
{
int Value = 0 ;
Wire.beginTransmission(EEPROM_24C01_I2CADDR);  
Wire.write(WoStehtDasImEeprom);    
Wire.endTransmission();  
Wire.requestFrom(EEPROM_24C01_I2CADDR,2);
delay(5);
if(Wire.available() == 2)
{
  Value = Wire.read();     
  Value = ( Value << 8 ) | (Wire.read()) ;
}
return Value ;
}


// /////////////////////////////////////////////////////////////
void ChangeIntegerValue(int WoStehtDasImEeprom)
// /////////////////////////////////////////////////////////////

{  
// READ EXISTING VALUE
unsigned int Number = 0 ;
unsigned int aux = 0;
byte Part[2];
Number = ReadInteger(WoStehtDasImEeprom);
Serial.print("\nActual value : ");
Serial.print(Number,DEC);
Serial.println("\n\nEnter new value. Press ENTER.\n");

InitBuffy();
WaitUserInput();

Number = Buffy[0] - 48 ;
aux=Buffy[1]-48 ; if((aux>=0)&&(aux<=9)){Number=Number*10+aux ;}
aux=Buffy[2]-48 ; if((aux>=0)&&(aux<=9)){Number=Number*10+aux ;}
aux=Buffy[3]-48 ; if((aux>=0)&&(aux<=9)){Number=Number*10+aux ;}
aux=Buffy[4]-48 ; if((aux>=0)&&(aux<=9)){Number=Number*10+aux ;}

Part[1] = ( Number & 0xFF);
Part[0] = ((Number >> 8) & 0xFF);

Wire.beginTransmission(EEPROM_24C01_I2CADDR); 
Wire.write(WoStehtDasImEeprom);
Wire.write(Part[0]);
delay(5); 
Wire.write(Part[1]);
Wire.endTransmission();
delay(5);
Serial.println("O.K.\n\n");
} 


// /////////////////////////////////////////////////////////////
float ReadFloat(int WoStehtDasImEeprom)
// /////////////////////////////////////////////////////////////

{
float Value = 0.0 ;
String StringOne = "";
union MyUnion{ float f; byte b[4]; };
MyUnion myData;
Wire.beginTransmission(EEPROM_24C01_I2CADDR);  
Wire.write(WoStehtDasImEeprom);    
Wire.endTransmission();  
Wire.requestFrom(EEPROM_24C01_I2CADDR,4);
delay(5);
if(Wire.available() == 4)
{
  myData.b[0] = Wire.read();     // MSB
  myData.b[1] = Wire.read();
  myData.b[2] = Wire.read();
  myData.b[3] = Wire.read();
}
Value = myData.f ;
return Value ;
}


// ///////////////////////////////////////////////////////////// 
// CHANGE A FLOAT VALUE OF EEPROM
// ///////////////////////////////////////////////////////////// 
void ChangeFloatValue(int WoStehtDasImEeprom)
{
String StringOne = "";
union MyUnion{ float f; byte b[4]; };
MyUnion myData;
float ActualValue = 0.0 ;
ActualValue = ReadFloat(WoStehtDasImEeprom) ;

Serial.print("Actual value : ");
Serial.println(ActualValue,4);
Serial.print("\n");
Serial.println(">>> Enter new value. Press ENTER.");

WaitUserInput();
for ( int w = 0 ; w < 9 ; w++ ) { StringOne += Buffy[w] ; }
myData.f = StringOne.toFloat() ;

for (int address = 0; address < 4; address++) 
  { 
    Wire.beginTransmission(EEPROM_24C01_I2CADDR); 
    Wire.write(address+WoStehtDasImEeprom);
    Wire.write(myData.b[address]); 
    Wire.endTransmission();
    delay(3);
  }  

Serial.println("\nO.K.\n");
}


// /////////////////////////////////////////////////////////////
// I2C Communication Routines ADC
// /////////////////////////////////////////////////////////////


#define ADC_MCP3221_I2CADDR 0x4E 

long  RSSI_ADC_RAW = 0 ;
double RSSI_ADC_VOLT = 0.0 ;


void UpDate_ADC_RSSI() 
{
  if(SENSOR_TYPE == 0xFFFF) SUPPLY_ADC = 5.0 ;

  Wire.requestFrom(ADC_MCP3221_I2CADDR, 2) ;   // 2 BYTES
  delay(5);
  if (Wire.available() == 2) 
  { 
    byte a = Wire.read() ; 
    byte b = Wire.read() ; 
    RSSI_ADC_RAW = ( a & 0x0F );  
    RSSI_ADC_RAW = (RSSI_ADC_RAW << 8) | b ;
    RSSI_ADC_VOLT = RSSI_ADC_RAW / 4096.0 ;
    RSSI_ADC_VOLT *= SUPPLY_ADC ;
  }
  else
  {
    // NO ANSWER == NO SENSOR
    SENSOR_TYPE = 0x0000 ;
    RSSI_ADC_VOLT = 0.003 ;   
  } 
}


void Apply_RSSI_CAL() 
{
  RSSI_ADC = RSSI_ADC_VOLT * SLOPE[F_POINTER] + INTERCEPT[F_POINTER] ;
 
  delay(50);
  // Some limit checking ...
  if(RSSI_ADC < L_LOW) RSSI_ADC = L_LOW;
  if(RSSI_ADC > L_HIGH) RSSI_ADC = L_HIGH ;
  
}

void CheckSensorType() 
{
  SENSOR_TYPE = 0x0000 ;  // NO SENSOR
  // UNPROGRAMMED SENSOR : SENSOR_TYPE = 0xFFFF = 65535
  RSSI_ARD_RAW = analogRead(RSSI_ARD_PIN) ;
  
  if( RSSI_ARD_RAW > MIN_RSSI_ARD_RAW ) 
  {
    // SOMETHING IS THERE !
    SENSOR_TYPE = ReadInteger(0x00) ;
  }
}

// ///////////////////////////////////////////////////////////// 
// READ CAL DATA FROM SENSOR
// ///////////////////////////////////////////////////////////// 
void InitSensor()
{
  SENSOR_TYPE =  ReadInteger(0x00) ;
  SERIAL_SENSOR = ReadInteger(0x02) ;
  SUPPLY_ADC = ReadFloat(0x04) ;
  F_LOW = ReadFloat(0x08) ;
  F_HIGH = ReadFloat(0x0C);

  // POPULATE FREQUENCY ARRAY
  float DIFF = (F_HIGH - F_LOW) / 100.0 ;
  F_ARRAY[0] = F_LOW ;
  F_ARRAY[1] = F_ARRAY[0] + 10.0 * DIFF ;
  F_ARRAY[2] = F_ARRAY[1] + 10.0 * DIFF ;
  F_ARRAY[3] = F_ARRAY[2] + 10.0 * DIFF ;
  F_ARRAY[4] = F_ARRAY[3] + 10.0 * DIFF ;
  F_ARRAY[5] = F_ARRAY[4] + 10.0 * DIFF ;
  F_ARRAY[6] = F_ARRAY[5] + 10.0 * DIFF ;
  F_ARRAY[7] = F_ARRAY[6] + 10.0 * DIFF ;
  F_ARRAY[8] = F_ARRAY[7] + 10.0 * DIFF ;
  F_ARRAY[9] = F_ARRAY[8] + 8.0 * DIFF ;
  F_ARRAY[10] = F_ARRAY[9] + 6.0 * DIFF ;
  F_ARRAY[11] = F_ARRAY[10] + 4.0 * DIFF ;
  F_ARRAY[12] = F_ARRAY[11] + 2.0 * DIFF ;

  L_LOW = ReadFloat(0x10) ;
  L_HIGH = ReadFloat(0x14) ;

  // POPULATE LEVEL ARRAYS
  for(int i=0; i<13; i++)
  {
    SLOPE[i] = ReadFloat(0x18 + i*8) ;  // VOLTAGE @ L_LOW
    INTERCEPT[i] = ReadFloat(0x1C + i*8) ;  // VOLTAGE @ L_HIGH
  }     
}


// //////////////////////////////////// 
// Read and display contents of Eeprom
// //////////////////////////////////// 
void ReadEeprom()
{
byte rdata = 0xAF ;

for (int address = 0; address < 128; address+=8) 
{  
  if (address < 16) Serial.print("0");
  Serial.print(address,HEX);  
  Serial.print("\t"); 
  // One row, HEX
  for (int add = 0; add < 8; add++) 
  { 
  Wire.beginTransmission(EEPROM_24C01_I2CADDR);  
  Wire.write(address+add);    
  Wire.endTransmission();  
  Wire.requestFrom(EEPROM_24C01_I2CADDR,1);
  delay(5);
  if (Wire.available() == 1) rdata = Wire.read();
  if (rdata < 16) Serial.print("0");  
  Serial.print(rdata, HEX);
  Serial.print(" ");
  }
  Serial.print("\t");
  // One row, ASCII
  for (int add = 0; add < 8; add++) 
  { 
  Wire.beginTransmission(EEPROM_24C01_I2CADDR);  
  Wire.write(address+add);    
  Wire.endTransmission();  
  Wire.requestFrom(EEPROM_24C01_I2CADDR,1);
  delay(5);
  if (Wire.available() == 1) rdata = Wire.read();
  if (rdata < 32) 
  {
  Serial.print(".");
  }
  else if (rdata > 126) 
     {
     Serial.print(".");
     }
     else
        {
        Serial.print(char(rdata));
        }
  Serial.print(" ");
  }
  Serial.println();  
} 
Serial.println("\nO.K.\n\n");
}


// /////////////////////////////////////////////////////////////
// ANALOG INPUTS
// /////////////////////////////////////////////////////////////

float SUPPLY_ARD_VOLT = 0.0 ;
float SUPPLY_ARD_VOLT_MIN = 11.0 ;
const float SUPPLY_DIVIDER = 2.4 / (2.4 + 12.0) ; // RESISTOR RATIO

void UpDateSupplyVoltage() 
{
  unsigned int SUPPLY_ARD_RAW = analogRead(SUPPLY_ARD_PIN) ;
  SUPPLY_ARD_VOLT = 0.001 * (float)(map(SUPPLY_ARD_RAW,0,1023,0,5000)) / SUPPLY_DIVIDER ;  
}


// /////////////////////////////////////////////////////////////
// SUBROUTINES DISPLAY.
// /////////////////////////////////////////////////////////////

void BAR(float value) 
{
  int BAR_LENGTH = (int)(120.0 + value) ;
  if (BAR_LENGTH > MAX_BAR_LENGTH) BAR_LENGTH = MAX_BAR_LENGTH ;
  if (BAR_LENGTH < MIN_BAR_LENGTH) BAR_LENGTH = MIN_BAR_LENGTH ;
  display.fillRect(0, BARY, BAR_LENGTH, 7, WHITE);
  display.fillRect(BAR_LENGTH, BARY+1, MAX_BAR_LENGTH-BAR_LENGTH, 5, BLACK);
  display.drawRect(0, BARY, 127, 7, WHITE);
}


void UpDateDisplayRSSI()
{
  int offset = 0 ;
  float RSSI_DISPLAY = 0.0 ;
  float FREQ_DISPLAY = 0.0 ;
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0); display.print("****");
  display.setCursor(39,0); display.print("Broadband RF Power Meter");
  display.setCursor(104,0); display.print("****");
  display.drawLine(0, 12, 128, 12, WHITE);
  display.setTextSize(2) ;
  
  // LEVEL
  display.setCursor(0,20);
  // SIGN
  if(RSSI_ADC < 0.0) display.print("- ") ;
  if(RSSI_ADC == 0.0) display.print("  ") ;
  if(RSSI_ADC > 0.0) display.print("+ ") ;
  
  RSSI_DISPLAY = RSSI_ADC ;
  if(RSSI_ADC < 0.0) RSSI_DISPLAY = -1.0 * RSSI_ADC ;
  
  display.setCursor(20,20);
  if(RSSI_DISPLAY < 10.0) display.print(" ") ;
  display.print(RSSI_DISPLAY,2) ;
  display.setCursor(90,20);
  display.print("dBm") ;

  // RSSI BAR
  // DRAW TICKS RSSI BAR
  display.setTextSize(1) ;
  // display.setCursor(0,35) ;
  for (int i=0; i<=6; i++)
  {
    display.drawLine(i*20, BARY-4, i*20, BARY+1, WHITE) ;
  }
  for (int i=0; i<=25; i++)
  {
    display.drawLine(i*5, BARY-2, i*5, BARY+1, WHITE) ;
  }

  BAR(RSSI_ADC) ;  
    
  display.setTextSize(1);
  display.setCursor(0, 57) ;

  // SUPPLY VOLTAGE CHECK
  UpDateSupplyVoltage() ;
  
  if(SUPPLY_ARD_VOLT < SUPPLY_ARD_VOLT_MIN) 
  {
    display.print("SUPPLY LOW");
  }
  if(SUPPLY_ARD_VOLT >= SUPPLY_ARD_VOLT_MIN)
  {
    // ////////////////////////////////////////////
    //        DISPLAY SENSOR + SERIALNUMBER
    // ////////////////////////////////////////////
    if(SENSOR_TYPE == 8307) display.print("AD8307");
    if(SENSOR_TYPE == 5537) display.print("LT5537");
    if(SENSOR_TYPE == 8318) display.print("AD8318");
    display.print("-");
    if(SERIAL_SENSOR < 1000) display.print("0");
    if(SERIAL_SENSOR < 100) display.print("0");
    if(SERIAL_SENSOR < 10) display.print("0");
    display.print(SERIAL_SENSOR,DEC);
    // ////////////////////////////////////////////
    //            DISPLAY FREQUENCY
    // ////////////////////////////////////////////
    display.setCursor(80, 57) ;
    //   > 10 GHz
    if(F_ARRAY[F_POINTER]>=10000.0)
    {
      FREQ_DISPLAY = F_ARRAY[F_POINTER] / 1000.0 ;
      display.print(FREQ_DISPLAY,1);  // FF.F GHz
      display.print(" GHz");
    }
    //   1 GHz ... 9.999 GHz
    if((F_ARRAY[F_POINTER]>=1000.0) && (F_ARRAY[F_POINTER]<10000.0))
    {
      FREQ_DISPLAY = F_ARRAY[F_POINTER] / 1000.0 ;
      display.print(FREQ_DISPLAY,2);  // F.FF GHz
      display.print(" GHz");
    }
    //   100 MHz ... 999.9 MHz
    if((F_ARRAY[F_POINTER]>=100.0) && (F_ARRAY[F_POINTER]<1000.0))
    {
      FREQ_DISPLAY = F_ARRAY[F_POINTER] ;
      display.print(FREQ_DISPLAY,0);  // FFF MHz
      display.print(" MHz");
    }
    //   10 MHz ... 99.99 MHz
    if((F_ARRAY[F_POINTER]>=10.0) && (F_ARRAY[F_POINTER]<100.0))
    {
      FREQ_DISPLAY = F_ARRAY[F_POINTER] ;
      display.print(FREQ_DISPLAY,1);  // FF.F MHz
      display.print(" MHz");
    }
    //   1 MHz ... 9.999 MHz
    if((F_ARRAY[F_POINTER]>=1.0) && (F_ARRAY[F_POINTER]<10.0))
    {
      FREQ_DISPLAY = F_ARRAY[F_POINTER] ;
      display.print(FREQ_DISPLAY,2);  // F.FF MHz
      display.print(" MHz");
    }
    //   0.1 MHz ... 0.999 MHz
    if((F_ARRAY[F_POINTER]>=0.1) && (F_ARRAY[F_POINTER]<1.0))
    {
      FREQ_DISPLAY = F_ARRAY[F_POINTER] * 1000.0 ;
      display.print(FREQ_DISPLAY,0);  // FFF kHz
      display.print(" kHz");
    }
    //   0.01 MHz ... 0.0999 MHz
    if((F_ARRAY[F_POINTER]>=0.01) && (F_ARRAY[F_POINTER]<0.1))
    {
      FREQ_DISPLAY = F_ARRAY[F_POINTER] * 1000.0 ;
      display.print(FREQ_DISPLAY,1);  // FF.F kHz
      display.print(" kHz");
    }
    //   < 0.01 MHz
    if(F_ARRAY[F_POINTER]<0.01)
    {
      FREQ_DISPLAY = F_ARRAY[F_POINTER] * 1000.0 ;
      display.print(FREQ_DISPLAY,2);  // F.FF kHz
      display.print(" kHz");
    }
   }
  display.display() ;
}


void UpDateDisplayCAL()
{
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0); display.print("****");
  display.setCursor(39,0); display.print("Broadband RF Power Meter");
  display.setCursor(104,0); display.print("****");
  display.drawLine(0, 12, 128, 12, WHITE);
  display.setTextSize(2) ;
  
  double RSSI_SUM = 0.0 ;
  display.setCursor(0,24);
  display.println("CAL SENSOR") ;
  display.setCursor(0,50);
  for(int i=0; i<100; i++)
    {
    UpDate_ADC_RSSI() ;
    RSSI_SUM += RSSI_ADC_VOLT ;
    delay(19) ;
    }
  RSSI_ADC_VOLT = RSSI_SUM / 100.0 ;

  // SUPPLY VOLTAGE CHECK
  UpDateSupplyVoltage() ;
  
  if(SUPPLY_ARD_VOLT < SUPPLY_ARD_VOLT_MIN) 
  {
    display.print("LOW BATT") ;    
  }
  
  if(SUPPLY_ARD_VOLT >= SUPPLY_ARD_VOLT_MIN) 
  {
    display.print("+ ") ;  
    display.print(RSSI_ADC_VOLT, 4) ;
    display.print(" V") ;  
  }
  display.display() ;
}


void UpDateDisplayNOHAVE()
{
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0); display.print("****");
  display.setCursor(39,0); display.print("Broadband RF Power Meter");
  display.setCursor(104,0); display.print("****");
  display.drawLine(0, 12, 128, 12, WHITE);
  display.setTextSize(2) ;
  display.setCursor(0,17);
  display.println("ERROR 404") ;
  display.println("DETECTOR") ;
  display.print("NOT FOUND") ;     
  display.display() ;
}


// /////////////////////////////////////////////////////////////
// S E T U P
// /////////////////////////////////////////////////////////////

void setup()
{
  Serial.begin(115200) ;

  Wire.begin() ;

  // INIT OLED
  display.begin(SH1106_SWITCHCAPVCC);

  // SHOW STARTUP SCREEN
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0); display.print("****");
  display.setCursor(39,0); display.print("Broadband RF Power Meter");
  display.setCursor(104,0); display.print("****");
  display.drawLine(0, 12, 128, 12, WHITE);
  display.setTextSize(1);
  display.setCursor(0, 21);
  display.println("A DC TO DAYLIGHT ");
  display.setCursor(0, 33);
  display.println("MICROWAVE POWERMETER");
  display.display();


  delay(999) ;
  
  pinMode(RotaryEncoder1, INPUT_PULLUP);
  pinMode(RotaryEncoder2, INPUT_PULLUP);
  pinMode(RotaryEncoder3, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(RotaryEncoder2), RotaryEncoderISR2, FALLING);
  attachInterrupt(digitalPinToInterrupt(RotaryEncoder3), RotaryEncoderISR3, FALLING);

  print_title() ;
  print_options() ;

  pinMode(RSSI_ARD_PIN, INPUT) ;

  delay(3000);

  InitSensor() ;
}


// /////////////////////////////////////////////////////////////
// M A I N L O O P
// /////////////////////////////////////////////////////////////

void loop()
{

  // //////////////////////////////////
  // EVALUATE ROTARY ENCODER
  // CHANGE DISPLAYED FREQUENCY 
  // //////////////////////////////////

  if (LEFT)
  {
    READY = false ;
    F_POINTER -= 1 ;
    if (F_POINTER < 0) F_POINTER = 0 ;
    LEFT = false ;
    RIGHT = false ;
    delay(149) ;
    READY = true ;
  }

  if (RIGHT)
  {
    READY = false ;
    F_POINTER += 1 ;
    if (F_POINTER > 12) F_POINTER = 12 ;
    LEFT = false ;
    RIGHT = false ;
    delay(149) ;
    READY = true ;
  }

  // //////////////////////////////////
  // HANDLE SENSORS
  // //////////////////////////////////
  SENSOR_TYPE_OLD = SENSOR_TYPE ;
  CheckSensorType() ;
  if(SENSOR_TYPE != SENSOR_TYPE_OLD) InitSensor() ;
  
  switch (SENSOR_TYPE)
  {
    case 0xFFFF :  
      // UNPROGRAMMED SENSOR
      // 0xFFFF = 65535
      UpDateDisplayCAL() ;
      delay(99) ;
      break ;
      
    case 0x0000 :  
      // NO SENSOR
      UpDateDisplayNOHAVE() ;
      delay(99) ;     
      break ;
      
    case 8307 :  
      UpDateSupplyVoltage() ;
      UpDate_ADC_RSSI() ;
      Apply_RSSI_CAL() ;       
      UpDateDisplayRSSI() ;
      break ;
      
    case 5537 :  
      UpDateSupplyVoltage() ;
      UpDate_ADC_RSSI() ;
      Apply_RSSI_CAL() ;     
      UpDateDisplayRSSI() ;
      break ;

    case 8313 :  
      UpDateSupplyVoltage() ;
      UpDate_ADC_RSSI() ;
      Apply_RSSI_CAL() ;     
      UpDateDisplayRSSI() ;
      break ;
  }
  
  // //////////////////////////////////
  // HANDLE SERIAL COMMANDS
  // //////////////////////////////////
  InitBuffy();
  ReadUserInput();
  unsigned int user_command ;
  // CHARACTER
  user_command = Buffy[0] ;
  switch (user_command)
  {    
      case '*' :
      // *IDN?
      if((Buffy[1]=='I')&&(Buffy[2]=='D')&&(Buffy[3]=='N')&&(Buffy[4]=='?'))
      {
        Serial.println("Levelmod V2.0 by Changpuak.ch (C) 09/2022") ;
      }
      // *FL? = Frequency Low in MHz
      if((Buffy[1]=='F')&&(Buffy[2]=='L')&&(Buffy[3]=='?'))
      {
        Serial.println(F_LOW,1) ;
      }
      // *FH? = Frequency High in MHz
      if((Buffy[1]=='F')&&(Buffy[2]=='H')&&(Buffy[3]=='?'))
      {
        Serial.println(F_HIGH,1) ;
      }
      // *LL? = Level Low in dBm
      if((Buffy[1]=='L')&&(Buffy[2]=='L')&&(Buffy[3]=='?'))
      {
        Serial.println(L_LOW,4) ;
      }
      // *LH? = Level High in dBm
      if((Buffy[1]=='L')&&(Buffy[2]=='H')&&(Buffy[3]=='?'))
      {
        Serial.println(L_HIGH,4) ;
      }
      // *SP0! = Set Frequency Pointer to 0
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='0')&&(Buffy[4]=='!'))
      {
        F_POINTER = 0 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SP1! = Set Frequency Pointer to 1
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='1')&&(Buffy[4]=='!'))
      {
        F_POINTER = 1 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SP2! = Set Frequency Pointer to 2
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='2')&&(Buffy[4]=='!'))
      {
        F_POINTER = 2 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SP3! = Set Frequency Pointer to 3
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='3')&&(Buffy[4]=='!'))
      {
        F_POINTER = 3 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SP4! = Set Frequency Pointer to 4
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='4')&&(Buffy[4]=='!'))
      {
        F_POINTER = 4 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SP5! = Set Frequency Pointer to 5
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='5')&&(Buffy[4]=='!'))
      {
        F_POINTER = 5 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SP6! = Set Frequency Pointer to 6
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='6')&&(Buffy[4]=='!'))
      {
        F_POINTER = 6 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SP7! = Set Frequency Pointer to 7
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='7')&&(Buffy[4]=='!'))
      {
        F_POINTER = 7 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SP8! = Set Frequency Pointer to 8
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='8')&&(Buffy[4]=='!'))
      {
        F_POINTER = 8 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SP9! = Set Frequency Pointer to 9
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='9')&&(Buffy[4]=='!'))
      {
        F_POINTER = 9 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SPA! = Set Frequency Pointer to 10
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='A')&&(Buffy[4]=='!'))
      {
        F_POINTER = 10 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SPB! = Set Frequency Pointer to 11
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='B')&&(Buffy[4]=='!'))
      {
        F_POINTER = 11 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *SPC! = Set Frequency Pointer to 12
      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='C')&&(Buffy[4]=='!'))
      {
        F_POINTER = 12 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;
      }
      // *LVL? = Asks for the corrected Level in dBm
      if((Buffy[1]=='L')&&(Buffy[2]=='V')&&(Buffy[3]=='L')&&(Buffy[4]=='?'))
      {
        UpDate_ADC_RSSI() ;   // MEASURE FIRST
        Apply_RSSI_CAL() ;  
        Serial.println(RSSI_ADC,2) ;
      }
      // *VOLT? = Asks for the raw voltage measured
      if((Buffy[1]=='V')&&(Buffy[2]=='O')&&(Buffy[3]=='L')&&(Buffy[4]=='T')&&(Buffy[5]=='?'))
      {
        UpDate_ADC_RSSI() ;   // MEASURE FIRST
        Serial.println(RSSI_ADC_VOLT,3) ;
      }
      // *REF? = Asks for the Reference Voltage of the Power Sensor
      if((Buffy[1]=='R')&&(Buffy[2]=='E')&&(Buffy[3]=='F')&&(Buffy[4]=='?'))
      {
        Serial.println(SUPPLY_ADC,3) ;
      }      
      break;

      case 'd' :
      // Display Eeprom Contents
      Serial.println("\n********* EEPROM CONTENTS ********") ;
      Serial.print("Sensor Type : ") ;
      Serial.println(ReadInteger(0x00),DEC);
      Serial.print("Serial No.  : ") ;
      Serial.println(ReadInteger(0x02),DEC);
      Serial.print("Reference Volt : ") ;
      Serial.println(ReadFloat(0x04),3);
      Serial.print("Min. Freq. [MHz]  : ") ;
      Serial.println(ReadFloat(0x08),4);
      Serial.print("Max. Freq. [MHz]  : ") ;
      Serial.println(ReadFloat(0x0C),4);
      Serial.print("Min. Level [dBm]  : ") ;
      Serial.println(ReadFloat(0x10),4);
      Serial.print("Max. Level [dBm]  : ") ;
      Serial.println(ReadFloat(0x14),4);
      for(int i=0; i<13; i++)
      {
        // INDEX
        if(i < 10 ) Serial.print("0") ;
        Serial.print(i,DEC) ; 
        // LOW VALUE
        Serial.print(" : SLOPE = ") ;
        Serial.print(ReadFloat(0x18 + i*8),3) ;
        // HIGH VALUE
        Serial.print(", INTERCEPT = ") ;
        Serial.print(ReadFloat(0x1C + i*8),3) ;
        Serial.println(" ") ;
      }         
      print_options();
      break;

      case 'f' :
      // Write minimum Frequency
      Serial.print("\nWrite minimum Frequency :") ;
      ChangeFloatValue(0x08) ;
      print_options();
      break;

      case 'g' :
      // Write maximum Frequency
      Serial.print("\nWrite minimum Frequency :") ;
      ChangeFloatValue(0x0C) ;
      print_options();
      break;
      
      case 'h' :
      // Hex Dump Eeprom
      Serial.println("\nHexdump Eeprom.\n");
      ReadEeprom() ;
      print_options();
      break;

      case 'i' :
      // Write lower Level
      Serial.print("\nWrite lower Level :") ;
      ChangeFloatValue(0x10) ;
      print_options();
      break;

      case 'j' :
      // Write upper Level
      Serial.print("\nWrite upper Level :") ;
      ChangeFloatValue(0x14) ;
      print_options();
      break;

      case 'l' :
      // Write SLOPE
      Serial.print("\nWrite SLOPE #") ;
      Serial.println(F_POINTER,DEC) ;
      ChangeFloatValue(0x18 + F_POINTER * 8) ;
      print_options();
      break;

      case 'm' :
      // Write INTERSEPT
      Serial.print("\nWrite INTERCEPT #") ;
      Serial.println(F_POINTER,DEC) ;
      ChangeFloatValue(0x1C + F_POINTER * 8) ;
      print_options();
      break;

      case 'n' :
      // Write Serial Number
      Serial.println("\nWrite Serial Number.");
      ChangeIntegerValue(0x02);
      print_options();
      break;
    
      case 'p' :
      // Set Pointer to Dataset
      {
      unsigned int aux ;
      Serial.println("\nSet Pointer to Dataset.");
      Serial.println("\nEnter new value. Press ENTER.\n");
      InitBuffy();
      WaitUserInput();
      F_POINTER = Buffy[0] - 48 ;
      aux=Buffy[1]-48 ; 
      if((aux >= 0)&&(aux <= 9)){F_POINTER = F_POINTER*10 + aux ;}
      if(F_POINTER > 12) F_POINTER = 12 ;
      InitBuffy();
      print_options();
      }
      break;
      
      case 'r' :
      // Write Reference Voltage
      Serial.println("\nWrite Reference Voltage.\n");
      ChangeFloatValue(0x04) ;
      print_options();
      break;

      case 's' :
      // Init Sensor
      Serial.println("\nInit Sensor.");
      InitSensor() ;
      Serial.print("Sensor Type : ");
      Serial.println(SENSOR_TYPE,DEC);
      print_options();
      break;

      case 't' :
      // Write Sensor Type
      Serial.println("\nWrite Sensor Type.");
      ChangeIntegerValue(0x00);
      print_options();
      break;
  }
   
  delay(99) ;
}


// /////////////////////////////////////////////////////////////
// INTERRUPT SERVICE ROUTINES
// /////////////////////////////////////////////////////////////

void RotaryEncoderISR2()
{
  if(READY)
  {
  LEFT = false ;
  RIGHT = false ;
  byte autre = digitalRead(RotaryEncoder3) ;
  if (autre > 0) LEFT = true ;
  if (autre < 1) RIGHT = true ;
  }
}

void RotaryEncoderISR3()
{
  if(READY)
  {
  LEFT = false ;
  RIGHT = false ;
  byte autre = digitalRead(RotaryEncoder2) ;
  if (autre > 0) RIGHT = true ;
  if (autre < 1) LEFT = true ;
  }
}


// /////////////////////////////////////////////////////////////
// END OF FILE.
// ////////////////////////////////////////////////

Your code contains a lot of constant text messages like:

each of them stored in a data memory and consume it.

try to add the F() macro to every constant text string:

Serial.println(F("Welcome to Broadband RF Power Meter"));  

and see, is it helps to compile the code on classic Arduino Nano board.

Couldn't all of this --

      if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[3]=='0')&&(Buffy[4]=='!'))
      {
        F_POINTER = 0 ; 
        Serial.println(F_ARRAY[F_POINTER],1) ;

stuff
having

Buffy[1]=='S')  (Buffy[2]=='P')  (Buffy[4]=='!')

in common
be replaced with

if((Buffy[1]=='S')&&(Buffy[2]=='P')&&(Buffy[4]=='!'))
{
  F_POINTER = atoi(Buffy[3]);
}

Dear friends,
thank you all for the help you have furnished me until now.

As I supposed exist an incompatibity between the Oled SH1106 display and Arduino Nano Evey, as in my last two projects in which both were present, the compilation errors were enormous.

Replaced the Oled SH1106 with a SSD1325 or TFT ST7735 and no more problems, although I had to revise the code for the replacement.
Thank you for all that you have done.
IZ5FCY Roberto