Advertisement
Guest User

Untitled

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