Go Down

Topic: Arduino Mega with double 7 segment display (Read 7496 times) previous topic - next topic

PaulRB


Rovers

#31
Feb 25, 2016, 07:42 pm Last Edit: Feb 25, 2016, 07:48 pm by Rovers
A shield cannot run a program...
Ops... And that is why I am a newbie...
I guess let me know how it goes with your Nano 3.

PaulRB

#32
Feb 25, 2016, 11:40 pm Last Edit: Feb 29, 2016, 12:43 pm by PaulRB
OK, I fixed it. I made a couple of errors in the code and did not spot them even though I read through it several times. Please accept my apologies!

Here's the working version:

Code: [Select]
#define DIGITS 2

const byte segmentPin[DIGITS][7] = {
  {15,14,13,12,11,10,9},
  {8,7,6,5,4,3,2}
};

const byte digitPattern[10] = {
  0b1111110,
  0b0110000,
  0b1101101,
  0b1111001,
  0b0110011,
  0b1011011,
  0b1011111,
  0b1110000,
  0b1111111,
  0b1111011
};

void setup() {
  //set all segment pins as outputs
  for (byte digit = 0; digit < DIGITS; digit++) {
    for (byte segment = 0; segment < 7; segment++) {
      pinMode(segmentPin[digit][segment], OUTPUT);
    }
  }
}

void displayNumber(int value) {

  //Set all digits to show the required value
  for (byte digit = 0; digit < DIGITS; digit++) {

    //Calculate the digit
    byte thisDigit = value % 10;
    //look up the segment pattern needed to display the digit
    byte thisPattern = digitPattern[thisDigit];
    //Calculate remaining digits of the number to be displayed
    value /= 10;

    //Set the segments for this digit
    for (byte segment = 0; segment < 7; segment++) {
      digitalWrite(segmentPin[digit][segment], bitRead(thisPattern, segment));
    }
  }
}

void loop() {

  //Show all values from 00 to 99 in turn for 1 second each
  for (int value = 0; value <= 99; value++) {
    displayNumber(value);
    delay(1000);
  }
}



Rovers

Oh you beat me! I found the same mistake and was just about to post the result, I just had to change the <= to < everywhere.

This is the code I have working right now, don't mind the different pin numbers, I changed just to they were more easly accesible while swapping between boards.
Code: [Select]
#define DIGITS 2

const byte segmentPin[DIGITS][7] = {
  {34,30,26,22,20,18,16},
  {14,12,10,8,6,4,2}
};

const byte digitPattern[10] = {
  0b1111110,
  0b0110000,
  0b1101101,
  0b1111001,
  0b0110011,
  0b1011011,
  0b1011111,
  0b1110000,
  0b1111111,
  0b1111011
};

void setup() {
  //set all segment pins as outputs
  for (byte digit=0; digit<DIGITS; digit++) {
    for (byte segment=0; segment<7; segment++) {
      pinMode(segmentPin[digit][segment], OUTPUT);
    }
  }
}

void loop() {
 
  //Show all values from 00 to 99 in turn for 1 second each
  for (int value=0; value<99; value++) {

    //take a copy of the value to be displayed
    int tempValue = value;

    //Set all digits to show the required value
    for (byte digit=0; digit<DIGITS; digit++) {

      //Calculate the digit
      byte thisDigit = tempValue % 10;
      //look up the segment pattern needed to display the digit
      byte thisPattern = digitPattern[thisDigit];
      //Calculate remaining digits of the number to be displayed
      tempValue /= 10;

      //Set the segments for this digit
      for (byte segment=0; segment<7; segment++) {
        digitalWrite(segmentPin[digit][segment], bitRead(thisPattern, segment));
      }
    }
    delay(500);
  }
}


I guess the next step is connect the second double display and make them both count...

PaulRB

As a newbie  you did very well to spot those mistakes!

The code i just posted has a small advantage over your code above. It adresses a question you asked a few posts ago.

Be careful about the currents if you attach more digits. The rules that Crossroads posted earlier are in terms of AVR port & pin numbers rather than Arduino pin numbers. this diagram should help you figure out the relationship.

What current is drawn by your two digits now? Put your multimeter where you drew a switch on your last diagram and measure the current. It should peak at "88".

Rovers

Right, I don't have a multimeter but I can see where the 88 comes from. I am going to attach another double digit display but this time with 330+220R just to play it safe. In this way a display should pull 88mA and the other, 74-75mA (no decimal point used), total 162. The important thing is to stay under 200mA correct? I know one display will be brigther than the other but at this moment I don't really mind, when I'll solder them to a board then I'll make sure I have the same resistance on each segment.


PaulRB

I don't have a multimeter
You should definitely get one. Just a £10 one will be fine.
display should pull 88mA and the other, 74-75mA (no decimal point used), total 162. The important thing is to stay under 200mA correct?
Not sure where you get 88mA from, i make it (5-2.1)/330*14=123mA for one and (5-2.1)/(330+220)*14=74mA for the other.

Its not quite as simple as 200mA limit. It depends which pins you are using. On Mega you can get away with using 330R for both displays as long as you spread the current over the right ports & pins. Have another look at Crossroads' post and the diagram i gave you the link to.

Rovers

#37
Feb 28, 2016, 02:52 am Last Edit: Feb 28, 2016, 03:05 am by Rovers
Right so, if I understood correctly, as long as I avoid the first 5 digital pin (so starting from digital 6), and from there plug in all my segment all the way up to pin 34, this sum can NOT exceed 200mA (since they are all "green, red and yellow" kind. This if the 200mA limit is not set for each of these colours so all the green can't exceed 200mA, all the red can't exceed 200mA and yellow the same. So my choices are:

- Either using higher resistance resistor (from around 500R) or setting 2 resistor in series for each segment.
-Otherwise would it matter if I split the GNDs of the 2 display into 2 different GNDs pin on the board? like the ones next to pin 13 and pin 52?

I know this is absolute basics of elettronics but I just wanna be sure I don't fry my board.

Thanks for all your help and yes I will get a multimeter as soon as possible!

CrossRoads

"The important thing is to stay under 200mA correct? "
200ma for the  groups of pins outlined earlier, yes. 800mA total.
If powering from USB, then 500mA total.
If powering from the barrel jack, then 800mA total.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Paul__B


PaulRB

#40
Feb 28, 2016, 08:07 am Last Edit: Feb 28, 2016, 08:22 am by PaulRB
I think you should go for the ~500R series resistors and the pins you suggested. This should keep it under 165mA in total. The two grounds thing is probably irrelavent because they will be connected together on the Mega's pcb anyway, but go ahead with it if you feel safer.

However, you need to avoid using the pins that your ethernet shield uses. You didn't say exactly which type of ethernet shield you have. The current version of the standard shield says:
Quote
Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header). This is on digital pins 10, 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. These pins cannot be used for general I/O. On the Mega, the hardware SS pin, 53, is not used to select either the W5100 or the SD card, but it must be kept as an output or the SPI interface won't work.
So you should not use pins 50 to 53, 4 or 10 for your display segments.

Rovers

Right, I'll give it a go tomorrow.
Just for the record this is the Ethernet Shield R3 I've got.
If I am not wrong this should also support POE right?

CrossRoads

Really.  Not with Vin = 12V for sure, but 7.5V so the regulator doesn't over heat, and the Mega can handle 800mA: 200mA/Vcc pin, there are 4 Vcc pins.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

PaulRB

#43
Feb 29, 2016, 09:02 am Last Edit: Feb 29, 2016, 09:14 am by PaulRB
Right, I'll give it a go tomorrow.
Just for the record this is the Ethernet Shield R3 I've got.
If I am not wrong this should also support POE right?


Yes, i seem to remember that vertical board running at an angle is the poe circuit. Here is the page link. But what voltage and current can your poe switch or whatever provide? And what current can the shield provide to the mega and at 9V? How much current at 5V can the mega's regulator provide with a 9V input, without overheating?

Paul__B

You mean they have the POE board supplying 9V instead of 5 V regulated? :smiley-eek:

Incredible!

Go Up