I have an Arduino Due that I'm doing a lot of really neat stuff with in one section of code I'm handeling a flowsensor and getting readings from it. the flowsensor is hooked up to an interrupt as it is a hallsensor device. The problem I'm having is that my math is based off of a single seconds worth of collection data, when I turn on the interrupt and sleep for a second do the calculations for flowrate in gp/m it works great. For My purposes however I can't afford to pause and collect data, so instead I calculate a change in time of greater than a thousand milliseconds. then I take the amount of time passed since the last loop, devide it by 1000 milliseconds or 1 second then I have the percentage of 1 second stored in a variable, I multiply the count by the percentage and should arrive at the number of counts per second. This however results in an ever growing number regardless of the fact that I zero the numbers prior to the next loop. Below I've included the way I formatted the expressions and the loop code for the flowsensor, I'd post the whole code but I don't want to waste space on stackoverflow and I know that portion of code already works with the sleep method.
long currentMillis = 0;
long lastMillis = 0;
int checkMillis = 0;
volatile int NbTopsFan; //measuring the rising edges of the signal
int newNbTopsFan;
float realNbTopsFan;
float realMillis;
float Calc;
float Calcd;
int Calcf;
int hallsensor = 7;
void loop()
{
if (lastMillis = 0) {
lastMillis = millis();
}
currentMillis = millis();
checkMillis = (currentMillis - lastMillis);
if (checkMillis >= 1000) {
realMillis = (checkMillis / 1000);
realNbTopsFan = (NbTopsFan * realMillis);
newNbTopsFan = realNbTopsFan;
Calc = (newNbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate
Calc = (Calc / 60);
Calc = (Calc / 3.78541);
Calc = (Calc * 1.47);
Calcd = (Calc * 100);
Calcf = Calcd;
NbTopsFan = 0;
lastMillis = 0;
checkMillis = 0;
realMillis = 0;
realNbTopsFan = 0;
newNbTopsFan = 0;
}
}
Your first line should read:
if ( lastMillis == 0 ) {
lastMillis = millis();
}
You need the double = to evaluate the variable
Related
based on some tutorial code I've found I coded a little synthesizer with three oscilators and four different waveform. It works well and I want to add an LFO to module the sounds. Since I didn't coded everything on my own I'm a bit confused of how I could fit the LFO formula on my code. This is more or less what I tried in order to implement the LFO formula on a sinewave.(This formula is something like this: sinewaveFormula + 0.5 * Sinefreq * sin(2pi*1) * time)
double normalize(double phase)
{
double cycles = phase/(2.0*pi);
phase -= trunc(cycles) * 2.0 * pi;
if (phase < 0) phase += 2.0*pi;
return phase;
}
double sine(double phase)
{ phase = normalize(phase); return (sin(phase));}
static void build_sine_table(int16_t *data, int wave_length) {
double phase_increment = (2.0f * pi) / (double)wave_length;
double current_phase = 0;
for(int i = 0; i < wave_length; i++) {
int sample = synthOsc(current_phase, oscNum, selectedWave, selectedWave2, selectedWave3, intensity, intensity2, intensity3) + 0.5 * ((current_phase* wave_length) / (2*pi)) * sin(2*pi*(1.0)) * wave_length;
data[i] = (int16_t)sample;
current_phase += phase_increment;
}
}
static void write_samples(int16_t *s_byteStream, long begin, long end, long length) {
if(note > 0) {
double d_sample_rate = sample_rate;
double d_table_length = table_length;
double d_note = note;
// get correct phase increment for note depending on sample rate and table length.
double phase_increment = (get_pitch(d_note) / d_sample_rate) * d_table_length;
// loop through the buffer and write samples.
for (int i = 0; i < length; i+=2) {
phase_double += phase_increment;
phase_int = (int)phase_double;
if(phase_double >= table_length) {
double diff = phase_double - table_length;
phase_double = diff;
phase_int = (int)diff;
}
if(phase_int < table_length && phase_int > -1) {
if(s_byteStream != NULL) {
int16_t sample = sine_waveform_wave[phase_int];
target_amp = update_envelope();
if(smoothing_enabled) {
// move current amp towards target amp for a smoother transition.
if(current_amp < target_amp) {
current_amp += smoothing_amp_speed;
if(current_amp > target_amp) {
current_amp = target_amp;
}
} else if(current_amp > target_amp) {
current_amp -= smoothing_amp_speed;
if(current_amp < target_amp) {
current_amp = target_amp;
}
}
} else {
current_amp = target_amp;
}
sample *= current_amp; // scale volume.
s_byteStream[i+begin] = sample; // left channel
s_byteStream[i+begin+1] = sample; // right channel
}
}
}
}
}
The code compile but there's no LFO on the sine. I don't understand how I could make this formula work with this code.
It may help to get a basic understanding of how a LFO actually works. It is not that difficult - as an LFO is just another oscillator that is mixed to the waveform you want to modulate.
I would suggest to remove your LFO formular from your call of synthOsc(), then you get a clean oscillator signal again. As a next step, create another oscillator signal for which you can use a very low frequency. Mix both signals together and you are done.
Expresssed in simple math, it is like this:
int the_sample_you_want_to_modulate = synthOsc1(...);
int a_sample_with_very_low_frequency = synthOsc2(...);
Mixing two waveforms is done through addition:
int mixed_sample = the_sample_you_want_to_modulate + a_sample_with_very_low_frequency;
The resulting sample will sweep now based on the frequency you have used for synthOsc2().
As you can see, to implement an LFO you actually do not need a separate formular. You already have the formular when you know how to create an oscillator.
Note that if you add two sine oscillators that have the exact same frequency, the resulting signal will just get louder. But when each has a different frequency, you will get a new waveform. For LFOs (which are in fact just ordinary oscillators - like in your build_sine_table() function) you typically set a very low frequency: 1 - 10 Hz is low enough to get an audible sweep. For higher frequencies you get chords as a result.
In short, I am reading a .wav file in MATLAB for the purposes of sending it to the ESP32 for an FFT analysis. The .wav file in question contains a recording of a Corona effect. My file has 96223 samples when inputted into MATLAB.
For now, I am trying to just get back a checksum so I can know that the data is sent correctly.
I have already tried using the code I've written for smaller sample sizes. For example, I get back the correct checksum when I send 200 samples although the code takes longer than I want it to take which is not good. More than that though, and I never get anything back because of timeouts.
This is my MATLAB code:
esp = serial('COM3');
set(esp, 'DataBits' , 8);
set(esp, 'StopBits', 1);
set(esp, 'BaudRate', 9600);
set(esp, 'Parity', 'none');
set(esp, 'terminator', 'LF');
%filename = 'test100.wav';
%corona = audioread(filename);
load('corona')
fopen(esp);
pause(0.1)
for i = 1:200
fprintf(esp, '%5.9f\n', corona(i,1));
pause(0.1);
end
output = fscanf(esp, '%f\n') %read the checksum
fclose(instrfind);
And this is my Arduino code:
#include <Arduino.h>
float sentData[200]; //initialize data array
int i = 0;
const int ledPin = 26;
float checksum = 0;
int CNT = 0;
void printFloat(float value, int places);
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
while (Serial.available() < 200)
{
digitalWrite(ledPin, HIGH); //keep the LED on while the data is being sent
}
while (Serial.available() != 0)
{
sentData[i] = Serial.parseFloat(); //parse the data to the array
i++;
}
Serial.flush();
delay(500);
digitalWrite(ledPin, LOW); //turn off the LED when data is fully parsed
for (size_t x = 0; x < 200; ++x)
{
checksum += sentData[x]; //calculate the sum of all elements in the sentData array
}
printFloat(checksum, 10); //send the checksum to the serial port for reading
}
void loop()
{
}
void printFloat(float value, int places)
{
// this is used to cast digits
int digit;
float tens = 0.1;
int tenscount = 0;
int i;
float tempfloat = value;
// if this rounding step isn't here, the value 54.321 prints as 54.3209
// calculate rounding term d: 0.5/pow(10,places)
float d = 0.5;
if (value < 0)
d *= -1.0;
// divide by ten for each decimal place
for (i = 0; i < places; i++)
d /= 10.0;
tempfloat += d;
// first get value tens to be the large power of ten less than value
if (value < 0)
tempfloat *= -1.0;
while ((tens * 10.0) <= tempfloat)
{
tens *= 10.0;
tenscount += 1;
}
// write out the negative if needed
if (value < 0)
Serial.print('-');
if (tenscount == 0)
Serial.print(0, DEC);
for (i = 0; i < tenscount; i++)
{
digit = (int)(tempfloat / tens);
Serial.print(digit, DEC);
tempfloat = tempfloat - ((float)digit * tens);
tens /= 10.0;
}
// if no places after decimal, stop now and return
if (places <= 0)
return;
// otherwise, write the point and continue on
Serial.print('.');
// now write out each decimal place by shifting digits one by one into the ones place and writing the truncated value
for (i = 0; i < places; i++)
{
tempfloat *= 10.0;
digit = (int)tempfloat;
Serial.print(digit, DEC);
// once written, subtract off that digit
tempfloat = tempfloat - (float)digit;
}
}
I expected to get back a checksum but I get a timeout when using very large sample sizes. I should also add that even though the ESP32 should be able to handle my file, I can't just push the whole file into the serial port because I get a buffer overflow error. Is there a solution to this?
First %5.9f doesn't make sense to me.
Thats minimum 5 characters with 9 digit precision. That 5 doesn't make sense as you'll always have at least 11 characters with 9 digit precision
Then let me do some maths for you:
96000 samples, 12 characters each (including \n) is a total of 10368000 bits.
At 9600 baud that's 1080 seconds of transfer time. -> 18 minutes.
As you add 0.1s pause after each sample you add another 9600 seconds to that.
Which leaves you with a total of 178 minutes (3 hours) of transfer time.
What do you expect?
For 200 samples its still 22,25s.
Hello i am creating a speedcube timer i just got the time centered but then i noticed that the time on it was too slow, I tried changing the usleep function from 1000 but it was either to fast or to slow, any ideas?
#include <ncurses.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int minutes = 0, milliseconds = 0, seconds = 0, x = 0, y = 0, text = 6, textminutes = 0, textseconds = 0, textmilliseconds = 0;
initscr();
while(1)
{
/*This block of code centers the text on the screen by incrementing each variable by one
for each number starting at ten, Then prints the time.*/
getmaxyx(stdscr,y,x);
if (seconds == 60 && minutes == 10){
textminutes += 1;
}
if (milliseconds == 1000 && seconds == 10){
textseconds += 0;
}
if (milliseconds == 10){
textmilliseconds += 1;
}
else if (milliseconds == 100)
{
textmilliseconds += 1;
}
else if(milliseconds == 1000)
{
textmilliseconds += 1;
}
int left_row = (x / 2) - (3 + textminutes + textseconds + textmilliseconds / 2);
mvprintw(y/2, left_row,"%d : %d : %d", minutes, seconds, milliseconds);
/*Sleep for 1 millisecond the increment the milliseconds
var i don't think that the timing is right though.
Then it refreshes and clears the screen to fetch the new contents.*/
usleep(1000);
milliseconds++;
if(milliseconds == 1000)
{
milliseconds = 0;
textmilliseconds -= 2;
seconds++;
if(seconds == 60)
{
seconds = 0;
textseconds -= 1;
minutes++;
}
}
refresh();
clear();
}
endwin();
return(0);
}
Your code seems to be written with the assumption that your process can run reliably once every millisecond, but you're probably running it on an operating system that has to perform other tasks, so you won't get to run every millisecond. Also, usleep might not be as accurate as you are hoping for it to be, and you're not accounting for the time it takes to do your calculations and output data to the terminal.
It's OK to use usleep to conserve CPU time. But when you want to figure out what time it is for displaying the time to the user, you should use a function that actually gets the time, like the C clock_gettime function perhaps.
You should never rely on an internal accumulator to accumulate elapsed wall time. Doing so not only captures the time consumed by usleep(), but also the execution time of formatting that string and any other calculations (the string format is likely to the big one).
Instead, get the system time at the start. Then, whenever you need to sample a new amount of time, get the system time again and subtract the start time from the now time. That'll give you the elapsed time and then you can adjust from there.
I am creating a project in nordic Micro that reads a value from an Analog input termninal an output it into UART. I am now trying to compress the data using GE Proficy Historian Compression so that only changed data are outputted in the UART. But my code is not working. Outputted data are sometime still redundant.
The idea of the program is to generate an interrupt every certain amout of time, read the adc value and if its different than previous value, output it to the UART port.
the algorithm is explained here
http://www.evsystems.net/files/GE_Historian_Compression_Overview.ppt
The main portion of the code that handles the interrupt is as shown below
void ADC_IRQHandler(void)
{
/* Clear dataready event */
NRF_ADC->EVENTS_END = 0;
// write ADC value to UART port
// Compression Algorithm should occur here
uint8_t current_Val= NRF_ADC->RESULT;
//simple_uart_put(current_evaluation);
// Construct error bands around the held point
float U_tolerance = current_Val + error_tolerance;
float L_tolerance = current_Val - error_tolerance; // integer type is selected since lower tolerance could be negative
if( first_Run == false)
{
float slope = ((current_Val - Archived_Val) / (held_Time - Archived_Time)) ;
if (slope > U_Slope || slope > L_Slope)
{
Archived_Val = current_Val; // update the archived value
held_Time = 1; // reset held time
simple_uart_put(current_Val);
first_Run = true;
Archived_Val = current_Val;
}
else
{
float Upper_Slope = (U_tolerance - Archived_Val) /( held_Time - Archived_Time);
float Lower_Slope = (L_tolerance - Archived_Val)/(held_Time- Archived_Time);
if(Upper_Slope < U_Slope) // lowest upper slope is always taken as a blanket boundry
{
U_Slope = Upper_Slope;
}
if(Lower_Slope < L_Slope)
{
L_Slope = Lower_Slope;
}
held_Time += time_increment;
}
}
if (first_Run == true) // first held point always outputted
{
// calculate the slopes of the two lines
float Upper_Slope = (U_tolerance - Start_Up_Val) /( held_Time - Archived_Time);
float Lower_Slope = (L_tolerance - Start_Up_Val)/(held_Time- Archived_Time);
// Update Max and Min slopes
if(Upper_Slope < U_Slope) // lowest upper slope is always taken as a blanket boundry
{
U_Slope = Upper_Slope;
}
if(Lower_Slope < L_Slope)
{
L_Slope = Lower_Slope;
}
held_Time += time_increment;
first_Run = false;
Archived_Val = current_Val;
}
}
The veriables are defined as follow
> uint32_t error_tolerance = 50; // error tolerance value for swining door algorithm
uint8_t Start_Up_Val = 100;
float held_Time = 1;
int Archived_Time = 0;
float U_Slope = 2500;
float L_Slope = 0;
//float slope;
uint8_t Archived_Val;
bool GE_Comp(uint8_t, uint8_t, uint8_t, int);
bool first_Run = true;
float time_increment = 0.1;
Thank you all for your contribution and mostly to #Weather Vane . It was exactly as you and others suggested, the interrupt handler was executing too much coding which prohibited it from proper functionality. I now fixed the problem by diverging the parts of the code to the main function as suggested.
Regareds
I want to run my Arduino for a specific amount of time (say 60 seconds) and collect
data from the analog pin with a specific sampling rate (say four samples a second).
I got the code working in matlab... but I want to use the arduino environment.
Please help me convert it.
a_pin = 0;
fs = 4; % sampling frequency (samplings per second)
mt = 20; % time for measurements
ind = 1;
nind = 1;
last_beep = 0;
tic;
while toc < mt
time(ind) = toc;
v(ind) = a.analogRead(a_pin);
% wait for appropriate time for next measurement
while( nind == ind )
nind = floor(toc*fs) + 1;
end
ind = nind;
end
Ok this is what i have so far in my sketch. Would this measure for 10 seconds taking
readings every 5?
int sensePin = 0;
unsigned long starttime = 0;
unsigned long endtime = 0;
int i = 0;
int n;
const int sizeofv = 50;
int v[sizeofv];
void setup(){
pinMode(sensePin, INPUT);
Serial.begin(9600);
}
void loop() {
starttime = millis();
endtime = starttime;
while ((endtime - starttime) <= 10000) // do this loop for up to 1000mS
{
i = i + 1;
v[i] = analogRead(sensePin);
endtime = millis();
delay(5000);
}
for(n=0; n < sizeofv; n++)
{
Serial.print(v[n]);
Serial.print('\n');
}
while(1) { }
}
I recommend using SimpleTimer library, it does all the work for you and is road proven.
There's one bug in your code, you're incrementing i prior to assigning the first value. So when you'll loop over the v[] array, you'll end up printing v[0] which will have an undefined value, and you'll never print the last recorded value of v (if you did not end up overflow the buffer and write outside its boundaries).
There's one glitch with your code, it's that you make a condition that you need to end it at 10000, and you make two delays of 5000. Theoretically it looks ok, but in practice, you may get a surprise having only one value. That's because code takes time to execute, and an analogRead() is particularly slow.
I think using loops within the loop() function make things more complicated, and add close to no flexibility. So here you have another take you could try, to use the loop() only by tracking states of the loop, based on some simple calculations:
#define TIME_WINDOW 10000
#define NB_STEPS 2
void setup() {
// ...
endtime = millis();
}
bool over = false;
void loop() {
if (!over) {
curtime = endtime - millis();
// get in this block for up to 1000mS
if (curtime <= TIME_WINDOW) {
v[i] = analogRead(sensePin);
// increments only once when 5000 has been reached.
if ((curtime / (TIME_WINDOW/NB_STEPS)) != i)
i = (curtime / (TIME_WINDOW/NB_STEPS));
} else {
for(n=0; n < NB_STEPS; n++)
Serial.println(v[n]);
over = true;
}
}
}
I did not test my code, so it may fail or have typos, but the idea is the following:
we define the time window and the number of measure steps
while the time window is not over:
store the measurement in the current array at current index
if integer division of spent time over time_window is different from current index
store the new value into the current index
e.g.: i = 4000/(10000/2) <=> i = 4000/5000 <=> i = 0 and i = 6000/(10000/2) <=> i = 1
when the time window is over:
print out each result
set over to true thus we're not entering the loop again
But a simpler (non flexible) solution for you would be:
void setup() {
int value1, value2, value3, value4;
value1 = analogRead(sensePin);
delay(2500);
value2 = analogRead(sensePin);
delay(2500);
value3 = analogRead(sensePin);
delay(2500);
value4 = analogRead(sensePin);
Serial.println("first value: %d", value1);
Serial.println("second value: %d", value2);
Serial.println("third value: %d", value3);
Serial.println("fourth value: %d", value4);
}
and there you got your solution!
HTH