I have done some research and have  found a much simpler yet more complex code to out put exactly the same pattern with seven LEDs.
Here is the new code:
const int nbr10SecBits = 3;  //10 seconds' number of pins.
const int first10SecPin = 2;  //First 10 seconds pin.
const int nbr1SecBits = 4;  //1 seconds' number of pins.
const int first1SecPin = 8;  //First 1 seconds pin.
// macros from DateTime.h
/* Useful Constants */
#define SECS_PER_MIN  (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY  (SECS_PER_HOUR * 24L)
/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)  
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)
void setup()
{
  Serial.begin(9600);
  for(int i=0; i < nbr10SecBits; i++)
    pinMode(i+first10SecPin, OUTPUT);
  for(int i=0; i < nbr1SecBits; i++)
    pinMode(i+first1SecPin, OUTPUT);    
}
void loop()
{
  unsigned long time = millis() / 1000;
  int tenSeconds = ((numberOfSeconds(time) - (numberOfSeconds(time) % 10))/ 10);
  int oneSeconds = numberOfSeconds(time) % 10 ;
  showBinary(tenSeconds,nbr10SecBits,first10SecPin);
  showBinary(oneSeconds,nbr1SecBits,first1SecPin);
  
  Serial.print(numberOfSeconds(time));
  Serial.print(tenSeconds);
  Serial.print(oneSeconds);
  Serial.println();
  delay(1000);
}
// this function sets the pins starting from firstPin to the binary value of the given number
void showBinary( int number, int nbrPins, int firstPin)
{
  for(int bit=0; bit < nbrPins; bit++)
  {
    boolean isBitSet =  bitRead(number, bit); // isBitSet will be true if given bit is set in this channel
    digitalWrite(bit + firstPin, isBitSet);
  }  
}
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment