const byte VRX = A0;
const byte VRY = A1;
const byte SW = 2;
struct packet {
bool forward, buttonPressed;
byte leftFront, leftBack, rightFront, rightBack;
};
void setup() {
// put your setup code here, to run once:
pinMode(VRX, INPUT);
pinMode(VRY, INPUT);
pinMode(SW, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
packet p;
int b = digitalRead(SW);
int vert = analogRead(VRY);
int horz = analogRead(VRX);
p.buttonPressed = !b;
// Serial.print(vert);
// Serial.print(" ");
// Serial.println(horz);
int speed;
if (vert < 512) {
Serial.print("Back");
p.forward = false;
speed = map(vert, 511, 0, 0, 255);
}
else {
Serial.print("Front");
p.forward = true;
speed = map(vert, 512, 1023, 0, 255);
}
// Serial.println(speed);
if (horz < 512) {
Serial.print("Right");
p.leftFront = speed;
p.leftBack = map(horz, 511, 0, speed, int(float(speed)*0.66));
p.rightFront = p.leftBack;
p.rightBack = map(horz, 511, 0, speed, 0);
}
else {
Serial.print("Left");
p.rightFront = speed;
p.rightBack = map(horz, 512, 1023, speed, int(float(speed)*0.66));
p.leftFront = p.rightBack;
p.leftBack = map(horz, 512, 1023, speed, 0);
}
Serial.print(p.buttonPressed);
Serial.print(" F:");
Serial.print(p.forward);
Serial.print(" LF:");
Serial.print(p.leftFront);
Serial.print(" RF:");
Serial.print(p.rightFront);
Serial.print(" LB:");
Serial.print(p.leftBack);
Serial.print(" RB:");
Serial.println(p.rightBack);
}