void setup() { Serial.begin(9600); digitalWrite(14, HIGH); digitalWrite(15, HIGH); } unsigned int V1; unsigned int L1; long LEFT = 255; long RIGHT = 255; long Throttle; //forwards is positive, reverse is negative long Percent; //percentage of power given to the right side void loop() { /////////////////////////////////////////////////////////////// //Read JOY Position /////////////////////////////////////////////////////////////// L1 = analogRead(0);//Joy latteral read analog value (1023 is the most left position and 0 is the most right position V1 = analogRead(1);//Joy vertical read analog value (1023 is the most up/forward position and 0 is the most down/reverse position) Percent = L1 + 1; //add 1 so I never have to divide by zero 0 Percent = Percent * 100; //scale up the value to apply a ratio to the "RIGHT" and "LEFT" Percent = (Percent/1024); //divide by the most possible value (all the way right) /////////////////////////////////////////////////////////////// //TEST JOY Position and calculate respective direction /////////////////////////////////////////////////////////////// if (V1>600 & Percent<60 & Percent>40)//Forward { RIGHT = 255; LEFT = 255; Serial.print("F"); } else if (V1>600 & Percent<96 & Percent>59)//Forward but left { RIGHT = 255; LEFT = (150-Percent)*255; LEFT = LEFT/100; Serial.print("FL"); } else if (V1>600 & Percent<41 & Percent>4)//Forward but right { RIGHT = (Percent+50)*255; RIGHT = RIGHT/100; LEFT = 255; Serial.print("FR"); } else if (V1<400 & Percent<60 & Percent>40)//Reverse { RIGHT = -255; LEFT = -255; Serial.print("B"); } else if (V1<400 & Percent<96 & Percent>59)//Reverse but left { RIGHT = -255; LEFT = (150-Percent)*(-255); LEFT = LEFT/100; Serial.print("BL"); } else if (V1<400 & Percent<41 & Percent>4)//Reverse but right { RIGHT = (Percent+50)*(-255); RIGHT = RIGHT/100; LEFT = -255; Serial.print("BR"); } else if (V1>399 & V1<601 & Percent>80)//Sharp left turn { RIGHT = +255; LEFT = -255; Serial.print("R"); } else if (V1>399 & V1<601 & Percent<20)//Sharp right turn { RIGHT = -255; LEFT = +255; Serial.print("L"); } else //stop { RIGHT = 0; LEFT = 0; Serial.print("S"); } /* Serial.print("THROTTLE|"); //print the throttle command to the serial port Serial.print(RIGHT); Serial.print("|"); Serial.print(LEFT); Serial.print(";"); */ delay(100);//give time for the XBee transfer }