Problems with sd on arduino nano

hello i have a problem with sd on arduino nano ,there's a code :

Serial.println("hhh");
    for (int i = 0; i <= 82; i++) {
      Serial.println(myList2[i]);
      Serial.println(myList[i]);
    }
    Serial.println("hhh");
    detachInterrupt(digitalPinToInterrupt(2));
    detachInterrupt(digitalPinToInterrupt(3));
    encoderPosition = 0;
		Serial.println("ENCODER_POSTION0");
		digitalWrite(5 , LOW);
    
		przycisk = 0;
		continue123 =0;
    SD_WRITE = 1;
    int liczba = EEPROM.read(0);

  // Sprawdzenie, czy wartość jest zdefiniowana (niezapisana wartość EEPROM to 255)
  if (liczba == 255) {
    liczba = 0; // Ustawienie początkowej wartości na 0
  } else {
    liczba += 1; // Zwiększenie wartości o 1
  }

  // Zapisanie nowej wartości do EEPROM
  EEPROM.write(0, liczba);

  // Wyświetlenie wartości w Serial Monitor
  Serial.print("Aktualna wartość: ");
  Serial.println(liczba);
  String fileName = "text" + String(liczba) + ".txt";
  myFile = SD.open(fileName, FILE_WRITE);
  if (myFile) {
  //myFile.println("NEXT_TEST:");
  
  Serial.println("Zapisywanie do karty sd...");
  for (int i = 0; i <= 82; i++) {
      Serial.println(float(myList[i]));
      myFile.println(myList2[i]);
      Serial.println(myList2[i]);
      myFile.println(float(myList[i]));
  }
  digitalWrite(5 , HIGH);
  myFile.close();
  }
  else{
    Serial.println("error opening test.txt");
  }

save_done = 1;

sd card is working normaly but only on mylist not mylist2 ?
There's a Mylist and Mylist2 typical values :

1354.00, 2763.00, 4172.00, 5580.00, 6990.00, 8398.00, 9807.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 9001.00, 8892.00, 7484.00, 6075.00, 4666.00, 3259.00, 1849.00, 441.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00
  • Mylist2
0.44, 0.39, 0.39, 0.39, 0.39, 0.39, 0.39, 0.39, 0.44, 0.39, 0.44, 0.39, 0.39, 0.44, 0.44, 0.39, 0.39, 0.39, 0.39, 0.39, 0.39, 0.39, 0.39, 0.39, 0.39, 0.44, 0.39, 0.34, 0.39, 0.39, 0.39, 0.44, 0.39, 0.39, 0.44, 0.49, 0.39, 0.39, 0.39, 0.44, 0.44, 0.39, 0.39, 0.39, 0.39, 0.39, 0.44, 0.44, 0.39, 0.44, 0.49, 0.44, 0.39, 0.44, 0.39, 0.44, 0.44, 0.44, 0.39, 0.44, 0.44, 0.39, 0.44, 0.44, 0.39, 0.44, 0.39, 0.39, 0.49, 0.34, 0.39, 0.34, 0.34, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00

-Mylist1
there's a definition of booth lists:
float myList[82] = {};

volatile long myList2[82] = {};
Please help -it's soo important to me :slight_smile:

There is a 50% possibility the problem is not in the part you posted.

ok

// Autor: Mariusz & Stanisław Kaczmarek 
#include <SD.h>
#include <EEPROM.h>
int SD_WRITE = 0;
File myFile;
#define PIN_SPI_CS 4
#include <ArrayList.h>

int save_done =0;
long cdk = 0;
volatile long encoderPosition = 0;
int continue321 = 1;
int encoderPinA = 2;
int encoderPinB = 3;
int lastEncoded = 0;  
int przycisk = 0;
int continue123 = 0;
int przerwa = 0;
int opi = 0;
float myList[82] = {};
volatile long myList2[82] = {};
long przerwa2 = 0;
#include <SPI.h>
#include <List.hpp>
#include <stdlib.h>

//List<int> myList; 
//List<int> myList2; 
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;

void setup() {

Serial.begin(2000000);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

 delay(1000);
 if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    digitalWrite(5 , HIGH);
    while (1);
  }


attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPinB), updateEncoder, CHANGE);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);

pinMode(A1 , INPUT);
pinMode(9, INPUT_PULLUP);
pinMode(5 , OUTPUT);
delay(1000);
digitalWrite(5 , LOW);

}





void loop() {
  //Serial.println(encoderPosition);
  if (encoderPosition >= 9000 && continue123 == 0 && przycisk == 1 ){
    encoderPosition = 9000;
		Serial.println("ENCODER_POSTION9000");
    
    continue123 = 1;
    digitalWrite(5 , HIGH);
    
  
	
		
  }

  if (encoderPosition <= 1000 && continue123 == 1  ){
    Serial.println("hhh");
    for (int i = 0; i <= 82; i++) {
      Serial.println(myList2[i]);
      Serial.println(myList[i]);
    }
    Serial.println("hhh");
    detachInterrupt(digitalPinToInterrupt(2));
    detachInterrupt(digitalPinToInterrupt(3));
    encoderPosition = 0;
		Serial.println("ENCODER_POSTION0");
		digitalWrite(5 , LOW);
    
		przycisk = 0;
		continue123 =0;
    SD_WRITE = 1;
    int liczba = EEPROM.read(0);

  // Sprawdzenie, czy wartość jest zdefiniowana (niezapisana wartość EEPROM to 255)
  if (liczba == 255) {
    liczba = 0; // Ustawienie początkowej wartości na 0
  } else {
    liczba += 1; // Zwiększenie wartości o 1
  }

  // Zapisanie nowej wartości do EEPROM
  EEPROM.write(0, liczba);

  // Wyświetlenie wartości w Serial Monitor
  Serial.print("Aktualna wartość: ");
  Serial.println(liczba);
  String fileName = "text" + String(liczba) + ".txt";
  myFile = SD.open(fileName, FILE_WRITE);
  if (myFile) {
  //myFile.println("NEXT_TEST:");
  
  Serial.println("Zapisywanie do karty sd...");
  for (int i = 0; i <= 82; i++) {
      Serial.println(float(myList[i]));
      //myFile.println(myList2[i]);
      //Serial.println(myList2[i]);
      myFile.println(float(myList[i]));
  }
  digitalWrite(5 , HIGH);
  myFile.close();
  }
  else{
    Serial.println("error opening test.txt");
  }

save_done = 1;
  
	}



	if (digitalRead(9) == LOW && przycisk == 0){
		Serial.println("START");
		encoderPosition = 0;
		przycisk = 1;
		Serial.println(encoderPosition);
    Serial.println(continue123);
    Serial.println(przycisk);
    
	}

  
  

	if (przycisk == 1 && (millis() - przerwa) >= 1000) {
    przerwa = millis(); // Zaktualizowanie przerwy zaraz na poczÄ…tku
    delay(1000);
    if (cdk < 82) { // Upewnij siÄ™, ĹĽe indeks nie przekracza rozmiaru tablicy
      myList[cdk] = float(analogRead(A1) / 20.46);
      myList2[cdk] = encoderPosition;
      Serial.println(myList2[cdk]);
      cdk++;
    } else {
      Serial.println("Tablica myList lub myList2 przekroczona!");
    }
  }
 

}

void updateEncoder() {
  int MSB = digitalRead(encoderPinA); // Most significant bit
  int LSB = digitalRead(encoderPinB); // Least significant bit

  int encoded = (MSB << 1) | LSB; // Converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; // Adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderPosition --;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderPosition ++;

  lastEncoded = encoded; // Store this value for next time
  //Serial.println(encoderPosition);
  //Serial.println(encoderPosition);
  
    


}

If anyone could help, it would be great, I have to do it for tomorrow at 6:00 am, I'm going somewhere else with the device and it should work, because I promised it would work, and now I got an unexpected error :frowning: please

If it were me in your position, I'd have produced all the information I could possibly think of about the "problem" right from the start. I wouldn't be making the people I was leaning on to help me pry the information out of me. You're the one with the deadline - do you want help or not?

For instance, what is the problem? You haven't said beyond a totally vague statement that "sd card is working normaly but only on mylist not mylist2". How is it not working with mylist2? What are you seeing? How does that differ from what you were expecting to see? Why haven't you shown us?

You haven't even told us what kind of Nano you're working with. Do you have any idea just how many different kinds there are?

Classic Nano (ATMega328P)
Nano ESP32
Nano 33 BLE
Nano 33 BLE Sense
Nano 33 BLE Sense Rev2
Nano Every
Nano 33 IoT
Nano RP2040 Connect

Which one is it?

How is the SD card attached? Which specific breakout board are you using? Where's your schematic? Where's a clear, well lit, in focus picture of your wiring?

You keep saying it's so important to you. Message received. Now step up and actually provide the necessary information for people to be able to help you.

sorry - my arduino nano is Classic Nano (ATMega328P) (Old bootloader ) - I don't think there are any problems with writing to the sd card, the code simply stops working when I write here instead of mylist I write mylist2 or I write both mylist and mylist2 at the same time and it shows the message:
error opening test.txt
, it makes new files every time TEXT41 for example but when the code says to write the list mylist2 to the sd card, these files are empty

for (int i = 0; i <= 82; i++) {
Serial.println(float(myList[i]));
//myFile.println(myList2[i]);
//Serial.println(myList2[i]);
myFile.println(float(myList[i]));
}

I don't know what else could be causing the problem, I checked both variables, they have 83 positions, both are made up of numbers only. There is no problem with the SD card, everything works, I use the SD.h library version 1.0.7, I can't send the wiring because it's a big project and it's in a resin-filled housing

Should I write anything else?

The list defined as array of long, but it's values seems to be a floats

If your lists have a 83 positions each, why did you defined it as lists of 82 elements?

and if your lists has defined as 82-elements arrays, why did try to print 83 lines to file?

Going beyond the array boundary - like in your case when you put 83 values ​​in an array of 82 elements - can easily cause the program to freeze.

sorry but that aren't working :frowning:

What exactly doesn't work?

You didn't answer the question, how long are your arrays - 82 or 83 elements? Why are you confused about this?

i have 83 elements - it doesn't work, the same as it didn't work before - I followed your earlier advice

Why you defined the arrays as 82-element ones?

What does the advice you mean? Could you answer with more detail, if you need a help?

float myList[83] = {};
float myList2[83] = {};

  • that's my definition now as you said .
  • why I defined 82 element ones ? - cuz I think the range of list 0-82 is 82 elements :slight_smile:
  • the advice is changing 82 elements in definition to 83 elements and change volititle long to float .

enyone can help me ?

please

ok
sorry

thats not working the same resoult - i try this :

// Autor: Mariusz i Stanisław Kaczmarek 
#include <SD.h>
#include <EEPROM.h>
int SD_WRITE = 0;
File myFile;
#define PIN_SPI_CS 4
#include <ArrayList.h>

int save_done =0;
long cdk = 0;
float encoderPosition = 0;
int continue321 = 1;
int encoderPinA = 2;
int encoderPinB = 3;
int lastEncoded = 0;  
int przycisk = 0;
int continue123 = 0;
int przerwa = 0;
int opi = 0;
float myList[83] = {};
float myList2[83] = {};
long przerwa2 = 0;

#include <SPI.h>
#include <List.hpp>
#include <stdlib.h>

//List<int> myList; 
//List<int> myList2; 
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;

void setup() {

Serial.begin(2000000);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

 delay(1000);
 if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    digitalWrite(5 , HIGH);
    while (1);
  }


attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPinB), updateEncoder, CHANGE);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);

pinMode(A1 , INPUT);
pinMode(9, INPUT_PULLUP);
pinMode(5 , OUTPUT);
delay(1000);
digitalWrite(5 , LOW);

}





void loop() {
  //Serial.println(encoderPosition);
  if (encoderPosition >= 9000 && continue123 == 0 && przycisk == 1 ){
    encoderPosition = 9000;
		Serial.println("ENCODER_POSTION9000");
    
    continue123 = 1;
    digitalWrite(5 , HIGH);
    
  
	
		
  }

  if (encoderPosition <= 1000 && continue123 == 1  ){
    //Serial.println("hhh");
    //for (int i = 0; i <= 82; i++) {
    //  Serial.println(myList2[i]);
    //  Serial.println(myList[i]);
    //}
    //Serial.println("hhh");
    detachInterrupt(digitalPinToInterrupt(2));
    detachInterrupt(digitalPinToInterrupt(3));
    encoderPosition = 0;
		Serial.println("ENCODER_POSTION0");
		digitalWrite(5 , LOW);
    
		przycisk = 0;
		continue123 =0;
    SD_WRITE = 1;
    int liczba = EEPROM.read(0);

  // Sprawdzenie, czy wartość jest zdefiniowana (niezapisana wartość EEPROM to 255)
  if (liczba == 255) {
    liczba = 0; // Ustawienie początkowej wartości na 0
  } else {
    liczba += 1; // Zwiększenie wartości o 1
  }

  // Zapisanie nowej wartości do EEPROM
  EEPROM.write(0, liczba);

  // Wyświetlenie wartości w Serial Monitor
  Serial.print("Aktualna wartość: ");
  Serial.println(liczba);
  //String fileName = "text" + String(liczba) + ".txt";
  String fileName = "/" + String(liczba) + ".txt";
  Serial.println(fileName);
  myFile = SD.open(fileName, FILE_WRITE);
  delay(1000);
  if (myFile) {
  //myFile.println("NEXT_TEST:");
  
  Serial.println("Zapisywanie do karty sd...");
  for (int i = 0; i <= 82; i++) {
      //Serial.println(float(myList[i]));
      myFile.println(float(myList[i]));
      //Serial.println(float(myList2[i]));
      //myFile.println(float(myList[i]));
  }
  
  digitalWrite(5 , HIGH);
  myFile.close();
  }
  else{
    Serial.println("error opening test.txt");
  }


  fileName = "/e" + String(liczba) + ".txt";
  Serial.println(fileName);
  myFile = SD.open(fileName, FILE_WRITE);
  delay(1000);
  if (myFile) {
  //myFile.println("NEXT_TEST:");
  
  Serial.println("Zapisywanie do karty sd...");
  for (int i = 0; i <= 82; i++) {
      //Serial.println(float(myList[i]));
      myFile.println(float(myList2[i]));
      //Serial.println(float(myList2[i]));
      //myFile.println(float(myList[i]));
  }
  
  digitalWrite(5 , HIGH);
  myFile.close();
  }
  else{
    Serial.println("error opening test.txt");
  }
save_done = 1;

	}



	if (digitalRead(9) == LOW && przycisk == 0){
		Serial.println("START");
		encoderPosition = 0;
		przycisk = 1;
		Serial.println(encoderPosition);
    Serial.println(continue123);
    Serial.println(przycisk);
    
	}

  
  

	if (przycisk == 1 && (millis() - przerwa) >= 1000) {
    przerwa = millis(); // Zaktualizowanie przerwy zaraz na poczÄ…tku
    delay(1000);
    if (cdk < 83) { // Upewnij siÄ™, ĹĽe indeks nie przekracza rozmiaru tablicy
      myList[cdk] = float(analogRead(A1) / 20.46);
      myList2[cdk] = float(encoderPosition);
      Serial.println(myList2[cdk]);
      cdk++;
    } else {
      Serial.println("Tablica myList lub myList2 przekroczona!");
    }
  }
 

}

void updateEncoder() {
  int MSB = digitalRead(encoderPinA); // Most significant bit
  int LSB = digitalRead(encoderPinB); // Least significant bit

  int encoded = (MSB << 1) | LSB; // Converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; // Adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderPosition --;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderPosition ++;

  lastEncoded = encoded; // Store this value for next time
  //Serial.println(encoderPosition);
  
  
    


}

then i have this on serial monitor

11:05:14.218 -> 1850.00
11:05:15.234 -> 441.00
11:05:15.234 -> ENCODER_POSTION0
11:05:15.234 -> Aktualna wartość: 61
11:05:15.234 -> /61.txt
11:05:16.278 -> error opening test.txt
11:05:16.278 -> /e61.txt
11:05:17.257 -> Zapisywanie do karty sd...

-"Zapisywanie do karty sd..." - this sings "save done "
and also i try to do that saves to sd cards in reverse order :

 Serial.println("Zapisywanie do karty sd...");
  for (int i = 0; i <= 82; i++) {
      //Serial.println(float(myList[i]));
      myFile.println(float(myList2[i]));
      //Serial.println(float(myList2[i]));
      //myFile.println(float(myList[i]));
  }
  
  digitalWrite(5 , HIGH);
  myFile.close();
  }
  else{
    Serial.println("error opening test.txt");
  }


  fileName = "/e" + String(liczba) + ".txt";
  Serial.println(fileName);
  myFile = SD.open(fileName, FILE_WRITE);
  delay(1000);
  if (myFile) {
  //myFile.println("NEXT_TEST:");
  
  Serial.println("Zapisywanie do karty sd...");
  for (int i = 0; i <= 82; i++) {
      //Serial.println(float(myList[i]));
      myFile.println(float(myList[i]));
      //Serial.println(float(myList2[i]));
      //myFile.println(float(myList[i]));
  }
  
  digitalWrite(5 , HIGH);
  myFile.close();
  }
  else{
    Serial.println("error opening test.txt");
  }
save_done = 1; // fragment of code that i mentioned

program now can write to sd mylist and mylist2 but to only one not to booth list

What are you building? What is it supposed to do? - thet's hard to explain there's 17 nanos and 5 esp 32 on that project - is a medical project based on the examination of the entire leg (tissues of fats, muscles and bones and cartilage)

it might be a memory issue. Your two float arrays occupied a

(83 + 83) *4 = 664 bytes - a third of all available memory of Arduino Nano controller.

Add to this that SD library creates a 500-byte buffer in memory - and it may very well turn out that your code simply does not have enough memory, because the Nano has only 2 KB of RAM

Show what the Arduino IDE writes when compiling

Test your code on MCU with more RAM - for example on Mega

thanks to everyone (except deltaG) for help, I finally solved the problem myself - for all who will have a similar issue - all I had to do was change the library to another - <SdFat.h> , here is the code:


#include <SdFat.h>
#include <EEPROM.h>
#include <SPI.h>

// Pin configuration
#define PIN_SPI_CS 4

SdFat SD;
SdFile myFile;
float encoderPosition = 0;
int continue123 = 0;
int button = 0;

float myList[83] = {};
float myList2[83] = {};

const int encoderPinA = 2;
const int encoderPinB = 3;
int lastEncoded = 0;
long cdk = 0;
long break = 0;
int save_done = 0;

void setup() {
 Serial.begin(2000000);
 pinMode(PIN_SPI_CS, OUTPUT);
 if (!SD.begin(PIN_SPI_CS)) {
 Serial.println("initialization failed!");
 while(1);
 }
 pinMode(encoderPinA, INPUT_PULLUP);
 pinMode(encoderPinB, INPUT_PULLUP);


 pinMode(A1, INPUT);
 pinMode(9, INPUT_PULLUP);
 pinMode(5, OUTPUT);
 delay(1000);
 digitalWrite(5, LOW);
}

void loop() {
 if (encoderPosition >= 9000 && continue123 == 0 && button == 1) {
 encoderPosition = 9000;
 Serial.println("ENCODER_POSITION9000");
 continue123 = 1;
 digitalWrite(5, HIGH);
 }

 if (encoderPosition <= 1000 && continue123 == 1) {
 detachInterrupt(digitalPinToInterrupt(encoderPinA));
 detachInterrupt(digitalPinToInterrupt(encoderPinB));
 encoderPosition = 0;
 Serial.println("ENCODER_POSITION0");
 digitalWrite(5, LOW);
 button = 0;
 continue123 = 0;

 int number = EEPROM.read(0);
 number = (number == 255) ? 0 : number + 1;
 EEPROM.write(0, number);
 String fileName = "/" + String(number) + ".txt";
 Serial.println(fileName);

 if (myFile.open(fileName.c_str(), O_WRITE | O_CREAT)) {
 Serial.println("Saving to SD card...");
 for (int i = 0; i <= 82; i++) {
 myFile.println(myList[i]);
 }
 myFile.close();
 digitalWrite(5, HIGH);
 } else {
 Serial.println("error opening " + fileName);
 }

 fileName = "/e" + String(number) + ".txt";
 Serial.println(fileName);
 if (myFile.open(fileName.c_str(), O_WRITE | O_CREAT)) {
 Serial.println("Saving to SD card...");
 for (int i = 0; i <= 82; i++) {
 myFile.println(myList2[i]);
 }
 myFile.close();
 digitalWrite(5, HIGH);
 } else {
 Serial.println("error opening " + fileName);
 }
 save_done = 1;
 }

 if (digitalRead(9) == LOW && button == 0) {
 Serial.println("START");
 encoderPosition = 0;
 button = 1;
 Serial.println(encoderPosition);
 Serial.println(continue123);
 Serial.println(button);
 while(analogRead(A1) / 20.46 < 0.80){
 delay(1);
 //encoderPosition = 0;
 Serial.println(encoderPosition);

 }
 attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE);
 attachInterrupt(digitalPinToInterrupt(encoderPinB), updateEncoder, CHANGE);
 }

 if (button == 1 && (millis() - break) >= 1000) {
 break = millis();
 //delay(1000);
 if (cdk < 83) {
 myList[cdk] = float(analogRead(A1) / 20.46);
 myList2[cdk] = float(encoderPosition);
 Serial.println(myList2[cdk]);
 Serial.println(myList[cdk]);
 cdk++;
 } else {
 Serial.println("Array myList or myList2 exceeded!");
 }
 }
}

void updateEncoder() {
 int MSB = digitalRead(encoderPinA);
 int LSB = digitalRead(encoderPinB);
 int encoded = (MSB << 1) | LSB;
 int sum = (lastEncoded << 2) | encoded;

 if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderPosition--;
 if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderPosition++;

 lastEncoded = encoded;
}

about this:

That is awfully ambitious for someone who doesn't know how to code

If you can't explain what it does then you have no hope of coding it. Please don't waste our time.
  • I can probably code better than you, the library just had some stupid error and I had to change it to another one.

Secondly:

You know Arduino isn't to be used in medical devices right?
  • do you know that at first you prototype on Arduino to then make a printed circuit board with the same ATMEGA-328P - and such boards are used in medicine

If you have nothing to do, at least don't bother others and don't take it out on others (do it on a punching bag :slight_smile: ) and let others have a normal discussion. And if you want to take part in the discussion, help someone or give some constructive criticism