Advertisement
tomwincr

Untitled

Jan 7th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <deque>
  3. #include <iostream>
  4. #include <numeric>
  5.  
  6. //initialize the double ended queue to length 24 and values 999.0 (impossible reading)
  7. std::deque <float> seq1(24,999.00);
  8. std::deque <int>::size_type i; //set data type for deque size descriptor
  9. float listindex = 0.0; //initialize data to be stored
  10. bool datavalid = false; //are there 24 valid items in the deque?
  11.  
  12. void setup() {
  13. // put your setup code here, to run once:
  14.  
  15. // no code here
  16.  
  17. }
  18.  
  19.  
  20. void loop() {
  21. // put your main code here, to run repeatedly:
  22.  
  23. delay(3000);
  24. Serial.println("opening code");
  25.  
  26. listindex += 1.5; //set value to add to queue
  27.  
  28. seq1.push_front(listindex); //add newest value to front of queue
  29. seq1.pop_back(); // remove oldest value from back of queue
  30. //calculate the moving average of the queued values
  31. float mavg = std::accumulate(seq1.begin(),seq1.end(),0.0)/seq1.size();
  32. //see if data is valid yet
  33. if (seq1.back() < 999.0 ){datavalid = true;}
  34.  
  35. //print summary
  36.  
  37. Serial.print("Is data valid? ");
  38. Serial.println(datavalid);
  39. Serial.print("The third element is ");
  40. Serial.println(seq1.at(2));
  41. Serial.print("The last element is ");
  42. Serial.println(seq1.back());
  43. Serial.print("The queue length is ");
  44. Serial.println(seq1.size());
  45. Serial.print("The moving average is ");
  46. Serial.println(mavg);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement