在android app中设置函数发生器频率来观察正弦波形

问题描述 投票:0回答:1

我构建了一个应用程序来观察实时图形。我在Arduino代码中将采样率设置为256hz。我通过函数发生器给出100hz的输出,但我获得了一个随机波形而不是正弦波形。

这是我的Arduino代码:

#include <SoftwareSerial.h> //import Software Serial library

SoftwareSerial myStream(0, 1); //pins for Rx and Tx respectively
int ECG;
void setup()
{
    // put your setup code here, to run once:
    pinMode(A0, INPUT); //LDR

    myStream.begin(115200);
    Serial.begin(9600);
}

void loop()
{
    ECG = analogRead(A0);
    Serial.println(ECG);

    if (myStream.available() > 0)
    {
        char re = myStream.read();

        switch (re)
        {
        case 'E':
            start();
            break;
        }
    }

    //about 256Hz sample rate
    delayMicroseconds(3900);
}

void start()
{
    while (1)
    {
        myStream.print('s');
        myStream.print(floatMap(analogRead(ECG), 0, 1023, 0, 255), 2);
        //about 256Hz sample rate
        delayMicroseconds(3900);

        if (Serial.available() > 0)
        {
            if (Serial.read() == 'Q')
                return;
        }
    }
}

float floatMap(float x, float inMin, float inMax, float outMin, float outMax)
{
    return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}

这是我的电路图。另外,我将示波器输出连接到A0和公共地。

enter image description here

如何在我的应用程序中获取正弦波形。非常感谢您的帮助。

arduino android-bluetooth
1个回答
0
投票

使用~256 Hz的采样率,每个100 Hz源的周期内只能得到2个异步读数。 analogRead增加了(在16 MHz Arduino上100 ...150μs)相当大的延迟抖动。

不确定SoftwareSerial发送@ 115200。 (收到的速度不会很快)

浮动也很慢。

不确定你的期望。


首先检查原始数据:

void setup() {
    Serial.begin(115200);  // allows for 10 char/ms
}

void loop() {
   static unsigned long oldmillis;
   if (millis() != oldmillis) {
    oldmillis = millis();
    // 1 kHz sample rate :  
    int ECG = analogRead(A0)*100L / 1024;
    Serial.println(ECG);  // 0 .. 99
   }
}

也许添加您的开始/停止通信或检查Arduino SerialPlotter。当没关系的时候,测试你是否通过蓝牙将这些数字输入你的应用程序......

© www.soinside.com 2019 - 2024. All rights reserved.