MPU6050 using as a switch for controlling speed and direction of bldc motor

I am newbie to coding, i made this project

but now i wish to take a step further by controlling the motor with MPU6050 (accelerometer + gyroscope)
i.e. when MPU6050 is tilted in one direction it works as a forward switch and when tilted in another direction it works as backward switch, and when it is stable it works as stop switch, more the tilt more the speed of motor (which we are doing with potentiometer currently).

Any help with code is appreciated !!
Cheers !!