Pages: [1]   Go Down
Author Topic: 4-Digit Display Error  (Read 2403 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I bought the following product from SeeedStudio: http://www.seeedstudio.com/wiki/Grove_-_4-Digit_Display
I have some problems with the following code. I don't get why something like tm1637.display(0,0); ends up displaying a 0 at the first digit of the 4-Digit_Display, but something like tm1637.display(1,x1); or tm1637.display(2,inputString[2]); displays only rubbish at the second and third digit.

Please help!!
Thank you for your input!
ygreq

Code:
/*
I started from the example from the link

http://www.arduino.cc/en/Tutorial/SerialEvent

Set to newline.

In this example you have to open the serial window and type 4 digits.
Ex: 1234 and press Enter.
See what happens on the serial monitor and on the 4-digit display 

*/

String inputString = "";         // a string to hold incoming data
char x1 = 0; // char to attribute to 2nd digit of the display

#include "TM1637.h"
#define CLK 2//pins definitions for TM1637 and can be changed to other ports       
#define DIO 3
TM1637 tm1637(CLK,DIO);




void setup() {
  // initialize serial:
  Serial.begin(9600);

  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
}


void loop() {

  if (Serial.available() > 0) {
    // get the new byte:
    char inChar = (char)Serial.read();
   
    // add it to the inputString:
    inputString += inChar;
   
 
    // if the incoming character is a newline,
     if (inChar == '\n') {
     
       Serial.print("string ");
       Serial.println(inputString); //print string
 
      tm1637.display(0,0); // display 0 on the first digit
     
      x1 = inputString[1]; // attach x1 to the second char in the string created
      Serial.print("2nd digit ");
      Serial.println(x1);  // print that second char to check if everything is ok
     
      tm1637.display(1,x1);// display the char attached to x1 on the second digit
                           // which does not work. Why??
      Serial.print("3rd digit ");
      Serial.println(inputString[2]);
      tm1637.display(2,inputString[2]);// display the char attached to 3rd char on
                              // the 3rd digit
                              // which does not work. Why??
       inputString = "";  // delete string
     
    }
  }
}
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you sure you are sending enough chars before \n ?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi tuxduino,

Yes! I am very sure! Actually everything works (send to serial via Serial.print, send to an LCD following the same logic) except sending to that 7-segment module.

I am pretty sure that the answer is somewhat related to tm1637.display(0,inputString[0]); and how it is created in the first place.

Here is a sketch created by the makers of the module. Look at the end of the sketch how tm1637.display (0,ListDisp[0]); is used in that case.
It refers to int8_t ListDisp[4]; from the beginning.

What would be the difference between int8_t ListDisp[4]; and something like String ListDisp[4]; or char ListDisp[4]?

Thank you for looking into this!


Code:
//  Author:Frankie.Chu
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#include "TM1637.h"
#define CLK 2//pins definitions for TM1637 and can be changed to other ports       
#define DIO 3
TM1637 tm1637(CLK,DIO);
void setup()
{
  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
}
void loop()
{
  int8_t NumTab[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};//0~9,A,b,C,d,E,F
  int8_t ListDisp[4];
  unsigned char i = 0;
  unsigned char count = 0;
  delay(150);
  while(1)
  {
    i = count;
    count ++;
    if(count == sizeof(NumTab)) count = 0;
    for(unsigned char BitSelect = 0;BitSelect < 4;BitSelect ++)
    {
      ListDisp[BitSelect] = NumTab[i];
      i ++;
      if(i == sizeof(NumTab)) i = 0;
    }
    tm1637.display(0,ListDisp[0]);
    tm1637.display(1,ListDisp[1]);
    tm1637.display(2,ListDisp[2]);
    tm1637.display(3,ListDisp[3]);
    delay(300);
  }
}
Logged

Valencia, Spain
Online Online
Faraday Member
**
Karma: 142
Posts: 5296
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi tuxduino,

Yes! I am very sure! Actually everything works (send to serial via Serial.print, send to an LCD following the same logic) except sending to that 7-segment module.

I am pretty sure that the answer is somewhat related to tm1637.display(0,inputString[0]); and how it is created in the first place.


I don't think the second parameter of  tm1637.display() is an ASCII char.
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi fungus,

You mean to say that it needs something else? Do you maybe know what that is?

 From what is shown in the loop in the second sketch
Code:
int8_t NumTab[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};//0~9,A,b,C,d,E,F
int8_t ListDisp[4];

it seems it is a char.

Thank you!
ygreq
« Last Edit: October 02, 2012, 02:52:37 am by ygreq » Logged

Valencia, Spain
Online Online
Faraday Member
**
Karma: 142
Posts: 5296
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi fungus,

You mean to say that it needs something else? Do you maybe know what that is?

 From what is shown in the loop in the second sketch
Code:
int8_t NumTab[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};//0~9,A,b,C,d,E,F
int8_t ListDisp[4];

it seems it is a char.

I didn't say it wasn't a char, I said it wasn't ASCII. Looks like they use
the value 0 to display a '0'.

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look carefully at the second sketch you posted. This line is the key one IMHO:

Code:
int8_t NumTab[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};//0~9,A,b,C,d,E,F
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

Yeap. It seems it used 0 to 15 as hex as it ends up printing from 0 to F.

But the key to understanding both:

Code:
int8_t NumTab[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};//0~9,A,b,C,d,E,F
int8_t ListDisp[4];

is really understanding what int8_t stand for. Which I don't have a clue about. smiley-sad I am guessing it is not arduino lingo per se, but C.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int8_t means 8 bit integer

I don't want to sound rude, but how difficult is to google for int8_t ?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No problem. Actually I did google it like this "int8_t arduino". I only found examples that were not that great (I didn't understand anything). At least in the first 20 searches. I googled "int8_t" just now and there are a few info concerning int8_t in C/C++.

I will see if I understand what int8_t means.

Thank you!

PS: What I know of C/C++ comes from learning arduino. So I am trying to avoid as much as possible info that is for c/c++ and stick to proper libraries. (In this case I would say it is a crappy library.) I am not a C/C++ programmer unfortunately.
« Last Edit: October 02, 2012, 04:24:27 am by ygreq » Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I will see if I understand what int8_t means.

As I said, It means 8 bit integer. The _t at the end stands for "type". The reason why sometimes you find int8_t, int16_t, uint32_t instead of char, int, unsigned long, etc. is C data types can be of different size if the code is compiled on different architectures. An "int" might be 16 bit wide on a 8 bit cpu, 32 bit on a 32 one and 32 or 64 bit on a 64 bit cpu. Likewise, "long" might be 128 bit on a core i7 and just 32 bit on a 486. Types like uint8_t, instead, are the same everywhere: 8 bit wide unsigned integer. It helps code portability.
There's a reason C has sizeof() smiley-wink
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, tuxduino!

Your information explains a lot! I will try to get something out of that LED module soon.

In the meantime I chose to use an LCD screen smiley

All the best to you!
ygreq
Logged

Pages: [1]   Go Up
Jump to: