Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15404

POV device creates time drift

$
0
0

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; 
} 

10 posts - 5 participants

Read full topic


Viewing all articles
Browse latest Browse all 15404

Trending Articles