#include <VirtualWire.h> //include library
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
const char *mx = "a"; //Max speed forward
const char *md = "b"; //Middle speed forward
const char *mn = "c"; //Minimum speed (off)
const char *lmd = "d"; //Left at middle speed
const char *rmd = "e"; //Right at middle speed
const char *lmx = "f"; //Left at max speed
const char *rmx = "g"; //Right at max speed
int v;
int s;
int x = 0;
int y = 1;

void setup() 
{
  vw_set_ptt_inverted(true);
  vw_set_tx_pin(8); //Set transmitter pin
  vw_setup(300); //Set transmission speed, slower means greater range
}

void loop()
{
  v = analogRead(x); //Read x axis of joystick (throttle) (I held my joystick the opposite way so axes are inverted)
  s = analogRead(y); //Read y axis of joystick (turn)
  if(v >= 800) //If read max
  {
    if (s >= 500 && s <= 600) //if read middle at max
    {
      vw_send((uint8_t *)mx, strlen(mx)); //send message "*mx" aka 'a'
      vw_wait_tx(); //wait for message to fully send
      delay(100); //delay so microcontroller can rest for a bit
    }
    if(s < 500)  //if read left at max
    {
      vw_send((uint8_t *)lmx, strlen(lmx));
      vw_wait_tx();
      delay(100);
    }
    else //of not either of above, it must be right at max
    {
      vw_send((uint8_t *)rmx, strlen(rmx));
      vw_wait_tx();
      delay(100);
    }
  }
  else if (v <= 50) //if minimum throttle
  {
      vw_send((uint8_t *)mn, strlen(mn));
      vw_wait_tx();
      delay(100);
  }
  else //if not minimum throttle or max throttle, it must be middle
  {
    if (s >= 30 && s <= 1000) //middle at middle speed
    {
      vw_send((uint8_t *)md, strlen(md));
      vw_wait_tx();    
      delay(100);
    }
    else if (s < 30) //left at middle speed
    {
      vw_send((uint8_t *)lmd, strlen(lmd));
      vw_wait_tx();
      delay(100);     
    }
    else //if not left or middle, it must be right at middle speed
    {
      vw_send((uint8_t *)rmd, strlen(rmd));
      vw_wait_tx();
      delay(100);    
    }
  }
}


