Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <LiquidCrystal.h> //Dołączenie bilbioteki
  2. #include <TimerOne.h>
  3. LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Informacja o podłączeniu nowego wyświetlacza
  4.  
  5.  
  6. using namespace std;
  7.  
  8. bool readyForReading = false;
  9. int sampleNumber = 0;
  10. //int suma = 0;
  11. //
  12. const int samplesNumberTake = 80; //Ile próbek
  13. const long interval = 250 ; //czas w ns
  14. //
  15.  
  16. struct AnalogReadHistory{
  17. int Analog0;
  18. int Analog1;
  19. int Analog2;
  20. int Analog3;
  21. };
  22.  
  23. AnalogReadHistory analogReadHistory[100]; //Z zapasek.
  24.  
  25. void setup() {
  26. lcd.begin(16, 2);
  27. lcd.setCursor(0, 0); //Ustawienie kursora
  28. pinMode(8, OUTPUT);
  29. pinMode(9, OUTPUT);
  30. Serial.begin(230400);
  31. Timer1.initialize(interval);
  32. Timer1.attachInterrupt(takeReading);
  33. readyForReading = true;
  34. }
  35.  
  36.  
  37. void takeReading() {
  38. if (sampleNumber < samplesNumberTake){
  39.  
  40. analogReadHistory[sampleNumber].Analog1 = analogRead(1);
  41. analogReadHistory[sampleNumber].Analog2 = analogRead(2);
  42. analogReadHistory[sampleNumber].Analog3 = analogRead(3);
  43. ++sampleNumber;
  44. }
  45. }
  46.  
  47. double currentForA1(AnalogReadHistory *analogReadHistory)
  48. {
  49. double suma = 0;
  50. for(int i=0; i<= samplesNumberTake; i++)
  51. {
  52. suma += pow (analogReadHistory[i].Analog1, 2);
  53. }
  54. return(sqrt(suma/samplesNumberTake));
  55. }
  56.  
  57. double currentForA2(AnalogReadHistory *analogReadHistory)
  58. {
  59. double suma = 0;
  60. for(int i=0; i<= samplesNumberTake; i++)
  61. {
  62. suma += pow (analogReadHistory[i].Analog2, 2);
  63. }
  64. return(sqrt(suma/samplesNumberTake));
  65. }
  66.  
  67. double amount(AnalogReadHistory *analogReadHistory)
  68. {
  69. double suma = 0;
  70. for(int i=0; i<= samplesNumberTake; i++)
  71. {
  72. suma += analogReadHistory[i].Analog3;
  73. }
  74.  
  75. return suma/(samplesNumberTake-1);
  76. }
  77.  
  78.  
  79. bool isSampleCorrect(AnalogReadHistory *analogReadHistory){
  80.  
  81. double w1 = currentForA1(analogReadHistory) - amount(analogReadHistory);
  82. double w2 = currentForA2(analogReadHistory) - amount(analogReadHistory);
  83. Serial.println(" ");
  84. Serial.print("wejście=");
  85. Serial.print(w1 );
  86. Serial.print(" wyjście=");
  87. Serial.print(w2);
  88. Serial.print(" offset=");
  89. Serial.println(amount(analogReadHistory));
  90.  
  91. if (abs(w1-w2) <= 35.0) return true;
  92. else return false;
  93.  
  94. }
  95.  
  96.  
  97. void loop() {
  98. if (sampleNumber >= samplesNumberTake){
  99. unsigned long currentMillis = millis();
  100. int correctSamples = 0;
  101. noInterrupts(); // Wyłącza Timer
  102. lcd.setCursor(0, 0);
  103.  
  104. if (isSampleCorrect(analogReadHistory) == false){
  105. lcd.setCursor(0, 0);
  106. lcd.print(" Rozne prady ");
  107. Serial.println("Urzadzenie nie dziala poprawnie: ");
  108. Serial.println("BAD");
  109. digitalWrite(8, HIGH);
  110. digitalWrite(9, LOW);
  111. }
  112. else
  113. {
  114. lcd.setCursor(0, 0);
  115. lcd.print("Dziala poprawnie");
  116. Serial.println("GOOD");
  117. Serial.println("Urzadzenie dziala poprawnie: ");
  118. digitalWrite(8, LOW);
  119. digitalWrite(9, HIGH);
  120.  
  121. }
  122. sampleNumber = 0;
  123. _delay_ms(2000);
  124. interrupts(); // Włącza Timer
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement