Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. // EMF Detector for LED Bargraph v1.0
  2. // 5.12.2009
  3. // original code/project by Aaron ALAI - aaronalai1@gmail.com
  4. // modified for use w/ LED bargraph by Collin Cunningham - collin@makezine.com
  5. // remodified an reanrangede for Project BlackKitty by Ed Nielsen - ed@darkgeej.dk 2017
  6.  
  7. #define NUMREADINGS 15 // raise this number to increase data smoothing
  8.  
  9. int senseLimit = 15; // raise this number to decrease sensitivity (up to 1023 max)
  10. int probePin = 0; // analog 5
  11. int val = 0; // reading from probePin
  12.  
  13. int LED1 = 2; //
  14. int LED2 = 3; // series
  15. int LED3 = 4; // in
  16. int LED4 = 5; // resistors
  17. int LED5 = 6; // with
  18. int LED6 = 7; // anodes
  19. int LED7 = 8; // bargraph
  20. int LED8 = 9; // LED
  21. int LED9 = 10; // to
  22. int LED10 = 11; // connections
  23.  
  24. // variables for smoothing
  25.  
  26. int readings[NUMREADINGS]; // the readings from the analog input
  27. int index = 0; // the index of the current reading
  28. int total = 0; // the running total
  29. int average = 0; // final average of the probe reading
  30.  
  31.  
  32. void setup() {
  33.  
  34. // set pins
  35. pinMode(2, OUTPUT); // specify LED bargraph outputs
  36. pinMode(3, OUTPUT);
  37. pinMode(4, OUTPUT);
  38. pinMode(5, OUTPUT);
  39. pinMode(6, OUTPUT);
  40. pinMode(7, OUTPUT);
  41. pinMode(8, OUTPUT);
  42. pinMode(9, OUTPUT);
  43. pinMode(10, OUTPUT);
  44. pinMode(11, OUTPUT);
  45.  
  46. // set serial
  47. Serial.begin(9600); // initiate serial connection for debugging/etc
  48.  
  49. for (int i = 0; i < NUMREADINGS; i++)
  50. readings[i] = 0; // initialize all the readings to 0
  51.  
  52.  
  53.  
  54. }
  55.  
  56.  
  57.  
  58. void loop()
  59. {
  60.  
  61.  
  62.  
  63.  
  64.  
  65. val = analogRead(probePin); // take a reading from the probe
  66.  
  67. if(val >= 1){ // if the reading isn't zero, proceed
  68.  
  69. val = constrain(val, 1, senseLimit); // turn any reading higher than the senseLimit value into the senseLimit value
  70. val = map(val, 1, senseLimit, 1, 1023); // remap the constrained value within a 1 to 1023 range
  71.  
  72. total -= readings[index]; // subtract the last reading
  73. readings[index] = val; // read from the sensor
  74. total += readings[index]; // add the reading to the total
  75. index = (index + 1); // advance to the next index
  76.  
  77. if (index >= NUMREADINGS) // if we're at the end of the array...
  78. index = 0; // ...wrap around to the beginning
  79.  
  80. average = total / NUMREADINGS; // calculate the average
  81.  
  82.  
  83. if (average > 5){ // if the average is over 50 ...
  84. digitalWrite(LED10, HIGH); // light the first LED
  85. }
  86. else{ // and if it's not ...
  87. digitalWrite(LED10, LOW); // turn that LED off
  88. }
  89.  
  90.  
  91. if (average > 10){ // and so on ...
  92. digitalWrite(LED9, HIGH);
  93. }
  94. else{
  95. digitalWrite(LED9, LOW);
  96. }
  97.  
  98. if (average > 25){
  99. digitalWrite(LED8, HIGH);
  100. }
  101. else{
  102. digitalWrite(LED8, LOW);
  103. }
  104.  
  105.  
  106. if (average > 35){
  107. digitalWrite(LED7, HIGH);
  108. }
  109. else{
  110. digitalWrite(LED7, LOW);
  111. }
  112.  
  113. if (average > 40){
  114. digitalWrite(LED6, HIGH);
  115. }
  116. else{
  117. digitalWrite(LED6, LOW);
  118. }
  119.  
  120. if (average > 45){
  121. digitalWrite(LED5, HIGH);
  122. }
  123. else{
  124. digitalWrite(LED5, LOW);
  125. }
  126.  
  127. if (average > 50){
  128. digitalWrite(LED4, HIGH);
  129. }
  130. else{
  131. digitalWrite(LED4, LOW);
  132. }
  133.  
  134. if (average > 55){
  135. digitalWrite(LED3, HIGH);
  136. }
  137. else{
  138. digitalWrite(LED3, LOW);
  139. }
  140.  
  141. if (average > 60){
  142. digitalWrite(LED2, HIGH);
  143. }
  144. else{
  145. digitalWrite(LED2, LOW);
  146. }
  147.  
  148. if (average > 70){
  149. digitalWrite(LED1, HIGH);
  150. }
  151. else{
  152. digitalWrite(LED1, LOW);
  153. }
  154.  
  155.  
  156. Serial.println(val); // use output to aid in calibrating
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement