Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. //Hardware
  2. #define distSensor A0 //Define what analog pin the distance sensor is on
  3. int ledPin = 13; //Define what pin the warning LED is connected to
  4.  
  5. //Arrays
  6. int distanceArray[9];
  7. int medianDistanceArray[9];
  8. int maxValuesArray[4];
  9. int maxIndexArray[4];
  10.  
  11. //Counters
  12. int i;
  13. int j;
  14. int k;
  15.  
  16. //Math variables
  17. int distance;
  18. float voltage;
  19. int medianDistance;
  20. float medianVoltage;
  21. int arraySum;
  22. int runningAverage;
  23. int maxIndex;
  24. int maxIndexTwo;
  25. int medianFilterFinal;
  26.  
  27. void setup(){
  28. Serial.begin(9600); //Default Serial baud rate
  29. pinMode(ledPin, OUTPUT); //Set LED pin as input
  30. pinMode(distSensor, INPUT); //Define the distance sensor as an input
  31. }
  32.  
  33. void loop(){
  34. int maxValue = 0;
  35. int maxValueTwo = 0;
  36.  
  37. arraySum = 0; //Clear arraySum
  38. runningAverage = 0; //Clear runningAverage
  39.  
  40.  
  41. //Running Average loop
  42. for (i=0; i<=8; i++){
  43. delay(100); //Slow down Arduino to check sensor every 100ms (10hz, as specified in lab assignment)
  44. voltage = analogRead(distSensor) * 0.0048828125; //Read the data from the sensor, multiply it by a conversion factor then assign to a variable
  45. distance = 27.726 * pow(voltage, -1.2045); //Conversion from voltage to distance
  46. distanceArray[i] = distance; //Input data into the array according to the counter
  47. arraySum = arraySum + distanceArray[i]; //Sum the data of the array
  48. runningAverage = arraySum / 9; //Average out the data in the array
  49. }
  50.  
  51. //Median Filtering loop
  52. for (j=0; j<=3; j++){
  53. for (k=0; k<=8; k++){
  54. delay(100); //Slow down Arduino to check sensor every 100ms (10hz, as specified in lab assignment)
  55. medianVoltage = analogRead(distSensor) * 0.0048828125; //Read the data from the sensor, multiply it by a conversion factor then assign to a variable
  56. medianDistance = 27.726 * pow(medianVoltage, -1.2045); //Conversion from voltage to distance
  57. medianDistanceArray[k] = medianDistance; //Input data into the array according to the counter
  58.  
  59. if(medianDistanceArray[k] > maxValue){
  60. maxValue = medianDistanceArray[k];
  61. maxIndex = k;
  62. maxValuesArray[j] = maxValue;
  63. maxIndexArray[j] = maxIndex;
  64. }
  65.  
  66. if(k=8){
  67. distanceArray[maxValue] = 0;
  68. }
  69. }
  70. if(maxValuesArray[j] > maxValueTwo){
  71. maxValueTwo = maxValuesArray[j];
  72. maxIndexTwo = j;
  73. if(j=3){
  74. medianFilterFinal = maxValueTwo;
  75. }
  76. }
  77. }
  78.  
  79. //Out of Range check
  80. if(distance > 80){ //If statement for detecting if the sensor is beyond its max range
  81. digitalWrite(ledPin, HIGH); //If the sensor is beyond max range, turn on the warning LED
  82. Serial.println("Out of functioning range"); //Print warning for beyond max range
  83. }
  84. else if(distance < 80){ //Else if for if sensor is within max range
  85. digitalWrite(ledPin, LOW); //Turn led off if within range
  86.  
  87. //Printing code
  88. Serial.print("Running Average: ");
  89. Serial.print(runningAverage);
  90. Serial.print("cm");
  91. Serial.println(" ");
  92. Serial.print("Median Filter: ");
  93. Serial.print(medianFilterFinal);
  94. Serial.print("cm");
  95. Serial.println(" ");
  96. Serial.println("-");
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement