Help displaying numbers on 7 segment display

im new to all this arduino shinnanigans and rather liking it…however i have a problem i cant sort out and was hoping there would be one or two of you able to assist

ok…i have a project for an airsoft ‘bomb’, lcd displays, buttons etc etc, all working as required, game modes running fine (i didnt write the games modes, credit goes to others that made it available on t’internet)

now…all is running fine until i decide to add two 4 digit 7 segment displays (one for RED team time, one for GREEN team time), no issues using the displays, they are working fine,
the issue im having is this (yes i know i get there eventually)

i can display the times for game length and time green or red team are in control on an lcd display no problem (it is displayed in the format (00:00:000),stored in unsigned long variable

what i would like to be able to do is take the first 4 digits of which ever team time (red or green) and display this on the red or green 4 digit display, if i just try to display the time as i would on an lcd it just doesnt display the right numbers,

help me please

You need to modify the code you didn't post.

Sorry, will post code up shortly, there are several modules to program so not quite sure how to do it

There are instructions for posting inside the second post in the forum.

here is the module of code i am having issues with, the code as a whole works apart from trying to display the first 4 digits of redtime and greentime on the red/green 4 digit displays, all pins and settings for the displays are working, it is just getting them to display what i need

as stated earlier , i did not write the original code, but have added bits for relay operations and a few other minor changes, these do not affect anything

all required libraries are set up in another module as are some global settings

void domination(){

  //SETUP INITIAL TIME 
  int minutos = GAMEMINUTES-1;
  boolean showGameTime=true;
  unsigned long a;
  unsigned long iTime=millis(); //  initialTime in millisec 
  unsigned long aTime;
 
  team=0;
  iZoneTime=0;
  aTime=0;
  redTime=0;
  greenTime=0;

  int largoTono = 50;
  // 0 = neutral, 1 = green team, 2 = red team
  a=millis();
  //Starting Game Code
  while(1)  // this is the important code, is a little messy but works good.
  {
    if(endGame){
      gameOver();
    }
    
    keypad.getKey();
    aTime=millis()- iTime;
    //Code for led blinking
    timeCalcVar=(millis()- iTime)%1000;
    if(timeCalcVar >= 0 && timeCalcVar <= 40)
    {
      digitalWrite(GREENRELAY, HIGH);
      digitalWrite(REDRELAY, HIGH);
      if(team==1)digitalWrite(GREENLED, HIGH);
      if(team==1)digitalWrite(GREENRELAY, LOW);
      if(team==2)digitalWrite(REDLED, HIGH);
      if(team==2)digitalWrite(REDRELAY, LOW);
      if(team==0)digitalWrite(BLUERELAY, LOW);  
    }
    if(timeCalcVar >= 50 && timeCalcVar <= 100)
    {    
      if(team==1)digitalWrite(GREENLED, LOW);
      if(team==1)digitalWrite(GREENRELAY, HIGH);  
      if(team==2)digitalWrite(REDLED, LOW);
      if(team==2)digitalWrite(REDRELAY, HIGH);
      if(team==0)digitalWrite(BLUERELAY, HIGH);
    }
    // Sound!!! same as Destroy 
    if(timeCalcVar >= 0 && timeCalcVar <= 40 && soundEnable)tone(tonepin,activeTone,largoTono);

    if(timeCalcVar >= 245 && timeCalcVar <= 255 && minutos-aTime/60000<2 && soundEnable)tone(tonepin,activeTone,largoTono);
    if(timeCalcVar >= 495 && timeCalcVar <= 510 && minutos-aTime/60000<4 && soundEnable)tone(tonepin,activeTone,largoTono);
    if(timeCalcVar >= 745 && timeCalcVar <= 760 && minutos-aTime/60000<2 && soundEnable)tone(tonepin,activeTone,largoTono);
    //Help to count 3 secs
    if(a+2000<millis()){
      a=millis();   
      showGameTime=!showGameTime;
      cls();
    }
    //THE NEXT TO METHODS SHOW "GAME TIME" AND "CONTROLED ZONE TIME" IT SHOWS 2 AND 2 SEC EACH

    if(showGameTime){ //THE SECOND IS /2
      lcd.setCursor(5,2);
      lcd.print("GAME TIME");
      lcd.setCursor(5,3);
      printTime(minutos, aTime);
    }
    else if (!showGameTime){

      lcd.setCursor(4,0);
      if(team == 0)lcd.print("NEUTRAL ZONE");
      if(team == 1)lcd.print(" GREEN ZONE");
      if(team == 2)lcd.print("  RED ZONE");
      if(team == 1)led.DigitDisplayWrite(pinClkGreen, pinDtaGreen, greenTime);
      if(team == 2)led.DigitDisplayWrite(pinClkRed, pinDtaRed, redTime);
      if(team>0){
        lcd.setCursor(5,1);
        printTimeDom(millis()-iZoneTime,true);
      }
    }

    //###########################CHECKINGS##################

    //Check If Game End
    if(minutos-aTime/60000==0 && 59-((aTime/1000)%60)==0)
    {
      gameOver();
    }

    //Check If IS neutral
    while((defusing || cancelando) && team > 0)
    {
      cls();
      if(team>0)lcd.print("NEUTRALIZING...");
      lcd.setCursor(0,1);
      unsigned int percent=0;
      unsigned long xTime=millis(); //start disabling time
      while(defusing || cancelando)
      {
        //check if game time runs out during the disabling
        aTime= millis()- iTime;
        if((minutos-aTime/60000==0 && 59-((aTime/1000)%60)==0) || minutos-aTime/60000>4000000000){ 
          endGame = true;
        }
        
        keypad.getKey();
        timeCalcVar = (millis()- xTime)%1000;

        if( timeCalcVar >= 0 && timeCalcVar <= 20)
        {
          if(soundEnable)tone(tonepin,alarmTone1,200);
        }
        if(timeCalcVar >= 480 && timeCalcVar <= 500)
        {
          if(soundEnable)tone(tonepin,alarmTone2,200);
          digitalWrite(REDLED, LOW);
        }

        unsigned long seconds= millis() - xTime;
        percent = (seconds)/(ACTIVATESECONDS*10);
        drawBar(percent);

        if(percent >= 100)
        {
          delay(1000);

          if(team==1){ 
            greenTime+=millis()-iZoneTime;
            iZoneTime=0; 

          }
          if(team==2){ 
            redTime+=millis()-iZoneTime;
            iZoneTime=0; 
          }
          team=0;
          break;
        }
      }
      cls();
    }

more code (was too big for one post)

also if anyone can help with how do display colons on the 4 didgit display would be a great held
robotdyn displays with backpack - using tm1637.h library

  //Capturing red

    while(defusing && team == 0 )
    {
      cls();
      if(team==0)lcd.print(" CAPTURING ZONE");
      lcd.setCursor(0,1);
      unsigned int percent=0;
      unsigned long xTime=millis(); //start disabling time
      while(defusing)
      {
        keypad.getKey();
        //check if game time runs out during the disabling
        aTime= millis()- iTime;
        if((minutos-aTime/60000==0 && 59-((aTime/1000)%60)==0) || minutos-aTime/60000>4000000000){ 
          endGame = true;
        }
        timeCalcVar = (millis()- xTime)%1000;

        if( timeCalcVar >= 0 && timeCalcVar <= 20)
        {
          digitalWrite(REDLED, HIGH);
          digitalWrite(REDRELAY, LOW);  
          if(soundEnable)tone(tonepin,alarmTone1,200);
        }
        if(timeCalcVar >= 480 && timeCalcVar <= 500)
        {
          if(soundEnable)tone(tonepin,alarmTone2,200);
          digitalWrite(REDLED, LOW);
          digitalWrite(REDRELAY, HIGH);
        }

        unsigned long seconds= millis() - xTime;
        percent = (seconds)/(ACTIVATESECONDS*10);
        drawBar(percent);

        if(percent >= 100)
        {
          digitalWrite(GREENLED, LOW);
          digitalWrite(GREENRELAY, HIGH);
          team=2;
          iZoneTime=millis();
          delay(1000);
          break;
        }
      }
      cls();
      digitalWrite(REDLED, LOW);
      digitalWrite(REDRELAY, HIGH);
    }

    //getting to green zone
    while(cancelando && team == 0 )
    {
      cls();
      if(team==0)lcd.print(" CAPTURING ZONE");
      lcd.setCursor(0,1);
      unsigned int percent=0;
      unsigned long xTime=millis(); //start disabling time
      while(cancelando)
      {
        keypad.getKey();
        //check if game time runs out during the disabling
        aTime= millis()- iTime;
        if((minutos-aTime/60000==0 && 59-((aTime/1000)%60)==0) || minutos-aTime/60000>4000000000){ 
          endGame = true;
        }
        timeCalcVar = (millis()- xTime)%1000;

        if( timeCalcVar >= 0 && timeCalcVar <= 20)
        {
          digitalWrite(GREENLED, HIGH);
          digitalWrite(GREENRELAY, LOW);  
          if(soundEnable)tone(tonepin,alarmTone1,200);
        }
        if(timeCalcVar >= 480 && timeCalcVar <= 500)
        {
          if(soundEnable)tone(tonepin,alarmTone2,200);
          digitalWrite(GREENLED, LOW);
          digitalWrite(GREENRELAY, HIGH);
        }

        unsigned long seconds= millis() - xTime;
        percent = (seconds)/(ACTIVATESECONDS*10);
        drawBar(percent);

        if(percent >= 100)
        {
          digitalWrite(GREENLED, LOW);
          digitalWrite(GREENRELAY, HIGH);
          team=1;
          iZoneTime=millis();
          delay(1000);
          break;
        }
      }
      cls();
      digitalWrite(GREENLED, LOW);
      digitalWrite(GREENRELAY, HIGH);  
    }
  }
}

void gameOver(){

  if(team==1)greenTime+=millis()-iZoneTime;
  if(team==2)redTime+=millis()-iZoneTime;
  digitalWrite(GREENLED, LOW);
  digitalWrite(GREENRELAY, HIGH);
  digitalWrite(REDLED, LOW);
  digitalWrite(REDRELAY, HIGH);
  while(!defusing){
    keypad.getKey();
    if(defusing){
      keypad.getKey();
      break;
    }
    lcd.clear();
    lcd.setCursor(3,0);
    lcd.print("TIME OVER!");
    lcd.setCursor(0,1);

    //check who team win the base
    if(greenTime>redTime){
      //greenteam wins
      lcd.print(" GREEN TEAM WINS");
      digitalWrite(GREENLED, HIGH);
      digitalWrite(GREENRELAY, LOW);
    }
    else{
      //redteam wins 
      lcd.print("  RED TEAM WINS");
      digitalWrite(REDLED, HIGH);
      digitalWrite(REDRELAY, LOW);
    }
    delay(3000);
    keypad.getKey();
    if(defusing){
      keypad.getKey();
      break;
    }
    cls();
    lcd.print("Red Time:");
    lcd.setCursor(5,1);
    printTimeDom(redTime,false);
    delay(3000);
    keypad.getKey();
    if(defusing){
      
      break;
    }
    cls();
    lcd.print("Green Time:");
    lcd.setCursor(5,1);
    printTimeDom(greenTime,false);
    delay(3000);
    keypad.getKey();
    if(defusing){
      keypad.getKey();
      break;
    }
  }
  cls();
  delay(100);
  lcd.print("Play Again?");
  lcd.setCursor(0,1);
  lcd.print("A : Yes B : No");
  while(1)
  {
    var = keypad.waitForKey();
    if(var == 'a' ){
      tone(tonepin,2400,30);
      cls();
      domination();
      break;
    }  
    if(var == 'b' ){
      tone(tonepin,2400,30);
      menuPrincipal();
      break;
    }  
  } 
}

what type of seven segment display are you using?

Robot dyn 4 dugit 7 segment with backpack using the tm1637.h library...one red display , one green

Hi,
A link to the specs/data of your display will help.

Thanks.. Tom.. :slight_smile:

The dispkays all work fine, i just need a way of extracting the first 4 digits from variables REDTIME and GREEN TIME so i can display them on the 4 digit displays

You said they were unsigned long. Just use math. ie: if I have 1234567 and I just want the first four I would divide by 1000 to get 1234.

Cheers, will give that a try once everthing reassembled, also does anyone have a lust of commands for the tm1637.h please

jarrod_99:
does anyone have a lust of commands for the tm1637.h

Steady on, old chap!

well i got there in the end, cheers Delta_G for the tip, displays still dont update all the time but do display correctly at end of game which will do me fine for the moment

could still do with a list of commands used for the tm1637.h library for clearing the display etc…

anyways roll on game day so i can run some scenarios with this airsoft bomb

Hi,
Looks really good, can you post a pic with it all lit up.

Tom... :slight_smile: