POV device creates time drift

I am creating a POV (Persistence of Vision) device, a device that spins really fast and creates a steady image through the fast blinking of LED's, that are arranged in a straight line.
My code displays the Time and Date, with text.
It seems to work perfect, except I noticed that there is a significant time delay, especially as the device spins faster. I am using a Nano and DS3231. Here is the setup that I comment out once the time is set, and the loop that reads the DS3231:


// hobbyprojects
// 15-06-2019  NanoDS3231propellerClock1.ino

#include "Wire.h"
// Function prototype



unsigned long tc,to = 0;
unsigned int j,x,nm,dl,bs,bt;
int i,m,f,r1,r2 = 0;
unsigned char b1[200],b2[200];
byte a,c,c1,c2;
char s;

//int LED1 = 2;
//int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7;
int LED7 = 8;
int LED8 = 9;
int LED9 = 10;
int LED10 = 11;
int LED11 = 12;
int LED12 = A1;
int LED13 = A2;
int LED14 = A3;
int LED15 = 3;
int LED16 = 13;

int sensorPin = 2;
int mode_key = A0;



unsigned int n,propeller_posn;
unsigned long previousTime = 0;

byte hour,minute,second,Day,Date,Month,Year;
byte tempMSB,tempLSB;
int val;

const PROGMEM unsigned char data[] = {
  0x3f, 0x23, 0x23, 0x3f, 0x01, 0x13, 0x3f, 0x03, 0x2f, 0x2b, 0x2b, 0x3b,
  0x2b, 0x2b, 0x2b, 0x3f, 0x39, 0x09, 0x09, 0x3f, 0x3b, 0x2b, 0x2b, 0x2f,
  0x3f, 0x2b, 0x2b, 0x2f, 0x21, 0x27, 0x29, 0x31, 0x3f, 0x2b, 0x2b, 0x3f,
  0x3b, 0x2b, 0x2b, 0x3f, 0x00, 0x00, 0x15, 0x00, 0x01, 0x40, 0x40, 0x7f,
  0x40, 0x40, 0x00, 0x00, 0x00, 0x41, 0x7f, 0x41, 0x00, 0x00, 0x00, 0x7f,
  0x20, 0x18, 0x20, 0x7f, 0x00, 0x00, 0x7f, 0x49, 0x49, 0x49, 0x41, 0x00,
  0x00, 0x7f, 0x41, 0x41, 0x22, 0x1c, 0x00,0x00, 0x07, 0x3c, 0x44, 0x3c, 0x07,
  0x00,0x00, 0x40, 0x40, 0x7f, 0x40, 0x40,0x00, 0x00, 0x7f, 0x49, 0x49, 0x49, 0x41,
  0x00,0x00, 0x01, 0x02, 0x04, 0x8, 0x10, 
};
void setup() 
{
  //pinMode(LED1,OUTPUT);  
  //pinMode(LED2,OUTPUT);  
  pinMode(LED3,OUTPUT);  
  pinMode(LED4,OUTPUT);  
  pinMode(LED5,OUTPUT);  
  pinMode(LED6,OUTPUT);  
  pinMode(LED7,OUTPUT);  
  pinMode(LED8,OUTPUT);  
  pinMode(LED9,OUTPUT);  
  pinMode(LED10,OUTPUT);  
  pinMode(LED11,OUTPUT);  
  pinMode(LED12,OUTPUT);  
  pinMode(LED13,OUTPUT);  
  pinMode(LED14,OUTPUT);  
  pinMode(LED15,OUTPUT);  
  pinMode(LED16,OUTPUT);  
  
  pinMode(sensorPin,INPUT_PULLUP); 
  pinMode(mode_key,INPUT_PULLUP); 
      
  attachInterrupt(0, usdl, RISING);     
  if(hour > 11)
  hour = hour - 12;
  
  Wire.begin();  
//  hour = 17;    // set hours
//  minute = 49;  // set minutes
  // second = 00;  // set seconds
// Day = 7;       // 1=Sunday, 7=Saturday
// Date = 03;     // 1 to 31
//  Month = 2;
//  Year = 24;

 // Wire.beginTransmission(0x68);
// Wire.write(0);
//  Wire.write(dec2bcd(second));
//  Wire.write(dec2bcd(minute));
//  Wire.write(dec2bcd(hour));
//  Wire.write(dec2bcd(Day));
//  Wire.write(dec2bcd(Date));
//  Wire.write(dec2bcd(Month));
//  Wire.write(dec2bcd(Year));
//  Wire.endTransmission();
 }
//=========================================






void loop() 
{

{
    val = digitalRead(sensorPin);    
    while (val == LOW)
  {
    val = digitalRead(sensorPin);
  }
  i = 0;
  while(i < 200)
  {
  b1[i] = 0x00;
  b2[i] = 0x00;
  i++;    
  }  
  r1 = 0;
  for(i = 0; i < 5; i++)
  {
  b1[r1] = 0;
  r1++;
  }
  nm = hour;
  if(nm == 0)
  nm = 12; 
  nm = nm/10;
  nm = nm%10;
  vt();
  c1 = 0;
  b1[r1] = 0;
  r1++;
  nm = hour ;
  if(nm == 0)
  nm = 12;   
  nm = nm%10;
  vt();
  nm = 10;
  vt();
  nm = minute/10;
  nm = nm%10;
  vt();
  b1[r1] = 0;  
  r1++;
  nm = minute;
  nm = nm%10;
  vt();
  nm = 10;
  vt();
  nm = second/10;
  nm = nm%10;
  vt();
  b1[r1] = 0;
  r1++;
  nm = second;
  nm = nm%10;
  vt();
  
  for(i = 0; i < 25; i++)
  {
  b1[r1] = 0;
  r1++;
  }  



  bt = 15;
  nm=Year;
  nm=nm%10;
  vb();
   b1[r1] = 0x00; 
     r1++;
  
   nm=Year;
  nm=nm/10;
  nm=nm%10;
  vb();
  b1[r1] = 0x00; 
      r1++;
  nm=0;
  vb();
  b1[r1] = 0; 
 r1++;
  nm=2;
  vb();
   b1[r1] = 0; 
   r1++;
  c1 = 0x1;
  b1[r1] = c1;
  r1++;
  c1 = 0x2;
  b1[r1] = c1;
  r1++;
  c1 = 0x4;
  b1[r1] = c1;
  r1++;
  c1 = 0x8;
  b1[r1] = c1;
  r1++;
  c1 = 0x10;// decimal point
  b1[r1] = c1;
  r1++;
  r1++;
 b1[r1] = 0; 
 r1++;
 nm= Date;
 nm=nm%10;
  vb();
  b1[r1] = 0;  
  r1++;
 nm=Date;
 nm=nm/10;
 nm=nm%10;
  vb();
  b1[r1] = 0;  
  r1++;
  c1 = 0x1;
  b1[r1] = c1;
  r1++;
  c1 = 0x2;
  b1[r1] = c1;
  r1++;
  c1 = 0x4;
  b1[r1] = c1;
  r1++;
  c1 = 0x8;
  b1[r1] = c1;
  r1++;
  c1 = 0x10;// decimal point
  b1[r1] = c1;
  r1++;
  b1[r1] = 0;  
  r1++;
  nm = Month;
  nm = nm%10;
vb();  
  b1[r1] = 0;  
  r1++;
 nm = Month;
  nm=nm/10;
  nm = nm%10;
  vb();

  
  for(i = 0; i < 12; i++)
  {
  b1[r1] = 0;
  r1++;
  }  
  bs = 9;
  bt = 28;
  r2 = 0;  
  mti();
  bs = 78;
  bt = 25;
  r2 = 0;  
  mte();
  bt = r1;
  r1 = 0;
   dc();
  Wire.beginTransmission(0x68);`This is where it starts reading info from the DS3231S`
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 7);
  second = bcd2dec(Wire.read() & 0x7f);
  minute = bcd2dec(Wire.read());
  hour = bcd2dec(Wire.read() & 0x3f);
  if(hour > 11)
  hour = hour - 12;

  // Now also read the day, date, month, and year
Day = bcd2dec(Wire.read()); // Day of the week, 1 = Sunday, 7 = Saturday
Date = bcd2dec(Wire.read()); // Date of the month
Month = bcd2dec(Wire.read() & 0x1F); // Month, strip the century flag
Year = bcd2dec(Wire.read()); // Year (assuming 21st century)


  Serial.print(Year);



    val = digitalRead(sensorPin);  
    while (val == HIGH)
    {
    val = digitalRead(sensorPin); 
    } 
  }  
}
void displayClear()
  {
  //digitalWrite(LED1,LOW);
  //digitalWrite(LED2,LOW);
  digitalWrite(LED3,LOW);
  digitalWrite(LED4,LOW);
  digitalWrite(LED5,LOW);
  digitalWrite(LED6,LOW);
  digitalWrite(LED7,LOW);
  digitalWrite(LED8,LOW);
  digitalWrite(LED9,LOW);
  digitalWrite(LED10,LOW);
  digitalWrite(LED11,LOW);
  digitalWrite(LED12,LOW);
  digitalWrite(LED13,LOW);
  digitalWrite(LED14,LOW);
  digitalWrite(LED15,LOW);
  digitalWrite(LED16,LOW);  
  }
byte dec2bcd(byte val)
{
  return((val/10*16)+(val%10));
}
byte bcd2dec(byte val)
{
  return((val/16*10)+(val%16));
}

void dc()
{
  while(r1 < bt)
 { 
  c = b1[r1];  
 if(c & 1)
  PORTC |= (1<<1);  
  else
  PORTC &= ~(1<<1);     
  if(c & 2)
  PORTC |= (1<<2);   
  else
  PORTC &= ~(1<<2);  
  if(c & 4)
  PORTC |= (1<<3);    
  else
  PORTC &= ~(1<<3); 
  if(c & 8)
  PORTD |= (1<<3);  
  else
  PORTD &= ~(1<<3);  
  if(c & 16)
  PORTB |= (1<<5);  
  else
  PORTB &= ~(1<<5);  
  c = b2[r1];
  if(c & 1)
  PORTD |= (1<<6);
  else
  PORTD &= ~(1<<6);   
  if(c & 2)
  PORTD |= (1<<7);  
  else
  PORTD &= ~(1<<7);  
  if(c & 4)
  PORTB |= (1<<0);   
  else
  PORTB &= ~(1<<0); 
  if(c & 8)
  PORTB |= (1<<1);  
  else
  PORTB &= ~(1<<1);   
  if(c & 16)
  PORTB |= (1<<2);   
  else
  PORTB &= ~(1<<2);
  if(c & 32)
  PORTB |= (1<<3);
  else
  PORTB &= ~(1<<3);
  if(c & 64)
  PORTB |= (1<<4);  
  else
  
  PORTB &= ~(1<<4);  
  delayMicroseconds(dl);  
  displayClear();
  delayMicroseconds(dl*2);
  r1++;
 }  
}
 void vt()
 {
  j=nm*4;
  i = 0;  
  while(i <= 3)
  { 
  c1 = (pgm_read_byte(&(data+j)[i]));
  c1 >>= 1; 
  b1[r1] = c1;
  i++;
  r1++;
  }
 }
 void vb() 
{
  j=nm*4;
  i = 3;  
  while(i >= 0)
 { 
  c = (pgm_read_byte(&(data+j)[i]));
  s=0;
  for(a = 0; a < 8; a++)
  {
    s <<= 1;
    s |= c & 1;
    c >>= 1;
  }
  s >>= 2;
  b1[r1] = s;
  i--;
  r1++;  
}
} 
 void mti()
 {
  r2 = 0;
  while(r2 < bt)
  { 
  c1 = (pgm_read_byte(&(data+45)[r2]));
  b2[r2+bs] = c1;
  r2++;
  }
 } 
 void mc()
 {
  r2 = 0;
  while(r2 < bt)
  { 
  c1 = (pgm_read_byte(&(data+97)[r2]));
  b1[r1] = c1;
  r1++;
  r2++;
  }
 }  
 void mte()
 {
  i = 0;
  r2 = bt;
  while(r2 >= 0)
  { 
  c = (pgm_read_byte(&(data+73)[r2]));
  s=0;
  for(a = 0; a < 8; a++)
  {
    s <<= 1;
    s |= c & 1;
    c >>= 1;
  }
  s = s >> 1;
  b2[i+bs] = s;
  r2--;
  i++;
  }
 } 
   void usdl()
{    
  f = 0;
  n = 0;
  tc = micros()-to;
  to = micros(); 
  f = 1;
  dl = tc/550; 
} 

Please edit your post to add the rest of the code, and use code tags. Instructions can be found in the "How to get the best out of this forum" post.

Most likely, the display code turns interrupts off at times, which stops the Arduino internal timekeeping.

1 Like

Define POV. It could be passenger on vehicle, point of view, etc.

Persistence of Vision

Sorry, for some reason I missed that you have a DS3231, which (if not counterfeit) keeps accurate time, independent of program operation.

If the time really does seem to drifting, check for correct DS3231 timekeeping with simple test code (no POV display).

Can you explain what you see that indicates a "time delay"?

If you think there is a speed issue with the i2c readings of the DS3231, you can synch the rtc with the time library, and have all the time readings in the code be based on the main processor using the time library without i2c communication.

It's noticeable, anywhere from a couple to 10 seconds every minute, depending on how fast im spinning the device.
I have no clue what I'm doing when it comes to programming, and I had the following routes I'm considering:
I noticed that one of the libraries had an example program called 'Now'. When I run that on serial, every 10 microseconds, it doesnt slow down the time, meaning that calling for data on the 3231 doesnt slow it down in this program. I would have to figure out how to implement that library into my program.
OR
Your suggestion - but I wouldnt know how complicated it would be to set up a whole time/calendar in the code.

I believe it is interference, I need to see a schematic of your setup and notes on any wire longer then 10"/25CM. Post links to technical information on the hardware devices.

Not sure what is wrong but since f, n, tc, to, and dl are
used in an interrupt service routine they should be declared volatile.

Also, your code may have been garbled when you posted it because there is a
comment enclosed in single quotes that caused a compile error


Longest lead I have is maybe an inch. Its a pretty tight schematic

I will go down this rabbit hole later today - thanks for the tip.
I only added that comment when I posted to the forum - 1st time - dont know the standard protocol.

Not complicated at all. Install the Arduino time library (TimeLib.h) and study the documentation and examples.

Okay - So I reprogrammed using TimeLib.H
It works, kind of
Now I have a new problem, but it looks like the same old problem, but now its easier to identify.
As the device is on, and spins around real fast, the seconds keep up with real time perfect, because it reads the time off the DS3231 at setup, sends the time to internal clock, and im updating that the whole time.
Now, when I shut off the device and turn it back on, I notice the time on the restart has fallen behind. The longer I had it on, the further behind it gets. In other words, as the device spins, it causes the DS3231 to slow down. Is that a sign of signal interference? I am using a wireless power charger, and its a couple inches away, from the device, but I am guessing this is the culprit.

Below is the code:


// hobbyprojects
// 15-06-2019  NanoDS3231propellerClock1.ino


// Function prototype


#include <TimeLib.h>
#include "Wire.h"

volatile unsigned long tc,to = 0;
unsigned int j,x,nm,bs,bt;
volatile unsigned int dl;
int i,m,r1,r2 = 0;
volatile int f = 0;
unsigned char b1[200],b2[200];
byte a,c,c1,c2;
char s;

//int LED1 = 2;
//int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7; 
int LED7 = 8;
int LED8 = 9;
int LED9 = 10;
int LED10 = 11;
int LED11 = 12;
int LED12 = A1;
int LED13 = A2;
int LED14 = A3;
int LED15 = 3;
int LED16 = 13;

int sensorPin = 2;
int mode_key = A0;



volatile unsigned int n;
unsigned long previousTime = 0;


int val;

const PROGMEM unsigned char data[] = {
  0x3f, 0x23, 0x23, 0x3f, 0x01, 0x13, 0x3f, 0x03, 0x2f, 0x2b, 0x2b, 0x3b,
  0x2b, 0x2b, 0x2b, 0x3f, 0x39, 0x09, 0x09, 0x3f, 0x3b, 0x2b, 0x2b, 0x2f,
  0x3f, 0x2b, 0x2b, 0x2f, 0x21, 0x27, 0x29, 0x31, 0x3f, 0x2b, 0x2b, 0x3f,
  0x3b, 0x2b, 0x2b, 0x3f, 0x00, 0x00, 0x15, 0x00, 0x01, 0x40, 0x40, 0x7f,
  0x40, 0x40, 0x00, 0x00, 0x00, 0x41, 0x7f, 0x41, 0x00, 0x00, 0x00, 0x7f,
  0x20, 0x18, 0x20, 0x7f, 0x00, 0x00, 0x7f, 0x49, 0x49, 0x49, 0x41, 0x00,
  0x00, 0x7f, 0x41, 0x41, 0x22, 0x1c, 0x00,0x00, 0x07, 0x3c, 0x44, 0x3c, 0x07,
  0x00,0x00, 0x40, 0x40, 0x7f, 0x40, 0x40,0x00, 0x00, 0x7f, 0x49, 0x49, 0x49, 0x41,
  0x00,0x00, 0x01, 0x02, 0x04, 0x8, 0x10, 
};



#define DS3231_I2C_ADDRESS 0x68

// Forward declarations

byte dec2bcd(byte val);
void usdl(); 
time_t getDS3231Time();


void setup() 
{
  Serial.begin(9600);
  Wire.begin();
  setSyncProvider(getDS3231Time); // Use DS3231 for time sync



  
  //pinMode(LED1,OUTPUT);  
  //pinMode(LED2,OUTPUT);  
  pinMode(LED3,OUTPUT);  
  pinMode(LED4,OUTPUT);  
  pinMode(LED5,OUTPUT);  
  pinMode(LED6,OUTPUT);  
  pinMode(LED7,OUTPUT);  
  pinMode(LED8,OUTPUT);  
  pinMode(LED9,OUTPUT);  
  pinMode(LED10,OUTPUT);  
  pinMode(LED11,OUTPUT);  
  pinMode(LED12,OUTPUT);  
  pinMode(LED13,OUTPUT);  
  pinMode(LED14,OUTPUT);  
  pinMode(LED15,OUTPUT);  
  pinMode(LED16,OUTPUT);  
  
  pinMode(sensorPin,INPUT_PULLUP); 
  pinMode(mode_key,INPUT_PULLUP); 
      
  attachInterrupt(0, usdl, RISING);     
  
int currentHour = hour(); // Get the current hour and store it in a variable

   // If you want to set the DS3231 time, ensure this function is correctly defined and called
//  setTimeOnDS3231(21, 57, 20, 1, 04, 02, 24); // Uncomment and use when needed

}

void loop() 
{
    Serial.print("Date: ");
    Serial.print(year()-2000);
    Serial.print("-");
    Serial.print(month());
    Serial.print("-");
    Serial.print(day());
    Serial.print(", Time: ");
    Serial.print(hour());
    Serial.print(":");
    Serial.print(minute());
    Serial.print(":");
    Serial.println(second());
  
{
    val = digitalRead(sensorPin);    
    while (val == LOW)
  {
    val = digitalRead(sensorPin);
  }
  i = 0;
  while(i < 200)
  {
  b1[i] = 0x00;
  b2[i] = 0x00;
  i++;    
  }  
  r1 = 0;
  for(i = 0; i < 5; i++)
  {
  b1[r1] = 0;
  r1++;
  }
  nm = hourFormat12();
  if(nm == 0)
  nm = 12; 
  nm = nm/10;
  nm = nm%10;
  vt();
  c1 = 0;
  b1[r1] = 0;
  r1++;
   nm = hourFormat12() ;
  if(nm == 0)
  nm = 12;   
  nm = nm%10;
  vt();
  nm = 10;
  vt();
    nm = minute()/10;
  nm = nm%10;
  vt();
  b1[r1] = 0;  
  r1++;
     nm = minute();
  nm = nm%10;
  vt();
  nm = 10;
  vt();
   nm = second()/10;
  nm = nm%10;
  vt();
  b1[r1] = 0;
  r1++;
     nm = second();
  nm = nm%10;
  vt();
  
  for(i = 0; i < 25; i++)
  {
  b1[r1] = 0;
  r1++;
  }  



  bt = 15;
   nm=year();
  nm=nm%10;
  vb();
   b1[r1] = 0x00; 
     r1++;
  
 nm=year();
  nm=nm/10;
  nm=nm%10;
  vb();
  b1[r1] = 0x00; 
      r1++;
  nm=0;
  vb();
  b1[r1] = 0; 
 r1++;
  nm=2;
  vb();
   b1[r1] = 0; 
   r1++;
  c1 = 0x1;
  b1[r1] = c1;
  r1++;
  c1 = 0x2;
  b1[r1] = c1;
  r1++;
  c1 = 0x4;
  b1[r1] = c1;
  r1++;
  c1 = 0x8;
  b1[r1] = c1;
  r1++;
  c1 = 0x10;// decimal point
  b1[r1] = c1;
  r1++;
  r1++;
 b1[r1] = 0; 
 r1++;
   nm = day();
 nm=nm%10;
  vb();
  b1[r1] = 0;  
  r1++;
  nm =day();
 nm=nm/10;
 nm=nm%10;
  vb();
  b1[r1] = 0;  
  r1++;
  c1 = 0x1;
  b1[r1] = c1;
  r1++;
  c1 = 0x2;
  b1[r1] = c1;
  r1++;
  c1 = 0x4;
  b1[r1] = c1;
  r1++;
  c1 = 0x8;
  b1[r1] = c1;
  r1++;
  c1 = 0x10;// decimal point
  b1[r1] = c1;
  r1++;
  b1[r1] = 0;  
  r1++;
 nm = month();
  nm = nm%10;
vb();  
  b1[r1] = 0;  
  r1++;
  nm = month();
  nm=nm/10;
  nm = nm%10;
  vb();

  
  for(i = 0; i < 12; i++)
  {
  b1[r1] = 0;
  r1++;
  }  
  bs = 9;
  bt = 28;
  r2 = 0;  
  mti();
  bs = 78;
  bt = 25;
  r2 = 0;  
  mte();
  bt = r1;
  r1 = 0;
   dc();
 
    val = digitalRead(sensorPin);  
    while (val == HIGH)
    {
    val = digitalRead(sensorPin); 
    } 
  }  
}
void displayClear()
  {
  //digitalWrite(LED1,LOW);
  //digitalWrite(LED2,LOW);
  digitalWrite(LED3,LOW);
  digitalWrite(LED4,LOW);
  digitalWrite(LED5,LOW);
  digitalWrite(LED6,LOW);
  digitalWrite(LED7,LOW);
  digitalWrite(LED8,LOW);
  digitalWrite(LED9,LOW);
  digitalWrite(LED10,LOW);
  digitalWrite(LED11,LOW);
  digitalWrite(LED12,LOW);
  digitalWrite(LED13,LOW);
  digitalWrite(LED14,LOW);
  digitalWrite(LED15,LOW);
  digitalWrite(LED16,LOW);  
  }
byte dec2bcd(byte val) {
  return ((val / 10) * 16) + (val % 10);
}
time_t getDS3231Time() {
  tmElements_t tm;

  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write((byte)0); // Set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // Request 7 bytes for time and date

  tm.Second = bcd2dec(Wire.read());
  tm.Minute = bcd2dec(Wire.read());
  tm.Hour = bcd2dec(Wire.read());
  tm.Wday = bcd2dec(Wire.read()); // Day of week
  tm.Day = bcd2dec(Wire.read());
  tm.Month = bcd2dec(Wire.read());
  tm.Year = CalendarYrToTm(bcd2dec(Wire.read()) + 2000);

  return makeTime(tm); // Correctly use makeTime with a tmElements_t structure
}
byte bcd2dec(byte val)
{
  return((val/16*10)+(val%16));
}

void dc()
{
  while(r1 < bt)
 { 
  c = b1[r1];  
 if(c & 1)
  PORTC |= (1<<1);  
  else
  PORTC &= ~(1<<1);     
  if(c & 2)
  PORTC |= (1<<2);   
  else
  PORTC &= ~(1<<2);  
  if(c & 4)
  PORTC |= (1<<3);    
  else
  PORTC &= ~(1<<3); 
  if(c & 8)
  PORTD |= (1<<3);  
  else
  PORTD &= ~(1<<3);  
  if(c & 16)
  PORTB |= (1<<5);  
  else
  PORTB &= ~(1<<5);  
  c = b2[r1];
  if(c & 1)
  PORTD |= (1<<6);
  else
  PORTD &= ~(1<<6);   
  if(c & 2)
  PORTD |= (1<<7);  
  else
  PORTD &= ~(1<<7);  
  if(c & 4)
  PORTB |= (1<<0);   
  else
  PORTB &= ~(1<<0); 
  if(c & 8)
  PORTB |= (1<<1);  
  else
  PORTB &= ~(1<<1);   
  if(c & 16)
  PORTB |= (1<<2);   
  else
  PORTB &= ~(1<<2);
  if(c & 32)
  PORTB |= (1<<3);
  else
  PORTB &= ~(1<<3);
  if(c & 64)
  PORTB |= (1<<4);  
  else
  
  PORTB &= ~(1<<4);  
  delayMicroseconds(dl);  
  displayClear();
  delayMicroseconds(dl*2);
  r1++;
 }  
}
 void vt()
 {
  j=nm*4;
  i = 0;  
  while(i <= 3)
  { 
  c1 = (pgm_read_byte(&(data+j)[i]));
  c1 >>= 1; 
  b1[r1] = c1;
  i++;
  r1++;
  }
 }
 void vb() 
{
  j=nm*4;
  i = 3;  
  while(i >= 0)
 { 
  c = (pgm_read_byte(&(data+j)[i]));
  s=0;
  for(a = 0; a < 8; a++)
  {
    s <<= 1;
    s |= c & 1;
    c >>= 1;
  }
  s >>= 2;
  b1[r1] = s;
  i--;
  r1++;  
}
} 
 void mti()
 {
  r2 = 0;
  while(r2 < bt)
  { 
  c1 = (pgm_read_byte(&(data+45)[r2]));
  b2[r2+bs] = c1;
  r2++;
  }
 } 
 void mc()
 {
  r2 = 0;
  while(r2 < bt)
  { 
  c1 = (pgm_read_byte(&(data+97)[r2]));
  b1[r1] = c1;
  r1++;
  r2++;
  }
 }  
 void mte()
 {
  i = 0;
  r2 = bt;
  while(r2 >= 0)
  { 
  c = (pgm_read_byte(&(data+73)[r2]));
  s=0;
  for(a = 0; a < 8; a++)
  {
    s <<= 1;
    s |= c & 1;
    c >>= 1;
  }
  s = s >> 1;
  b2[i+bs] = s;
  r2--;
  i++;
  }
 } 
/*
void setTimeOnDS3231(int hour, int minute, int second, int wday, int date, int month, int year) {
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // Set DS3231 register pointer to 00h
  Wire.write(dec2bcd(second));
  Wire.write(dec2bcd(minute));
  Wire.write(dec2bcd(hour));
  Wire.write(dec2bcd(wday));
  Wire.write(dec2bcd(date));
  Wire.write(dec2bcd(month));
  Wire.write(dec2bcd(year)); // Year needs to be offset from 2000
  Wire.endTransmission();
}
*/
void usdl()
{    
  f = 0;
  n = 0;
  tc = micros()-to;
  to = micros(); 
  f = 1;
  dl = tc/550; 
} 



Confirmed.
I moved the receiver of the power coil on the other side of the device - everything works as it should.

One more stupid mistake added to my stupid mistake pile. EMI - electromagnetic interference, look into it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.