Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #Written to control Fan and Heating device through a MOSFET
- #to create a temperature controlled incubator
- #Data received through the serial port changes the value of the temperature
- int temppin = 2;
- int heatpin = 3;
- int fanpin = 4;
- char *settemp;
- char *temperature;
- double currtemp;
- int start = 0;
- int y = 0;
- int temp_find(double totemp)
- {
- int heatval;
- int fanval;
- int timeout = 0;
- int chances = 0;
- while(1)
- {
- currtemp = analogRead(temppin);
- if(totemp > (currtemp - 1) && totemp < (currtemp + 1)){ break; }
- else if(totemp > currtemp)
- {
- heatval = log(totemp/currtemp)*100;
- if(heatval > 256){ heatval = 256; }
- digitalWrite(heatpin, heatval);
- }
- else if(totemp < currtemp)
- {
- fanval = log(totemp/currtemp)*100;
- if(fanval > 256){ fanval = 256; }
- digitalWrite(fanpin, fanval);
- }
- if(timeout > 1000 && chances < 3)
- {
- Serial.println("Error cannot stabilize temp. Pausing for 5 seconds");
- delay(5000);
- chances++;
- timeout = 0;
- }
- if(chances >=3)
- {
- Serial.println("Something is wrong with your temperature control!!!!");
- }
- timeout++;
- }
- delay(50);
- return currtemp;
- }
- char *token(char buffer[])
- {
- int h;
- char stringser[255] = {0};
- for(h = 0; h < strlen(buffer); h++)
- {
- if(buffer[h] != ':'){ stringser[h] = buffer[h]; }
- else{ break; }
- }
- return stringser;
- }
- void *clalloc(void *p, int val, int sizem)
- {
- p = malloc(sizem);
- memset(p, val, sizem);
- return p;
- }
- void setup()
- {
- Serial.begin(9600);
- clalloc(temperature, '\0', (sizeof(char)*10));
- clalloc(settemp,'\0', (sizeof(char)*10));
- settemp = "50";
- }
- void loop()
- {
- char buff[254] = {0};
- int x = 0;
- char *empty;
- char *temp;
- temperature = settemp;
- while(Serial.available() > 0)
- {
- buff[x] = Serial.read();
- //if(buff[x] == '\n'){ break; }
- x++;
- if(x == 254){ break; }
- }
- if(strstr(buff, "ID"))
- {
- Serial.println("ID:Thermo:IDEND");
- }
- if(strstr(buff, "Temp"))
- {
- temperature = token(buff);
- settemp = temperature;
- Serial.print(temperature);
- Serial.println(":Temp:SUCCESS");
- }
- else
- { temperature = settemp;}
- Serial.print(temperature);
- Serial.print(":");
- Serial.print(y);
- Serial.println(":END");
- y++;
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement