James's RF Transmitter and Receiver Attempt

Hello,

I'm having troubles figuring out why this code wont work, can someone knowledgeable please take a look and advise? When I input into directions for my car into the serial monitor I only receive back "wrong entry'. Many thanks. James

// TRANSMITTER CODE

//NOTE :- THIS CODE IS USED WHEN YOU WANT TO CONTROL THE ROBOT VIA THE COMPUTER!!!!

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);	  // Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
     
    vw_setup(2000);	 // Bits per sec
        vw_set_tx_pin(3); //Transmitter Data Pin to Digital Pin 3
}//close setup

void loop()
{
  
  if (Serial.available() > 0)
  {
     int sendByte = Serial.read();//Starts reading data from the serial monitor once condition is met
     
 
  switch (sendByte){
 
    case 'f':  //if the controller type f
  {
    
    char *msg2 = "f";
    digitalWrite(13, true); // Flash a light to show transmitting
    vw_send((uint8_t *)msg2, strlen(msg2));//send byte to the receiver
    vw_wait_tx(); // Wait until the whole message is gone
    digitalWrite(13, false);
    break;
     
  }
    case 'b':  //if controller types b
    {
   char *msg2 = "b";
   digitalWrite(13, true); // Flash a light to show transmitting
   vw_send((uint8_t *)msg2, strlen(msg2));//send byte to the receiver
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(13, false);  
   break;
    }
    
    case 's': //if controller types s
    {
   char *msg2 = "s";
   digitalWrite(13, true); // Flash a light to show transmitting
   vw_send((uint8_t *)msg2, strlen(msg2));//send byte to the receiver
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(13, false);  
   break;
    }
    
    case 'l': //if controller types l
  {  
   char *msg2 = "l";
   digitalWrite(13, true); // Flash a light to show transmitting
   vw_send((uint8_t *)msg2, strlen(msg2));//send byte to the receiver
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(13, false);    
   break;
  }     
    case 'r': //if controller types r
    {
   char *msg2 = "r";
   digitalWrite(13, true); // Flash a light to show transmitting
   vw_send((uint8_t *)msg2, strlen(msg2));//send byte to the receiver
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(13, false);   
   break;
    }
    default://if any other value is entered
    {
      
       Serial.println("wrong entry");//print wrong entry in the serial monitor
    }
   }// close switch-case
  }//close if 
 }//close loop
  
 // End Of Code

Have you tried the example programs on the VitualWire Web page to verify that you have connected the transmitter and receiver correctly and its all working ?

Where did the code come from ?

Do you have capitals lock on or off?

Something like this simplified code should be equivalent to what you have now:

void loop()
{
  if (Serial.available())
  {
    byte sendByte = Serial.read();

    switch (sendByte) {
      case 'f':
      case 'b':
      case 's':
      case 'l':
      case 'r':
          digitalWrite(13, true); // Flash a light to show transmitting
          vw_send(&sendByte, 1);//send byte to the receiver
          vw_wait_tx(); // Wait until the whole message is gone
          digitalWrite(13, false);
          break;
      default:
          Serial.println("wrong entry");
    }// close switch-case
    
  }//close if
}//close loop

Hello,

Thanks very much for reviewing and providing input. Yes, your code is much cleaner (I love it) and it works! I like how you've orientated the switch. My next problem (if I may) is that both motors don't drive at the same time. Left, yes. Right, yes. Both forward... ulgh. Can you see anything obvious from my receiver code that might be causing that? The motors spin when "f" is sent but only for a brief moment. The left and right spin continuously when "l" or "r" is received.

// RECIEVER CODE

//NOTE :- THIS RECEIVER CODE IS USED WHEN YOU WANT TO CONTROL THE ROBOT VIA THE COMPUTER
  
  #include <VirtualWire.h>

//declaring pins for moto right 
 
 int enA = 5; 
 int in1 = 7; 
 int in2 = 8;
 
// delaring pins for motor left
 int enB = 6; 
 int in3 = 9;
 int in4 = 10;


 int motorPin[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15};//array for storing pin nos

void setup()
{
    Serial.begin(9600);	// Debugging only
    Serial.println("setup");
    // Initialise the IO and ISR
    vw_setup(2000);	 // Bits per sec
    vw_set_rx_pin(2);//Receiver at Digital Pin 2
    vw_rx_start();// Start the receiver PLL running

    
 for (int i = 0; i < 12; i++) 
 {
        pinMode(motorPin[i], OUTPUT);
 }//close for loop

/*
This is basically what the for loop does :-
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
*/

}//close setup

void loop()
{
  
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
	int i;

        digitalWrite(13, true); // Flash a light to show received good message
	// Message with a good checksum received, dump it.
	
	for (i = 0; i < buflen; i++)
	{
	    Serial.print(buf[i]);
        if(buf[i] == '8')
        {
          forward();//go forward when 8 is pressed
        }
        if(buf[i] == '2')
      {
        backward();//go backward when 2 is pressed
      }
        if(buf[i] == ' ')
      {
        stopMotor();//stop/brake when   is pressed
      }
        if(buf[i] == '4')
      {
        left();//go left when 4 is pressed
      }
        if(buf[i] == '6')
        {
          right();//go right when 6 is pressed
        }
	    
	}//close for loop
	
        digitalWrite(13, false);

   }//close main if
}//close loop
//you can print the data entered when debugging by adding Serial.println

////////////////////////////////////////////////////////////////////////////////////////

//set of functions
void forward()
{
  digitalWrite(enA,200);
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  
  digitalWrite(enB,200);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);

  }
  
  
void backward()
{
  digitalWrite(enA,200);
  digitalWrite(in1,LOW);
  digitalWrite(in2,HIGH);
  
  digitalWrite(enB,200);
  digitalWrite(in3,LOW);
  digitalWrite(in4,HIGH);
 
}

void left()
{
  digitalWrite(enA,200);
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(enB,200);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);

}

void right()
{
  digitalWrite(enA,200);
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(enB,200);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
 
}

void stopMotor()
{
  digitalWrite(enA,200);
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(enB,200);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);

}
  //End Of Code

Are you powering the motors directly from the arduino?

No, I'm powering through a motor driver L298n

No, I'm powering through a motor driver L298n

Where is the power coming from? THAT was the question that you failed to answer.