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.
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
}
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.