Advertisement
ootsidebox

TheBoxV1

Mar 31st, 2020
8,782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.02 KB | None | 0 0
  1. /*
  2.  * Author: Jean Noel Lefebvre - www.ootsidebox.fr - march 31th 2020
  3.  *
  4.  * */
  5.  
  6. //https://learn.adafruit.com/adafruit-led-backpack/0-54-alphanumeric-9b21a470-83ad-459c-af02-209d8d82c462
  7. //http://wiki.seeedstudio.com/Grove-Infrared_Temperature_Sensor/
  8. //http://wiki.seeedstudio.com/Grove-Light_Sensor/
  9. //https://github.com/thomasfredericks/Metro-Arduino-Wiring
  10.  
  11. #include <Wire.h>
  12. #include "Adafruit_LEDBackpack.h"
  13. #include <math.h>
  14. #include <Metro.h> //Include Metro library
  15.  
  16. #define LIGHT_SENSOR A2
  17. #define SUR_TEMP_PIN A0 // Analog input pin connect to temperature sensor SUR pin
  18. #define OBJ_TEMP_PIN A1 // Analog input pin connect to temperature sensor OBJ pin
  19. #define BUZZER 3
  20. #define BP 2
  21. #define SEUIL_TEMP 59
  22. #define HIGH_TEMP 75
  23. #define SEUIL_LIGHT 60
  24. #define TIMER 30
  25.  
  26. //#define TEST
  27.  
  28. float temp_calibration=0;       //this parameter was used to calibrate the temperature
  29.  
  30. //float objt_calibration=0.000; //this parameter was used to calibrate the object temperature
  31. float temperature_range=10;    //we make a map of temperature-voltage according to sensor datasheet. 10 is the temperature step when sensor and
  32.                                //object distance is 9CM.
  33. float offset_vol=0.014;        //this parameter was used to set the mid level voltage,when put the sensor in normal environment after 10 min,
  34.                                //the sensor output 0.For example,the surrounding temperature is 29℃,but the result is 27℃ via the sensor,
  35.                                //you should set the reerence to 0.520 or more,according to your sensor to change.
  36.                                //the unit is V
  37. float tempValue = 0;
  38. float objtValue= 0;  
  39. float current_temp=0;
  40. float temp=0;
  41. float temp1=0;
  42. float temp2=0;
  43. unsigned int temp3=0;
  44. const float reference_vol=0.500;
  45. unsigned char clear_num=0;//when use lcd to display
  46. float R=0;
  47. float voltage=0;
  48.  
  49.  
  50. long res[100]={
  51.                  318300,302903,288329,274533,261471,249100,237381,226276,215750,205768,
  52.                  196300,187316,178788,170691,163002,155700,148766,142183,135936,130012,
  53.                  124400,119038,113928,109059,104420,100000,95788,91775,87950,84305,
  54.                  80830,77517,74357,71342,68466,65720,63098,60595,58202,55916,
  55.                  53730,51645,49652,47746,45924,44180,42511,40912,39380,37910,
  56.                  36500,35155,33866,32631,31446,30311,29222,28177,27175,26213,
  57.                  25290,24403,23554,22738,21955,21202,20479,19783,19115,18472,
  58.                  17260,16688,16138,15608,15098,14608,14135,13680,13242,12819,
  59.                  12412,12020,11642,11278,10926,10587,10260,9945,9641,9347,
  60.                  9063,8789,8525,8270,8023,7785,7555,7333,7118,6911};
  61.                  
  62. float obj [13][12]={
  63. /*0*/             { 0,-0.274,-0.58,-0.922,-1.301,-1.721,-2.183,-2.691,-3.247,-3.854,-4.516,-5.236}, //
  64. /*1*/             { 0.271,0,-0.303,-0.642,-1.018,-1.434,-1.894,-2.398,-2.951,-3.556,-4.215,-4.931},  //→surrounding temperature,from -10,0,10,...100
  65. /*2*/             { 0.567,0.3,0,-0.335,-0.708,-1.121,-1.577,-2.078,-2.628,-3.229,-3.884,-4.597},   //↓object temperature,from -10,0,10,...110
  66. /*3*/             { 0.891,0.628,0.331,0,-0.369,-0.778,-1.23,-1.728,-2.274,-2.871,-3.523,-4.232},
  67. /*4*/             { 1.244,0.985,0.692,0.365,0,-0.405,-0.853,-1.347,-1.889,-2.482,-3.13,-3.835},
  68. /*5*/             { 1.628,1.372,1.084,0.761,0.401,0,-0.444,-0.933,-1.47,-2.059,-2.702,-3.403},
  69. /*6*/             { 2.043,1.792,1.509,1.191,0.835,0.439,0,-0.484,-1.017,-1.601,-2.24,-2.936},
  70. /*7*/             { 2.491,2.246,1.968,1.655,1.304,0.913,0.479,0,-0.528,-1.107,-1.74,-2.431},
  71. /*8*/             { 2.975,2.735,2.462,2.155,1.809,1.424,0.996,0.522,0,-0.573,-1.201,-1.887},
  72. /*9*/             { 3.495,3.261,2.994,2.692,2.353,1.974,1.552,1.084,0.568,0,-0.622,-1.301},
  73. /*10*/            { 4.053,3.825,3.565,3.27,2.937,2.564,2.148,1.687,1.177,0.616,0,-0.673},
  74. /*11*/            { 4.651,4.43,4.177,3.888,3.562,3.196,2.787,2.332,1.829,1.275,0.666,0},
  75. /*12*/            { 5.29,5.076,4.83,4.549,4.231,3.872,3.47,3.023,2.527,1.98,1.379,0.72}
  76. };
  77.  
  78. int Light;
  79. float Heat;
  80. int Timer=0;
  81. int Minute=59;
  82.  
  83. Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
  84. Metro ledMetro = Metro(1000);
  85. enum States{INIT, COUNT, END, ERR};
  86. int Automate=INIT;
  87. bool TemperatureOK=false;
  88. bool LightOK=false;
  89.  
  90.  
  91.  
  92. //**********************************************************************************
  93. void setup()
  94. {
  95.   pinMode(BP, INPUT_PULLUP);
  96.   Serial.begin(9600);
  97.   analogReference(INTERNAL);//set the refenrence voltage 1.1V,the distinguishability can up to 1mV.
  98.   alpha4.begin(0x70);  // pass in the address
  99.   alpha4.clear();
  100.   alpha4.writeDisplay();
  101.   PrintLed(alpha4,"TboX");
  102.   tone(3, 3000, 500);
  103.   delay(1000);
  104.   Automate=INIT;
  105. }
  106.  
  107. void loop()
  108. {
  109.   static int Point=1;
  110.   float T1=measureSurTemp();//measure the Surrounding temperature around the sensor
  111.   float T2=measureObjectTemp();
  112.   Heat=T1+0;
  113.  
  114.   if ((ledMetro.check() == 1) )
  115.   {
  116. #ifndef TEST
  117.      if (Automate==COUNT)
  118. #endif
  119.      {
  120.       if(Point) Point=0;
  121.       else Point=1;
  122.       Minute--;
  123.       if(Minute==0)
  124.       {
  125.         Minute=59;
  126.         if(Timer) Timer--;
  127.         PrintSensors();
  128.       }
  129.      }
  130.    
  131.     if(Automate==END) tone(3, 3000, 100);
  132.     if(Automate==ERR) tone(3, 300, 200);
  133.   }
  134.  
  135. #ifdef TEST
  136.   TemperatureOK=true;
  137. #else
  138.   if (Heat >= SEUIL_TEMP) TemperatureOK=true;
  139.   else TemperatureOK=false;
  140. #endif
  141.   Light=analogRead(LIGHT_SENSOR)/10;
  142.   if(Light >= SEUIL_LIGHT) LightOK=true;
  143.   else LightOK=false;
  144.  
  145.   if(Heat>=HIGH_TEMP)  
  146.       {
  147.         tone(3, 300, 200);
  148.         delay(500);
  149.       }
  150.  
  151.   switch(Automate)
  152.   {
  153.     case INIT:
  154.       Timer=0;
  155.       if((!digitalRead(BP)) && TemperatureOK)
  156.       {
  157.         Automate=COUNT;
  158.         Timer=TIMER;
  159.         tone(3, 3000, 500);
  160.       }
  161.       if((!digitalRead(BP)) && !TemperatureOK)
  162.       {
  163.         PrintLed(alpha4,"lowT");
  164.         tone(3, 300, 200);
  165.       }
  166.       else PrintLedVal(alpha4,Timer,(int)Heat,Point);
  167.       //Serial.println("INIT");
  168.     break;
  169.    
  170.     case COUNT:
  171.       if(Timer==0) Automate=END;
  172.       if(!LightOK || !TemperatureOK) Automate=ERR;
  173.       PrintLedVal(alpha4,Timer,(int)Heat,Point);
  174.       //Serial.println("COUNT");
  175.     break;
  176.    
  177.     case END:
  178.       if(!digitalRead(BP)) Automate=INIT;
  179.       PrintLed(alpha4,"END.");
  180.       //Serial.println("END");
  181.     break;
  182.  
  183.     case ERR:
  184.        if(!digitalRead(BP)) Automate=INIT;
  185.          PrintLed(alpha4,"Err.");
  186.          //Serial.println("ERR");
  187.     break;
  188.    
  189.   }
  190. }
  191.  
  192. void PrintSensors()
  193. {
  194.   Serial.print(Timer);
  195.   Serial.print(", ");
  196.   Serial.print((int)Heat);
  197.   Serial.print(", ");
  198.   Serial.print(100);
  199.   Serial.println();
  200. }
  201. //*************************************************************
  202. float binSearch(long x)// this function used for measure the surrounding temperature
  203. {
  204.   int low,mid,high;
  205.   low=0;
  206.   //mid=0;
  207.   high=100;
  208.   while (low<=high)
  209.   {
  210.     mid=(low+high)/2;
  211.     if(x<res[mid])
  212.       low= mid+1;
  213.     else//(x>res[mid])
  214.       high=mid-1;
  215.   }
  216.   return mid;
  217. }
  218. //************************************************************
  219. float arraysearch(float x,float y)//x is the surrounding temperature,y is the object temperature
  220. {
  221.   int i=0;
  222.   float tem_coefficient=100;//Magnification of 100 times  
  223.   i=(x/10)+1;//Ambient temperature      
  224.   voltage=(float)y/tem_coefficient;//the original voltage  
  225.   //Serial.print("sensor voltage:\t");    
  226.   //Serial.print(voltage,5);  
  227.   //Serial.print("V");      
  228.   for(temp3=0;temp3<13;temp3++)  
  229.   {    
  230.     if((voltage>obj[temp3][i])&&(voltage<obj[temp3+1][i]))        
  231.     {    
  232.       return temp3;        
  233.     }    
  234.   }
  235. }
  236. float measureSurTemp()
  237. {  
  238.   unsigned char i=0;
  239.   float current_temp1=0;    
  240.   int signal=0;  
  241.   tempValue=0;
  242.  
  243.   for(i=0;i<10;i++)       //    
  244.   {    
  245.     tempValue+= analogRead(SUR_TEMP_PIN);      
  246.     delay(10);    
  247.   }  
  248.   tempValue=tempValue/10;  
  249.   temp = tempValue*1.1/1023;    
  250.   R=2000000*temp/(2.50-temp);  
  251.   signal=binSearch(R);    
  252.   current_temp=signal-1+temp_calibration+(res[signal-1]-R)/(res[signal-1]-res[signal]);
  253.   return current_temp;
  254. }
  255. //***********************************************************
  256. float measureObjectTemp()
  257. {
  258.   unsigned char i=0;  
  259.   unsigned char j=0;  
  260.   float sur_temp=0;  
  261.   unsigned int array_temp=0;  
  262.   float temp1,temp2;
  263.   float final_temp=0;
  264.   objtValue=0;  
  265.   for(i=0;i<10;i++)
  266.   {
  267.     objtValue+= analogRead(OBJ_TEMP_PIN);
  268.     delay(10);
  269.     }      
  270.   objtValue=objtValue/10;//Averaging processing    
  271.   temp1=objtValue*1.1/1023;//+objt_calibration;
  272.   sur_temp=temp1-(reference_vol+offset_vol);            
  273.   array_temp=arraysearch(current_temp,sur_temp*1000);        
  274.   temp2=current_temp;        
  275.   temp1=(temperature_range*voltage)/(obj[array_temp+1][(int)(temp2/10)+1]-obj[array_temp][(int)(temp2/10)+1]);        
  276.   final_temp=temp2+temp1;        
  277.   return(final_temp);
  278. }
  279. //******************************************************************************
  280. void PrintLed(Adafruit_AlphaNum4 &alpha4, char _buffer[])
  281. {
  282.   alpha4.writeDigitAscii(0, _buffer[0]);
  283.   alpha4.writeDigitAscii(1, _buffer[1]);
  284.   alpha4.writeDigitAscii(2, _buffer[2]);
  285.   alpha4.writeDigitAscii(3, _buffer[3]);
  286.   alpha4.writeDisplay();
  287.   Wire.endTransmission();
  288. }
  289.  
  290. //******************************************************************************
  291. void PrintLedVal(Adafruit_AlphaNum4 &alpha4, int ValueG, int ValueD, boolean Point)
  292. {
  293.   char _buffer[]="AB";
  294.   int P;
  295.   if(Point) P=1;
  296.   else P=0;
  297.  
  298.     itoa (ValueG,_buffer,10);
  299.     if(ValueG<=9)
  300.       {
  301.         alpha4.writeDigitAscii(0, '0');
  302.         alpha4.writeDigitAscii(1, _buffer[0],P);
  303.       }
  304.     else
  305.     {
  306.         alpha4.writeDigitAscii(1, _buffer[1],P);
  307.         alpha4.writeDigitAscii(0, _buffer[0]);
  308.     }
  309.   itoa (ValueD,_buffer,10);
  310.   if(ValueD<=9)
  311.   {
  312.     alpha4.writeDigitAscii(2, '0');
  313.     alpha4.writeDigitAscii(3, _buffer[0],0);
  314.   }
  315.   else
  316.   {
  317.     alpha4.writeDigitAscii(3, _buffer[1],0);
  318.     alpha4.writeDigitAscii(2, _buffer[0]);
  319.   }
  320.   alpha4.writeDisplay();
  321.   Wire.endTransmission();
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement