Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Can this even be done? String to call procedure

Status
Not open for further replies.

flat5

Member
Code:
void random_code() {
  String code_char = "abcdefghijklmnopqrstuvwxyz,.0123456789\0";
  String send_char;
  randNumber = random(38);
  send_char = "send_" + (code_char.substring(randNumber, randNumber + 1) + "()");
//
// !!! I need code here to call a procedure that is the string send_char !!!
//
Serial.println(send_char); // what command(s) do I put here?
  delay(5);
}

// These are examples of the procedures I want void random_code() to call
void send_a() {
  dit(); dah();
  letter_space();
}

void send_b() {
  dah(); dit(); dit(); dit();
  letter_space();
}

Hope it's clear what I want to do. Also hope it can be done. I'm skeptical.

Will substring recognise "\0" as end of string, or will it look for char 38? I could remove random and test for it.
 
Sorry I put it in quotes. In the sketch it is not in quotes.
I should just test it.

Any idea how to do what I want to accomplish?
 
So you want a substring to say "send " + random ASCII char + "()"

I would use sprintf..

C:
char buff[17];
char ptr = &buff[0];
sprintf(buff,"send %c ()", random(38) + 48);  

while(*ptr != 0)
    send(*ptr++);

Random (38 ) + 48 will do the same as you have BUT the a~z will be uppercase..
I take it this is just a test program?
 
The point is not to output text to the screen. My routine will do that.
It was the first step in developing what I DO want to do.
I'm very new to 'C' and needed the skill to get the random grab of a character from a string.

What I want to do is have random Morse characters sent so I can improve my Morse speed.
Code practice.
I do not want to display send_a().
I want to invoke the function. To randomly invoke a group of procedures.
 
You do realize that a string is just an array..
C:
char str[] = { "Hello I'm a large string, Testing 1 2 3 4 5 6 7"};
char buff[17];
char ptr = &buff[0];
sprintf(buff,"send %c ()", str[random(38)] );

while(*ptr != 0)
    send(*ptr++);

Note*** using the string object will only complicate things....
 
[bad word] strings.
It has very little to do with the purpose of this thread.
My first 5 lines handle the string part just fine. No #includes, no obtuse code.
Sorry you don't like it.

You keep focusing on the wrong details in my posts.
I find it frustrating and annoying!
Only one person responds and ignores the point of the thread.

You seem to have little interest in what the OP is writing about.
If you can not help me with the reason I posted here don't post, please.
Sorry to be so direct. It's the best I can do.

To randomly invoke a group of procedures while the program is running.
Maybe it can't be done, I don't know.

I'm sure there are better ways of achieving the result I want.
The Morse lookup tree in the encoder/decoder thread I posted would be a better way to do things but is probably too complex for me at this point.

If anyone has an interest, the thread of the sketch I'm trying to add to is:
https://www.electro-tech-online.com/threads/tone-irreceive-problem.144299/#post-1216730

It is not the encoder/decoder thread.
 
Sorry you don't like it.
No problem! I looked at your first post and it appeared to me that you just needed to grab a random character from a string.....

I'm very new to 'C' and needed the skill to get the random grab of a character from a string.
.

But I also realize when I'm not helping...... Good luck!!
 
yo bro , chill, we do our best, but cant read minds yet

you cant just invoke by sending string, you need code to execute from strings value, your other example is best bet:
value=3;
switch (value) {
case 3: instruction(3);

or

value ="send"
instruction(value);

but then you need to write instruction sub to work off the invalue:

void instruction(string a){
switch(a){
case send: send_data;break
case get: get_data;break
case freeze: while(1);break
}}

I cant telly you what \0 does cause i skipped chapter on operators, but if i want to see whats in my string i create a breakpoint in my debugger and add to watch

ie, so if you want to make whatever \0 is as a terminator,, you need to reflect that in your switch
 
Last edited:
Well this is embarrassing.
I managed to write my procedure but it uses strings to do the work.
Code:
void random_code() {
  // send a random characters Morse equivlent
  char* code_char[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                       "0","1","2","3","4","5","6","7","8","9",
                       ",",".","?","!",":","""","'","="};
  char* codes[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
                   "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", // 26 letters
                   "-----", ".----", "..---", "...--", "....-", ".....","-....", "--...","---..","----.", // 10 numbers
                   "--..--", ".-.-.-","..--..","..--.","---...",".-..-.",".----.","-...-" // 8 punctuation
                  };
  String send_char;
  String answers;
  for (int t = 0; t < 70 ; t++) {
    randNumber = random(43);
    send_char = codes[randNumber];
    String x;
    for (int i = 0; i < send_char.length(); i++) {
      x = send_char.substring(i, i + 1);
      if (x == ".") {
        dit();
      }
      else {
        dah();
      }
    }
    letter_space();
    letter_space();
    answers.concat(code_char[randNumber]);
    answers.concat(" ");
  }
  Serial.println(answers);
}
Edit: changed the random max to 43. Forgot about zero.
 
Last edited:
Read minds? I posted four times what I wanted to achieve and only the fact that I used Serial.print to see how things were going got anyone's attention. I did not expect print to call a procedure. When I'm repeatedly misunderstood I get annoyed, it seems.
 
1) String to call procedure
2) \0 what does it do
3)
I want to invoke the function. To randomly invoke a group of procedures.
4)
I'm very new to 'C' and needed the skill to get the random grab of a character from a string.

different questions and all very vague,. I see now, but you were really just looking to convert from ascii to moris, but due to the questioning it sounded like you were doing something more complicated, or having a problem with a different part of the syntax.

Often i ask questions which i dont get answers and have found that usually its cos i am asking in a misunderstood format. sometimes it helps to rephrase the question, or in this case, psudo code of what you want could help.

For example,. I initially thought you were having problems with just the terminator \0, I have seen code where you can process the string and stop at the terminator, without the for loop being used, while loop instead, I have never used Serial.print, but assume it prints to serial, \0 is usually sent on data lines to affirm end of data string (if that is the terminator op):)

sry to be of not much help this time, glad you got it thou
 
It seems that professional programmers may be in another world than me and I have to be very clear and specific in expressing my thoughts. Otherwise the reader will think down another path.
I have to try to be less cryptic. The least of my problems was \0.
I have asked a moderator to remove this thread.

edit:
"i create a breakpoint in my debugger and add to watch"
That is what the Serial.print was for. To see how things were going.
It would not have remained in the sketch.
 
You don't have a debugger to do breakpoints with using arduino ide.
You shouldn't remove your thread the stuff your posting is good. I been looking at this but I don't have much time to write any code, Work a lot these days. LOL
But there's a easy way to do what you want if you wanted to you could use a table with ASCII to Morse code then you'd get both the letters and the dit dat's at the same time
 
Last edited:
See all you really need is to use AScII. If a "a" it's 97 so if get a ". -" Which you could just say that you got a 97 which you could easily change that to be sent out whatever you want LCD or Serial so you can see it. I haven't had any time to write any code but I like what your working on been thinking of making a sender that would send text out ASCII to Morse code. Just want it useful so I need to no what tone should be and how long for the dit and the das.
And how long the dead space should be so we have a good reading at the end " 111 102 116 104 101 108 105 110 101 o f t h e l i n e "
 
be80be, a lot of your questions will be answered in my other thread.
https://www.electro-tech-online.com/threads/morse-encoder-decoder-my-first-project.144143/
Read the comments in the code and checkout the great code table.
Full comments from the author are in the first code post.
Google him or something to find his page and more info.

About deleting this thread...
The relevant code and the complete project are in another thread.
https://www.electro-tech-online.com/threads/tone-irreceive-problem.144299/
 
I've moved the project to another site.
For now, I'm happy with the present version.
I believe it is a useful program for learning Morse code and practice.
If interested send me a private message.
 
To answer the original question, yes it can be done but requires pointers.
You setup your functions and an array of pointers to the functions,
Code:
char funcA(char dat){
    return(dat*dat);
}

char funcB(char dat){
    return(dat+dat);
}

char funcC(char dat){
    return(dat+5);
}

char (*function[3])(char)={funcA,funcB,funcC};

Then to call a function it is simply x=function[offset](value); where offset is the function to call (A, B or C = 0, 1, 2)

So
x=function[0](6); would make x = 6*6 = 36
x=function[1](6); would make x = 6+6 = 12

x=function[rand(3)](data); would call a random function

HTH

Mike.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top