I am trying to convert a three digit integer into three digits in a char array to analyze each one. My use for this is reading a voltage, seperating the digits of the number the arduino makes, and reading them. I am using a piezo buzzer to buzz out each digit with a pause in between. For some reason when I run this, it beeps a random number of times, pauses, then does that over and over. Here's my code for reference. Thanks!
Awesome! Thanks for this. I also found a way using modulus like you said. However I am getting roughly the same number no matter what voltage analogRead reads. Here is my revised code. I am going to use the sprintf method and see if it makes any difference. Thanks!
int val = 0, val1 = 0, val2 = 0, val3 = 0, finalval = 0, first, second, third;
void setup() {
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
delay(5000);
}
void loop() {
val = analogRead(2);
finalval = val;
int one = finalval / 100;
int two = (finalval / 10) % 10;
int three = finalval % 10;
first = 0;
while (first != one) {
tone(1, 4500);
delay(250);
noTone(1);
delay(250);
first++;
}
delay(2000);
second = 0;
while (second != two) {
tone(1, 4500);
delay(250);
noTone(1);
second++;
delay(250);
}
delay(2000);
third = 0;
while (third != three) {
tone(1, 4500);
delay(250);
noTone(1);
third++;
delay(250);
}
delay(5000);
}
There's the problem I'm running into. I'm using an ATTiny85. I can't print anything because there isn't a serial out. This is why I'm using the buzzer.
I am very new to all this but found that there are functions in the core library's that will 'format' a number and make a string.
Since strings are arrays, using dtostrf (DoubleToString formatted) you can go from 123.456 to [1],[2],[3],- ,[4],[6],[\0] in 1 step.
dtostrf(YourNumber, TotalStringLength, NumberOfDecimals, TheTargetArray)
TotalStringLength > Must include all characters the '.' and the null terminator
NumberOfDecimals > The output is rounded .456 become .46 at 2 decimals
TheTargetArray > I suspect 'char strBuffer[]' will work with out a preceding definition but I haven't tried yet
zdillman:
There's the problem I'm running into. I'm using an ATTiny85. I can't print anything because there isn't a serial out. This is why I'm using the buzzer.
There are ways of getting serial out, but are you sure you are getting good readings? How is it wired up?
zdillman:
There's the problem I'm running into. I'm using an ATTiny85. I can't print anything because there isn't a serial out. This is why I'm using the buzzer.
Sorry ...
TheTargetArray > 'char strBuffer[]' will not work, you have to initialise the array first.
sizeof() will just report the whole array size not its contents.
The string object will do what you need, conversion, finding length and picking individual characters but it is apparently memory hungry.
That said you could easily step through any array with a simple loop.
Al
Dyslexic bloke, you are my hero for the day. I've been looking for this function and it works perfectly for sending acquired doubles over a radio, which is expecting a string / array.
Dyslexicbloke:
I am very new to all this but found that there are functions in the core library's that will 'format' a number and make a string.
Since strings are arrays, using dtostrf (DoubleToString formatted) you can go from 123.456 to [1],[2],[3],1. ,[4],[6],[\0] in 1 step.
dtostrf(YourNumber, TotalStringLength, NumberOfDecimals, TheTargetArray)
TotalStringLength > Must include all characters the '.' and the null terminator
NumberOfDecimals > The output is rounded .456 become .46 at 2 decimals
TheTargetArray > I suspect 'char strBuffer[]' will work with out a preceding definition but I haven't tried yet
Hope this helps
Al
[/quote]