Pages: 1 [2]   Go Down
Author Topic: 7 Segment Library for Arduino 1.0  (Read 2158 times)
0 Members and 1 Guest are viewing this topic.
Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you know how to use 2 7-segment displays?

Yes, why do you ask?
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you're 11 huh?
Logged

Camaçari
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i belive you can't make a funcion with the same name over other funcion of arduino like setup(); change the funcion name
Logged

smiley-grin

Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now, the library is working, and I would like to add another function to this.  The new library will be named Pineapple, and will use a shift register to drive the 7 segment led.  I have made the new .h file, and the .cpp file, and example code to work.  This library uses the shifter library from bildr, also.  So I need the shifter library to work in the Pineapple library.  I do not know how to initiate the shifter library in my .cpp file or .h file.  This is the current .h, .cpp, and example code for my library.

.cpp file
Code:
#include "Arduino.h"
#include "Pineapple.h"
#include "Shifter.h"

uint8_t Pineapple::pins(int a, int b, int c, int d, int e, int f, int g, int dp, boolean common) {
   
    _a=a;
    _b=b;
    _c=c;
    _d=d;
    _e=e;
    _f=f;
    _g=g;
    _dp=dp;
    _common=common;
   
    segmentPins[0] = _dp;
    segmentPins[1] = _g;
    segmentPins[2] = _f;
    segmentPins[3] = _e;
    segmentPins[4] = _d;
    segmentPins[5] = _c;
    segmentPins[6] = _b;
    segmentPins[7] = _a;
   
    for(int i=0; i < 8; i++) {
       
        pinMode(segmentPins[i], OUTPUT);
    }
   
   
}

void Pineapple::write(int number) {
    boolean isBitSet;
   
    numeral[0] = B11111100;  // 0
    numeral[1] = B01100000;  // 1
    numeral[2] = B11011010;  // 2
    numeral[3] = B11110010;  // 3
    numeral[4] = B01100110;  // 4
    numeral[5] = B10110110;  // 5
    numeral[6] = B10111110;  // 6
    numeral[7] = B11100000;  // 7
    numeral[8] = B11111110;  // 8
    numeral[9] = B11100110;  // 9
   
    if (_common == HIGH) {
       
        for(int segment=1; segment < 8; segment++) {
            if(number < 0 || number > 9) {
                isBitSet = 0;
            }else{
                isBitSet = bitRead(numeral[number], segment);
            }
            isBitSet = ! isBitSet;
            shifter.setPin(segmentPins[segment], isBitSet);
            shifter.write();
        }
    }else{
        for(int segment=1; segment < 8; segment++) {
            if(number < 0 || number > 9) {
                isBitSet = 0;
            }else{
                isBitSet = bitRead(numeral[number], segment);
            }
            shifter.setPin(segmentPins[segment], isBitSet);
            shifter.write();
        }
    }
    if (_number == '.' && _common == HIGH) {
        shifter.setPin(_dp, LOW);
        shifter.write();
    }
    if (_number == '.' && _common == LOW) {
        shifter.setPin(_dp, HIGH);
        shifter.write();
    }
    if (_common == HIGH) {
        shifter.setPin(_dp, HIGH);
        shifter.write();
    }
    if (_common == LOW) {
        shifter.setPin(_dp, LOW);
        shifter.write();
    }
}

The current .h file.
Code:
#ifndef Pineapple_h
#define Pineapple_h

#include "Arduino.h"

class Pineapple
{
public:
    uint8_t pins(int a, int b, int c, int d, int e, int f, int g, int dp, boolean common);
    void write(int number);
   
private:
    int _a, _b, _c, _d, _e, _f, _g, _dp, _number;
    byte numeral[10];
    int segmentPins[8];
    boolean _common;
};

#endif

The example code.
Code:
#include "Shifter.h"
#include "Pineapple.h"

#define serial 4 //SER_IN
#define registerClock 3 //L_CLOCK
#define serialClock 2 //CLOCK

#define number 1

Shifter shifter(serial, registerClock, serialClock, number);
Pineapple pineapple;

void setup() {
  pineapple.pins(0, 1, 2, 3, 4, 5, 6, 7, HIGH);
}

void loop() {
  pineapple.write(1);
}

The error message I get says the shifter wasn't declared in this scope, and I know that that means I have to initiate the shifter library in the pineapple library.  Any help?
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Remove the leading 'shifter.' wherever used in your .cpp file
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks lloyddean for your reply.  Yes, I'm 11, but I don't post that here because I want people to take me seriously.  For the code, I got a TON of error messages.  It said when I take out all of the shifter.'s, that all of the names with out shifter. are "not declared in this scope".  Here is my current .cpp file. 

Code:
#include "Arduino.h"
#include "Pineapple.h"
#include "Shifter.h"

uint8_t Pineapple::pins(int a, int b, int c, int d, int e, int f, int g, int dp, boolean common) {
   
    _a=a;
    _b=b;
    _c=c;
    _d=d;
    _e=e;
    _f=f;
    _g=g;
    _dp=dp;
    _common=common;
   
    segmentPins[0] = _dp;
    segmentPins[1] = _g;
    segmentPins[2] = _f;
    segmentPins[3] = _e;
    segmentPins[4] = _d;
    segmentPins[5] = _c;
    segmentPins[6] = _b;
    segmentPins[7] = _a;
   
    for(int i=0; i < 8; i++) {
       
        pinMode(segmentPins[i], OUTPUT);
    }
   
   
}

void Pineapple::write(int number) {
    boolean isBitSet;
   
    numeral[0] = B11111100;  // 0
    numeral[1] = B01100000;  // 1
    numeral[2] = B11011010;  // 2
    numeral[3] = B11110010;  // 3
    numeral[4] = B01100110;  // 4
    numeral[5] = B10110110;  // 5
    numeral[6] = B10111110;  // 6
    numeral[7] = B11100000;  // 7
    numeral[8] = B11111110;  // 8
    numeral[9] = B11100110;  // 9
   
    if (_common == HIGH) {
       
        for(int segment=1; segment < 8; segment++) {
            if(number < 0 || number > 9) {
                isBitSet = 0;
            }else{
                isBitSet = bitRead(numeral[number], segment);
            }
            isBitSet = ! isBitSet;
            setPin(segmentPins[segment], isBitSet);
            write();
        }
    }else{
        for(int segment=1; segment < 8; segment++) {
            if(number < 0 || number > 9) {
                isBitSet = 0;
            }else{
                isBitSet = bitRead(numeral[number], segment);
            }
            setPin(segmentPins[segment], isBitSet);
            write();
        }
    }
    if (_number == '.' && _common == HIGH) {
        setPin(_dp, LOW);
        write();
    }
    if (_number == '.' && _common == LOW) {
        setPin(_dp, HIGH);
        write();
    }
    if (_common == HIGH) {
        setPin(_dp, HIGH);
        write();
    }
    if (_common == LOW) {
        setPin(_dp, LOW);
        write();
    }
}
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where is 'setPin'?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

setPin is in about the middle
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

More specific then.  Where is 'setPin' defined.  I see no implementation for it
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would you define setPin and write() if they are already part of the shifter library?
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all my apologies revert back to your original "Pineapple.cpp" as posted above.  I didn't notice you we're using a third party library a gave wrong advice.

All you'll need to do as add a line to your "Pineapple.cpp" file as below letting the compiler know that 'shifter' has been defined outside of the current compilation unit (file) as follows:

Code:
extern Shifter shifter;

uint8_t Pineapple::pins(int a, int b, int c, int d, int e, int f, int g, int dp, boolean common) {
Logged

Pages: 1 [2]   Go Up
Jump to: