Read PPM with DigitalRead and Serial.priint result

I am trying to read the the PPM output from one Arduino and display the output on another.
There are only two different signals sent out
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 //OFF
Or
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1, 0, 1, 0, 1, 1, 1, 1, 0, 0 //ON
The code that sends them is below, I had hoped it would simply be a case of a common ground and digital out on master connected to digital in on the slave and then DigitalRead that pin and then receive one of the strings above....how wrong could I have been ?
I am getting various readings, none of which relate to data sent.
Does anyone please have a start up example I could use as my foundation

#define transmitPin 4 // Digital Pin 4
 
// on
bool data[] = {
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1, 0, 1, 0, 1, 1, 1, 1, 0, 0
};
 
// off
//bool data[] = {
//  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
//  0, 0, 0, 0, 0, 0, 0, 0, 0, 0
//};
 
int shortDelay = 250;
int longDelay = 500;
int preambleDelay = 1000;
 
void setup() {
  Serial.begin(9600);
  pinMode(transmitPin, OUTPUT);
  delay(3000);
}
 
void loop() {
  sendPreamble();
  sendData();
  sendPreamble();
  sendData();
  delay(30000);
}
 
void sendPreamble() {
  for (int i = 0; i < 4; i++) {
    digitalWrite(transmitPin, HIGH);
    delayMicroseconds(preambleDelay);
    digitalWrite(transmitPin, LOW);
    delayMicroseconds(preambleDelay);
  }
}
 
void sendData() {
  for (int i = 0; i < sizeof(data); i++) {
    bool b = data[i];
    if (b == 1) {
      digitalWrite(transmitPin, HIGH);
      delayMicroseconds(longDelay);
      digitalWrite(transmitPin, LOW);
      delayMicroseconds(shortDelay);
    } else {
      digitalWrite(transmitPin, HIGH);
      delayMicroseconds(shortDelay);
      digitalWrite(transmitPin, LOW);
      delayMicroseconds(longDelay);
    }
  }
}

I see a sketch that looks like it turns pin 4 on and off but I don't see what's supposed to read that.

In the meantime you might find the contents of this page interesting. Your 25 bits of data will fit in a single long or unsigned long variable.

http://playground.arduino.cc/Code/BitMath

GoForSmoke:
I see a sketch that looks like it turns pin 4 on and off but I don't see what's supposed to read that.

In the meantime you might find the contents of this page interesting. Your 25 bits of data will fit in a single long or unsigned long variable.

Arduino Playground - HomePage

Thanks I will read up on BitMath

That code is for the transmitter (master) I am struggling for code for the receiver (slave) that will read the string

With this sketch I can read the received data on serial monitor like this
0
0
0
0
0
0
0
1
1
0
1
0
1
1
How do I alter it to display as
0000001101011

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
}

void loop() {
  int data = digitalRead(2);
  Serial.println(data, DEC);
}

0's and 1's can be stored, operated on and read as bits.

Your transmit holds the pin HIGH or LOW for 250 to 500 millis at a time.

Your receive will read the pin about once per milli. It needs to look for preamble writes and then data writes but the relative HIGH and LOW times that you sent.

int shortDelay = 250;
int longDelay = 500;
int preambleDelay = 1000;

..........

// preamble is 4 times HIGH for 1 sec then LOW for 1 sec
void sendPreamble() {
  for (int i = 0; i < 4; i++) {
    digitalWrite(transmitPin, HIGH);
    delayMicroseconds(preambleDelay);
    digitalWrite(transmitPin, LOW);
    delayMicroseconds(preambleDelay);
  }
}

..........

void sendData() {
  for (int i = 0; i < sizeof(data); i++) {
    bool b = data[i];
    if (b == 1) {
// data 1 is HIGH for 0.5 sec then LOW for 0.25 sec
      digitalWrite(transmitPin, HIGH);
      delayMicroseconds(longDelay);
      digitalWrite(transmitPin, LOW);
      delayMicroseconds(shortDelay);
    } else {
// data 0 is HIGH for 0.25 sec then LOW for 0.5 sec
      digitalWrite(transmitPin, HIGH);
      delayMicroseconds(shortDelay);
      digitalWrite(transmitPin, LOW);
      delayMicroseconds(longDelay);
    }
  }
}

Consider that your reads won't always catch the full timing you still have

preamble times are > 500 millis up to 1000 millis with more than 1000 is some error, perhaps not sending.

long (Morse code dash) times are > 250 millis and < 500.

short (Morse code dot) times are > 50 millis and < 250.
The > 50 is to put some kind of limit to filter out spikes or bad transmit errors.

If you got the transmit code from somewhere, spend more time knowing what it does and things will clear up. Add lines to make led13 turn on and off to match pin 4 HIGH and LOW just as an aid in that effort.

How do I alter it to display as
0000001101011

Don't use println() where you don't want a new line added after you print, just use print().
Your code will need to know when the message ends for when to make a new line.

Thank you for your help, this is getting harder than I imagined.
The signal is for a simple on off switch
0000000000000000000000000 turns off
and
0000000000000001010111100 turns on
I need to increase the range and reliability that currently uses 433mhz transmitter and receiver, I get a lot of interference in that frequency range so wanted to use HC-05, which I would also then be able to use an app on my phone to control. Alas the actual output of the real transmitter (the code is actually a emulator for the real one) uses ppm and not compatible with the HC-05 that requires ttl.
Fundamentally my goal is to use a ATTINY85 to read the ppm and convert it to ttl for the HC-05
Hope this makes sense

Why do you not concentrate on the trailing 8 bits?

Throw away the leading bits and make a simple comparison with the last BYTE.
If it contains at least 3 "1"s it should be a "1" - even with a lot of transmission problems.

arduinoaleman:
Why do you not concentrate on the trailing 8 bits?

Throw away the leading bits and make a simple comparison with the last BYTE.
If it contains at least 3 "1"s it should be a "1" - even with a lot of transmission problems.

With the following sketch I am half way there
When a 1 is recieved it prints "1on" the signal is sent every 20seconds or so, my next part I need help with is if 1 is not recieved within 25seconds it would print "0off"
I have looked at blink without delay, but can not get my head round how to reset a timer to 25seconds every time 1 is recieved. If 1 is not recieved and the counter reaches 0 "0off" is printed and loops until 1 is received once more.

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
}
 
void loop() {
  int byte = digitalRead(2);
  Serial.print(byte);
  {if (byte >0)
  Serial.println("on");
 
  }
}

Can anyone please advise where I am going wrong with this code?
it has a countdown timer that once it gets to <1 it prints POWER OFF
The timer gets 4000 added each time a high pulse hits pin 2 (6 high within signal to turn ON no highs turn OFF)
On each loop timer = timer -- so in theory if the 6 highs are not received within 24ish seconds the timer runs out and prints POWER OFF.
The routine runs fine for a good few loops, then suddenly for no reason I can see the timer value will lose 60,000 putting its value to a negative. If I leave the sketch running the 4000 for each high gets added and we are back in business.
POWER ON
217
28000
POWER ON
223
28000
POWER ON
226
28000
POWER ON
216
28000
POWER ON
447
28000
POWER ON
448
32000
POWER OFF
224
-29536
POWER OFF
0
-29536
POWER OFF

Does anything jump out to anyone that could be causing the problem, apart from a thick operator ?

byte PWM_PIN = 2;
int pwm_value;
int timer;

void setup() {
  pinMode(PWM_PIN, INPUT);
  Serial.begin(115200);
}

void loop() {
  pwm_value = pulseIn(PWM_PIN, HIGH);
  timer = timer --;
  Serial.println(pwm_value);
  Serial.println(timer);


  if (pwm_value >= 400 && pwm_value <= 600)
  {



    timer = timer + 4000;

  }
  if (timer >= 1)
  {
    Serial.println("POWER ON");
  }
  if (timer <= 1)
  {
    Serial.println("POWER OFF");
  }

}

andi968:
Can anyone please advise where I am going wrong with this code?
it has a countdown timer that once it gets to <1 it prints POWER OFF
The timer gets 4000 added each time a high pulse hits pin 2 (6 high within signal to turn ON no highs turn OFF)
On each loop timer = timer -- so in theory if the 6 highs are not received within 24ish seconds the timer runs out and prints POWER OFF.
The routine runs fine for a good few loops, then suddenly for no reason I can see the timer value will lose 60,000 putting its value to a negative. If I leave the sketch running the 4000 for each high gets added and we are back in business.
POWER ON
217
28000
POWER ON
223
28000
POWER ON
226
28000
POWER ON
216
28000
POWER ON
447
28000
POWER ON
448
32000
POWER OFF
224
-29536
POWER OFF
0
-29536
POWER OFF

Does anything jump out to anyone that could be causing the problem, apart from a thick operator ?

byte PWM_PIN = 2;

int pwm_value;
int timer;

void setup() {
  pinMode(PWM_PIN, INPUT);
  Serial.begin(115200);
}

void loop() {
  pwm_value = pulseIn(PWM_PIN, HIGH);
  timer = timer --;
  Serial.println(pwm_value);
  Serial.println(timer);

if (pwm_value >= 400 && pwm_value <= 600)
  {

timer = timer + 4000;

}
  if (timer >= 1)
  {
    Serial.println("POWER ON");
  }
  if (timer <= 1)
  {
    Serial.println("POWER OFF");
  }

}

Resolved with a couple of extra lines, the problem always occurred when timer exceeded 32,000 it turned a positive into a negative !!
A little cheat is when the timer passes 10,000 I subtract 5,000, it now works like a charm

if (timer >= 10000)
  {
    timer = timer - 5000;
  }
  if (timer <= 0)
  {
    timer = +50;
  }

What you call timer should be called counter. It counts. Arduino has time functions and a standard C time library if you want to make things happen on time.

Change your counter to type unsigned long and if you do use millis() or micros(), use unsigned long variables for your time variables.