First Project Help Needed (Single Digit, Seven Seg Display)

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
gadget-dad
 
Posts: 2
Joined: Sun Jan 08, 2012 12:48 pm

First Project Help Needed (Single Digit, Seven Seg Display)

Post by gadget-dad »

Hardware: Uno, shift register, single digit 7 seg display.I know this isn't practical, but to get to this point I have learned a lot (some incorrectly I am sure). As a base I used the arduino time_h library example w/o hardware.

Goal: Two blinks for minutes (tens then ones), Two blinks for seconds (tens and ones), blank, update

Progress: minutes good < 10 minutes, Seconds good if under < 20 sec, blank good, update good.

Problem: after 20 secs (i = 26) seconds flash 2-1-6 and after 30 (i=39) seconds flash 3-2-1-9. I am not even sure how this is possible, but I am interested to learn.

Code: Select all

#include <Time.h>  

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 
 int data = 2; 
int clock = 3;
int latch = 4;
 int ledclear = B00000000;
 int number0 = B10110111;
 int number1 = B00010100; 
 int number2 = B10101101;
 int number3 = B10011101;
 int number4 = B00011110;
 int number5 = B10011011;
 int number6 = B10111010;
 int number7 = B00010101;
 int number8 = B10111111;
 int number9 = B00011111;
 int value[10];
 
void setup()  {
    pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);  
  pinMode(latch, OUTPUT);
value[0] = number0; 
value[1] = number1;
value[2] = number2;
value[3] = number3;
value[4] = number4;
value[5] = number5;
value[6] = number6;
value[7] = number7;
value[8] = number8;
value[9] = number9;

  Serial.begin(9600);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println(" for sync message");
}

void loop(){    
  if(Serial.available() ) 
  {
    processSyncMessage();
  }
 
  if(timeStatus()!= timeNotSet)   
  {
    digitalWrite(13,timeStatus() == BANNED); // on if synced, off if needs refresh  
    digitalClockDisplay();  
  }
  delay(3000);
}

void digitalClockDisplay(){

  // digital clock display of the time
  Serial.print(hour());
  
  
  printDigits(minute());
 int k = minute();
   if( k < 10 )
  {
  digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, value[0]);
  digitalWrite(latch, HIGH);
 
   delay(400);
      digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, ledclear);
  digitalWrite(latch, HIGH);
  delay(200);
    digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, value[k]);
  digitalWrite(latch, HIGH);
    }
     delay(400);
      digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, ledclear);
  digitalWrite(latch, HIGH);
    delay(200);
  printDigits(second());
   int val = second();
 int units = val;
int tens = 0;


   if( units < 10 )
  {
  digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, value[0]);
  digitalWrite(latch, HIGH);
 
   delay(400);
      digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, ledclear);
  digitalWrite(latch, HIGH);
  delay(150);
    digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, value[val]);
  digitalWrite(latch, HIGH);
  }
  else
  while(units >= 10){
    int i = 0;
int tens = (units / 10);
units = units - 10;
i = tens; 
    digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, value[i]);
  digitalWrite(latch, HIGH);
  delay(400);
  digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, ledclear);
  digitalWrite(latch, HIGH);
  delay(150);
   digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, value[units]);
  digitalWrite(latch, HIGH);
  }
    
delay(400);

   digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, ledclear);
  digitalWrite(latch, HIGH);
  delay(400);
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
   
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ; 
    Serial.print(c);  
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    }  
  }
}

time_t requestSync()
{
  Serial.print(TIME_REQUEST);  
  return 0; // the time will be sent later in response to serial mesg
}

odometer
 
Posts: 98
Joined: Sun Aug 21, 2011 11:01 pm

Re: First Project Help Needed (Single Digit, Seven Seg Display)

Post by odometer »

Your code to display a number as digits is all wrong. If you follow it from start to finish as the controller would, then it will become quite clear why you are seeing the "countdown". Hint: keep careful track of the number in the "units" variable.

There is actually a much simpler code for this, that does not require loops.

Code: Select all

int number = 25; // can be anything from 0 to 99
int tens = (number/10);
int ones = (number%10);
For higher numbers:

Code: Select all

int number = 17890; // can be anything from 0 up to maximum for int (or even for unsigned int)
int myriads = (number/10000);
int thousands = (number/1000)%10;
int hundreds = (number/100)%10;
int tens = (number/10)%10;
int ones = number%10;

User avatar
gadget-dad
 
Posts: 2
Joined: Sun Jan 08, 2012 12:48 pm

Re: First Project Help Needed (Single Digit, Seven Seg Display)

Post by gadget-dad »

Thank you odometer.

Finished= http://youtu.be/EO9ynufwI5Q

Yes, there isn't much to it, but I really did learn a lot.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”