Project upgrade Kids-car

Hello to all,
I’m a beginner in Arduino, but I like it.
I do have the ideas and the knowledge of electronics as well,
but programming is not my strongest point …

But here is my Project.

For my children I got a kids car that can be controlled locally and remotely.
I have already replaced the standard electronics with RC electronics

A 3CH receiver, ch1 steering wheel (driven by a esc instead of a a servo),
ch2 drive esc, ch3 controls a reedrelay for remote/local throttle
and a simple servo tester as a local on/off throttle.

I want to take over the steering wheel at all times on the remote, this is directly connected on the RC receiver, this works well and that remains the same.

The problem is the speed control.

The throttle is now on or off and the speed can be controlled by a potentiometer on the dashboard.

All this works but … not the way I like it.

I want to make it as a normal throttle, for this I would like to use a simple foot throttle from Ebay.
It has a output of 0.8 @0% to 4.2 volts @100%,
which I connect to A0.
That singal must be converted to:
on the throttle 0% on the servo (aka ESC) 90 °
and 100% 180 ° on the servo (forward on ESC) and
and 100% 0 ° on the servo (reverse on ESC,
which I connect to D2.
Forwards and backwards is controlled by a switch, this is on D3

I write a code to make my idea work.
It’s not finished yet, but the local throttle section works like I had in mind.

But the pass-through is not working well.
The servo (instead of ESC to see what happens) is very nervous and
occasionally turns slowly in a corner and back.
The input and output are almost the same on the scope.
(screenshot in attachment, green Line is source(from receiver)/input D4, yellow is output D2 to the servo)

D5 I use now as a local/remote switch.

I still have to find a good way as pass-through
and a way to use a servo signal (ch3) as trigger to switch from
remote to local control.

I’m a noob in programming, so tips and tricks are welcome.
What I made here is by copying and pasting, modify and trying.

#include <Servo.h>

Servo esc;                // create servo object to control a RC-ESC
const int buttonPin1 = 5; // Switch from remote to foot throttle (for now, it wil be a servo singal)
const int buttonPin2 = 3; // Switch from forward to backward
int potpin = 0;           // analog pin used to connect the Foot Throttle (0% = 0.8v 100% = 4.2v)
int val;                  // variable to read the value from the analog pin
int buttonState1 = 0;     // variable for reading the pushbutton 1 status
int buttonState2 = 0;     // variable for reading the pushbutton 2 status
int servoin = 4;          // Remote servo-Singal input from RC-reciever
int servoswitch = 9;       // Switch singal remote or foot throttle

void setup() {
  esc.attach(2);              // attaches the servo on pin 3 to the servo object
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(servoin, INPUT);
  Serial.begin(9600);         // Just for calibration and debug
}

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState1 == HIGH) 
  // Remote
  {
  digitalWrite(2, digitalRead(4));
  }
  else
  // Foot throttle
  {
  if (buttonState2 == HIGH)        // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  //Backward
  {
  val = analogRead(potpin);        // reads the value of the potentiometer (value between 0 and 1023)(0,8v=164 and 4,2v = 859)
  val = map(val, 164, 859, 90, 0);  // scale it to use it with the servo (value between 0 and 180)
  esc.write(val);                  // sets the servo position according to the scaled value
  //delay(15);
  }
  else
  //Forward
  {
  val = analogRead(potpin);          // reads the value of the potentiometer (value between 0 and 1023)(0,8v=164 and 4,2v = 859)
  val = map(val, 164, 859, 90, 180);  // scale it to use it with the servo (value between 0 and 180)
  esc.write(val);                    // sets the servo position according to the scaled value
  //delay(15);
    }

   //just for calibration and debug

  int sensorValue = analogRead(A0);             // read the input on analog pin 0:
  float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  Serial.print(sensorValue);                    // print out the ADC value you read:
  Serial.print("    ");
  Serial.print(voltage);                        // print out the voltage value you read:
  Serial.print("    ");
  Serial.println(val);                          // print out the servo value you read:
  }
}