Tutaj kod, którego użyłam:
/*
* Copyright 2019, OYMotion Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "EMGFilters.h"
#define _DEBUG 0
#define SensorInputPin A0 // input pin number
// IMPORTANT NOTE: Please unplug any power adapters when using sEMG sensors.
// Only battery powered systems an be connected directly/indirectly to sEMG
// sensors.
//
// Connect sensor to arduino board then compile & upload this sketch.
// When running, open SerilaPlot window (Menu->Tools->Serial Plotter) then
// you can see the curves.
EMGFilters myFilter;
// Set the input frequency.
//
// The filters work only with fixed sample frequency of
// `SAMPLE_FREQ_500HZ` or `SAMPLE_FREQ_1000HZ`.
// Inputs at other sample rates will bypass
SAMPLE_FREQUENCY sampleRate = SAMPLE_FREQ_1000HZ;
// Time interval for processing the input signal.
unsigned long long interval = 1000000ul / sampleRate;
// Set the frequency of power line hum to filter out.
//
// For countries with 60Hz power line, change to "NOTCH_FREQ_60HZ"
NOTCH_FREQUENCY humFreq = NOTCH_FREQ_50HZ;
void setup() {
/* add setup code here */
myFilter.init(sampleRate, humFreq, true, true, true);
// open serial
Serial.begin(1000000);
}
void loop() {
// Note: `micros()` will overflow and reset every about 70 minutes.
unsigned long long timeStamp = micros();
int data = analogRead(SensorInputPin);
// filter processing
int dataAfterFilter = myFilter.update(data);
#if !_DEBUG
/*Serial.print(data);
Serial.print(" ");*/
Serial.println(dataAfterFilter);
#endif
// Usually, you should still have (interval - timeElapsed) to do other work.
// Otherwise, you would have to lower down the `sampleRate`.
unsigned long long timeElapsed = micros() - timeStamp;
#if _DEBUG
Serial.print("Filters cost time: ");
Serial.println(timeElapsed);
#else
delay(1);
#endif
}
W ostatnim #else producent zamiast delay(1); użył delay((interval - timeElapsed) / 1000);
Szczerze dopiero zaczęłam przygodę z mikrokontrolerami, dlatego zupełnie nie wiem w jaki sposób dokonywać pomiarów co 1ms, skoro delay służy do czego innego.
Dodano po 28 [minuty]:
Nie do końca rozumiem czemu w tym momencie:
delay((interval - timeElapsed) / 1000);
użyte jest interval - timeElapsed (jak dla mnie starczyłoby samo timeElapsed). No ale według tego co napisałeś @kicajbas do wykonywania pomiarów co określony czas nie używa się delay.
Mam już dane z czujnika (dataAfterFilter). Będę je zapisywać do pliku csv i obok kolumny z danymi muszę mieć czas, czyli skoro pomiary potrzebuję co 1ms mój csv powinien wyglądać tak:
dane z 1 pomiaru | 1ms
dane z 2 pomiaru | 2ms
dane z 3 pomiaru | 3ms
.
.
.
itd