Hi.
I'm currently writing code to read analog values every 7812 microseconds (128 Hz) sampling rate
Then I store the information in an array with 128 values and this takes 1 second to fill the array and then send it through the serial port
The problem is that when the array is full and sent, the processor takes 100 milliseconds to start taking new readings
I mean, when the transmission instructions are executed, it consumes 100 milliseconds, and at this time, the analog reading process does not take place, which means that 100 milliseconds are interrupted every one second in sampling.
float valHam[128];
long valTime[128];
float input=0;
unsigned long pmicros=micros();
int s1i = 0;
void setup() {
Serial.begin(115200);
pmicros=micros();
}
void loop() {
if (micros() - pmicros >= 7812) {//sampling
//Time Array
valTime[s1i]= millis();
input=analogRead(1);
valHam[s1i]=input;
s1i = ++s1i % 128;
if (s1i == 0) {sendData();}
pmicros =micros();
}
}
void sendData() {
for(int r=0; r<128; r++) {
Serial.print(valHam[r]);
if (r<127) {Serial.print(",");}
}
Serial.print("|");
for (int r=0; r<128; r++) {
Serial.print(valTime[r]);
if (r<127) {Serial.print(",");}
}
Serial.println("|");
}
Result Example:
0,0,0,0,0,0,0,0,0.......,0|1000,1008,1016,.....2024|
0,0,0,0,0,0,0,0,0.......,0|2132,2140,2148,.....,3165|
its mean the differnce time into tow reading 100ms (2132-8-2024)=100
can you help me to reduce that 100ms delay in sending?
Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).
—-
Print becomes blocking if you fill up the outgoing buffer… why do you wait to have 128 measures to start printing? Why don’t you print as you go?
Thanks
i send it to mobile app via blutooth module and when sending data every 8 milliseconds, the application, I saw sometimes a loss in some values, so I resorted to sending data every 1 second in the form of a packet
1. You have an int type array containing 128 samples which means that you have 128x2-byte (256-byte) information.
2. At Bd = 115200 and frame length of 10, you need about (1/115200)* (2568 + 2562) = 22 ms to transmit the whoe array of Step-1. Why are you saying that it is 100 ms?
3. The 22 ms transmission dealy time of Step-2 could be reduced to about 11 ms ususing Bd = 230400.