Advertisement
ZaynerTech

Arduino Code For Website Interaction

Mar 31st, 2014
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #Written to control Fan and Heating device through a MOSFET
  6. #to create a temperature controlled incubator
  7. #Data received through the serial port changes the value of the temperature
  8.  
  9. int temppin = 2;
  10. int heatpin = 3;
  11. int fanpin = 4;
  12.  
  13. char *settemp;
  14. char *temperature;
  15. double currtemp;
  16.  
  17. int start = 0;
  18. int y = 0;
  19.  
  20. int temp_find(double totemp)
  21. {
  22.   int heatval;
  23.   int fanval;
  24.   int timeout = 0;
  25.   int chances = 0;
  26.  
  27.   while(1)
  28.   {
  29.    
  30.    currtemp = analogRead(temppin);
  31.    if(totemp > (currtemp - 1) && totemp < (currtemp + 1)){ break; }
  32.    else if(totemp > currtemp)
  33.    {
  34.      heatval = log(totemp/currtemp)*100;
  35.      if(heatval > 256){ heatval = 256; }  
  36.      digitalWrite(heatpin, heatval);
  37.    }
  38.    else if(totemp < currtemp)
  39.    {
  40.      fanval = log(totemp/currtemp)*100;
  41.      if(fanval > 256){ fanval = 256; }  
  42.      digitalWrite(fanpin, fanval);
  43.    }
  44.    if(timeout > 1000 && chances < 3)
  45.    {
  46.      Serial.println("Error cannot stabilize temp. Pausing for 5 seconds");
  47.      delay(5000);
  48.      chances++;  
  49.      timeout = 0;
  50.    }  
  51.    if(chances >=3)
  52.    {
  53.      Serial.println("Something is wrong with your temperature control!!!!");
  54.    }
  55.    
  56.    timeout++;
  57.   }  
  58.   delay(50);
  59.   return currtemp;
  60. }
  61.  
  62.  
  63. char *token(char buffer[])
  64. {
  65.    int h;
  66.    char stringser[255] = {0};
  67.    for(h = 0; h < strlen(buffer); h++)
  68.    {
  69.      if(buffer[h] != ':'){ stringser[h] = buffer[h]; }
  70.      else{ break; }
  71.    }
  72.    
  73.    return stringser;
  74.  
  75. }
  76. void *clalloc(void *p, int val, int sizem)
  77. {
  78.   p = malloc(sizem);
  79.   memset(p, val, sizem);
  80.   return p;
  81. }
  82.  
  83. void setup()
  84. {
  85.   Serial.begin(9600);
  86.  
  87.   clalloc(temperature, '\0', (sizeof(char)*10));
  88.   clalloc(settemp,'\0', (sizeof(char)*10));
  89.  
  90.   settemp = "50";
  91.  
  92.  
  93. }
  94.  
  95. void loop()
  96. {
  97.   char buff[254] = {0};
  98.   int x = 0;
  99.   char *empty;
  100.   char *temp;
  101.  
  102.   temperature = settemp;
  103.  
  104.   while(Serial.available() > 0)
  105.   {
  106.    
  107.    buff[x] = Serial.read();
  108.    //if(buff[x] == '\n'){ break; }
  109.    x++;
  110.    if(x == 254){ break; }
  111.   }
  112.  
  113.   if(strstr(buff, "ID"))
  114.   {
  115.    Serial.println("ID:Thermo:IDEND");
  116.   }
  117.  
  118.   if(strstr(buff, "Temp"))
  119.   {
  120.     temperature = token(buff);
  121.     settemp = temperature;
  122.    
  123.     Serial.print(temperature);
  124.     Serial.println(":Temp:SUCCESS");
  125.    
  126.   }
  127.   else
  128.   { temperature = settemp;}
  129.  
  130.   Serial.print(temperature);
  131.   Serial.print(":");
  132.   Serial.print(y);
  133.   Serial.println(":END");
  134.  
  135.   y++;
  136.   delay(1000);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement