Shop OBEX P1 Docs P2 Docs Learn Events
Can't execute function in C based on keyboard input? — Parallax Forums

Can't execute function in C based on keyboard input?

2coolcode2coolcode Posts: 5
edited 2013-09-03 17:04 in Learn with BlocklyProp
I am trying to blink a led on the board which works fine, but I am trying to pass how long I want the pause to be in between each blink based on a keyboard input
I want the pause() function to execute based on what is entered in the keyboard, so for example if I enter "35" , i want the function to be passed as pause(35);
this doesn't work , instead if times out as if there's a non-numeric trailing character in the value or as if it's reading something else
been a long time since I dove back into C , can someone see what's going wrong here?


#include "simpletools.h" // Include simpletools




int main() // main function

{






while(1)

{



print("\n enter blink interval:");

scanf("%c", vpause);

print("\n you entered, %c\n");






high(0);

pause(vpause);


low(0);


pause(vpause);


}

}

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2013-09-03 15:22
    Use %d in your format string instead of %c. You also need to pass a pointer to scanf, so use &vpause instead of vpause in scanf. You must be getting some warning messages when you compile. Modify your code until you can make all the warning messages go away.
  • SRLMSRLM Posts: 5,045
    edited 2013-09-03 15:35
    You also need to pass a variable to your second print statement. I don't have simpletools so I had to make a few slight modifications, but the following works for getting and returning user input:
    //#include "simpletools.h" // Include simpletools
    #include <stdio.h>
    
    int main() // main function
    {
    	while(1)
    	{
    		
    		printf("\n enter blink interval:");
    
    		int vpause;
    		scanf("%d", &vpause);
    
    		printf("\n you entered, %d\n", vpause);
    
    		//high(0);
    		//pause(vpause);
    		//low(0);
    		//pause(vpause); 
    	}
    }
    
  • jazzedjazzed Posts: 11,803
    edited 2013-09-03 15:53
    SimpleTools scan works like scanf in the same way that print works like printf ... the most common format specifiers are supported.

    Working examples can be found in the simpletext test program - also in the SimpleIDE package. https://code.google.com/p/propsideworkspace/source/browse/Learn/Simple Libraries/Text Devices/libsimpletext/libsimpletext.c
  • SRLMSRLM Posts: 5,045
    edited 2013-09-03 16:10
    @jazzed

    Can simpletools be bundled with the other libraries already available in the plain GCC release?
  • jazzedjazzed Posts: 11,803
    edited 2013-09-03 16:40
    SRLM wrote: »
    @jazzed

    Can simpletools be bundled with the other libraries already available in the plain GCC release?

    If that's what Parallax wants. I'll ping Jeff about it.
  • jazzedjazzed Posts: 11,803
    edited 2013-09-03 17:04
    I forgot to mention that the entire workspace is kept on-line in Google Code: https://code.google.com/p/propsideworkspace/source/checkout
Sign In or Register to comment.