Pages: [1]   Go Down
Author Topic: 7-segment LED Displays  (Read 831 times)
0 Members and 1 Guest are viewing this topic.
Florida
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have recently started programming with the Arduino Mega 2560, and i currently have it hooked up to a seven-segment display. Using the datasheet, I figured out what pins on the display correspond to what part on the display. The input voltage (3.3V) is on what I have labeled as pin #3, and is hooked up to the 3.3V pin on the Arduino, and to trigger any symbol, you have to put negative/ground on the pin corresponding to that symbol. I have the pins on the display individually hooked up to the Arduino, from 6 to 12. When I was programming it, I figured it would be much easier to define multiple output pins on the Arduino as say, a number, so like this: int one = 7, 8; if the pins corresponding to those symbols made the number one, and then use digitalWrite(one, LOW) to activate those pins as ground and for it to display the number 1. To make a long story short, it did not work this way, instead it showed the number 8 (aka all the pins were grounded). I then tried to do the opposite, and set one to high, again to no avail. Is there something that I am missing? I understand that I could just access the pins individually, however it would be much simpler to just define one as being pins 7 and 8 and just write one as high/low instead of defining 7 and 8 separately. An example of my code is below.


================BEGIN CODE================

int toptop = 12; // The very top segment is connected to pin 12 on the Arduino
int lfttop = 11; // The left top segment is pin 11
int lftbtm = 10; // The lower left segment is pin 10
int btmbtm = 9; // The very bottom segment is on pin 9
int rtebtm = 8; // The right bottom segment is on 8
int rtetop = 7; //The upper right segment is on pin 7
int midmid = 6; //The middle segment is on pin 6

void setup()
{
  pinMode(toptop, OUTPUT); //Setting all of them as outputs
  pinMode(lfttop, OUTPUT);
  pinMode(lftbtm, OUTPUT);
  pinMode(btmbtm, OUTPUT);
  pinMode(rtebtm, OUTPUT);
  pinMode(rtetop, OUTPUT);
  pinMode(midmid, OUTPUT);
}

void loop()
{
  int seven = toptop, rtetop, rtebtm; // Note, I have tried using both these ways and using just
  int one = rtebtm + rtetop; // the pin numbers with no success.
 
  digitalWrite(one, LOW);
}

================END CODE================
Logged

~ODYSSEUS~

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50123
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I figured it would be much easier to define multiple output pins on the Arduino as say, a number, so like this: int one = 7, 8; if the pins corresponding to those symbols made the number one, and then use digitalWrite(one, LOW) to activate those pins as ground and for it to display the number 1. To make a long story short, it did not work this way
I'm not surprised.

You need to create a function to display a 0, and a function to display a 1, and a function to display a 2, etc. In each function, you have the correct number of calls to digitalWrite() to manipulate ONE pin at a time.

Google C comma operator to see why your attempt to define seven the way you did failed. seven was actually assigned the value 8. Then, one was assigned the value 8 + 7, or 15. Changing the state of pin 15, of course, has no impact because nothing is connected to that pin.
Logged

Florida
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That actually makes a lot of sense. Thank you for helping me out. Any ideas on how to make those functions?
Logged

~ODYSSEUS~

USA
Offline Offline
Jr. Member
**
Karma: 4
Posts: 92
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://playground.arduino.cc//Main/InterfacingWithHardware#Output

scroll down to 7 segment displays...
Logged


Offline Offline
Sr. Member
****
Karma: 9
Posts: 252
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like... and I'm writing code in the air, but you will get the idea.


Code:
void digitOne() {
allOFF();
digitalWrite(rtetop, HIGH);
digitalWrite(rtebtm, HIGH);
}

Keep going for each number

void allOFF() {
digitalWrite(rtetop, LOW);
//And all the others OFF
}


BTW, try to get some Shift Registers and use them for the digits, it's much easier.
« Last Edit: April 21, 2013, 07:34:32 pm by American2020 » Logged

My 3x3x3 Led Cube

[url=http://arduino.cc/forum/index.php/topic,1642

Offline Offline
Sr. Member
****
Karma: 9
Posts: 252
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

BTW, don't forget, the pins outputs will provide 5V and not 3,3

And in any case, you always have to use a resistor somewhere for this displays.
Logged

My 3x3x3 Led Cube

[url=http://arduino.cc/forum/index.php/topic,1642

Florida
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setup()
{
  pinMode(12, OUTPUT); //Setting all of them as outputs
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
}
void allOFF()
{
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
}

void digitZero()
{
  digitalWrite(6, HIGH);
}

void loop()
{
  allOFF;
  delay(3000);
  digitZero;
  delay(3000);
}

I tried this, again to no avail. What am I doing wrong???
Logged

~ODYSSEUS~

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50123
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is NOT how to call a function:
Code:
  digitZero;

This is:
Code:
  digitZero();
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to setup the hardware of the 7 segments and connect them to a single byte wide port on the Arduino.

This will allow you to then assign bit values to the individual segments and you can output a single byte with a particular sum of bits in order to turn on the segments for a given character.

Here is a link to an article on bit wise port manipulation:
http://www.arduino.cc/en/Reference/PortManipulation

I am an experienced hardware programmer and new to the Arduino.  This is how I would do it in assembler or in another language.

Good Luck,

Peter

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50123
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You need to setup the hardware of the 7 segments and connect them to a single byte wide port on the Arduino.
Great idea, except that not all Arduinos have a 8 bit port that does not include the serial pins.
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 252
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setup()
{
  pinMode(12, OUTPUT); //Setting all of them as outputs
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
}
void allOFF()
{
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
}

void digitZero()
{
  digitalWrite(6, HIGH);
}

void loop()
{
  allOFF;
  delay(3000);
  digitZero;
  delay(3000);
}

I tried this, again to no avail. What am I doing wrong???


When you calling a function you wrote, call them like

allOFF();

digitZero();

And remember, digitZero, wouldn't be only digitalWrite(6, HIGH), but all the segments that makes the Zero.

If you happen to use ShiftRegisters, this is how the code would look...

Code:

const int latchPin = 5;  // Pin connected to Pin 12 of 74HC595 (Latch)
const int dataPin  = 6;  // Pin connected to Pin 14 of 74HC595 (Data)
const int clockPin = 7;  // Pin connected to Pin 11 of 74HC595 (Clock)

// Describe each digit in terms of display segments
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
const byte numbers[16] = {
                    0b11111100,
                    0b01100000,
                    0b11011010,
                    0b11110010,
                    0b01100110,
                    0b10110110,
                    0b10111110,
                    0b11100000,
                    0b11111110,
                    0b11100110,
                    0b11101110,
                    0b00111110,
                    0b10011100,
                    0b01111010,
                    0b10011110,
                    0b10001110
};


void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  // count from 0 to 255 and display the number
  // on the LEDs
  for (int numberToDisplay = 0; numberToDisplay < 16; numberToDisplay++) {
    // take the latchPin low so
    // the LEDs don't change while you're sending in bits:
    digitalWrite(latchPin, LOW);
    // shift out the bits:
   
//    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay); 
   
    shiftOut(dataPin, clockPin, LSBFIRST, numbers[numberToDisplay]); 
   
    //take the latch pin high so the LEDs will light up:
    digitalWrite(latchPin, HIGH);
    // pause before next value:
    delay(1000);
  }
}

And you can also practice using "SWITCH CASE"

Make only 1 function, and you send the value when you call it...

turnDigit(5);

and when you write the funcion, you receive that value and use it in a switch-case to see what combo of leds you have to turn on.

Try to find some library, for sure there is something already wrote by someone.

Code:
const int latchPin = 8;  // Pin connected to Pin 12 of 74HC595 (Latch)
const int dataPin  = 11;  // Pin connected to Pin 14 of 74HC595 (Data)
const int clockPin = 12;  // Pin connected to Pin 11 of 74HC595 (Clock)

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  // count from 0 to 255 and display the number
  // on the LEDs
  for (int i = 0; i < 10; i++) {

    displayNumber(i);

    delay(1000);
  }
 
  displayNumber(0);

  delay(1000);
 
  for (int i = 9; i > 0; i--) {

    displayNumber(i);

    delay(1000);
  }
}

void displayNumber(int value) {
  switch (value) {
    case 0:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11111100);
      digitalWrite(latchPin, HIGH);
      break;
    case 1:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b01100000);
      digitalWrite(latchPin, HIGH);
      break;
    case 2:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11011010);
      digitalWrite(latchPin, HIGH);
      break;
    case 3:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11110010);
      digitalWrite(latchPin, HIGH);
      break;
    case 4:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b01100110);
      digitalWrite(latchPin, HIGH);
      break;
    case 5:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b10110110);
      digitalWrite(latchPin, HIGH);
      break;
    case 6:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b10111110);
      digitalWrite(latchPin, HIGH);
      break;
    case 7:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11100000);
      digitalWrite(latchPin, HIGH);
      break;
    case 8:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11111110);
      digitalWrite(latchPin, HIGH);
      break;
    case 9:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b11100110);
      digitalWrite(latchPin, HIGH);
      break;
    case '.':
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b00000001);
      digitalWrite(latchPin, HIGH);
      break;
    case 'OFF':
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b00000000);
      digitalWrite(latchPin, HIGH);
      break;
    default:
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 0b00000000);
      digitalWrite(latchPin, HIGH);
      break;
  }
}
Logged

My 3x3x3 Led Cube

[url=http://arduino.cc/forum/index.php/topic,1642

Pages: [1]   Go Up
Jump to: