//Transmitter Code
//Includes and Vars
#include<Wire.h>
#include <VirtualWire.h>
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int forward = 8;
//Done at the beginning of the program.
void setup()
{
Serial.begin(9600);
Serial.println("setup");
vw_setup(2000);
vw_set_tx_pin(12);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
}
//Done constantly as its running.
void loop()
{
//Sets Gyro working wires.
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
//Serial.print("AcX = "); Serial.print(AcX);
//Series of 'if' statements which react to ranges from the gyro and send a message to the receiver.
if(AcX<-6000&&AcX>-17000)
{
char *msg2 = "a";
vw_send((uint8_t *)msg2, strlen(msg2));
vw_wait_tx();
Serial.println("a");
}
else if(AcX>6000&&AcX<17000)
{
char *msg2 = "b";
vw_send((uint8_t *)msg2, strlen(msg2));
vw_wait_tx();
Serial.println("b");
}
else if(AcY<-6000&&AcY>-17000)
{
char *msg2 = "c";
vw_send((uint8_t *)msg2, strlen(msg2));
vw_wait_tx();
Serial.println("c");
}
else if(AcY>6000&&AcY<17000)
{
char *msg2 = "d";
vw_send((uint8_t *)msg2, strlen(msg2));
vw_wait_tx();
Serial.println("d");
}
else if(AcZ>6000&&AcZ<18000)
{
char *msg2 = "f";
vw_send((uint8_t *)msg2, strlen(msg2));
vw_wait_tx();
Serial.println("f");
}
else if(AcZ<-6000&&AcZ>-18000)
{
char *msg2 = "g";
vw_send((uint8_t *)msg2, strlen(msg2));
vw_wait_tx();
Serial.println("g");
}
else
{
char *msg2 = "e";
vw_send((uint8_t *)msg2, strlen(msg2));
vw_wait_tx();
Serial.println("e");
}
Serial.println(AcZ);
}