Pros inertial sensor turning PID

I am using the below code to make accurate turns with the gyro. Unfortunately it messes up with values over 180 degrees and spins constantly if using multiple calls of gyroTurn() one after the other. Does anybody have any improvements or fixes? Also I want to make a AccMove() command that uses the inertial sensor’s accelerometer to move accurate distances. This will eventually be implemented into auton. This is PROS C++ btw.

#include "main.h"


void initialize()
{
	pros::lcd::initialize();
}


void disabled()
{

}


void competition_initialize()
{

}


void autonomous()
{

}

void gyroTurn(float deg)
{
	pros::Motor left (1);
	pros::Motor right (2, true);
	left.set_brake_mode(pros::E_MOTOR_BRAKE_BRAKE);
	right.set_brake_mode(pros::E_MOTOR_BRAKE_BRAKE);
	left.set_gearing(pros::E_MOTOR_GEARSET_36);
	right.set_gearing(pros::E_MOTOR_GEARSET_36);

	pros::Imu imu(18);

		float error = 10.0;
		float integral = 0.0;
		float derivative = 0.0;
		float perror = 0.0;
		float value = 0.0;

		float target = deg;
		float Ki = 0.0;
		float Kd = -2.0;
		float Kp = -5.0;


		while (abs(error) > 0.1 || left.get_actual_velocity() > 0.1)
		{
				pros::lcd::print(0, "val: %f\n", imu.get_yaw());
				error =  target - imu.get_yaw();
				printf("%f \n", error);
				integral = integral + error;
				if (abs(error) < 2)
				{
					integral = 0.0;
				}
				derivative = error - perror;
				perror = error;
				value = (integral*Ki) + (derivative*Kd) + (error*Kp);

				left.move(-value);
				right.move(value);

				pros::delay(5);
			}
			left.move(0);
			right.move(0);
}

void opcontrol()
{

	pros::Motor left (1);
	pros::Motor right (2, true);
	left.set_brake_mode(pros::E_MOTOR_BRAKE_BRAKE);
	right.set_brake_mode(pros::E_MOTOR_BRAKE_BRAKE);
	left.set_gearing(pros::E_MOTOR_GEARSET_36);
	right.set_gearing(pros::E_MOTOR_GEARSET_36);

	pros::Imu imu(18);

	imu.reset();
	while (imu.is_calibrating())
	{
	pros::delay(100);
	}



	float error = 0.0;
	float integral = 0.0;
	float derivative = 0.0;
	float perror = 0.0;
	float value = 0.0;

	float target =  90;
	float Ki = 0.0;
	float Kd = -2.0;
	float Kp = -5.0;

	gyroTurn(180);
	gyroTurn(90);
}