Go Down

### Topic: Need some maths help please! (RESOLVED) (Read 759 times)previous topic - next topic

#### BulletMagnet83

##### Apr 07, 2012, 03:59 pmLast Edit: Apr 07, 2012, 10:48 pm by BulletMagnet83 Reason: 1
This might sound pretty stupid, but I actually don't know WHAT to google to get a simple answer, so hopefully you guys can help me out :-) I bet it's one of those things that's really obvious once I know, but I'm hungover and my brain's not firing on all cylinders... hehe

Anyway, let's say I have a 4 digit number for example 1234. I want to split this up into thousands, hundreds, tens and units across 4 variables, ending up with each one holding a value of 0-9 so I can then push these out to my 7-segment module.

There MUST be a nice neat operation to do this, but hell if I can find it :-P Thanks in advance for the help!

EDIT: Here's the code I'm fiddling with, it should be apparent what I'm trying to accomplish.

Code: [Select]
`int CLK = 10;int SIN = 11;int LAT = 12;byte digitData[10] = {  0x3F, 0x6, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x7, 0x7F, 0x67};// Array to hold segment patterns for digits 0 - 9void setup () {    pinMode(CLK, OUTPUT);  pinMode(SIN, OUTPUT);  pinMode(LAT, OUTPUT);    }void loop () {    while(1) {  unsigned long time = (millis() / 1000);int D0 = time ?????????????int D1 = time ?????????????int D2 = time ?????????????int D3 = time ?????????????    digitalWrite(LAT, LOW);  shiftOut(SIN, CLK, MSBFIRST, digitData[D0]);  shiftOut(SIN, CLK, MSBFIRST, digitData[D1]);  shiftOut(SIN, CLK, MSBFIRST, digitData[D2]);  shiftOut(SIN, CLK, MSBFIRST, digitData[D3]);  digitalWrite(LAT, HIGH);      }          }`

#### blackm0k

#1
##### Apr 07, 2012, 04:15 pm
I think you just need to use integer division and modulo division (remainder) like so:

Code: [Select]
`int number = 1234;int d1, d2, d3, d4;int working;d1 = number / 1000; // Gives 1 in this exampleworking = number % 1000; // Gives 234 in this exampled2 = working / 100; // Gives 2 in this exampleworking = working % 100; // Gives 34 in this exampled3 = working / 10; // Gives 3 in this exampled4 = working % 10; // Gives 4 in this example`

#### BulletMagnet83

#2
##### Apr 07, 2012, 04:18 pm
That looks like exactly what I need! Thanks, I'll play with it now. I couldn't for the life of me figure out how to format the damn statements to do what I want.

Lifesaver!

#### Marek080

#3
##### Apr 07, 2012, 04:19 pm
The search term is "AVR BCD conversion".

#### BulletMagnet83

#4
##### Apr 07, 2012, 04:24 pm
Noted for future reference, Marek. I really am amazed how much of a n00b I can be sometimes, I'm sure I've actually used that stuff in my old BASIC programming days

Anyway, code works now... counting up like a beauty

Go Up