Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. const int sampleWindow = 100; // Sample window width in mS (50 mS = 20Hz)
  2. unsigned int sample;
  3.  
  4. void setup()
  5. {
  6. Serial.begin(9600);
  7. }
  8.  
  9.  
  10. void loop()
  11. {
  12. unsigned long startMillis= millis(); // Start of sample window
  13. unsigned int peakToPeak = 0; // peak-to-peak level
  14.  
  15. unsigned int signalMax = 0;
  16. unsigned int signalMin = 1024;
  17.  
  18. // collect data for 50 mS
  19. while (millis() - startMillis < sampleWindow)
  20. {
  21. sample = analogRead(14);
  22. if (sample < 1024) // toss out spurious readings
  23. {
  24. if (sample > signalMax)
  25. {
  26. signalMax = sample; // save just the max levels
  27. }
  28. else if (sample < signalMin)
  29. {
  30. signalMin = sample; // save just the min levels
  31. }
  32. }
  33. }
  34. peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
  35. double volts = (peakToPeak * 5.0) / 1024; // convert to volts
  36. if( volts < 0.02 ) {
  37. Serial.println("row: 0");
  38. } else if (volts >= 0.02 && volts < 0.30) {
  39. Serial.println("row: 1");
  40. } else if (volts >= 0.30 && volts < 0.60) {
  41. Serial.println("row: 2");
  42. } else if (volts >= 0.60 && volts < 0.90) {
  43. Serial.println("row: 3");
  44. } else if (volts >= 0.90 && volts < 1.20) {
  45. Serial.println("row: 4");
  46. } else if (volts >= 1.20 && volts < 1.50) {
  47. Serial.println("row: 5");
  48. } else if (volts >= 1.50 && volts < 1.80) {
  49. Serial.println("row: 6");
  50. } else if (volts >= 1.80 && volts < 2.10) {
  51. Serial.println("row: 7");
  52. } else if (volts >= 2.10 && volts < 2.40) {
  53. Serial.println("row: 8");
  54. } else if (volts >= 2.40 && volts < 2.70) {
  55. Serial.println("row: 9");
  56. } else if (volts >= 2.70 && volts < 3.00) {
  57. Serial.println("row: 10");
  58. } else if (volts >= 3.00 && volts < 3.30) {
  59. Serial.println("row: 11");
  60. } else if (volts >= 3.30 && volts < 3.60) {
  61. Serial.println("row: 12");
  62. } else if (volts >= 3.60) {
  63. Serial.println("row: 13");
  64. }
  65.  
  66. Serial.println(volts);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement