Extracting On/Off channels from a PWM output

Well, after being able to decode servo signals from a PPM stream, i come with another ideia of extracting on/off channels from one output of my R/c receiver, i would like to thanks to mem, he is being a great help for helping me on these tasks.

Bellow is my new sketch, wich i can compile it, but i think i¬īm missing something, i wonder if you could help with your knowledge. :slight_smile:

#include <Servodecode.h>
#include <ServoTimer2.h>

void setup() // run once, when the sketch starts
{

Serial.begin(38400);
pinMode(2,OUTPUT);

ServoDecode.begin();

}

void loop() // run over and over again

{
int count=0, i, pulsewidth ;

{

int i =1;{
Serial.print(‚ÄúCh‚ÄĚ);
Serial.print(i);
Serial.print("= “);
pulsewidth = ServoDecode.GetChannelPulseWidth(i);
Serial.print(pulsewidth);
Serial.println(‚ÄĚ");
}

if((pulsewidth >= 900) && (pulsewidth <= 1200))
{
digitalWrite(2,HIGH);
}

else {
digitalWrite(2,LOW);
}
count++;

}

}

thanks

GMV

I hope this code will get you going :

#include <ServoDecode.h>
#include <ServoTimer2.h>

#define FIRST_RELAY_PIN 2  
#define NUMBER_OF_RELAYS 4  

// setRelay sets the pin for the given channel to the given state
void setRelay( int channel, boolean state)
{
  if(channel <= NUMBER_OF_RELAYS) // this logic assumes relays are assigned from chan 1 upwards
    digitalWrite(channel + FIRST_RELAY_PIN -1, state); // subtract 1 because first channel is 1 (not 0)  
}

void setup()                    // run once, when the sketch starts
{
  Serial.begin(38400);
  pinMode(2,OUTPUT);
  ServoDecode.begin();
}


int count=0;

void loop()
{
  int pulsewidth ;

  for(int i = 1; i <= NUMBER_OF_RELAYS; i++)
  {  
    Serial.print("Ch");
    Serial.print(i);
    Serial.print("= ");
    pulsewidth = ServoDecode.GetChannelPulseWidth(i);
    Serial.print(pulsewidth);
    Serial.println("");    
    if((pulsewidth >= 900) && (pulsewidth <= 1200))
    {
      setRelay(i,HIGH);
    }        
    else 
    {
      setRelay(i,LOW);
    }
  }
  count++;   
}

have fun!

Hi mem and thanks, well my first impressions are, for testing with 1 channel, i¬īve changed ¬ī NUMBER_OF_RELAYS ¬ī to 1, and connected pin 8 to the servo output 1 of my Rx, then connected a Led to the output 2, i could monitorize the information of ch1=1500, but that was the only thing i could see, the time doesn¬īt change even if a move the joystick, mem‚Ķcould you upload the sketch to your board and connect it to your Rx and check it again, i think the code is not reading the correct input and thus it can not run the correct way ! :slight_smile:

thanks again

GMV

I have everything in pieces at the moment so probably want be able to try anything here till later in the week. But we should be able to get you going by looking at some debug output.

Try modifying the setRelay code with the following:

if(channel <= NUMBER_OF_RELAYS) // this logic assumes relays are assigned from chan 1 upwards
{
 digitalWrite(channel + FIRST_RELAY_PIN -1, state); // subtract 1 because first channel is 1 (no
 Serial.print("ch="); Serial.print(channel); Serial.print(", pin= ");
 Serial.print(channel + FIRST_RELAY_PIN -1); Serial.print(" set "); state? Serial.println("HIGH"):Serial.println("LOW");
}

Then run the code see what happens.

It will help me if you describe what you expected to happen and the output.
It would also help if you posted the actual code you run, use the code block icon (its the pound symbol #) to maintian formatting.

Good luck

I think i know where the problem is, the library¬īs (ServoDecode.* and ServoTimer2.*) are for extracting 8 channels from a PPM frame, so the defined variables have diferent values, so it will be worthless to make it work without new library¬īs, what¬īs your opinion ?

thanks

GMV

It will work on less than 8 channels, I have tested ServDecode with a 5 channel receiver and a 6 channel transmitter. You can test that it works by running the example sketch and viewing the serial print statements.

Mem, here it is the sketch like you told me to do .

#include <ServoDecode.h>
#include <ServoTimer2.h>

#define FIRST_RELAY_PIN 2
#define NUMBER_OF_RELAYS 4

// setRelay sets the pin for the given channel to the given state
void setRelay( int channel, boolean state)
{
 /* if(channel <= NUMBER_OF_RELAYS) // this logic assumes relays are assigned from chan 1 upwards
    digitalWrite(channel + FIRST_RELAY_PIN -1, state); // subtract 1 because first channel is 1 (not 0)
    */
    
    if(channel <= NUMBER_OF_RELAYS) // this logic assumes relays are assigned from chan 1 upwards
{
 digitalWrite(channel + FIRST_RELAY_PIN -1, state); // subtract 1 because first channel is 1 (no
 Serial.print("ch="); Serial.print(channel); Serial.print(", pin= ");
 Serial.print(channel + FIRST_RELAY_PIN -1); Serial.print(" set "); state? Serial.println("HIGH"):Serial.println("LOW");
}  

    
}

void setup()                    // run once, when the sketch starts
{
  Serial.begin(38400);
  pinMode(2,OUTPUT);
  ServoDecode.begin();
}


int count=0;

void loop()
{
  int pulsewidth ;

  for(int i = 1; i <= NUMBER_OF_RELAYS; i++)
  {
    Serial.print("Ch");
    Serial.print(i);
    Serial.print("= ");
    pulsewidth = ServoDecode.GetChannelPulseWidth(i);
    Serial.print(pulsewidth);
    Serial.println("");
    if((pulsewidth >= 900) && (pulsewidth <= 1200))
    {
      setRelay(i,HIGH);
    }
    else
    {
      setRelay(i,LOW);
    }
  }
  count++;
}

Pin 8 is connected to Rx channel 1, and i would expect that Led on pin 2(ch1) turns on when respective stick is at (900 to 1200) position, the same for the other 3 channels.

My Futaba Tx is an FF7, and my receiver is an Octava MZK 8ch (berger).

i can see, the 4 channels are assuming 1500 by default, thats all i see .

GMV

If you are connected to the output of one channel from the receiver then you will only get a single channel recognized by the decoder. ServoDecode needs to get pulses for all the channels. Have a read through the early part of the other thread for more information about this.

Thanks mem, as suspected, this is going to be an hard task for me to get it working, but i¬īll try to change the code one more time ! >:(

GMV