Greenville, IL
Offline
Edison Member
Karma: 11
Posts: 1288
Warning Novice on board! 0 to 1 chance of errors!
|
 |
« on: June 21, 2011, 05:38:02 pm » |
I have sketched a quick program to output 128 HEX integers "0-7E" . The problem is that I would like to have 00,01,02,.....0F etc. for the integers below 10. Here is what I am getting: 0'1'2'3'4'5'6'7'8'9'A'B'C'D'E'F'10'11'12'13'14'15'16'17'18'19'1A'1B'1C'1D'1E'1F'20'21'22'23'24'25'26'27'28'29'2A'2B'2C'2D'2E'2F'30'31'32'33'34'35'36'37'38'39'3A'3B'3C'3D'3E'3F'40'41'42'43'44'45'46'47'48'49'4A'4B'4C'4D'4E'4F'50'51'52'53'54'55'56'57'58'59'5A'5B'5C'5D'5E'5F'60'61'62'63'64'65'66'67'68'69'6A'6B'6C'6D'6E'6F'70'71'72'73'74'75'76'77'78'79'7A'7B'7C'7D'7E' Here is the code: /* Serial Call and Response in ASCII L */
int talk = 0; // output int int inByte = 0; // just a place to store input for later
void setup() { // start serial port at 9600 bps: Serial.begin(9600);
}
void loop() { // if we get a valid byte, read analog ins: if (Serial.available() > 0) { // get incoming byte: inByte = Serial.read(); while (talk < 127){ Serial.print(talk, HEX); Serial.print("'"); talk = talk + 1; }
} }
Thanks for any help' Mark
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 12
Posts: 2857
ruggedcircuits.com
|
 |
« Reply #1 on: June 21, 2011, 05:40:48 pm » |
How about: while (talk < 127){ if (talk < 16) Serial.print("0"); Serial.print(talk, HEX); Serial.print("'"); talk = talk + 1; } -- The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons
|
|
|
|
|
Logged
|
|
|
|
|
Greenville, IL
Offline
Edison Member
Karma: 11
Posts: 1288
Warning Novice on board! 0 to 1 chance of errors!
|
 |
« Reply #2 on: June 21, 2011, 05:48:25 pm » |
Thank you for the bullet fast response! I put the code in and it is exactly what I wanted! One of these days I will realize that not everything has to be difficult.  Thanks, Mark
|
|
|
|
|
Logged
|
|
|
|
|
'round the world...
Offline
Edison Member
Karma: 19
Posts: 2305
|
 |
« Reply #3 on: June 21, 2011, 06:53:50 pm » |
sprintf() might do that if you specify leading zeros. The previous response is, however, way more efficient.
|
|
|
|
|
Logged
|
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o. Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum). Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.
Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5915
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #4 on: June 21, 2011, 08:31:32 pm » |
sprintf() might do that if you specify leading zeros. The previous response is, however, way more efficient. I would check out sprintf since if I end up trying to do 3-digit hex numbers, I'll be making minimal changes from 2-digits if I used sprintf 
|
|
|
|
|
Logged
|
|
|
|
|
Greenville, IL
Offline
Edison Member
Karma: 11
Posts: 1288
Warning Novice on board! 0 to 1 chance of errors!
|
 |
« Reply #5 on: June 21, 2011, 08:56:04 pm » |
Thanks for the idea of sprintf(). I will test that out too. I think I have seen that used in a sketch posted on the forum.
Thanks, Mark
|
|
|
|
|
Logged
|
|
|
|
|
'round the world...
Offline
Edison Member
Karma: 19
Posts: 2305
|
 |
« Reply #6 on: June 21, 2011, 10:38:53 pm » |
on printf it would look like: printf("%02X \n", 10); I saw this somewhere a few days ago. I'm not sure if it was here or on a general programming forum. Give it a go.
|
|
|
|
|
Logged
|
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o. Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum). Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.
Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5915
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #7 on: June 21, 2011, 11:03:37 pm » |
on printf it would look like: printf("%02X \n", 10); I saw this somewhere a few days ago. I'm not sure if it was here or on a general programming forum. Give it a go. printf in regular computers outputs to default output device, the screen. But since arduino has no default output device, it should do nothing even if it exists. You should use sprintf, which outputs to string, thus the s-printf. Just like scanf would scan from default input, the keyboard, on a regular computer, and doesn't do anything on an arduino. But if you want to recognize numbers, say from serial input, you can do sscanf, the string scanf to pick numbers from a sentence.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #8 on: June 22, 2011, 02:17:12 am » |
printf() can be set to work (to certain extent) - http://www.arduino.cc/playground/Main/Printf -
|
|
|
|
|
Logged
|
|
|
|
|
|