Hello all,
I want to control the sampling rate, can we make some GUI for controlling the sampling rate of the program.
In the given code, I want to change in sampling rate, for changing the sample rate I have change baud rate and also used the timer but still no change in sampling rate. I have use timer 3 (shown in the code ) and changed value (1e4) with a different time period. it gives the only approx 40 samples per second and the required sampling rate is 20HZ to 3KHZ.
Please provide some solution for changing the sampling rate and this sampling rate can be changed through GUI.
#include <avdweb_SAMDtimer.h>
#include <Scheduler.h>
#include <time.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_BMP280.h>
#define VBATPIN A7
#define BMP_SCK (SCL)
#define BMP_SDI (SDA)
int count = 0;
int VCC = A4;
int led = 5;
int led1 = 10;
int led2 = 11;
int led3 = 12;
int led4 = 13;
const int chipSelect = 4;
const int xInput = A0;
const int yInput = A1;
const int zInput = A2;
int RawMin = -11;
int RawMax = 1020;
// Take multiple samples to reduce noise
const int sampleSize = 1;
Adafruit_BMP280 bmp; // I2C
File dataFile;
char filename[15];
uint16_t fileNumber=0;
SAMDtimer mytimer1 = SAMDtimer(3, TC_COUNTER_SIZE_16BIT, A0, 1e4);
SAMDtimer mytimer2 = SAMDtimer(3, TC_COUNTER_SIZE_16BIT, A1, 1e4);
SAMDtimer mytimer3 = SAMDtimer(3, TC_COUNTER_SIZE_16BIT, A2, 1e4);
SAMDtimer mytimer4 = SAMDtimer(3, TC_COUNTER_SIZE_16BIT, SCL, 1e4);
SAMDtimer mytimer5 = SAMDtimer(3, TC_COUNTER_SIZE_16BIT, SDA, 1e4);
void setup(){
Serial.begin(250000);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
// pinMode(led, INPUT_PULLUP);
Serial.println(F("BMP280 test"));
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
while (!Serial){
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
strcpy(filename, "/LOG00000.TXT");
for(uint16_t i = 0; i<10000; i++) {
filename[8] = '0' + i%10;
filename[7] = '0' + (i/10)%10;
filename[6] = '0' + (i/100)%10;
filename[5] = '0' + (i/1000)%10;
filename[4] = '0' + i/10000;
Serial.println(filename);
if (! SD.exists(filename)){
fileNumber=i;
break;
}}
}
uint16_t lineNum=0,lineNumLimit=1000;
uint8_t i = 0;
int count1 = 1;
void loop()
{
count1++;
Serial.println(count1);
uint32_t sec = micros() / 1000000;
uint32_t milli = millis();
uint32_t mil = milli % 1000;
//uint32_t minn60 = minn % 60;
uint32_t sec60 = sec % 60;
uint32_t minn = sec / 60;
uint32_t minn60 = minn % 60;
uint32_t hour = sec / 3600;
uint32_t hour60 = hour % 60;
//Read raw values
int xRaw = ReadAxis(xInput);
int yRaw = ReadAxis(yInput);
int zRaw = ReadAxis(zInput);
// Convert raw values to 'milli-Gs"
long xScaled = map(xRaw, RawMin, RawMax, -200000, 200000);
long yScaled = map(yRaw, RawMin, RawMax, -200000, 200000);
long zScaled = map(zRaw, RawMin, RawMax, -200000, 200000);
// re-scale to fractional Gs
float xAccel = xScaled / -1000.0;
float yAccel = yScaled / -1000.0;
float zAccel = zScaled / 1000.0;
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");
Serial.print("X, Y, Z :: ");
Serial.print(xRaw);
Serial.print(", ");
Serial.print(yRaw);
Serial.print(", ");
Serial.print(zRaw);
Serial.print(" :: ");
Serial.print(xAccel,0);
Serial.print("G, ");
Serial.print(yAccel,0);
Serial.print("G, ");
Serial.print(zAccel,0);
Serial.println("G");
if((lineNum%lineNumLimit) == 0)
{
dataFile.close();
filename[8] = '0' + fileNumber%10;
filename[7] = '0' + (fileNumber/10)%10;
filename[6] = '0' + (fileNumber/100)%10;
filename[5] = '0' + (fileNumber/1000)%10;
filename[4] = '0' + fileNumber/10000;
Serial.print("\ncurrent filename is : ");
Serial.println(filename);
fileNumber++;
dataFile = SD.open(filename, FILE_WRITE);
dataFile.print("HH:MM:SS:mmmm - x");
dataFile.print(",");
dataFile.print("y");
dataFile.print(",");
dataFile.print("z");
dataFile.print(",");
dataFile.print("Temp");
dataFile.print(",");
dataFile.print("Press");
dataFile.print(",");
dataFile.println("Altitu");
}
dataFile.print(hour60);
dataFile.print(":");
dataFile.print(minn60);
dataFile.print(":");
dataFile.print(sec60);
dataFile.print(":");
dataFile.print(mil);
dataFile.print(" - ");
dataFile.print(xAccel,0);
dataFile.print(",");
dataFile.print(yAccel,0);
dataFile.print(",");
dataFile.print(zAccel,0);
dataFile.print(",");
dataFile.print(bmp.readTemperature());
dataFile.print(",");
dataFile.print(bmp.readPressure());
dataFile.print(",");
dataFile.println(bmp.readAltitude(1013.25));
// dataFile.println(count1);
lineNum++;
}
// Take samples and return the average
int ReadAxis(int axisPin)
{
long reading = 0;
analogRead(axisPin);
delay(1);
for (int i = 0; i < sampleSize; i++)
{
reading += analogRead(axisPin);
}
return reading/sampleSize;
}