Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Data types in Arduino Mega2560

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
817
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,492
Please let me know that I want to display value "345632" using Arduino Mega2560 with 6 digits 7 segment display.

I programmed successfully but it cannot display value greater than 65535.

I used int type to store value and tried long type but no effect.

Which data type I use for store "345632" this value?
 

The size of the built-in data types is as much determined by the specific compiler as the underlying platform or architecture of the target.

Are you using the Arduino compiler/IDE to program the device?

If so, the Arduino reference states the long and unsigned long are both 32-bit or 4 bytes in length, which should provide a range of -2,147,483,648 to 2,147,483,647 and 0 to 4,294,967,295, respectfully, more than enough to store a six digit number.

Perhaps, there is some other underlying issue with your code implementation.

Please post your code, so the specific method to which the data type in question is utilized, can be determined.

The issue maybe located in your routine to factor out each digit, rather than data type storing the number.

BigDog
 

Please see the code. Sorry for uncommented Code.

Code:
#include <LiquidCrystal.h>
#include <LedControl.h>
#include <Keypad.h>
#ilude <stdio.h>
byte inc=0;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char store[20];
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {25, 24, 23, 22}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {28, 27, 26}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
 
LedControl lc=LedControl(50,51,53,2);
LiquidCrystal lcd(35, 34, 33, 32, 31, 30);

void setup() {
   /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  for(byte intro=0 ; intro<=1; intro++)
  {
  lc.shutdown(intro,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(intro,8);
  /* and clear the display */
  lc.clearDisplay(intro);
  }
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4); 
  
}

void MAX(char val[],byte addr)
{
   unsigned long temp_val = atoi(val);  
   
   if(temp_val>=0 && temp_val<=9)
   {          
   lc.setChar(addr,0,(byte)temp_val,false); 
   }
   else if(temp_val>=10 && temp_val<=99)
   {
   int one,ten;  
   one = temp_val%10;
   ten = (temp_val/10)%10;
   lc.setChar(addr,0,(byte)ten,false); 
   lc.setChar(addr,1,(byte)one,false); 
   }
   else if(temp_val>=100 && temp_val<=999)
   {
   int one,ten,hundred;
   one = temp_val%10;
   ten = (temp_val/10)%10;
   hundred = (temp_val/100)%10; 
   lc.setChar(addr,0,(byte)hundred,false); 
   lc.setChar(addr,1,(byte)ten,false); 
   lc.setChar(addr,2,(byte)one,false);   
   }
   else if(temp_val>=100 && temp_val<=999)
   {
   int one,ten,hundred;
   one = temp_val%10;
   ten = (temp_val/10)%10;
   hundred = (temp_val/100)%10; 
   lc.setChar(addr,0,(byte)hundred,false); 
   lc.setChar(addr,1,(byte)ten,false); 
   lc.setChar(addr,2,(byte)one,false);   
   }
   else if(temp_val>=1000 && temp_val<=9999)
   {
   int one,ten,hundred,thousand;
   one = temp_val%10;
   ten = (temp_val/10)%10;
   hundred = (temp_val/100)%10; 
   thousand = (temp_val/1000)%10; 
   lc.setChar(addr,0,(byte)thousand,false); 
   lc.setChar(addr,1,(byte)hundred,false); 
   lc.setChar(addr,2,(byte)ten,false); 
   lc.setChar(addr,3,(byte)one,false);   
   }
   else if(temp_val>=10000 && temp_val<=99999)
   {
   int one,ten,hundred,thousand;
   long tenthousand;
   one = temp_val%10;
   ten = (temp_val/10)%10;
   hundred = (temp_val/100)%10; 
   thousand = (temp_val/1000)%10; 
   tenthousand = (temp_val/10000)%10;
   lc.setChar(addr,0,(byte)tenthousand,false); 
   lc.setChar(addr,1,(byte)thousand,false); 
   lc.setChar(addr,2,(byte)hundred,false); 
   lc.setChar(addr,3,(byte)ten,false); 
   lc.setChar(addr,4,(byte)one,false);   
   }

}


void loop()
{
  char key = keypad.getKey();  
  if(key == '#' || key == '*')
  {
  if(key == '#')
  {
  lcd.print(store);
  MAX(store,0);
  inc=0;
  }   
  if(key == '*')
  {
  lcd.clear(); 
  for(byte erase=0 ; erase<=20 ; erase++)
  store[erase]=0;
  for(byte dig_clear=0 ; dig_clear<=6 ; dig_clear++)
  lc.setChar(0,dig_clear,' ',false); 
  inc=0; 
  } 
  }    
  else if(key)
  {    
  store[inc] = key;
  inc++;      
  }
}
 

The following code section is most likely the issue:

else if(temp_val>=10000 && temp_val<=99999)
{
int one,ten,hundred,thousand;
long tenthousand;
one = temp_val%10;
ten = (temp_val/10)%10;
hundred = (temp_val/100)%10;
thousand = (temp_val/1000)%10;
tenthousand = (temp_val/10000)%10;
lc.setChar(addr,0,(byte)tenthousand,false);
lc.setChar(addr,1,(byte)thousand,false);
lc.setChar(addr,2,(byte)hundred,false);
lc.setChar(addr,3,(byte)ten,false);
lc.setChar(addr,4,(byte)one,false);
}

As this portion of your code only checks if the temp_val is essential only five digits long, as the conditional test values highlighted in RED indicate and as a six digit value of temp_val exceeds the conditional test value of 99999, the contents of final else if are not executed as conditional test is false.

If you wish to build upon your existing code framework, you will need to add another else if structure which appropriately tests for a six digit temp_val.

For Example:

Code:
   else if(temp_val>=[COLOR="#FF0000"]100000[/COLOR] && temp_val<=[COLOR="#FF0000"]999999[/COLOR])
   {
   ...
   [COLOR="#FF0000"]// Contents which appropriately handle the case of a six digit temp_val[/COLOR]
   ...
   }


However, your existing framework is certainly not very scalable, as it requires additional else if structures for each additional digit you wish to handle in the future.

Another possible option is to implement a recursive routine which then can handle virtually any size integer, however, as each recursive call to the recursive routine consumes available stack space, there is a practical limit to such use of recursive routines when implemented on a microcontroller with limited stack resources and should be carefully considered.

You might considering splitting the above task into two routines, the first a recursive routine which can process a integer of type long into its corresponding array of digits and the second to then take this array and output the appropriate digits to the 7 segment displays.

BigDog
 

The use of atoi() is also wrong.
Code:
unsigned long temp_val = atoi(val);

This is not the recommended solution.
Consider what you do:

You have a character array with the ascii values of the digits and you convert it to number, then add some more code and then a lot of divisions to convert it back to the digits that the initial array included.

Why all this beating around the bush?

- - - Updated - - -

Code:
char i;
for(i=0;val[i];i++)
   lc.setChar(addr,i,val[i]-'0',false);
or
Code:
for(;*val;val++)
   lc.setChar(addr,i,*val-'0',false);
 
Why all this beating around the bush?

Excellent point, xenos!

I totally overlooked that little tidbit, probably as I was looking for a different issue, why utilizing a type long was not producing the desired effects.

And as I suspected, the issue was not the type, rather the routine used to extract the digits.

BigDog
 

Thank you all,

I used unsigned long = atol(); instead of unsigned int = atoi(); and problem solved.

All the code remain same.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top