Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<arduinoPlatform.h>
- #include<tasks.h>
- #include<interrupts.h>
- #include<stdio.h>
- #include<serial.h>
- #include <pwm.h>
- #include<data.h>
- #include <ctime>
- extern serial Serial;
- const int pin = 26,
- measureCount = 20;
- int oldState,
- startTime,
- elapsedTime = 0,
- periodTime = 0,
- tempSum = 0,
- tempCnt = 0,
- lastAvrg = -1;
- void tracking(int id, void* ptrt){
- static int first = true;
- bool error = false;
- int newState = digitalRead(pin);
- if(oldState > newState){
- elapsedTime = millis() - startTime;
- }else if(oldState < newState){
- int timeLocal = millis();
- periodTime = timeLocal - startTime;
- startTime = timeLocal;
- if(!first){
- Serial.print("(");
- Serial.print(tempCnt + 1);
- Serial.print(".) ");
- Serial.print(elapsedTime);
- Serial.print(' ');
- Serial.println(periodTime);
- int temperature = map(elapsedTime, 0, 1000, 0, 45);
- if(lastAvrg != -1){
- if((temperature > (lastAvrg + (lastAvrg / 2))) ||
- (temperature < (lastAvrg - (lastAvrg / 2)))){
- time_t now = time(0);
- char* date = ctime(&now);
- Serial.print("[ERROR] Temperature is 50% diff from last avrg (");
- Serial.print(temperature);
- Serial.print(") [");
- Serial.print(date);
- Serial.println("]");
- error = true;
- }
- }
- if(!error){
- tempSum += temperature;
- }else{
- tempSum += lastAvrg;
- }
- if(tempCnt++ == 19){
- int avrg = tempSum / measureCount;
- tempCnt = 0;
- Serial.print("[AVERAGE] Average temperature last 20 measurings is ");
- Serial.println(avrg);
- if(lastAvrg == avrg){
- Serial.println("[INFO] This average is the same as the one before.");
- }
- lastAvrg = avrg;
- tempSum = 0;
- tempCnt = 0;
- }
- }else{
- first = false;
- }
- }
- oldState = newState;
- }
- void setup()
- {
- Serial.begin(9600);
- oldState = digitalRead(pin);
- startTime = millis();
- pwmSin(pin, 1000, 0.1);
- createTask(tracking, 1, TASK_ENABLE, NULL);
- }
- void loop()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment