Manuever rover using " w,a,s,d "

Hey guys, i got a problem now that i’ve used switch case to maneuver my rover. But the problem is, when there isn’t any character being key-ed in, the rover will still continue moving & it can’t stop.
Any idea?

Are you using kbhit() so that you are only checking the keys when one is actually pressed?

Hey, i’m not very sure if i’m using kbhit() cos i’m new to this software. Can you please take a look at my code?
Thank you very much.

float PE6,PE5; //PE6 and PE5 is pin 38 and 37 respectively
int i2,i1;
char str2[6],str1[6];
char move, gear;
char buffer[100];
/void delay( int x )
{
int i;
for(i = 0; i < x; i++);
}
/

void Waveform ( float Number2,float Number1 )
{
// Compute the % of duty cycle based 1024 counts and left shift by 2 bits
i2= (int)(Number2 * 4 * 1024 / 100 );
i1= (int)(Number1 * 4 * 1024 / 100 );

// Write the most significant 8 bits of the counter to PWMxR and least 2 bits to PWLxR
WrPortI ( PWM2R, NULL, i2>>2 );
BitWrPortI ( PWL2R, &PWL2RShadow, i2>>0, 6 );
BitWrPortI ( PWL2R, &PWL2RShadow, i2>>1, 7 );

WrPortI ( PWM1R, NULL, i1>>2 );
BitWrPortI ( PWL1R, &PWL1RShadow, i1>>0, 6 );
BitWrPortI ( PWL1R, &PWL1RShadow, i1>>1, 7 );

// Set the period of the output PWM
// ? only set bit 5 of PWLxR when suppressing the alternate duty cycle
WrPortI ( TAT9R, NULL, 179 ); //freq=200Hz
BitWrPortI ( PWL2R, &PWL2RShadow, 1, 5 ); //freq=50Hz
BitWrPortI ( PWL1R, &PWL1RShadow, 1, 5 ); //freq=50Hz

//Select the desired input/output direction for each pin via PEDDR.
//Set alternative peripheral output function via PEAHR and then enable it via PEFR
WrPortI ( PEAHR, NULL, 0xAA ); // set PE7-4 to alt out 2
WrPortI ( PEFR, NULL, 0xF0 ); // enable alt output on PE7-4
WrPortI ( PEDDR, NULL, 0xF0 ); // make PE7-4 output
}

main ()
{
gear = 1; // start at gear 1
move = 0;
PE6 = 7.25;
PE5 = 7.25;
Waveform ( PE6,PE5 );

while( move != ‘p’)
{

  //delay (5000);
move = getchar();
	switch( move )
  {
           case 'W':
           case 'w':        //move forward
        	switch( gear )
           {

               case 1:    //shift to forward gear 1
                  PE6 = 7.28;
	            	PE5 = 6.7;
              	break;


              case 2:    //shift to forward gear 2
              	PE6 = 7.25;
                 PE5 = 6.1;
                 break;
              case 3:    //shift to forward gear 3
              	PE6 = 7.22;
                 PE5 = 5.5;
                 break;
              case 4:    //shift to forward gear 4
              	PE6 = 7;
                 PE5 = 4;
                 break;
           }
            Waveform( PE6,PE5 );
           break;
  		case 'A':
			case 'a':        //move left
            	PE6 = 8.4;
            	PE5 = 7.2;
            	Waveform( PE6,PE5 );
           	break;

        case 'S':
         case 's':        //move backward
        	switch ( gear )
           {
           	case 1:    //shift to backward gear 1
              	PE6 = 7.15;
            		PE5 = 8;
                 break;
              case 2:    //shift to backward gear 2
              	PE6 = 7.2;
                 PE5 = 8.5;
                 break;
              case 3:    //shift to backward gear 3
              	PE6 = 7.3;
                 PE5 = 8.7;
                 break;
              case 4:    //shift to backward gear 4
              	PE6 = 7.7;
                 PE5 = 9.6;
                 break;
           }
            Waveform( PE6,PE5 );
           break;
        case 'D':
         case 'd':        //move right
            PE6 = 6;
            PE5 = 7.2;
            Waveform( PE6,PE5 );
           break;
         case ' ':        //stationary position
            PE6 = 7.25;
            PE5 = 7.25;
            Waveform( PE6,PE5 );
            break;
        case '1':        //gear 1
        	gear = 1;
           break;
        case '2':        //gear 2
        	gear = 2;
           break;
        case '3':        //gear 3
        	gear = 3;
           break;
        case '4':        //gear 4
        	gear = 4;
           break;
         default:
        	//PE6 = 7.2;
           //PE5 = 7.2;
           PE6 = 7.25;
            PE5 = 7.25;
            Waveform( PE6,PE5 );
           break;
  }
  //Waveform( PE6,PE5 );   //display waveform

}
}

looking at the code, it will keep moving until you press the space bar (I think).

The code will however pause at the getchar until a key is pressed which means your app cannot do anything else whilst waiting. Using kbhit() will allow you to only do the keypress checking when needed and the app can then make other decisions about the motion of the rover (collision detection etc).

If you have a look at the demo apps for the BACnet stack (see link in my sig). There are some examples of using kbhit() to allow the app continue doing house keeping tasks until a key is pressed and needs to be processed.

Regards,
Peter

I have downloaded the file but I can’t exactly find which demo. May I know which folder is in it?

Have a look at the ports\bl4s100\main.c file for an example.

Regards,
peter

hey, i saw the samples.
As i am very unfamiliar, can i ask if am i suppose to replace the whole switch case and use only if loop?

The code in the sample just shows how to set up your code to only call getchar() when there is actually a character to be read so that you can have your code do useful tasks whilst waiting for a user to press a key. You will still need your switch statements.

The sample code performs a series of tasks in an endless loop and responds to keypresses only when it has to.

Regards,
Peter

Hey, the kbhit() really helps me alot. But now i got another problem is that there is this program port connected to the rcm5400 but my teacher wants me to not used that programming port and use wifi instead. So that the rover can move freely.
Do you have got any idea, where should i start from?

Your 2 most likely options here are to use the Telnet interface or a Web Page interface.

In the first case you would run a telnet client on the PC and control the rover using the keyboard as iit is currently done (but using serial port comms instead of the keyboard getchar()/kbhit())

In the second case you would use buttons on a web page hosted on the RCM5400 to control the rover.

either way life just got a little more complicated :slight_smile:

Regards,
Peter