Variable result is corrupted after assigning delay

Hi all,

As the title says i am having a bit of an issue.

I am retrieving an ADC value and both printing it via serial and sending trough SPI.

I have no issues doing that at the loop execution rate, but since that is way too fast i added a delay.

The code looks like this:

unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)
int sensorPin = A0;    // select the input pin for the potentiometer

Void setup()
{}
Void loop()
      {
       unsigned long currentMillis = millis();

       if(currentMillis - previousMillis >= interval) 
      {
      previousMillis = currentMillis;   
                
          adc = analogRead(sensorPin);     
         
          Serial.println(adc);          
         
          PORTD &= ~_BV(4);                      //Turn Shift register CS PIN LOW); // connects to RCLK
          SPI.transfer (highByte (adc));        // send out upper 8 bits MSB first 
          SPI.transfer (lowByte (adc));         // send out lower 8 bits
          PORTD |= _BV(4);                       //Turn Shift register CS PIN high - data output changes on this rising edge
         }

The data returns corrupted. For example a reading of 1023 comes as 1039. If I remove the delay everything works correctly.

Has anyone faced this issue before?

That code cannot compile. Post your code.

You're right!!

I just figured out that the culprit was the Adafruit Nokia5110 LCD library.

I am not sure if it was just a timing issue or what was happening since I wasn't using the LCD (just initialized on the setup).

Do you know of any other good libraries for this LCD that can use HW SPI?

[And yes, I CAN google them, but I am asking a recommendation on a good and light one.]