Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
- Committer:
- 2236693B
- Date:
- 2018-02-14
- Revision:
- 4:878821708feb
- Parent:
- 3:c9f4c9c84e36
- Child:
- 5:651843de21ce
File content as of revision 4:878821708feb:
//Rango says, hey there Conor!
#include "mbed.h"
DigitalOut myled(LED1);
AnalogIn Ain(PTB1);
int const MAX_B = 10;
int const alpha = 0.5;
int const AVG_LEN = 160;
int sample_buffer[2*MAX_B] = {};
int local_buffer[2*MAX_B] = {};
int avg_buffer[AVG_LEN] = {};
int read = 0;
int write = 0;
int avg_write = 0;
int avg_sum;
bool first;
float avg;
float pre_avg;
Ticker sampler;
void sampling () { //Sample Signal
unsigned int sample = Ain.read_u16();
sample_buffer[write++] = sample;
write = write%(2*MAX_B);
}
float scale (float point) { //Multiply by scale factor
return point*7 ;
}
float filter (float output) { //Filter signal digtally to make more precise
float point = alpha*output + (1- alpha)*pre_avg;
point = scale(point);
return point;
}
float get_trend(){ //Normalise signal by removing trend
float point;
pre_avg = avg;
avg = avg_sum/AVG_LEN;
int normalised = avg_buffer[avg_write-1] - avg;
point = filter(normalised);
return point;
}
float normalise() { //Normalise values
float point ;
if (first) {
for (int i =0; i <MAX_B; i++) {
avg_buffer[avg_write] = local_buffer[i];
avg_write = (++avg_write) % AVG_LEN;
}
if (write == 0) {
first = false;
}
}
else {
for (int i =0; i <MAX_B; i++) {
int data = local_buffer[i];
avg_sum = avg_sum + data - avg_buffer[avg_write];
avg_buffer[avg_write] = data;
avg_write = (++avg_write) % AVG_LEN;
point = get_trend();
}
}
return point; //The tenth point
}
float data_process() {
return normalise();
}
void write_to_display(float output) {
}
void identify_pulse(float ouput) {
}
int main() {
sampler.attach(&sampling, 0.0125); //Sample at 80Hz
while(1) {
if (write-read > MAX_B || read-write > MAX_B) { // |write-read| > MAX_B/2
for(int i = 0; i < MAX_B/2; i++) {
local_buffer[i] = sample_buffer[read];
read = (++read) % MAX_B;
}
}
float output = data_process();
identify_pulse(output);
write_to_display(output);
}
}

