Thread: Condensing my code

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Condensing my code

    Hello, I would like to condense my code down but I am not sure how I can do it. I repeat the same lines of code (putchar commands), but modify whats in the the writespeech parentheses when I want to speak something different, so I was wondering if there was a way to call the commands each time I want to use it. The code I repeat is a serries of commands that I send to my text to speech module, with the text I want to speak right in the middle. Everytime I want to speak a different line of text I need to repeat these lines of command with different text inside. The text gets spoken after the 0x1A command is sent.

    Code:
       putchar(0x80); //sp03 commands
       DelayMs(1);
       putchar(0x01);
       DelayMs(1);
       putchar(0x05);
       DelayMs(1);
       putchar(0x01);
       DelayMs(1);
    
       //sends text to speech module
       Writespeech("Welcome to the accessible home vital signs monitor");
       DelayMs(1);
    
       putchar(0x1A);  //speaks it after 0x1A is sent
       DelayMs(1);
       putchar(0x00);
    If the line to be spoken wasn't smack in the middle, I could declare it as a function and call the function when I needed to send the commands. How can I change this so I don't have to keep repeating the commands everytime I want to speak my text?
    Last edited by Dave_Sinkula; 04-25-2007 at 05:53 PM. Reason: Fixed long lines.

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    How about this:

    Code:
    void saySomething(char text[])
    {
    putchar(0x80);	//sp03 commands
    DelayMs(1);
    putchar(0x01);
    DelayMs(1);
    putchar(0x05);
    DelayMs(1);
    putchar(0x01);
    DelayMs(1);
    			
    Writespeech(text);  //sends text to speech module
    DelayMs(1);
    
    putchar(0x1A);  //speaks it after 0x1A is sent
    DelayMs(1);
    putchar(0x00);
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Ok thanks KONI. So basically when I want to call this function, I willl have to forst put it into an array using sprintf, then call the function?? Like this??
    Code:
    sprintf(saySomething, "Hello world");
    saySomething(char text[15])
    I am new to C, is this the correct approach?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Sort of. The idea is right, the code isn't. There are many ways to store data into a buffer -- and if the data is just a plain string, you don't even need a buffer:
    Code:
    void function(char buffer[]);
    
    char buffer[15];
    sprintf(buffer, "This is number %d", 5);
    function(buffer);
    
    strcpy(buffer, "Hello, World!");
    function(buffer);
    
    function("Goodbye, world!");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Thank you, I am getting errors though in my compiler (Hi-Tech PICC). My code is the following format:

    Code:
    void saysomething(char text[25])
    {
    putchar(0x80);	//sp03 commands
    DelayMs(1);
    putchar(0x01);
    DelayMs(1);
    putchar(0x05);
    DelayMs(1);
    putchar(0x01);
    DelayMs(1);
    			
    Writespeech(text);  //sends text to speech module
    DelayMs(1);
    
    putchar(0x1A);  //speaks it after 0x1A is sent
    DelayMs(1);
    putchar(0x00);
    }
    
    main()
    {
    char text[25];
    
    	strcpy(text, "Hello World");
    	saysomething(text);
    }
    I get the error that my signatures don't match. Am I not declaring something correct? I also tried removing the 25 from the array up top in the void function.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Make sure that the necessary libraries, including: <stdio.h> and <string.h> are included
    and that you return 0 from int main.

    Also double check the spelling of DelayMs and Writespeech: if you're calling them incorrectly (by typing in another case, for example) you'll get compile-time errors. Other than that, I have no idea why you're signatures don't match yet. They look like they do.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Make sure that you have included the appropraite library's for your DelayMs and whitespeech function.

    ssharish2005

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by baseball07 View Post
    Hello, I would like to condense my code down but I am not sure how I can do it. I repeat the same lines of code (putchar commands), but modify whats in the the writespeech parentheses when I want to speak something different, so I was wondering if there was a way to call the commands each time I want to use it. The code I repeat is a serries of commands that I send to my text to speech module, with the text I want to speak right in the middle. Everytime I want to speak a different line of text I need to repeat these lines of command with different text inside. The text gets spoken after the 0x1A command is sent.
    Code:
       putchar(0x80); //sp03 commands
       DelayMs(1);
       putchar(0x01);
       DelayMs(1);
       putchar(0x05);
       DelayMs(1);
       putchar(0x01);
       DelayMs(1);
    
       //sends text to speech module
       Writespeech("Welcome to the accessible home vital signs monitor");
       DelayMs(1);
    
       putchar(0x1A);  //speaks it after 0x1A is sent
       DelayMs(1);
       putchar(0x00);
    (half code really)
    Code:
    Speak(int n, int i, *pointer to the right array index of char pointers)  {
      
      for (i = 0; i < 5; i++)  {
         char ch = char Array[i]
         putchar(ch)
         DelayMS(n)
    
         if (i == 4) {
            WriteSpeech(pointer to array of char pointers to text messages)
             DelayMS(n) 
         }
      } /* end of for */
      putchar(i)  /* the final putchar has no matching delayMS() */
    } /* end of function */
    A little log should help with setting up the right messages:
    ===========
    1 - Welcome
    2 - Inquiry
    3- Calling for help
    etc.,
    where #1 is the pointer to the welcome string of char's

    Adak
    Last edited by Adak; 04-26-2007 at 02:47 AM.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    ahhhh i didnt have <string.h>, thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM