Go Down

Topic: 7 Segment Library problem (Read 5 times) previous topic - next topic

raghvmahashabde

i wanted to make a seven segment library so that there is no hotch-potch in the schematic.
the problem is the library is not working even in its initial stage.
please tell the bugs.
Here is the header file
Code: [Select]
/*
  SevenSeg.h - Library for using seven segment.
  Created by Raghvendra Mahashabde, September 23, 2012.
  Released into the public domain.
*/
#ifndef SevenSeg_h
#define SevenSeg_h

#include "Arduino.h"

class SevenSeg
{
  public:
    vaid begin(int pin1,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8);
    void displayNum(int input);
    void type(int common);
    void writeDot(byte dot);
  private:
    int pinList[8]={pin1,pin2,pin3,pin4,pin5,pin6,pin7,pin8};
    int _pin;
    int printPin;
    void sevenSegWrite(byte digit);
    void writeDot(byte dot);
    byte Hp;
    byte Lp;
    byte segCount
    byte seven_seg_digits[10][7] = { { Hp,Hp,Hp,Hp,Hp,Hp,Lp },  // = 0
                                       { Lp,Hp,Hp,Lp,Lp,Lp,Lp },  // = 1
                                                           { Hp,Hp,Lp,Hp,Hp,Lp,Hp},  // = 2
                                                           { Hp,Hp,Hp,Hp,Lp,Lp,Hp },  // = 3
                                                           { Lp,Hp,Hp,Lp,Lp,Hp,Hp },  // = 4
                                                           { Hp,Lp,Hp,Hp,Lp,Hp,Hp},  // = 5
                                                           { Hp,1,Hp,Hp,Hp,Hp,Hp },  // = 6
                                                           { Hp,Hp,Hp,Lp,Lp,Lp,Lp },  // = 7
                                                           { Hp,Hp,Hp,Hp,Hp,Hp,Hp },  // = 8
                                                           { Hp,Hp,Hp,Hp,Lp,Hp,Hp }   // = 9
};
};

#endif

here is the c-file
Code: [Select]

#include "Arduino.h"
#include "SevenSeg.h"
void SevenSeg::begin(int pin1,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8)
{
pinMode(pinList[0],  OUTPUT); 
pinMode(pinList[1],  OUTPUT);
pinMode(pinList[2],  OUTPUT);
pinMode(pinList[3],  OUTPUT);
pinMode(pinList[4],  OUTPUT);
pinMode(pinList[5],  OUTPUT);
pinMode(pinList[6],  OUTPUT);
pinMode(pinList[7],  OUTPUT);
digitalWrite(pinList[0], Lp);
digitalWrite(pinList[1], Lp);
digitalWrite(pinList[2], Lp);
digitalWrite(pinList[3], Lp);
digitalWrite(pinList[4], Lp);
digitalWrite(pinList[5], Lp);
digitalWrite(pinList[6], Lp);
digitalWrite(pinList[7], Lp);

}

void SevenSeg::writeDot(byte dot) {
  digitalWrite(9, dot);
}
   
void sevenSegWrite(byte digit) {
  int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinList[printPin], seven_seg_digits[digit][segCount]);
    ++printPin;
  }
}
void type(int common)
{
if(common==0)
{
byte Hp=1;
byte Lp=0;
}
else if(common==1)
{
byte Hp=0;
byte Lp=1;
}
}
void SevenSeg::displayNum(int input)
{
  if(input==0)
  {
    sevenSegWrite(0);
  }
  if(input==1)
  {
    sevenSegWrite(1);
  }
  if(input==2)
  {
    sevenSegWrite(2);
  }
  if(input==3)
  {
    sevenSegWrite(3);
  }
  if(input==4)
  {
    sevenSegWrite(4);
  }
  if(input==5)
  {
    sevenSegWrite(5);
  }
  if(input==6)
  {
    sevenSegWrite(6);
  }
  if(input==7)
  {
    sevenSegWrite(7);
  }
  if(input==8)
  {
    sevenSegWrite(8);
  }
  if(input==9)
  {
    sevenSegWrite(9);
  }
}


the working code is here
Code: [Select]
// Arduino 7 segment display example software
// http://www.hacktronics.com/Tutorials/arduino-and-7-segment-led.html
// License: http://www.opensource.org/licenses/mit-license.php (Go crazy)



// Define the LED digit patters, from 0 - 9
// Note that these patterns are for common cathode displays
// For common anode displays, change the 1's to 0's and 0's to 1's
// 1 = LED on, 0 = LED off, in this order:

//                                    Arduino pin: 2,3,4,5,6,7,8
byte seven_seg_digits[10][7] = { { 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
                                                           };
                                                           
int e1=14,e2=15;                                                           

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(e1, OUTPUT);
  pinMode(e2, OUTPUT);
  writeDot(1);  // start with the "dot" off
}

void writeDot(byte dot) {
  digitalWrite(9, dot);
}
   
void sevenSegWrite(byte digit) {
  byte pin = 2;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pin, seven_seg_digits[digit][segCount]);
    ++pin;
  }
}

void loop() {
for(int number=0; number<100; number++)
  {
  int U,T;
  U=number%10;
  T=(number/10)%10;
  for(int i=0;i<125;i++)
  {
  digitalWrite(e1, 1);
  digitalWrite(e2, 0);
  segase(U);
  writeDot(1);
  delay(2);
  digitalWrite(e1, 0);
  digitalWrite(e2, 1);
  segase(T);
  writeDot(0);
  delay(2);
  }
  }
}

void segase(int input)
{
  if(input==0)
  {
    sevenSegWrite(0);
  }
  if(input==1)
  {
    sevenSegWrite(1);
  }
  if(input==2)
  {
    sevenSegWrite(2);
  }
  if(input==3)
  {
    sevenSegWrite(3);
  }
  if(input==4)
  {
    sevenSegWrite(4);
  }
  if(input==5)
  {
    sevenSegWrite(5);
  }
  if(input==6)
  {
    sevenSegWrite(6);
  }
  if(input==7)
  {
    sevenSegWrite(7);
  }
  if(input==8)
  {
    sevenSegWrite(8);
  }
  if(input==9)
  {
    sevenSegWrite(9);
  }
}

PaulS

Quote
the problem is the library is not working even in its initial stage.

You have the hardware. You have some expectations about what the software will do. You have the visual feedback on what that software actually does. You know what the differences are between your expectations and your reality are.

And, yet, you'd like for us to help you debug the library.

I think it is just possible that you might need to provide some more details.


raghvmahashabde

does anyone know of an externel compiler???
arduino is not compiling the library properly and says there are various errors.
here is the edited SevenSeg.cpp
Code: [Select]
int pin1,pin2,pin3,pin4,pin5,pin6,pin7,pin8;
#include "Arduino.h"
#include "SevenSeg.h"
byte pinListin[8]={pin1,pin2,pin3,pin4,pin5,pin6,pin7,pin8};
void SevenSeg::begin(int pin1,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8)
{
pinMode(pinListin[0],  OUTPUT); 
pinMode(pinListin[1],  OUTPUT);
pinMode(pinListin[2],  OUTPUT);
pinMode(pinListin[3],  OUTPUT);
pinMode(pinListin[4],  OUTPUT);
pinMode(pinListin[5],  OUTPUT);
pinMode(pinListin[6],  OUTPUT);
pinMode(pinListin[7],  OUTPUT);
digitalWrite(pinListin[0], Lp);
digitalWrite(pinListin[1], Lp);
digitalWrite(pinListin[2], Lp);
digitalWrite(pinListin[3], Lp);
digitalWrite(pinListin[4], Lp);
digitalWrite(pinListin[5], Lp);
digitalWrite(pinListin[6], Lp);
digitalWrite(pinListin[7], Lp);
}

void SevenSeg::writeDot(byte dot) {
  digitalWrite(pinListin[7], dot);
}
byte seven_seg_digits[10][7] = { { Hp,Hp,Hp,Hp,Hp,Hp,Lp },  // = 0
                                    { Lp,Hp,Hp,Lp,Lp,Lp,Lp },  // = 1
                                    { Hp,Hp,Lp,Hp,Hp,Lp,Hp},  // = 2
                                    { Hp,Hp,Hp,Hp,Lp,Lp,Hp },  // = 3
                                    { Lp,Hp,Hp,Lp,Lp,Hp,Hp },  // = 4
                                    { Hp,Lp,Hp,Hp,Lp,Hp,Hp},  // = 5
                                    { Hp,1,Hp,Hp,Hp,Hp,Hp },  // = 6
                                    { Hp,Hp,Hp,Lp,Lp,Lp,Lp },  // = 7
                                    { Hp,Hp,Hp,Hp,Hp,Hp,Hp },  // = 8
                                    { Hp,Hp,Hp,Hp,Lp,Hp,Hp }   // = 9
};
void SevenSeg::type(int common)
{
if(common==0)
{
byte Hp=1;
byte Lp=0;
}
else if(common==1)
{
byte Hp=0;
byte Lp=1;
}
}
void SevenSeg::displayNum(int input)
{
  if(input==0)
  {
    int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[0][segCount]);
    ++printPin;
  }
  if(input==1)
  {
    int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[1][segCount]);
    ++printPin;
  }
  if(input==2)
  {
   int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[2][segCount]);
    ++printPin;
  }
  if(input==3)
  {
   int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[3][segCount]);
    ++printPin;
  }
  if(input==4)
  {
    int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[4][segCount]);
    ++printPin;
  }
  if(input==5)
  {
    int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[5][segCount]);
    ++printPin;
  }
  if(input==6)
  {
    int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[6][segCount]);
    ++printPin;
  }
  if(input==7)
  {
    int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[7][segCount]);
    ++printPin;
  }
  if(input==8)
  {
    int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[8][segCount]);
    ++printPin;
  }
  if(input==9)
  {
    int printPin = 0;
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pinListin[printPin], seven_seg_digits[9][segCount]);
    ++printPin;
  }
}

and here is the edited header file
Code: [Select]
/*
  SevenSeg.h - Library for using seven segment.
  Created by Raghvendra Mahashabde, September 23, 2012.
  Released into the public domain.
*/
#ifndef SevenSeg_h
#define SevenSeg_h

#include "Arduino.h"

class SevenSeg
{
  public:
    void begin(int pin1,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8);
    void displayNum(int input);
    void type(int common);
    void writeDot(byte dot);
  private:
    byte pinListin;
    int printPin;
    byte Hp;
    byte Lp;
    byte segCount;
    byte seven_seg_digits;
};

#endif

PaulS

Quote
does anyone know of an externel compiler???
arduino is not compiling the library properly and says there are various errors.

Then, fix them. Any other compiler is going to tell you the same things.

Code: [Select]
byte pinListin[8]={pin1,pin2,pin3,pin4,pin5,pin6,pin7,pin8};
Exactly what values did you just store in this array? What values are in pin1, pin2, etc.? Since they appear to be global variables, they are all initialized to 0.

Code: [Select]
void SevenSeg::begin(int pin1,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8)
{
pinMode(pinListin[0],  OUTPUT); 
pinMode(pinListin[1],  OUTPUT);
pinMode(pinListin[2],  OUTPUT);
pinMode(pinListin[3],  OUTPUT);
pinMode(pinListin[4],  OUTPUT);
pinMode(pinListin[5],  OUTPUT);
pinMode(pinListin[6],  OUTPUT);
pinMode(pinListin[7],  OUTPUT);

You pass a list of pins to the function, then set some other pins. Why? Why bother passing pins to the function if you aren't going to use them?

Code: [Select]
void SevenSeg::writeDot(byte dot) {
  digitalWrite(pinListin[7], dot);
}

Writing to the serial port pin is rarely a good idea.

Code: [Select]
byte seven_seg_digits[10][7] = { { Hp,Hp,Hp,Hp,Hp,Hp,Lp },  // = 0
                                    { Lp,Hp,Hp,Lp,Lp,Lp,Lp },  // = 1
                                    { Hp,Hp,Lp,Hp,Hp,Lp,Hp},  // = 2
                                    { Hp,Hp,Hp,Hp,Lp,Lp,Hp },  // = 3
                                    { Lp,Hp,Hp,Lp,Lp,Hp,Hp },  // = 4
                                    { Hp,Lp,Hp,Hp,Lp,Hp,Hp},  // = 5
                                    { Hp,1,Hp,Hp,Hp,Hp,Hp },  // = 6
                                    { Hp,Hp,Hp,Lp,Lp,Lp,Lp },  // = 7
                                    { Hp,Hp,Hp,Hp,Hp,Hp,Hp },  // = 8
                                    { Hp,Hp,Hp,Hp,Lp,Hp,Hp }   // = 9
};

Where have you assigned values to Hp and Lp? The values are class members. The array is not. You can't use class members this way.

The displayNum() function is atrocious. The only difference I see between the blocks of code is that the index into the seven_seg_digits array changes, to be whatever input was. Just use input in place of the hardcoded value, and get rid of 90% of your code.

A for statement can declare and operate on multiple variables.
Code: [Select]
for(byte printPin = 0, seqCount = 0; seqCount < 7; seqCount++, printPin++)
{
}

Although why you need two values that always contain the same value is a mystery.

Finally, you have not said what the compiler is complaining about, so its hard to really help you.

Go Up