Nano Classic to Nano Every sketch compiling issues

I have a simple sketch for selecting audio inputs that works perfectly with a Nano Classic. I am trying to get it to work on a Nano Every and am getting the following errors:

C:\Users\fortr\Documents\Arduino\Input-NanoV2\Input-NanoV2.ino: In function 'void setup()':
C:\Users\fortr\Documents\Arduino\Input-NanoV2\Input-NanoV2.ino:49:1: error: 'PCICR' was not declared in this scope
PCICR = 0b00000010; //PCIE1: pin Change interrupt Enable 1
^~~~~
C:\Users\fortr\Documents\Arduino\Input-NanoV2\Input-NanoV2.ino:50:1: error: 'PCMSK1' was not declared in this scope
PCMSK1 = 0b00000011; //Enable PIN Change Interrupt for A0 and A1
^~~~~~
exit status 1

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

Here is the actual code:

/**********************************************************************************
This program controls the functions of relays on a PCBs to set Input Selection,
The controller is a Arduino Nano and the pins relate to the processor named pins
not the pin numbers of a 30 pin DIP device.
***********************************************************************************/

// Global variables
// Input Relay control output pin variables
const int Relay1=6, Relay2=7, Relay3=8, Relay4=9;
// LED control output pins variables
const int LED1=2, LED2=3, LED3=4, LED4=5;
// Volume Encoder D14/A0 and D15/A1
const int EncoderAPin=A0, EncoderBPin=A1;
// variables have to be volatile for use in the Interrupt Service Routine and in the Main Loop
volatile byte seqA = 0;
volatile byte seqB = 0;
volatile boolean InputUpInt = false;
volatile boolean InputDownInt = false;
int input=1;


// the setup function runs once when you press reset or power the board
void setup() {
//set input and LED pins to outputs to control relays
pinMode(LED1, OUTPUT); //pin D3 (see variable names above)
pinMode(LED2, OUTPUT); //pin D4
pinMode(LED3, OUTPUT); //pin D5
pinMode(LED4, OUTPUT); //pin D6
pinMode(Relay1, OUTPUT); //pin D7
pinMode(Relay2, OUTPUT); //pin D8
pinMode(Relay3, OUTPUT); //pin D9
pinMode(Relay4, OUTPUT); //pin D10

//set volume Encoder, input, and mute button pins to input
pinMode(EncoderAPin, INPUT); //pin D14
pinMode(EncoderBPin, INPUT); //pin D15

//set output pins for volume and mute to off, and input select relay to input 1
//Volume is in binary converted from Decimal, The input relay control is BCD
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);

PCICR = 0b00000010; //PCIE1: pin Change interrupt Enable 1
PCMSK1 = 0b00000011; //Enable PIN Change Interrupt for A0 and A1

} //End of Setup

// Interrupt Service Routine
// Every detent in the rotary encoder generates 4 pin changes, 2 on Pin A and 2 on Pin B
ISR (PCINT1_vect){
//When Interrupt triggered read encoder pins
boolean A_val = digitalRead(EncoderAPin);
boolean B_val = digitalRead(EncoderBPin);

//Record Pin A and B sequences in the global variables
seqA <<= 1; //shift the current byte date over one bit to the left
seqA |= A_val; //OR in, add, the new bit value, 0 or 1, of the encoder pin A
seqB <<= 1; //shift the current byte date over one bit to the left
seqB |= B_val; //OR in, add, the new bit value, 0 or 1, of the encoder pin A
//Mask the Most Significant four bits
seqA &= 0b00001111; // wipes off the previous four reads that were moved
seqB &= 0b00001111; // to the four higher bits
//Compare the recorded sequence with the expected sequence
if (seqA == 0b00000011 && seqB == 0b00001001){
InputUpInt = true;
}
if (seqA == 0b00001001 && seqB == 0b00000011){
InputDownInt = true;
}
} // end ISR

void loop() {

//Booleans for task status, to be set true when a task is selected
bool inputUp=false, inputDown=false, task=false;

if(InputUpInt){
task=true;
InputUpInt=false;
if(input < 4){ // if not at 4 then add 1
input=input+1;
}
}//end of if(InputUpInt)

if(InputDownInt){
task=true;
InputDownInt=false;
if(input > 1){ // input not 1 then subtract 1
input=input-1;
}
}//end of if(inputDownInt)

//Perform the appropriate task based on input
if(task){
digitalWrite(LED1, LOW);
digitalWrite(Relay1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(Relay2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(Relay4, LOW);

switch(input){
case 1: digitalWrite(LED1, HIGH);
digitalWrite(Relay1, HIGH);
break;
case 2: digitalWrite(LED2, HIGH);
digitalWrite(Relay2, HIGH);
break;
case 3: digitalWrite(LED3, HIGH);
digitalWrite(Relay3, HIGH);
break;
case 4: digitalWrite(LED4, HIGH);
digitalWrite(Relay4, HIGH);
break;
default: break;
} // End Switch
} //End IF

// delay(5); // delay needed to smooth out control
} // End of Loop

I did not write the code and am not familiar with this programming. Any ideas?

Welcome to the forum

The Nano classic uses a different processor than the Nano Every so has different registers.

The sketch that you posted tries to use registers not present in the Nano Every, hence the errors such as

'PCICR' was not declared in this scope

As pointed out, that is a processor-specific command.

There are general purpose interrupt libraries for Arduino, but be sure to check whether the Nano Every is supported.

Useful search phrase "arduino pin change interrupt library"

To make things easy on yourself consider going back to the standard Nano, you indicated the code works there.

Yep, best advice yet!

@rickray1
If you still have the Nano Every... it uses

attachInterrupt(digitalPinToInterrupt(anypin), isrfunction, statechange);

(link to document).

I do not have a NanoEvery, so I moved your sketch pins around to fit a Nano R3, using pins 2 and 3 for the Encoder CLK and DAT, pins 4 through 7 for the LEDs and pins 5 through 8 for the relays. The NanoEvery has the ability to interrupt on every pin, so you could use your original pinning, and adjust the following sketch.

// https://forum.arduino.cc/t/nano-classic-to-nano-every-sketch-compiling-issues/1458406

const int rel[] = {8, 9, 10, 11}, led[] = {4, 5, 6, 7}, enc[] = {2, 3};
volatile bool ISRflag = 0, datapin; // used inside ISR
int input = 0; // input selector

void setup() {
  for (int i = 4; i < 12; i++) // all relays ans leds...
    pinMode(i, OUTPUT); // set to OUTPUT
  pinMode(enc[0], INPUT); //pin 2 ENC CLK
  pinMode(enc[1], INPUT); //pin 3 ENC DAT

  bool relstate[] = {1, 0, 0, 0}; // set relays
  bool ledstate[] = {1, 0, 0, 0}; // lset leds
  for (int i = 0; i < 4; i++) { // all OFF
    digitalWrite(led[i], relstate[i]);
    digitalWrite(rel[i], ledstate[i]);
  }
  attachInterrupt(digitalPinToInterrupt(enc[1]), ISRfunction, FALLING);
}

void ISRfunction() {
  ISRflag = 1;
  datapin = digitalRead(enc[0]); // read data pin state
}

void loop() {
  if (ISRflag) {
    delay(100); // debounce
    if (datapin == HIGH) {
      input++;
      if (input > 3)
        input = 3;
    }
    if (datapin == LOW) {
      input--;
      if (input < 0)
        input = 0;
    }
    for (int i = 0; i < 4; i++) {
      digitalWrite(led[i], LOW); // leds off
      digitalWrite(rel[i], LOW); // relays off
    }
    digitalWrite(led[input], HIGH); // selected led ON
    digitalWrite(rel[input], HIGH); // selected relay ON
    ISRflag = 0; // reset ISR flag
  }
}
diagram.json for the wokwi nerds
{
  "version": 1,
  "author": "xfpd",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -14.4, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-relay-module",
      "id": "relay1",
      "top": -259.8,
      "left": -156.8,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-relay-module",
      "id": "relay2",
      "top": -192.6,
      "left": -156.8,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-relay-module",
      "id": "relay3",
      "top": -125.4,
      "left": -156.8,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-relay-module",
      "id": "relay4",
      "top": -58.2,
      "left": -156.8,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-led-bar-graph",
      "id": "bargraph1",
      "top": -196.8,
      "left": 139.2,
      "attrs": { "color": "lime" }
    },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -287.24, "left": -28.8, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": 19.2, "left": -19.8, "attrs": {} },
    {
      "type": "wokwi-ky-040",
      "id": "encoder1",
      "top": -90.9,
      "left": 124.8,
      "rotate": 180,
      "attrs": {}
    }
  ],
  "connections": [
    [ "relay4:GND", "relay3:GND", "black", [ "h19.2", "v0.4" ] ],
    [ "relay3:GND", "relay2:GND", "black", [ "h19.2", "v-9.2" ] ],
    [ "relay2:GND", "relay1:GND", "black", [ "h19.2", "v0.4" ] ],
    [ "relay4:VCC", "relay3:VCC", "red", [ "h9.6", "v-67.2" ] ],
    [ "relay3:VCC", "relay2:VCC", "red", [ "h9.6", "v-67.2" ] ],
    [ "relay2:VCC", "relay1:VCC", "red", [ "h9.6", "v-57.6" ] ],
    [ "gnd1:GND", "relay4:GND", "black", [ "v0" ] ],
    [ "vcc1:VCC", "relay1:VCC", "red", [ "v0" ] ],
    [ "gnd1:GND", "nano:GND.3", "black", [ "v0" ] ],
    [ "nano:2", "encoder1:CLK", "orange", [ "v0" ] ],
    [ "nano:3", "encoder1:DT", "blue", [ "v0" ] ],
    [ "gnd1:GND", "encoder1:GND", "black", [ "v0" ] ],
    [ "vcc1:VCC", "encoder1:VCC", "red", [ "v0" ] ],
    [ "nano:8", "relay1:IN", "green", [ "v0" ] ],
    [ "nano:9", "relay2:IN", "green", [ "v0" ] ],
    [ "nano:10", "relay3:IN", "green", [ "v0" ] ],
    [ "nano:11", "relay4:IN", "green", [ "v0" ] ],
    [ "bargraph1:C10", "bargraph1:C9", "green", [ "h0" ] ],
    [ "bargraph1:C9", "bargraph1:C8", "green", [ "h0" ] ],
    [ "bargraph1:C8", "bargraph1:C7", "green", [ "h0" ] ],
    [ "gnd1:GND", "bargraph1:C10", "black", [ "v-115.2", "h211.2" ] ],
    [ "nano:4", "bargraph1:A10", "green", [ "v0" ] ],
    [ "nano:5", "bargraph1:A9", "green", [ "v0" ] ],
    [ "nano:6", "bargraph1:A8", "green", [ "v0" ] ],
    [ "nano:7", "bargraph1:A7", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}

If you want to "bare metal" the NanoEvery... this page is full of details...

Since I have soldered the Nano Every to the board I'm using, i might as well bite the bullet and learn how to fix this.


WOW, I changed the first line to this and it is working!

const int rel[] = {6, 7, 8, 9}, led[] = {2, 3, 4, 5}, enc[] = {A0, A1};

One issue, the encoder is working the opposite direction from the LED's. If you turn the encoder clockwise, the LED's light in counter-clockwise direction.
How do I change that?

Just reorder you led array:

led[] = {7,6,5,4}

The fix was to change the mode from FALLING to RISING.
Here is the line changed:

attachInterrupt(digitalPinToInterrupt(enc[1]), ISRfunction, RISING);

Thank you everyone for all the help. It is working perfectly. I did tweek the delay to 200 to make the selector slow down, making it much easier to select a source.