Go Down

Topic: how to work with SevSeg.h ? (Read 2232 times) previous topic - next topic

firashelou

did you checked the pictures of the datasheet i attached at the start of the topic ? and the file i attached for the "library" it's a personal library just to know the numbers like :

Code: [Select]

//0 Left digit
 
  /*digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW); //pin 6 of arduino is connected  to pin 5 of the display LOW for 1 digit and HIGH with pin 10 for 2 digits
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH); // pin 11 of arduino is connected to pin 10 of the display HIGH for 1 digit
  */
 
  //1 Left digit
 
  /*digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);//pin 6 of arduino is connected  to pin 5 of the display LOW for 1 digit and HIGH with pin 10 for 2 digits
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH); // pin 11 of arduino is connected to pin 10 of the display HIGH for 1 digit
  */
 
  //2 Left digit
 
  /*digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);//pin 6 of arduino is connected  to pin 5 of the display LOW for 1 digit and HIGH with pin 10 for 2 digits
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH); // pin 11 of arduino is connected to pin 10 of the display HIGH for 1 digit
  */
 
  //3 Left digit
 
  /*digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);//pin 6 of arduino is connected  to pin 5 of the display LOW for 1 digit and HIGH with pin 10 for 2 digits
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW); // pin 11 of arduino is connected to pin 10 of the display HIGH for 1 digit
  */
Aiming for the unreachable !

floresta


firashelou


I give up.

Don


dude thanks a lot for all your time i finally solved it :)

here is the new codes:

Code: [Select]

  void setup(){
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
  }
 
  void displayDigit(int digit){
    if(digit == 0){
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
   
    }else if(digit == 1){
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
   
    }else if(digit == 2){
      //2 Left digit
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, HIGH);
      digitalWrite(10, LOW);
     
    }else if(digit == 3){   
      //3 Left digit
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
   
    }else if(digit == 4){
      //4 Left digit
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
     
    }else if(digit == 5){
      //5 Left digit
      digitalWrite(2, LOW);
      digitalWrite(3, HIGH);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      digitalWrite(10, HIGH);
     
    }else if(digit == 6){
      //6 Left digit
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, HIGH);
     
    }else if(digit == 7){
      //7 Left digit
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
     
    }else if(digit == 8){
      //8 Left digit
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
     
    }else if(digit == 9){
      //9 Left digit
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
     
    }
  }
 
  void loop(){
    digitalWrite(6, HIGH);
    digitalWrite(11, LOW);
    displayDigit(1);
    delay(10);
    digitalWrite(6, LOW);
    digitalWrite(11, HIGH);
    displayDigit(5);
    delay(10);
  }
Aiming for the unreachable !

floresta

Quote
i finally solved it

Not exactly, as you will most likely discover when you attempt to display data that is changing.  At that point you might want to go back and implement all of the steps that I outlined, in the order that I specified.

Don

firashelou


Quote
i finally solved it

Not exactly, as you will most likely discover when you attempt to display data that is changing.  At that point you might want to go back and implement all of the steps that I outlined, in the order that I specified.

Don


actully yes, when it comes to changing data !! :S
Aiming for the unreachable !

pwillard

#20
Jul 12, 2013, 07:50 pm Last Edit: Jul 12, 2013, 07:54 pm by pwillard Reason: 1
You know, I may go overboard with my code comments... but I share my code with other people so I don't like to leave a lot of questions...

My opinion is that your code could have been a bit simpler since all those pin manipulations are boggling to read through. I also still feel there is no need for a library for 7 segment displays.  I also show that it is a good idea to keep parts pin-outs in the comments... so you save time revisiting this code later...

While I know my example here is just a single common anode "digit", perhaps you can get some ideas how to make your code more efficient and readable.

Code: [Select]


//==============================================================================
//  SSSS EEEEE V   V  SSSS EEEEE  GGGG        III  N   N OOOOO
// S     E     V   V S     E     G             I   NN  N O   O
//  SSS  EEEE  V   V  SSS  EEEE  G GGG         I   N N N O   O
//     S E      V V      S E     G   G  ..     I   N  NN O   O
// SSSS  EEEEE   V   SSSS  EEEEE  GGG   ..    III  N   N OOOOO
//==============================================================================

//==============================================================================
// Program:      sevseg.ino
// Author:       Pete Willard
// Version:      1.0
// Target:       UNO R3, IDE 1.03
// Date:         2013/07/12
// Time:         10:34:02
// Notes:        
 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//               Uses Serial I/O
// Reference:
//
// Seven segment LED layout for the  Indicator
// Arduino pins must be sequential
// Common Anode Display
// Part Number HDSP 5082-7730
// Pin1  Cathode A
// Pin2  Cathode F
// Pin3  Common Anode
// Pin4  No Pin
// Pin5  No Pin
// Pin6  Cathode Decimal
// Pin7  Cathode E
// Pin8  Cathode D
// Pin9  No Connection
// Pin10 Cathode C
// Pin11 Cathode G
// Pin12 No Connection
// Pin13 Cathode B
// Pin14 Common Anode

// Arduino Pins 2-8 connect to LED display Cathodes through a
// Bourns 4114R-1-221 DIP Resistor array
// I get these from Mouser.com
// http://www.mouser.com/Search/Refine.aspx?Keyword=4114R-1-221
//==============================================================================

//=====[ GLOBAL VARIABLES ]=====================================================
int LEDstartpin = 2;

byte seven_seg_digits[10][7] = {
// Arduino pin:
//    2,3,4,5,6,7,8    
   { 0,0,0,0,0,0,1 }, // = 0
   { 1,0,0,1,1,1,1 }, // = 1
   { 0,0,1,0,0,1,0 }, // = 2
   { 0,0,0,0,1,1,0 }, // = 3
   { 1,0,0,1,1,0,0 }, // = 4
   { 0,1,0,0,1,0,0 }, // = 5
   { 0,1,0,0,0,0,0 }, // = 6
   { 0,0,0,1,1,1,1 }, // = 7
   { 0,0,0,0,0,0,0 }, // = 8
   { 0,0,0,0,1,0,0 }  // = 9
};
//    a b c d e f g ------> LED segment - Standard LED segment naming
//  Note, these columns *should* line up, but posting formatted *code* is
//  a bit of a gamble.


//=====[ SETUP ]================================================================
void setup() {
//      in case you need debugging
Serial.begin(9600);
Serial.flush();

// Set all pins to Outut Mode
int a;
for(a=0;a < 13;a++){ pinMode(a, OUTPUT);
}

}
//==============================================================================

//=====[ LOOP ]=================================================================
void loop() {
   for ( int ArgVal = 0; ArgVal <10; ArgVal++) {
   
  ArgVal = constrain(ArgVal,0,9);  // in case you MANUALLY pass arguments in the future
  sevenSegWrite(ArgVal);
    Serial.println(ArgVal);
  delay(1000);
  }
  Serial.println("looping...");

}


//=====[ SUBROUTINES ]==========================================================

// Pass the digit value that you want displayed,  IE; 0-9
void sevenSegWrite(byte digit) {
   byte pin = LEDstartpin;
   for (byte segCount = 0; segCount < 7; ++segCount) {
       digitalWrite(pin, seven_seg_digits[digit][segCount]);
       ++pin;
   }
}




firashelou


You know, I may go overboard with my code comments... but I share my code with other people so I don't like to leave a lot of questions...

My opinion is that your code could have been a bit simpler since all those pin manipulations are boggling to read through. I also still feel there is no need for a library for 7 segment displays.  I also show that it is a good idea to keep parts pin-outs in the comments... so you save time revisiting this code later...

While I know my example here is just a single common anode "digit", perhaps you can get some ideas how to make your code more efficient and readable.

Code: [Select]


//==============================================================================
//  SSSS EEEEE V   V  SSSS EEEEE  GGGG        III  N   N OOOOO
// S     E     V   V S     E     G             I   NN  N O   O
//  SSS  EEEE  V   V  SSS  EEEE  G GGG         I   N N N O   O
//     S E      V V      S E     G   G  ..     I   N  NN O   O
// SSSS  EEEEE   V   SSSS  EEEEE  GGG   ..    III  N   N OOOOO
//==============================================================================

//==============================================================================
// Program:      sevseg.ino
// Author:       Pete Willard
// Version:      1.0
// Target:       UNO R3, IDE 1.03
// Date:         2013/07/12
// Time:         10:34:02
// Notes:        
 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//               Uses Serial I/O
// Reference:
//
// Seven segment LED layout for the  Indicator
// Arduino pins must be sequential
// Common Anode Display
// Part Number HDSP 5082-7730
// Pin1  Cathode A
// Pin2  Cathode F
// Pin3  Common Anode
// Pin4  No Pin
// Pin5  No Pin
// Pin6  Cathode Decimal
// Pin7  Cathode E
// Pin8  Cathode D
// Pin9  No Connection
// Pin10 Cathode C
// Pin11 Cathode G
// Pin12 No Connection
// Pin13 Cathode B
// Pin14 Common Anode

// Arduino Pins 2-8 connect to LED display Cathodes through a
// Bourns 4114R-1-221 DIP Resistor array
// I get these from Mouser.com
// http://www.mouser.com/Search/Refine.aspx?Keyword=4114R-1-221
//==============================================================================

//=====[ GLOBAL VARIABLES ]=====================================================
int LEDstartpin = 2;

byte seven_seg_digits[10][7] = {
// Arduino pin:
//    2,3,4,5,6,7,8    
   { 0,0,0,0,0,0,1 }, // = 0
   { 1,0,0,1,1,1,1 }, // = 1
   { 0,0,1,0,0,1,0 }, // = 2
   { 0,0,0,0,1,1,0 }, // = 3
   { 1,0,0,1,1,0,0 }, // = 4
   { 0,1,0,0,1,0,0 }, // = 5
   { 0,1,0,0,0,0,0 }, // = 6
   { 0,0,0,1,1,1,1 }, // = 7
   { 0,0,0,0,0,0,0 }, // = 8
   { 0,0,0,0,1,0,0 }  // = 9
};
//    a b c d e f g ------> LED segment - Standard LED segment naming
//  Note, these columns *should* line up, but posting formatted *code* is
//  a bit of a gamble.


//=====[ SETUP ]================================================================
void setup() {
//      in case you need debugging
Serial.begin(9600);
Serial.flush();

// Set all pins to Outut Mode
int a;
for(a=0;a < 13;a++){ pinMode(a, OUTPUT);
}

}
//==============================================================================

//=====[ LOOP ]=================================================================
void loop() {
   for ( int ArgVal = 0; ArgVal <10; ArgVal++) {
   
  ArgVal = constrain(ArgVal,0,9);  // in case you MANUALLY pass arguments in the future
  sevenSegWrite(ArgVal);
    Serial.println(ArgVal);
  delay(1000);
  }
  Serial.println("looping...");

}


//=====[ SUBROUTINES ]==========================================================

// Pass the digit value that you want displayed,  IE; 0-9
void sevenSegWrite(byte digit) {
   byte pin = LEDstartpin;
   for (byte segCount = 0; segCount < 7; ++segCount) {
       digitalWrite(pin, seven_seg_digits[digit][segCount]);
       ++pin;
   }
}






nice simple code but i have a question maybe few but gonna start by this :
seven_seg_digits[10][7] does this mean 2 arrays, one is 10 and the second is 7 length ?
Aiming for the unreachable !

pwillard

#22
Jul 12, 2013, 08:38 pm Last Edit: Jul 12, 2013, 08:40 pm by pwillard Reason: 1
Yes, a multidimensional array.

Each row in the array defines which LED is ON/OFF (columns) for a particular digit.

IE; if you want a 1 to display on the LED, you read the SECOND row (which is 1 since we start at zero) in the array and assign each of the 7 array values to the sequential Arduino pins.  Compare the comment at the top of the array (arduono pins) to the bottom (LED PINS)

By using sequential pins on arduino, we can process the array by increment PINS, as shown in the subroutine... sevenSegWrite()

All done to minimize how much coding is needed... because I'm basically lazy.

firashelou


Yes, a multidimensional array.

Each row in the array defines which LED is ON/OFF (columns) for a particular digit.

IE; if you want a 1 to display on the LED, you read the SECOND row (which is 1 since we start at zero) in the array and assign each of the 7 array values to the sequential Arduino pins.  Compare the comment at the top of the array (arduono pins) to the bottom (LED PINS)

By using sequential pins on arduino, we can process the array by increment PINS, as shown in the subroutine... sevenSegWrite()

All done to minimize how much coding is needed... because I'm basically lazy.


it is not easy at all :S this probably will take half of the project time to program 4 displays of 2 digit each and continouslly which means 3 numbers on different displays !!
Aiming for the unreachable !

Go Up