Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <deque>
- #include <iostream>
- #include <numeric>
- //initialize the double ended queue to length 24 and values 999.0 (impossible reading)
- std::deque <float> seq1(24,999.00);
- std::deque <int>::size_type i; //set data type for deque size descriptor
- float listindex = 0.0; //initialize data to be stored
- bool datavalid = false; //are there 24 valid items in the deque?
- void setup() {
- // put your setup code here, to run once:
- // no code here
- }
- void loop() {
- // put your main code here, to run repeatedly:
- delay(3000);
- Serial.println("opening code");
- listindex += 1.5; //set value to add to queue
- seq1.push_front(listindex); //add newest value to front of queue
- seq1.pop_back(); // remove oldest value from back of queue
- //calculate the moving average of the queued values
- float mavg = std::accumulate(seq1.begin(),seq1.end(),0.0)/seq1.size();
- //see if data is valid yet
- if (seq1.back() < 999.0 ){datavalid = true;}
- //print summary
- Serial.print("Is data valid? ");
- Serial.println(datavalid);
- Serial.print("The third element is ");
- Serial.println(seq1.at(2));
- Serial.print("The last element is ");
- Serial.println(seq1.back());
- Serial.print("The queue length is ");
- Serial.println(seq1.size());
- Serial.print("The moving average is ");
- Serial.println(mavg);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement