Cannot detect pulse between upper and lower limit

Hi All

I am playing with a 433Mhz Transmitter and Receiver , captured the data on my digital scope so now I want to

reproduce (Decoding) the results with a Nano.

I want to start reading the data after a HI pulse of 2300uS , every few seconds the Receiver gives out 2 pulses equal to 3050uS.

The serial monitor displays these pulses with a lot of zeros in between , this should not happen .The 2300uS pulse

must first be detected and were does the Zeros come from ?

Serial monitor: This should not happen.

123
0  0  0  0  0  0  0  3051  3364  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3049  3360  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3050  Decoding 
123
0  0  0  0  0  0  0  3050  3362  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3049  3367  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3048  Decoding 
123
0  0  0  0  0  0  0  3047  3368  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3047  3361  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3049  Decoding 
123
0  0  0  0  0  0  0  3049  3361  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3050  3354  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3048  Decoding 
123
0  0  0  0  0  0  0  3054  3365  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3042  3364  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3048  Decoding 
123
0  0  0  0  0  0  0  3053  3366  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3052  3363  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3053  Decoding

When Transmitter is pressed.

123
0  0  0  0  0  0  0  3056  3366  0  0  0  0  0  0  0  0  0  229  231  223  227  229  218  228  226  220  226  225  222  219  224  218  221  2325  455  450  447  429  449  Decoding 
123
0  0  239  230  230  231  224  224  223  224  221  222  211  218  213  225  222  217  2335  451  204  443  449  445  210  216  445  435  446  446  422  434  440  194  431  203  203  201  201
const unsigned int numReadings = 40;
unsigned int myArray[numReadings];
const int readPin = 2; //D2

long startmilis;
long endmilis;
long duration;

long  sync = 0;
int count = 0;
int val1 = 0;

//==================
void readSyncLow()
{
  sync = pulseIn(readPin, LOW);
}
//==================
void readSyncHi()
{
  sync = pulseIn(readPin, HIGH);
}

//==================
void setup()
{
  pinMode (readPin, INPUT);
  digitalWrite(readPin, HIGH); //internal pull-up
  Serial.begin (9600);
}

void loop()
{
  Serial.print("Decoding");

  sync = 123;//Test value ..Does not change after readSyncHi ????

  while ((sync > 1000) && (sync < 2400))//Must be around 2300uS
  {
    readSyncHi();
  }

  for (unsigned int i = 0; i < numReadings; i++ )//i = 100
  {
    duration = pulseIn (readPin, HIGH);
    myArray[i] = duration ;
  }
  Serial.println(" ");
  Serial.println(sync);

  for (unsigned int i = 0; i < numReadings; i++)
  {
    Serial.print ( myArray[i]);
    Serial.print("  ");
  }

  delay(5000);
}

Thanks in advance for any help.

readSyncHi is never being called. Your while loop prevents it because sync is set to 123.

Yep .. I totally messed that up , the 123 is just a test value to see what the serial monitor will print .

So how do I keep on reading pulsIn until a value of close to 2300uS is detected?

Maybe get rid of the while and use a if condition after pulseIn?

Thanks.

You could use do while instead.

I tried the do ..while statement but the results is the same.

[color=#434f54][iurl=https://www.arduino.cc/reference/en][color=#374146]Reference[/color][/iurl] > [iurl=https://www.arduino.cc/reference/en#language][color=#374146]Language[/color][/iurl] > [iurl=https://www.arduino.cc/reference/en#structure][color=#374146]Structure[/color][/iurl] > [iurl=https://www.arduino.cc/reference/en#control-structure][color=#374146]Control structure[/color][/iurl] > Dowhile[/color]
do...while[Control Structure]
[color=#434f54][color=#e67e22]Description[/color]
[color=#000000]The 
[code]do…​while

loop works in the same manner as the

while

loop, with the exception that the condition is tested at the end of the loop, so the do loop will always run at least once.[/color]
Syntax

[code]do {
  // statement block
} while (condition);[/code]

Parameters

condition

: a boolean expression that evaluates to

true

or

false

.[/color]
Example Code

[code]int x = 0;
do {
  delay(50);          // wait for sensors to stabilize
  x = readSensors();  // check the sensors
} while (x < 100);[/code]

[/code]

This is my attempt.

do
  {
    sync = pulseIn(readPin, HIGH); //
  }
  while (sync < 2000 && sync > 2400);// or even... while (sync > 2000 && sync < 2400)

The serial monitor prints sync as 0 , but the Arduino reference states that the do will always execute at least once.

The do while is running once but apparently it didn't see a pulse before timing out. It doesn't repeat because you need an or instead of the and. i.e. || not &&.

mikedb:
I want to start reading the data after a HI pulse of 2300uS , every few seconds the Receiver gives out 2 pulses equal to 3050uS.

. Are the 3050 pulses HIGH or LOW?

mikedb:
The serial monitor displays these pulses with a lot of zeros in between , this should not happen .The 2300uS pulse

must first be detected and were does the Zeros come from ?

The pulseIn() function has a timeout which defaults to one second (1000000 microseconds). If it doesn't see the start and end of a pulse before the timeout occurs, it returns 0. To set your own timeout, use the optional third argument to pulseIn(). It's an unsigned long number of microseconds.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.