flasing led + flickering.

Hi. I've just come here again to let you know that i finally succeded setting up the rtc clock ( and can be set up manually using pushbuttons ), but unfortunately i've been having another 2 problems.

  1. If i try to reduce the brightness of each multiplexed digit by using analogWrite instead, i get some flicker effect. That is not happening if i reduce the speed of multiplexed system. At lower speed it seems to work nicely.
  2. What can i do to flash the middle small dot 2 times per second. If i try using millis() function eventually flashing led and minutes will be out of phase.
/*
 Code under (cc) by Manuel Gonzalez, www.codingcolor.com
 http://creativecommons.org/license/cc-gpl
 Pins 12, 11, 5, 4, 3, 2 to LCD
 Analog pins 4 (SDA),5(SCL) to Chronodot
 Pins 6 (hour), 7(min) buttons 
*/

#include <Wire.h>

const int hourButtonPin = 9;
const int minButtonPin = 10;
const int calibButtonPin = 11;
const int dateButtonPin = 12;
const int multiButtonPin = 13;
const int dataPin = 2;
const int latchPin = 3;
const int clockPin = 4;

int hourButtonState;
int minButtonState;

int seconds; //00-59;
int minutes; //00-59;
int hours;//1-12 - 00-23;
int day;//1-7
int date;//01-31
int month;//01-12
int year;//0-99;

int d1 = 5;
int d2 = 6; 
int d3 = 7; 
int d4 = 8;
int digitDelay = 3;

void setup()
{
  pinMode(hourButtonPin,INPUT);
  pinMode(minButtonPin,INPUT);
  pinMode(calibButtonPin, INPUT);
  pinMode(dateButtonPin, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  
  Wire.begin();
  Serial.begin(9600);
  hourButtonState = 0;
  minButtonState = 0;
  ////////////////////////////////
  seconds = 00;
  minutes = 00;
  hours = 14;
  day = 5;
  date = 21;
  month = 8;
  year = 14;
//  initChrono();//just set the time once on your RTC
  ///////////////////////////////
}

void loop()
{
  check_buttons();
  get_time();
  get_date();
  seg7_TimeDisplay();
  seg7_DateDisplay();

//  printDate();  
}

void initChrono()
{
  set_time();
  set_date();
}

void set_time()
{
   Wire.beginTransmission(104);
   Wire.write(0);
   Wire.write(decToBcd(seconds));
   Wire.write(decToBcd(minutes));
   Wire.write(decToBcd(hours));
   Wire.endTransmission();
}

void set_date()
{
  Wire.beginTransmission(104);
  Wire.write(3);
  Wire.write(decToBcd(day));
  Wire.write(decToBcd(date));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}
void get_date()
{
  Wire.beginTransmission(104); 
  Wire.write(3);//set register to 3 (day)
  Wire.endTransmission();
  Wire.requestFrom(104, 4); //get 5 bytes(day,date,month,year,control);
  day   = bcdToDec(Wire.read());
  date  = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year  = bcdToDec(Wire.read());
}

void get_time()
{
  Wire.beginTransmission(104); 
  Wire.write(0);//set register to 0
  Wire.endTransmission();
  Wire.requestFrom(104, 3);//get 3 bytes (seconds,minutes,hours);
  seconds = bcdToDec(Wire.read() & 0x7f);
  minutes = bcdToDec(Wire.read());
  hours = bcdToDec(Wire.read() & 0x3f);  
}

void setHour()
{
  hours++;
  delay(150); 
  if(hours > 23)
  {
   hours = 0; 
   minutes = 0;
  }
  seconds=0;
  set_time();
  
}
void setMinutes()
{
  delay(150);
  minutes++;  
  if(minutes > 59)
  {
   minutes = 0;   
  }
  seconds=0;  
  set_time();  
}
void check_buttons()
{
  hourButtonState = digitalRead(hourButtonPin);
  minButtonState = digitalRead(minButtonPin);
  if(hourButtonState == HIGH){
    setHour();
  }
  
  if(minButtonState == HIGH){
    setMinutes();
  }
  
  while(digitalRead(calibButtonPin) == HIGH){
    seconds=0;
    set_time();    
  }
}

void printDate()
{
  Serial.print(hours);
  Serial.print(":");
  Serial.print(minutes);
  Serial.print(":");
  Serial.print(seconds);
  Serial.print(" // ");
  Serial.print(day);
  Serial.print(".");
  Serial.print(month);
  Serial.print(".");
  Serial.print(year);
  Serial.println();
}

void seg7_TimeDisplay()
{
    int vec[10]={191, 134, 219, 207, 230, 237, 253, 135, 255, 239};
    int vec2[10]={63,6,91,79,102,109,125,7,127,111};
    int digit1,digit2,digit3,digit4; 
    digit1=hours/10;
    digit2=hours%10;
    digit3=minutes/10;
    digit4=minutes%10;
    
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~0);
          digitalWrite(latchPin, HIGH);
          digitalWrite(d1, HIGH);
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~vec2[digit1]);
          digitalWrite(latchPin, HIGH);
          delay(digitDelay);
          digitalWrite(d1, LOW);


          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~0);
          digitalWrite(latchPin, HIGH);
          digitalWrite(d2, HIGH);
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~vec[digit2]);
          digitalWrite(latchPin, HIGH);
          delay(digitDelay);
          digitalWrite(d2,LOW);  
       
           
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~0);
          digitalWrite(latchPin, HIGH);
          digitalWrite(d3, HIGH);
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~vec2[digit3]);
          digitalWrite(latchPin, HIGH);
          delay(digitDelay);
          digitalWrite(d3,LOW);  
          
          
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~0);
          digitalWrite(latchPin, HIGH);
          digitalWrite(d4, HIGH);
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~vec2[digit4]);
          digitalWrite(latchPin, HIGH);
          delay(digitDelay);
          digitalWrite(d4,LOW);     
}

void seg7_DateDisplay()
{
    while(digitalRead(dateButtonPin) == HIGH){
    int vec[10]={191, 134, 219, 207, 230, 237, 253, 135, 255, 239};
    int vec2[10]={63,6,91,79,102,109,125,7,127,111};
    int digit1_,digit2_,digit3_,digit4_;
    digit1_=date/10;
    digit2_=date%10;
    digit3_=month/10;
    digit4_=month%10;
    
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~0);
          digitalWrite(latchPin, HIGH);
          digitalWrite(d1,HIGH);
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~vec2[digit1_]);
          digitalWrite(latchPin, HIGH);
          delay(digitDelay);
          digitalWrite(d1,LOW);  


          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~0);
          digitalWrite(latchPin, HIGH);
          digitalWrite(d2, HIGH);
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~vec[digit2_]);
          digitalWrite(latchPin, HIGH);
          delay(digitDelay);
          digitalWrite(d2,LOW);  
       
           
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~0);
          digitalWrite(latchPin, HIGH);
          digitalWrite(d3, HIGH);
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~vec2[digit3_]);
          digitalWrite(latchPin, HIGH);
          delay(digitDelay);
          digitalWrite(d3,LOW);  
          
          
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~0);
          digitalWrite(latchPin, HIGH);
          digitalWrite(d4, HIGH);
          digitalWrite(latchPin, LOW);
          shiftOut(dataPin, clockPin, MSBFIRST, ~vec2[digit4_]);
          digitalWrite(latchPin, HIGH);
          delay(digitDelay);
          digitalWrite(d4,LOW);    
  }
}
///////////////////////////////////////////////////////////////////////

byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}
  1. What can i do to flash the middle small dot 2 times per second. If i try using millis() function eventually flashing led and minutes will be out of phase.

Not if you do it right.

The delay() in setHour() is wrong. Look at the state change detection example to see how how to deal with when the switch becomes pressed, rather than is pressed.