Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. int thermistorPin = A0;
  2. int ldrPIN = A2;
  3.  
  4. int tempLED = 6;
  5. int lightLED = 3;
  6.  
  7. int tempThresholds = 1;
  8. float* tempRange = 0;
  9. float tempMIN;
  10. float tempMAX;
  11.  
  12. int lightThresholds = 1;
  13. float* lightRange = 0;
  14. float lightMIN;
  15. float lightMAX;
  16.  
  17. void setup() {
  18.   Serial.begin(9600);
  19.   pinMode(tempLED, OUTPUT);
  20.   pinMode(lightLED, OUTPUT);
  21.  
  22.   tempMIN = 20.0;
  23.   tempMAX = 30.0;
  24.   tempThresholds = 50;
  25.  
  26.   lightMIN = 450;
  27.   lightMAX = 600;
  28.   lightThresholds = 20;
  29.  
  30.  
  31.   if (tempRange != 0) {
  32.     delete [] tempRange;
  33.   }
  34.  
  35.   if (lightRange != 0) {
  36.     delete [] lightRange;
  37.   }
  38.  
  39.   tempRange = new float[tempThresholds];
  40.   lightRange = new float[lightThresholds];
  41.  
  42.   float tempX = tempMAX - tempMIN;
  43.   for(int i=0; i<tempThresholds; i++){
  44.     tempRange[i] = ((i+1)*tempX)/tempThresholds;
  45.   }
  46.  
  47.   float lightX = lightMAX - lightMIN;
  48.   for(int i=0; i<lightThresholds; i++){
  49.     lightRange[i] = ((i+1)*lightX)/lightThresholds;
  50.   }
  51. }
  52.  
  53. void loop() {
  54.   Serial.println(lightRange[0]);
  55.   Serial.println(lightRange[1]);
  56.   Serial.println(lightRange[2]);
  57.   Serial.println(lightRange[3]);
  58.   Serial.println(lightRange[4]);
  59.   Serial.println(lightRange[5]);
  60.   Serial.println(lightRange[6]);
  61.   Serial.println(lightRange[7]);
  62.   Serial.println(lightRange[8]);
  63.  
  64.   int Tc = getCelsius(analogRead(thermistorPin));
  65.   int Light = analogRead(ldrPIN);
  66.  
  67.   int tempVoltage = getTempVoltage(Tc);
  68.   int lightVoltage = getLightVoltage(Light);
  69.  
  70.   Serial.print("Temperature: ");
  71.   Serial.print(Tc);
  72.   Serial.println(" C");
  73.  
  74.   Serial.print("Light: ");
  75.   Serial.print(Light);
  76.   Serial.println("");
  77.  
  78.  
  79.  
  80.  
  81.   if(tempVoltage >= 0 && tempVoltage <= 255){
  82.     analogWrite(tempLED,tempVoltage);
  83.     Serial.print("Temp Voltage: ");
  84.     Serial.println(tempVoltage);  
  85.   }
  86.   Serial.println("");
  87.  
  88.   if(lightVoltage >= 0 && lightVoltage <= 255){
  89.     Serial.print("LIGHT Voltage: ");
  90.     Serial.println(lightVoltage);
  91.     analogWrite(lightLED,lightVoltage);  
  92.   }
  93.  
  94.  
  95.   delay(500);
  96. }
  97.  
  98. int getCelsius(int sensor){
  99.   float R1 = 10000;
  100.   float logR2, R2, T, Tc, Tf;
  101.   float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
  102.   R2 = R1 * (1023.0 / (float)sensor - 1.0);
  103.  
  104.   logR2 = log(R2);
  105.   T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  106.   int temp = T - 273.15;
  107.   return temp;
  108. }
  109.  
  110. int getTempVoltage(int C){
  111.   int tempIndex = 0;
  112.   float tempValue = tempMAX - C;
  113.  
  114.   if(tempRange[tempIndex]-tempValue < 1){
  115.     while(tempRange[tempIndex]-tempValue < 1){
  116.       tempIndex++;
  117.        
  118.       if(tempIndex >= tempThresholds){
  119.         tempIndex = tempThresholds-1;
  120.         break;
  121.       }
  122.     }
  123.      
  124.     return ((tempIndex+1)*255)/tempThresholds;
  125.   }else{
  126.       return 0;
  127.   }
  128. }
  129.  
  130. int getLightVoltage(int L){
  131.   int lightIndex = 0;
  132.   float lightValue = lightMAX - L;
  133.  
  134.   if(lightRange[lightIndex]-lightValue < 1){
  135.     while(lightRange[lightIndex]-lightValue < 1){
  136.       Serial.print(lightIndex);
  137.       Serial.print(" ");
  138.       Serial.println(lightRange[lightIndex]-lightValue);
  139.       lightIndex++;
  140.        
  141.       if(lightIndex >= lightThresholds){
  142.         lightIndex = lightThresholds-1;
  143.         break;
  144.       }
  145.     }
  146.      
  147.       return ((lightIndex+1)*255)/lightThresholds;
  148.     }
  149.     else{
  150.       return 0;
  151.   }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement