Easy beginners question

I have a simple code:

and I have one bug I cant seem to fix.

in the setup loop Im trying to start with pin 13 high, and it just doesnt work. The only time I get the pin 13 to go high is only after a PPM signal reaches the pin 3.
And I would like it to be high even prior to turning on the remote control... So I simply defined it in the setup to go high, and this does not work :confused:

Any clue as to why?
By the way the rest of the code ensures that at no time pin 13 and 11 are high at the same time, that's why the additional stuff.
Thanks for your time in advance!
Sp1k3

#define PPM_Pin 3  //this must be 2 or 3
int ppm[16];  //array for storing up to 16 servo signals
int LED=13;
int VID=11;
// int start = 0;

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

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
 pinMode(VID, INPUT);
 digitalWrite(VID,LOW);
 pinMode(LED, OUTPUT);
 digitalWrite(LED,HIGH);
 pinMode(VID, OUTPUT);
}

void loop(){


//   digitalWrite(VID,LOW);
 //  delay(10);
 //  digitalWrite(LED,HIGH);



if(ppm[6]<1750) {

   digitalWrite(LED,LOW);
   delay(10);
   digitalWrite(VID,HIGH);}

  else { //if (ppm[3]>=1250) {

   digitalWrite(VID,LOW);
   delay(10);
   digitalWrite(LED,HIGH);}

Please read #7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

And I would like it to be high even prior to turning on the remote control... So I simply defined it in the setup to go high, and this does not work

I'll betcha' it DOES go high... :wink:

Add a couple of seconds of delay at the end of setup() so you have time to check it.

if(ppm[6]<1750) {

     digitalWrite(LED,LOW);
     delay(10);
     digitalWrite(VID,HIGH);}

Add a serial print statement to check the value of ppm[6] (and maybe ppm[3] while you're at it). That should show you why LED is low.

The problem is that there is no ppm signal yet and so according to my logic it should switch LED HIGH...
but it stays low. One the remote is turned on clearly with 2ms then it immediadetly turns ON...
So it appears that tbe code is stuck until ppm comes...
Ill try dely in setup<← great idea thanks

So YES with a DELAY it now shows that it actually turns ON, but then as long there is no PPM signal coming he appears to be stuck in the IF loop.
What can I do so that the LED pin stays high in this state?

Sp1k3:
So YES with a DELAY it now shows that it actually turns ON, but then as long there is no PPM signal coming he appears to be stuck in the IF loop.
What can I do so that the LED pin stays high in this state?

You could start by showing all of your code and not just part of it.

And please post it between code tags - not inline. Code tags are generated using the </> button in the "Reply" window.
(It wouldn't hurt to also edit your first post and do the same.)

PROBLEM FIXED

Thanks to hint given me with the delay, I was able to follow what was happening in the code. Basically as long as there was no PPM signal generating interrupts, the ppm values were all 0 causing the loop to always follow the else condition...
So I upgraded the code and this fixed the issue. I knew it was something stupid.

#define PPM_Pin 3  //this must be 2 or 3
int ppm[16];  //array for storing up to 16 servo signals
  int LED=13;
  int VID=11;
 // int start = 0;

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

  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
   pinMode(VID, INPUT);
   digitalWrite(VID,LOW);
   pinMode(LED, OUTPUT);
   digitalWrite(LED,HIGH);
   pinMode(VID, OUTPUT);
   delay(1000);
}

void loop(){
    // digitalWrite(LED,LOW);
    // delay(500);
   //digitalWrite(LED,HIGH);
   //lay(3000);
  //   digitalWrite(VID,LOW);
   //  delay(10);
   //  digitalWrite(LED,HIGH);
  

 
  if(ppm[6]<500) {    
    
     digitalWrite(VID,LOW);
     delay(10);
     digitalWrite(LED,HIGH);
     delay(10);}
  else if(ppm[6]>1750) {
     digitalWrite(VID,LOW);
     delay(10);
     digitalWrite(LED,HIGH);
     delay(10);}
   else  {    
     digitalWrite(LED,LOW);
     delay(10);
     digitalWrite(VID,HIGH);
     delay(10);}
    
  //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;

  counter = TCNT1;
  TCNT1 = 0;

  if(counter < 1020){  //must be a pulse if less than 510us
    pulse = counter;
  }
  else if(counter > 3820){  //sync pulses over 1910us
    channel = 0;
  }
  else{  //servo values between 510us and 2420us will end up here
    ppm[channel] = (counter + pulse)/2;
    channel++;
  }
}

I also fixed in my previous posts the code so that it is according to the forums standards. Hope it looks ok now.
Thanks for the support :slight_smile:

Sp1k3:
I also fixed in my previous posts the code so that it is according to the forums standards. Hope it looks ok now.

Thanks for doing that. :slight_smile: