Advertisement
mikael2933

Arduino kode uden display

Apr 18th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const byte interruptPin = 3, btnPin = 9, diodePin = 12;
  2. const float TIMER = 400, DEVIATION = 0.26;
  3. volatile long count = 0;
  4. long startTime;
  5. unsigned long last0, last1, last2, notMetal=1;
  6.  
  7. void setup() {
  8.   Serial.begin(9600);
  9.   startTime = millis();
  10.   pinMode(diodePin, OUTPUT);
  11.   attachInterrupt(digitalPinToInterrupt(interruptPin), on_interrupt, RISING);
  12. }
  13.  
  14. void loop() {
  15.   if (digitalRead(btnPin) == HIGH) {
  16.     notMetal = (last2+last1+last0)/3;
  17.     delay(125);
  18.     Serial.print("New notMetal: ");
  19.     Serial.println(notMetal);
  20.   }
  21.  
  22.   if (millis()-startTime >= TIMER) {
  23.     noInterrupts();
  24.     Serial.println(count);
  25.     float countDeviation = float(count)/notMetal
  26.     Serial.println(countDeviation);
  27.     if (countDeviation >= 1+DEVIATION/100 || countDeviation <= 1-DEVIATION/100) {
  28.       Serial.println("METAL");
  29.       digitalWrite(diodePin, HIGH);
  30.     } else {
  31.       digitalWrite(diodePin, LOW);
  32.     }
  33.  
  34.     last2 = last1;
  35.     last1 = last0;
  36.     last0 = count;
  37.     count = 0;
  38.     startTime = millis();
  39.     interrupts();
  40.   }
  41. }
  42.  
  43. void on_interrupt() {
  44.   count++;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement