Go Down

Topic: How to read spesific numbers? (Read 1 time)previous topic - next topic

Impervium

Jul 20, 2011, 07:04 pm
This is probably pretty basic for most of you guys, but I haven't figured it out yet.

Let's say I got a number above 10, and I want to split said number, for example:
number: 1337
and I want to split it to:
Number 1: 1
Number 2: 3
number 3: 3
number 4: 7

I want to be able to select a specific number, got it?
Any ideas? BTW I'm programming for an Arduino.

Impervium

#1
Jul 20, 2011, 07:30 pm
I'd like to know how to do both. But first of all I need to seperate numeral values.
Most sensors gives me numeral inputs.
If I want to "write back" to the Arduino, lets say a numeric value, I'd like it to be able to assign different values to different places.

I got this 4 digit 7-segment display hooked up, directly to my Arduino.
Soon I'm going to use a shift register to drive it, (as I recently learned how to use a shift register).

Point is, I'm testing it out, and if I write numerals on the Serial Monitor, I want it to be able to display it.
Like if I write 1337, I want the Arduino to display this.
That would go ok, but I can only assign 1 value per digit on the display. I can't tell the display to write "1337" on one digit.

So I need it to split it up and assign it to different digits.

By the way, someone might know why, but for some odd reason, every time I write the "Serial.begin(9600)" into the Arduino code, my first 2 digits on the display becomes blurry. Why is this?

AWOL

#2
Jul 20, 2011, 07:33 pm
101 = 10
102 = 100
103 = 1000
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Impervium

#3
Jul 20, 2011, 07:38 pm
I don't follow. How's this helping my problem? (Ok, I'm a bit fresh to this, call me a n00b or whatnot, but do you have any example code?)

Btw, I figured out why my 2 digits became blurry, and the problem has been taken care of.

Chagrin

#4
Jul 20, 2011, 07:41 pm
1337 % 10 = 7
1337 / 10 % 10 = 3
1337 / 100 % 10 = 3
1337 / 1000 % 10 = 1

See http://arduino.cc/en/Reference/Modulo

Impervium

#5
Jul 20, 2011, 07:48 pm
Thanks!
Then comes my next question:
Except writing a program in say.. processing, what is the shortest way to get the Arduino to understand a 1 (Byte) as a 1 (text) ?

If I use the Serial Monitor, I will be sending 1337 as BYTE.
I need to be receiving it like plain text.

Either that, or I'm going to need to start using processing again, to test this out.
Suggestions?

robtillaart

#6
Jul 20, 2011, 08:02 pmLast Edit: Jul 21, 2011, 02:18 pm by robtillaart Reason: 1
when you receive 1 as byte add the value 0 as char

byte c = Serial.read();
c = c + '0';

will do

Please spend some time in the reference section and the tutorial section of arduino.cc as there is an amazing about of knowledge, samples etc to learn from. Time spend there is no waste of time...

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

ianpugh

#7
Jul 21, 2011, 12:52 am
Not sure on the specifics of the arduino c/c++ derivative, but I'd start with type casting from int to char[] or string.

I was needing something similar the other day, but never investigated.

Is type casting an option?

Impervium

#8
Jul 21, 2011, 02:07 pm
So you want to make a string into a complete number?
Like:

char yourstring[5] =('1', '2', '3', '4'];

and have the output be = 1234?

AWOL

#9
Jul 21, 2011, 02:16 pm
Quote
but I'd start with type casting from int to char[] or string.

That might be useful for splitting into pairs of hex digits, but unless the number was expressed in BCD, not much use for decimal.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up