Homemade electric scooter
In this part we will take a look over the code I've used for this homemade electric scooter. Te idea is more than easy. Read the potentiometer value, create the PWM signal and control the motor.
5. Code
Electric scooter code:

First, make sure that you ahve the servo.h library because we will need that. Usually ESCs have a range from 1000us to 2000us pulse for the control where 1000us is no rotation and 2000us is full throttle.
But in this case the ESC that I've used has a double range. For signals from 1000us to 1500us it will spin CW and signals from 1500us to 2000us CCW. So in the code I'll make sure the signal will always be between 1505 and 2000us so it will never spin backwards.

So first we include the servo library. This, using the servo.writeMicroseconds, will create the PWM signal. We create a servo variable named "motor" in this case. Next we create the variables for the analog pin A0, which will be the input from the potentiometer. Also I create the variable for the speed signal that will go from 1505 to 2000.

Now in the Setup loop we attach the servo signal to pin D3 and start the signal with a 1505 value so the ESC won't enter into setup mode. Next I add a small delay so the scooter won't start immidietly.

In the loop we read the analog pin value. This will give us a value between 0 and 1024 where 0 is GND and 1024 is 5V. Depending on how you've connected the potentiometer, you'll have to map the value from 1024 to 600 or backwards. It's map till 600 because there will never be a full rotation of the potentiometer, since that is not posible on the scooter rudder.
If the throttle input is higher that the real speed, we increase the speed till we have the same value. If it is lowe, we decrease the ral value till we have the same amount. That's it. Now we constrain the PWM valeus from 1505 to 2000 and write the real value to digital pin D3.

