Help please with this code

I have tried to make this code work hundreds of times, but I fail, please help me!
I injected a ppm signal from a remote control (trainer jack port) on pin 2 on arduino pro mini, as written in the code.
at arduino outputs 3, 4, 5 … I connected servo, but it does not work
I want to say that the code is not written by me, I tried to make it work
Thank you very much!

/*This program puts the servo values into an array,
 reagrdless of channel number, polarity, ppm frame length, etc...
 You can even change these while scanning!*/

#define PPM_Pin 2  //this must be 2 or 3
#define multiplier (F_CPU/8000000)  //leave this alone
int ppm[16];  //array for storing up to 16 servo signals
byte servo[] = {1,4,5,6,7,8,9,10};  //pin number of servo output
#define servoOut  //comment this if you don't want servo output
//#define DEBUG

void setup()
{
  Serial.begin(115200);
  Serial.println("ready");

  #if defined(servoOut)
  for(byte i=0; sizeof(servo)-1; i++) pinMode(servo[i], OUTPUT);
  #endif
 
  pinMode(PPM_Pin, INPUT);
  attachInterrupt(PPM_Pin - 2, read_ppm, CHANGE);

  TCCR1A = 0;  //reset timer1
  TCCR1B = 0;
  TCCR1B |= (1 << CS11);  //set timer1 to increment every 0,5 us
}

void loop()
{
  //You can delete everithing inside loop() and put your own code here
  int count;
  while(ppm[count] != 0){  //print out the servo values
    Serial.print(ppm[count]);
    Serial.print("  ");
    count++;
  }
  Serial.println("");
  delay(100);  //you can even use delays!!!
}



void read_ppm(){  //leave this alone
  static unsigned int pulse;
  static unsigned long counter;
  static byte channel;
  static unsigned long last_micros;

  counter = TCNT1;
  TCNT1 = 0;

  if(counter < 710*multiplier){  //must be a pulse if less than 710us
    pulse = counter;
    #if defined(servoOut)
    if(sizeof(servo) > channel) digitalWrite(servo[channel], HIGH);
    if(sizeof(servo) >= channel && channel != 0) digitalWrite(servo[channel-1], LOW);
    #endif
  }
  else if(counter > 1910*multiplier){  //sync pulses over 1910us
    channel = 0;
    #if defined(DEBUG)
    Serial.print("PPM Frame Len: ");
    Serial.println(micros() - last_micros);
    last_micros = micros();
    #endif
  }
  else{  //servo values between 710us and 2420us will end up here
    ppm[channel] = (counter + pulse)/multiplier;
    #if defined(DEBUG)
    Serial.print(ppm[channel]);
    Serial.print("  ");
    #endif
    
    channel++;
  }
}
 for(byte i=0; sizeof(servo)-1; i++)

Did you forget something here?

at arduino outputs 3, 4, 5 … I connected servo,

byte servo[] = {1,4,5,6,7,8,9,10}; Pin 3 would be a problem, as would pin 1

I tried to make it work

Do you still have the original code?

I’m not sure of the polarity of the pulses on the trainer port, but I’d be inclined to flip-flop the interrupt between RISING and FALLING within the ISR, so it’s easier to see if you’re in a pulse or an inter-pulse gap, and advance the channel pointer only when you get a long sync.

yes, original code is here

ppm polarity is negative, I use Hitec

But that code doesn’t output to pin 3 either.
Did you try simply changing the “servo” array to only use the pins you’re interested in?

I connected 3 servo to pins 4,5 and 6
I injected a ppm signal on pins 2 and 3

That’s not what you wrote originally.

Compiled, but untested. (the github code was junk too)

#define PPM_Pin 2  //this must be 2 or 3
#define multiplier (F_CPU/8000000)  //leave this alone
byte servo[] = {3};                 //pin number of servo output
const byte nChannels = sizeof (servo) / sizeof (servo [0]);


void setup()
{
  Serial.begin(115200);
  Serial.println("ready");

  for(byte i = 0; i < nChannels; i++) {
    pinMode(servo [i], OUTPUT);
    digitalWrite (servo [i], LOW);
  }
 
  pinMode(PPM_Pin, INPUT);
  attachInterrupt(digitalPinToInterrupt (PPM_Pin), read_ppm, CHANGE);

  TCCR1A = 0;  //reset timer1
  TCCR1B = 0;
  TCCR1B |= (1 << CS11);  //set timer1 to increment every 0,5 us
}

void loop()
{
}

void read_ppm()
{
  static unsigned int pulse;
  static unsigned long counter;
  static byte channel;

  counter = TCNT1;
  TCNT1 = 0;

  if(counter < 710*multiplier){  //must be a pulse if less than 710us
    if(channel < nChannels) 
      digitalWrite (servo [channel], HIGH);
    if(channel <= nChannels && channel != 0) 
      digitalWrite (servo [channel-1], LOW);
  }
  else if(counter > 1910*multiplier){  //sync pulses over 1910us
    channel = 0;
  }
  else {  //servo values between 710us and 2420us will end up here
     channel++;
  }
}

can you modify it to work?

By that, I assume you tried my code with a servo on pin 3, and nothing happened?

I don’t have any hardware to test it.

Time to get your oscilloscope out.

yes, yes it works. :slight_smile:
but it works weird, when I move the stick to the left, it works perfectly normal, but when I move it to the right it makes a 180 degree turn alone