Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 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 ; //Co ile próbki w ns//zmiana o 1000
  14. //
  15.  
  16. struct AnalogReadHistory{
  17. int Analog0;
  18. int Analog1;
  19. int Analog2;
  20. int Analog3;
  21. // int Analog4;
  22. // int Analog5;
  23. // unsigned long readTime;
  24. };
  25.  
  26. AnalogReadHistory analogReadHistory[100]; //Z zapasek.
  27.  
  28. void setup() {
  29. lcd.begin(16, 2);
  30. lcd.setCursor(0, 0); //Ustawienie kursora
  31. pinMode(8, OUTPUT);
  32. pinMode(9, OUTPUT);
  33. //lcd.print("Tutaj pojawi sie"); //Wyświetlenie tekstu
  34. //lcd.setCursor(0, 1); //Ustawienie kursora
  35. //lcd.print("pomiar napiecia "); //Wyświetlenie tekstu
  36. Serial.begin(230400);
  37. Timer1.initialize(interval);
  38. Timer1.attachInterrupt(takeReading);
  39. readyForReading = true;
  40. }
  41.  
  42.  
  43. void takeReading() {
  44. if (sampleNumber < samplesNumberTake){
  45.  
  46. analogReadHistory[sampleNumber].Analog1 = analogRead(1);
  47. analogReadHistory[sampleNumber].Analog2 = analogRead(2);
  48. analogReadHistory[sampleNumber].Analog3 = analogRead(3);
  49. ++sampleNumber;
  50. }
  51. }
  52.  
  53. double currentForA1(AnalogReadHistory *analogReadHistory)
  54. {
  55. double suma = 0;
  56. for(int i=0; i<= samplesNumberTake; i++)
  57. {
  58. suma += pow (analogReadHistory[i].Analog1, 2);
  59. }
  60. return(sqrt(suma/samplesNumberTake));
  61. }
  62.  
  63. double currentForA2(AnalogReadHistory *analogReadHistory)
  64. {
  65. double suma = 0;
  66. for(int i=0; i<= samplesNumberTake; i++)
  67. {
  68. suma += pow (analogReadHistory[i].Analog2, 2);
  69. }
  70. return(sqrt(suma/samplesNumberTake));
  71. }
  72.  
  73. double amount(AnalogReadHistory *analogReadHistory)
  74. {
  75. double suma = 0;
  76. for(int i=0; i<= samplesNumberTake; i++)
  77. {
  78. suma += analogReadHistory[i].Analog3;
  79. }
  80.  
  81. return suma/(samplesNumberTake-1);
  82. }
  83.  
  84.  
  85. bool isSampleCorrect(AnalogReadHistory *analogReadHistory){
  86. Serial.print("A1=");
  87. double w1 = currentForA1(analogReadHistory) - amount(analogReadHistory);
  88. double w2 = currentForA2(analogReadHistory) - amount(analogReadHistory);
  89. Serial.print(w1 );
  90. Serial.print(" A2=");
  91. Serial.print(w2);
  92. Serial.print(" offset=");
  93. Serial.println(amount(analogReadHistory));
  94.  
  95. if (abs(w1-w2) <= 40) return true;
  96. else return false;
  97.  
  98. }
  99.  
  100.  
  101. void loop() {
  102. if (sampleNumber >= samplesNumberTake){
  103. unsigned long currentMillis = millis();
  104. int correctSamples = 0;
  105. noInterrupts(); // Wyłącza Timer
  106. lcd.setCursor(0, 0);
  107.  
  108. if (isSampleCorrect(analogReadHistory) == false){
  109. lcd.setCursor(0, 0);
  110. lcd.print(" Rozne prady ");
  111. Serial.print("Niepoprawne odczyty: ");
  112. Serial.println("BAD");
  113. digitalWrite(8, HIGH);
  114. digitalWrite(9, LOW);
  115. }
  116. else
  117. {
  118. lcd.setCursor(0, 0);
  119. lcd.print("Dziala poprawnie");
  120. Serial.println("GOOD");
  121. Serial.print("Poprawne odczyty: ");
  122. digitalWrite(8, LOW);
  123. digitalWrite(9, HIGH);
  124.  
  125. }
  126. //Serial.println(sampleNumber-correctSamples);
  127. sampleNumber = 0;
  128. _delay_ms(2000);
  129. interrupts(); // Włącza Timer
  130. }
  131. }
  132. // if (currentMillis - previousMillis >= interval) {
  133. // //Ustawienie kursora
  134. // lcd.print("Voltage "); //Wyświetlenie tekstu
  135. // lcd.print(voltage);
  136. // // lcd.setCursor(0, 1); //Ustawienie kursora
  137. // // lcd.print("mi nie wychodzi "); //Wyświetlenie tekstu
  138. // //wywala dane do komputera
  139. // Serial.print(analogReadHistory.Analog2);
  140. // Serial.print(" ");
  141. // Serial.print(analogReadHistory.Analog3);
  142. // Serial.print(" ");
  143. // Serial.println(analogReadHistory.Analog4);
  144. // Serial.println(analogReadHistory.readTime);
  145. // lastDataProcessed = true;
  146. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement