Decoding On-Off Keying modulation

Hi, I am having issues with coding a sketch that will decode the signal that is seen in the picture below.
What I am trying to do is basically sample the voltage at a specific interval using analogRead() and after that using an if.... else statement with a voltage threshold value to determine which values are going to be a '1' or '0' printed on the serial monitor.
I am having problems trying to sample a specific interval as my current code below is continuously sampling the voltage through analogRead().
Delays do not seem to be working probably because it is a blocking function.
Hopefully, the picture below can help to visualize what I am attempting.

Any suggestions on how to achieve this specific sampling interval would be appreciated.
I am sure there is a more intelligent way of approaching this issue.

Code/General Idea:

int analogPin = 3;     
                       

int val = 0;           



void setup()

{

  Serial.begin(9600);         

}



void loop()

{
  val = analogRead(analogPin);    
  Serial.println(val);
 
  if(val>100)
  {
    Serial.print(1);
  }
  else
  {
    Serial.print(0);
  }
  
}

Hello
try

if(val>100)
  {
    Serial.print("1");
  }
  else
  {
    Serial.print("0");
  }

Trying hard to see the improvement there

If you're not doing anything else, AND accounting for the time taken by analogRead, that shouldn't necessarily be a problem.

Hi, and thanks for the reply.
The time for analogRead to process (100 microseconds) was taken into account and there are no other processes running in the background.
I suppose there must be an issue with where the delay is positioned.
Previously it was positioned right after the if...else statement.
Could this be the issue ?

int analogPin = 3;     
                       

int val = 0;           



void setup()

{

  Serial.begin(9600);         

}



void loop()

{
  val = analogRead(analogPin);    
  Serial.println(val);
 
  if(val>100)
  {
    Serial.print(1);
  }
  else
  {
    Serial.print(0);
  }
  delay(); //Positioned delay here previously
}

How long?

It was set at 1.22ms.
Which is about two pulse widths of 560us each and adding the 100us analogRead processing time.

But that would be delayMicroseconds (1220).
Also you may need to take into account the slow baud rate.

Yeah, I think using delay(1.12) may have been affecting it along with the slow baud rate which I have overlooked entirely.
Anyway, thanks for the advice and I'll try and incorporate these inputs when I'm back in the lab.

You mean "delay(1)" :wink:

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