Advertisement
safwan092

Untitled

Dec 22nd, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. const int numReadings = 10;
  2.  
  3. int readings[numReadings]; // the readings from the analog input
  4. int readIndex = 0; // the index of the current reading
  5. int total = 0; // the running total
  6. int average = 10; // the average
  7.  
  8. int inputPin = A2;
  9.  
  10. void setup() {
  11. // initialize serial communication with computer:
  12. Serial.begin(9600);
  13. pinMode(13,OUTPUT);
  14. // initialize all the readings to 0:
  15. for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  16. readings[thisReading] = 0;
  17. }
  18. }
  19.  
  20. void loop() {
  21. // subtract the last reading:
  22. total = total - readings[readIndex];
  23. // read from the sensor:
  24. readings[readIndex] = analogRead(inputPin);
  25. // add the reading to the total:
  26. total = total + readings[readIndex];
  27. // advance to the next position in the array:
  28. readIndex = readIndex + 1;
  29.  
  30. // if we're at the end of the array...
  31. if (readIndex >= numReadings) {
  32. // ...wrap around to the beginning:
  33. readIndex = 0;
  34. }
  35.  
  36. // calculate the average:
  37. average = total / numReadings;
  38. //Serial.println(average);
  39. // send it to the computer as ASCII digits
  40. if (average < 5) {
  41. Serial.println("Object Detected !!");
  42. digitalWrite(13, 1);
  43.  
  44. }
  45. else {
  46. Serial.println("------------------");
  47. digitalWrite(13, 0);
  48. }
  49.  
  50. delay(1); // delay in between reads for stability
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement