PAS Sensor for electric monsterbike.

I will try to keep it short. I build high power electric bike and want sometimes use it legal. I use controller (Sabvoton) which dont reed PAS (pedal asist sensor). Basicly what sensor does is it's sending pulses of voltage (0-5v) when pedaling. Contains hall sensor and 12 magnets attached to pedal cranck.
Output should work like 0-5v throttle.
Caution: when not pedaling it give constant voltage according to distance to nearest magnet.

My problem is even if i don't connect signal wire D2 to arduino nano it start. Only output D11 wire was connected. I have tried Analog and digital input and output. Hope anybody can see code and help.

//Hardware constants
const int PASPin = 2;    // input from PAS
const int ledPin = 13, PWMOut=11;  // the pin that the LED is attached to and the PWM output pin

//Software constants
const unsigned long activityTimeoutMS = 500; // Allowed PAS signal inactivity time before turning off
const int startPulses = 2; // Number of PAS pulses needed before turning on
const int lowPWMValue = 56, highPWMValue = 170; // PWM values to drive throttle input, default 56 (1,1 V) and 170 (3,4 V), U=n/255*5V, n=U/5V*255

// Variables
volatile int inputEdges = 0; // counter for the number of pulses since last reset
volatile unsigned long lastEdgeTime = 0; // timestamp of last PAS pulse
bool state=false; // variable holding information about the state of the output

void setup() {
  pinMode(PASPin, INPUT); // initialize the PAS pin as a input
  attachInterrupt(digitalPinToInterrupt(PASPin), pulse, RISING); //Each rising edge on PAS pin causes an interrupt
  pinMode(ledPin, OUTPUT); // initialize the LED as an output
  pinMode(PWMOut, OUTPUT); // initialize the PWM pin as an output
}


void loop() {
  //If PAS signal is inactive for too long, turn off everything
  unsigned long curTime=millis();
  if ((curTime>lastEdgeTime)&&((curTime-lastEdgeTime)>activityTimeoutMS)) {
    turnOff();
  }
  
  //If system is off, check if the impulses are active
  if ((!state)&&((millis()-lastEdgeTime)<activityTimeoutMS)) {
    //if impulses are active, check if there were enough pulses to turn on
    if (inputEdges>startPulses) {
      turnOn();
    }
  }
  
  //Use LED for status info
  digitalWrite(ledPin, state);
}

//Turn off output, reset pulse counter and set state variable to false
void turnOff() {
  noInterrupts();
  analogWrite(PWMOut, lowPWMValue);
  inputEdges=0;
  state=false;
  interrupts();
}

//Turn on output and set state variable to true
void turnOn() {
  analogWrite(PWMOut, highPWMValue);
  state=true;
}

//Interrupt subroutine, refresh last impulse timestamp and increment pulse counter (until 10000 is reached)
void pulse() {
  lastEdgeTime=millis();
  if (inputEdges<100) {
    inputEdges++;
  }  
}

Its not my code. If anybody is interested in thred you can find here:

  pinMode(PASPin, INPUT); // initialize the PAS pin as a input

You need either an external pullup or pulldown resistor or to use the internal pullup resistor.

volatile int inputEdges = 0; // counter for the number of pulses since last reset

Why do you need an int, when you constrain the value to be between 0 and 100?

My problem is even if i don't connect signal wire D2 to arduino nano it start.

What starts? Doing what?

Starts means pin D11 output around 2,5v and bike runs instantly. I know anything about arduino. Nothing in programing. I was already trying use some components to design this converter. used PIR sensor board and planing make board based on 555 timer. Just fount this thread about arduino PAS converter.

I will try some small value resistor on input but what i said even without connected input from PAS arduino output 2.5v. Led on arduino nano named L is not on when it outputs 2,5v.
Appreciate your help

I have measured voltage again and shows 3.29v on output from arduino and led named L is red. I have tried use resistor and pull up internal resistor with no positive effect.

When i connect input wire D2 to GND red led turn off but arduino continue output signal on D11

Without a pullup or pulldown resistor (of about 10K), PASPin will float and cause interrupts regularly. Your code is interpreting these false signals correctly.

You have no OFF for the output at pin11, it is either at highPWMValue (+/- 3.3V) or lowPWMValue (+/- 1.1V).
With a wildly fluctuating floating input, constantly causing your output from high to low, you would expect to see a reading of +/- 2.2V.

Seems to me that everything is working okay. Not the way you want it to, but the way you told it to (aren't computers great?)

Also,
if ((curTime>lastEdgeTime)&&...
is somewhat redundant and may cause you problems if you run for more than 54 days without a reset.

@dariusz08
did you get around the problem?
Im new to arduinos, tried your code and also see my servo just flickering around, the led lights up but flickers also then turns off
Many thanks