Scanf function

I tried making a code in which the user could choose manually what the robot has to do.

Example code(completely made up and might be wrong)


#include "UserAPI.h"
#include <stdio.h>

int input;

void main ( void )
{
         printf("select a code to execute"\n);
         printf("1. drive forward for 2 seconds\n");
         printf("2. drive backward for 2 seconds\n");
         scanf(" %d", &input);
         if(input = 1)
         {
              ...........robot drives forward
          }
         else if( input = 2)
         {
              .............robot drives backward
         }
         else
         {
               .. error
         }
         getchar();
}

The code above states that the robot will move forward or backward depending on the user input.

Is it possible to input data using the scanf function in EasyC 1.x or on any Vex copmatible platform?

I tried using the scanf statement and I got an error after compiling the program;

call of function without prototype

Is this because the #include<stdio.h> statement is not included in the EasyC code.

The easyC most likely will compile the Scanf function, but how are you input to the controller?

I tried compiling the scanf function, but it does not work. I will input the information using a keyboard.

Does anyone know if scanf, getc, etc. are supported in the c18 compiler?
Also, is there any example code of a VEX controller reading from a serial port?

Thanks!

Mark

No it is not supported in the mcc18 compiler.

Are you referring to a hyperterminal connections and readings? Search the forum this has been mentioned before.

Technic-R-C

Thanks!

Mark

Unfortunately, scanf() is about the most useless ‘C’ library function. I think I used it for a required Class Assignment in 1988, and have not used it since.

The message “call of function without prototype” means that you did not include the header file that Defined [FONT=Courier New]scanf()[/FONT].
Typically, [FONT=Courier New]scanf()[/FONT] is Defined in the file [FONT=Courier New]stdio.h[/FONT], but the MCC18 compiler does not seem to have [FONT=Courier New]scanf()[/FONT], Defined in any Header File.

If you really want to, you can locate the ‘C’ source code for a scanf() function, but that is really a lot of extra code, that you will not ever use.

Now, how about a simple way to do what you want.

If you want to stick with numbers, Start with the VexUserCode_DDT code and Modify the [FONT=Courier New]case statement[/FONT] in the function [FONT=Courier New]Handle_Debug_Input()[/FONT], in the file [FONT=Courier New]PicSerialDrv.c[/FONT]. You can display your Help Message in the [FONT=Courier New]Process_Debug_Stream()[/FONT] Function in the same Source File.

( Code Example to follow… )

Here is some code I have been working on (on and off) the last two days. It will read a Single character sent to the Vex Controller and then act on it. I started with the VexUserCode_DDT project and then modified the Functions Handle_Debug_Input() and Process_Debug_Stream(), plus some other small changes to make it work all together.

This is Very Rough code, and it still does not work like I would like it to, but I have your other questions to answer… :wink:



#define DISPLAY_STATE_HELP         0
#define DISPLAY_STATE_OPT1         1
#define DISPLAY_STATE_OPT2         2
#define DISPLAY_STATE_OPT3         3
#define DISPLAY_STATE_OPT4         4
#define DISPLAY_STATE_OPT5         5
#define DISPLAY_STATE_IDLE       254
#define DISPLAY_STATE_UNKNOWN  255

#define INPUT_STATE_HELP        10
#define INPUT_STATE_OPT1        11
#define INPUT_STATE_OPT2        12
#define INPUT_STATE_OPT3        13
#define INPUT_STATE_OPT4        14
#define INPUT_STATE_OPT5        15
#define INPUT_STATE_IDLE       254
#define INPUT_STATE_UNKNOWN    255


unsigned char display_state    = DISPLAY_STATE_IDLE;
unsigned char input_state    = INPUT_STATE_IDLE;



<<SNIP>>


/*******************************************************************************
* FUNCTION NAME: Handle_Debug_Input
* PURPOSE:       State machine for incomming PC data.  When the full packet has
*                been received, the state is set to 8 allowing Process_Debug_Stream
*                to handle the new packet.
* CALLED FROM:   CheckUartInts
* ARGUMENTS:     1
*     Argument       Type             IO   Description
*     --------       -------------    --   -----------
*     data           unsigned char    I    incoming data byte
*******************************************************************************/

void Handle_Debug_Input(unsigned char data)  
{

  switch (data)
  {
    case '0':  // 
      input_state    = INPUT_STATE_HELP;
      display_state    = DISPLAY_STATE_HELP;
      break;
    case '1' : // 
      input_state    = INPUT_STATE_OPT1;
      display_state    = DISPLAY_STATE_OPT1;
      break;
    case '2' : //
      input_state    = INPUT_STATE_OPT2;
      display_state    = DISPLAY_STATE_OPT2;
      break;
    case '3' : //
      input_state    = INPUT_STATE_OPT3;
      display_state    = DISPLAY_STATE_OPT3;
      break;
    case '4' : //
      input_state    = INPUT_STATE_OPT4;
      display_state    = DISPLAY_STATE_OPT4;
      break;
    case '5' : //
      input_state    = INPUT_STATE_OPT5;
      display_state    = DISPLAY_STATE_OPT5;
      break;
    default:
      input_state    = INPUT_STATE_UNKNOWN;
      display_state    = DISPLAY_STATE_UNKNOWN;
      break;
  }
}



<<SNIP>>


/*******************************************************************************
* FUNCTION NAME: Process_Debug_Stream
* PURPOSE:       Transmits a completed packet to the PC.
* CALLED FROM:   User application layer (Process_Data_From_Local_IO)
* ARGUMENTS:     none
*******************************************************************************/

void Process_Debug_Stream(void)   /* Called from user_routines_fast.c */
{
  static unsigned char *ptr;

  switch(display_state)
  {

    case DISPLAY_STATE_HELP :
      printf("\nSelect a code to execute\n");
      printf("0. display this help\n");
      printf("1. drive forward for 2 seconds\n");
      printf("2. drive backward for 2 seconds\n");
      printf("3. turn left\n");
      printf("4. turn right\n");
      printf("5. turn around\n");
      printf("\nIN>");


      display_state    = DISPLAY_STATE_IDLE;
      input_state    = INPUT_STATE_IDLE;

      break;

    case DISPLAY_STATE_OPT1 :
      printf("\nMOVING forward for 2 seconds\n\nIN>");


      display_state    = DISPLAY_STATE_IDLE;
      input_state    = INPUT_STATE_IDLE;

      break;

    case DISPLAY_STATE_OPT2 :
      printf("\nMOVING backward for 2 seconds\n\nIN>");


      display_state    = DISPLAY_STATE_IDLE;
      input_state    = INPUT_STATE_IDLE;

      break;

    case DISPLAY_STATE_OPT3 :
      printf("\nTURNING left\n\nIN>");


      display_state    = DISPLAY_STATE_IDLE;
      input_state    = INPUT_STATE_IDLE;

      break;

    case DISPLAY_STATE_OPT4 :
      printf("\nTURNING right\n\nIN>");


      display_state    = DISPLAY_STATE_IDLE;
      input_state    = INPUT_STATE_IDLE;

      break;

    case DISPLAY_STATE_OPT5 :
      printf("\nTURNING around\n\nIN>");


      display_state    = DISPLAY_STATE_IDLE;
      input_state    = INPUT_STATE_IDLE;

      break;

    case DISPLAY_STATE_IDLE :
      break;

    default  :    

      printf("\nUNKNOWN COMMAND\n\nIN>");

      display_state    = DISPLAY_STATE_IDLE;
      input_state    = INPUT_STATE_IDLE;
      break;
  }


    INTCONbits.PEIE = 0;    //Disable peripheral interrupt
    debugIntState = 0;
    INTCONbits.PEIE = 1;    //Enable peripheral interrupt


}



As usual, just add the files in the Zip File to the VRC Competition Template directory and Load the Project “VexUserCode_CTL.mcp” and Build It, and then Download It.
VexUserCode_CTL.zip (26.1 KB)