Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int thermistorPin = A0;
- int ldrPIN = A2;
- int tempLED = 6;
- int lightLED = 3;
- int tempThresholds = 1;
- float* tempRange = 0;
- float tempMIN;
- float tempMAX;
- int lightThresholds = 1;
- float* lightRange = 0;
- float lightMIN;
- float lightMAX;
- void setup() {
- Serial.begin(9600);
- pinMode(tempLED, OUTPUT);
- pinMode(lightLED, OUTPUT);
- tempMIN = 20.0;
- tempMAX = 30.0;
- tempThresholds = 50;
- lightMIN = 450;
- lightMAX = 600;
- lightThresholds = 20;
- if (tempRange != 0) {
- delete [] tempRange;
- }
- if (lightRange != 0) {
- delete [] lightRange;
- }
- tempRange = new float[tempThresholds];
- lightRange = new float[lightThresholds];
- float tempX = tempMAX - tempMIN;
- for(int i=0; i<tempThresholds; i++){
- tempRange[i] = ((i+1)*tempX)/tempThresholds;
- }
- float lightX = lightMAX - lightMIN;
- for(int i=0; i<lightThresholds; i++){
- lightRange[i] = ((i+1)*lightX)/lightThresholds;
- }
- }
- void loop() {
- Serial.println(lightRange[0]);
- Serial.println(lightRange[1]);
- Serial.println(lightRange[2]);
- Serial.println(lightRange[3]);
- Serial.println(lightRange[4]);
- Serial.println(lightRange[5]);
- Serial.println(lightRange[6]);
- Serial.println(lightRange[7]);
- Serial.println(lightRange[8]);
- int Tc = getCelsius(analogRead(thermistorPin));
- int Light = analogRead(ldrPIN);
- int tempVoltage = getTempVoltage(Tc);
- int lightVoltage = getLightVoltage(Light);
- Serial.print("Temperature: ");
- Serial.print(Tc);
- Serial.println(" C");
- Serial.print("Light: ");
- Serial.print(Light);
- Serial.println("");
- if(tempVoltage >= 0 && tempVoltage <= 255){
- analogWrite(tempLED,tempVoltage);
- Serial.print("Temp Voltage: ");
- Serial.println(tempVoltage);
- }
- Serial.println("");
- if(lightVoltage >= 0 && lightVoltage <= 255){
- Serial.print("LIGHT Voltage: ");
- Serial.println(lightVoltage);
- analogWrite(lightLED,lightVoltage);
- }
- delay(500);
- }
- int getCelsius(int sensor){
- float R1 = 10000;
- float logR2, R2, T, Tc, Tf;
- float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
- R2 = R1 * (1023.0 / (float)sensor - 1.0);
- logR2 = log(R2);
- T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
- int temp = T - 273.15;
- return temp;
- }
- int getTempVoltage(int C){
- int tempIndex = 0;
- float tempValue = tempMAX - C;
- if(tempRange[tempIndex]-tempValue < 1){
- while(tempRange[tempIndex]-tempValue < 1){
- tempIndex++;
- if(tempIndex >= tempThresholds){
- tempIndex = tempThresholds-1;
- break;
- }
- }
- return ((tempIndex+1)*255)/tempThresholds;
- }else{
- return 0;
- }
- }
- int getLightVoltage(int L){
- int lightIndex = 0;
- float lightValue = lightMAX - L;
- if(lightRange[lightIndex]-lightValue < 1){
- while(lightRange[lightIndex]-lightValue < 1){
- Serial.print(lightIndex);
- Serial.print(" ");
- Serial.println(lightRange[lightIndex]-lightValue);
- lightIndex++;
- if(lightIndex >= lightThresholds){
- lightIndex = lightThresholds-1;
- break;
- }
- }
- return ((lightIndex+1)*255)/lightThresholds;
- }
- else{
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement