Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h> //Dołączenie bilbioteki
- #include <TimerOne.h>
- LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Informacja o podłączeniu nowego wyświetlacza
- using namespace std;
- bool readyForReading = false;
- int sampleNumber = 0;
- //int suma = 0;
- //
- const int samplesNumberTake = 80; //Ile próbek
- const long interval = 250 ; //Co ile próbki w ns//zmiana o 1000
- //
- struct AnalogReadHistory{
- int Analog0;
- int Analog1;
- int Analog2;
- int Analog3;
- // int Analog4;
- // int Analog5;
- // unsigned long readTime;
- };
- AnalogReadHistory analogReadHistory[100]; //Z zapasek.
- void setup() {
- lcd.begin(16, 2);
- lcd.setCursor(0, 0); //Ustawienie kursora
- pinMode(8, OUTPUT);
- pinMode(9, OUTPUT);
- //lcd.print("Tutaj pojawi sie"); //Wyświetlenie tekstu
- //lcd.setCursor(0, 1); //Ustawienie kursora
- //lcd.print("pomiar napiecia "); //Wyświetlenie tekstu
- Serial.begin(230400);
- Timer1.initialize(interval);
- Timer1.attachInterrupt(takeReading);
- readyForReading = true;
- }
- void takeReading() {
- if (sampleNumber < samplesNumberTake){
- analogReadHistory[sampleNumber].Analog1 = analogRead(1);
- analogReadHistory[sampleNumber].Analog2 = analogRead(2);
- analogReadHistory[sampleNumber].Analog3 = analogRead(3);
- ++sampleNumber;
- }
- }
- double currentForA1(AnalogReadHistory *analogReadHistory)
- {
- double suma = 0;
- for(int i=0; i<= samplesNumberTake; i++)
- {
- suma += pow (analogReadHistory[i].Analog1, 2);
- }
- return(sqrt(suma/samplesNumberTake));
- }
- double currentForA2(AnalogReadHistory *analogReadHistory)
- {
- double suma = 0;
- for(int i=0; i<= samplesNumberTake; i++)
- {
- suma += pow (analogReadHistory[i].Analog2, 2);
- }
- return(sqrt(suma/samplesNumberTake));
- }
- double amount(AnalogReadHistory *analogReadHistory)
- {
- double suma = 0;
- for(int i=0; i<= samplesNumberTake; i++)
- {
- suma += analogReadHistory[i].Analog3;
- }
- return suma/(samplesNumberTake-1);
- }
- bool isSampleCorrect(AnalogReadHistory *analogReadHistory){
- Serial.print("A1=");
- double w1 = currentForA1(analogReadHistory) - amount(analogReadHistory);
- double w2 = currentForA2(analogReadHistory) - amount(analogReadHistory);
- Serial.print(w1 );
- Serial.print(" A2=");
- Serial.print(w2);
- Serial.print(" offset=");
- Serial.println(amount(analogReadHistory));
- if (abs(w1-w2) <= 40) return true;
- else return false;
- }
- void loop() {
- if (sampleNumber >= samplesNumberTake){
- unsigned long currentMillis = millis();
- int correctSamples = 0;
- noInterrupts(); // Wyłącza Timer
- lcd.setCursor(0, 0);
- if (isSampleCorrect(analogReadHistory) == false){
- lcd.setCursor(0, 0);
- lcd.print(" Rozne prady ");
- Serial.print("Niepoprawne odczyty: ");
- Serial.println("BAD");
- digitalWrite(8, HIGH);
- digitalWrite(9, LOW);
- }
- else
- {
- lcd.setCursor(0, 0);
- lcd.print("Dziala poprawnie");
- Serial.println("GOOD");
- Serial.print("Poprawne odczyty: ");
- digitalWrite(8, LOW);
- digitalWrite(9, HIGH);
- }
- //Serial.println(sampleNumber-correctSamples);
- sampleNumber = 0;
- _delay_ms(2000);
- interrupts(); // Włącza Timer
- }
- }
- // if (currentMillis - previousMillis >= interval) {
- // //Ustawienie kursora
- // lcd.print("Voltage "); //Wyświetlenie tekstu
- // lcd.print(voltage);
- // // lcd.setCursor(0, 1); //Ustawienie kursora
- // // lcd.print("mi nie wychodzi "); //Wyświetlenie tekstu
- // //wywala dane do komputera
- // Serial.print(analogReadHistory.Analog2);
- // Serial.print(" ");
- // Serial.print(analogReadHistory.Analog3);
- // Serial.print(" ");
- // Serial.println(analogReadHistory.Analog4);
- // Serial.println(analogReadHistory.readTime);
- // lastDataProcessed = true;
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement