Hi everyone. Its turning bloody cold in London!!
I have a question about chars and strings.
I am working with a paper tape punch and am using a program that uses the serial port allowing a user to enter letters, one at a time into the port (as chars) which are sent to the paper tape punch. I would like to know how to make a string out of the characters that the user types so I can store the string in eeprom. Can anyone help me with converting entered chars into a string?
Thanks,
Steve.S
As each character is entered put it into an array of chars, increment the array index and put a zero in that position. Carry on until entry is complete, perhaps indicated by entering a special character such Carriage Return. At that time the array will hold a C style string properly zero terminated.
Do a search for the 'serial input basics - updated' thread.
UKHeliBob:
As each character is entered put it into an array of chars, increment the array index and put a zero in that position. Carry on until entry is complete, perhaps indicated by entering a special character such Carriage Return. At that time the array will hold a C style string properly zero terminated.
Thanks UKHelibob. That makes sense. Do you mean something like this:
char str[i] = {'\0'};
i++;
Do a search for the 'serial input basics - updated' thread.
Thanks sterretje. I've found the thread. I'll have a read now.
Another thing I want to do is, once I've sorted the data on the paper tape into a string I want to identify the first part of the string and with it, trigger a wav file on the Audio board I'm using. I know how to trigger the audio but I'm not sure about the string identification..
Why even bother putting it in an array if you want to put it into eeprom? Just put it there and keer an index to where to put the next.
Do you mean something like this:
Something like that but not quite !
Read Robin's thread for the nitty gritty details of how to do it.
septillion:
Why even bother putting it in an array if you want to put it into eeprom? Just put it there and keer an index to where to put the next.
The EEPROM save is just there so I can remember what people punched into the tape later. The punched tape is being punched by random people at Senate House Library in London. If we record the data into eeprom then we have it stored.
I need to convert the chars into strings so I can then identify the string and use it to trigger a wav file..
ALright. Keep in mind the eeprom isn't that big on a Uno but you can add external or maybe better, a sd card.
But then to the question and some counter questions. The serial input basics is already a nice bit of info.
But answer to yourself and us:
-
How long a string does it need to match? Because you only need to save as many chars (at a time) as the length of the longest string to match.
-
How does it need to match? (a) Is there a start char? (b) Is there an end char? (c) does it need to randomly match?
-
When does it need to match? As soon as the last matching char is typed? Or at the end of a string (whateven the end of the string is)?
If you want to match the string "hello", the three cases
a) A start char "{" for example. So it will match "{hello" or "{hello world" but not "hello" or "I say hello"
b) An end char like "}". So it would match "hello}" or "i say hello}" but not "hello" or "hello world}"
c) just match it, so everything like "hello", "hello world" "I say hello" "he said hello to me" "abigpileofhelloandsomemore"
Yes, you need to be more concrete about what will done, how much stored and what limits you set.
The project described so far may need a lot of storage, EEPROM in an Uno is only 1024 bytes.
I have used teletype machines to punch NC/CNC tapes before. But that was for cutting metal.
Sometimes with these kind of projects, they become a "cart before the horse" problem. The real issue here is not the punching of paper tape, it is the storage and sorting and scanning of the stored information for matching purposes ... to play a tune or such.
Forget about the EEPROM is my advice unless you wish to use an external flash chip in lieu of the EEPROM. Devices such as the ESP8266 have 3M of flash that can be configured as SPIFFS, so that is better but honestly, the trick is probably to use an SD card.
With the SD card, you have an issue (not uncommon to flash in general) in that you must work with blocks of memory, typically 512 Bytes. This is certainly going to cause you grief with any sort or string match unless you utilize a line-buffer of arbitrary length .... that is a maximum typing length including the CR/LF/null sequence. Personally, I think the brute-source search may be far better than attempting to sort. Maybe you can use an index scheme on the first letter to help optimize the search result-set but I think it will be a problem keeping things sorted out (pun intended!)
Ray
I even think the hole storing part (no matter if it's EEPROM, SD, web etc) is a minor detail. I don't think the storage should be the source for the comparing but just act as a "we can check it later"-storage.
But explaining in more detail what you want to match is key here
I have a keyword finder that given 1 char at a time can find match or not of up to 255 words, it keeps up with 250000 baud.. but it's not for beginners or the faint-hearted. There's one sketch just to turn word lists into source code for the sketch that uses the match function, the data goes into flash taking a bit over twice the size of the list characters. It could make a really good autofill too.
This doesn't read chars and then try to strstr() an array or buffer, it matches every character received and either returns no match when none is possible or the number of the matched word right after the last char is received. The data set lets failed matches go right to the next possible match, etc. I use strxxx and memxxx commands for example sketch words just because they share beginning chars.
The last taker never got back to me. Heck, the example runs. It's non-blocking even.
If one list isn't enough words, more than one can be used. A list of 250 20-char on average words takes 10604 bytes, a Mega2560 could run over 20 of those. Spread the words out and no search would take long to find no match in its list, the number of lists to check drops quick, it's likely to run faster with every char matched.
it keeps up with 250000 baud..
<...>
If one list isn't enough words, more than one can be used.
Now, that is impressive.
Ray
One important little "tidbit" - when you declare an array to put your characters in, make sure that as you add characters to the array (and increment the index) you don't go past the end of the array. If you do, you start writing over other variables etc. which can cause very odd things to happen in your program.
mrburnette:
Now, that is impressive.Ray
40 micros between characters gives 640 cycles. That's enough to check a few alternates to match or not just one char. I could make it a bit faster and increase the maximum using fatter data space, addresses instead of indexes....