Advertisement
RuiViana

Sensor_Temp_e_Fluxo

Oct 16th, 2016
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <OneWire.h>
  2. int DS18S20_Pin = 3; //DS18S20 Signal pin on digital 3
  3. OneWire ds(DS18S20_Pin); // on digital pin 3
  4.  
  5. int NbTopsFan; int Calc;                //Varibles used for calculations
  6. int hallsensor = 2; typedef struct      //The pin location of the sensor
  7. {
  8.   char fantype;
  9.   unsigned int fandiv;
  10. } fanspec;
  11. fanspec fanspace[3] = {{0, 1}, {1, 2}, {2, 8}}; char fan = 1;
  12. //-------------------------------------------
  13. void setup(void)
  14. {
  15.   Serial.begin(9600);
  16.   pinMode(hallsensor, INPUT);
  17.   attachInterrupt(0, rpm, RISING);
  18. }
  19. //---------------------------------
  20. void rpm ()
  21. //This is the function that the interupt calls
  22. {
  23.   NbTopsFan++;
  24. }
  25. //------------------------------------------
  26. void loop(void)
  27. {
  28.   float temperature = getTemp();
  29.   Serial.println(temperature);
  30.   delay(2000);                    //just here to slow down the output so it is easier to read
  31.  
  32.   NbTopsFan = 0;                  //Set NbTops to 0 ready for calculations
  33.   sei();                          //Enables interrupts
  34.   delay (1000);                   //Wait 1 second
  35.   cli();                          //Disable interrupts
  36.   Calc = ((NbTopsFan * 60) / fanspace[fan].fandiv);
  37.   Serial.print (Calc, DEC);       //Prints the number calculated above
  38.   Serial.print (" rpm\r\n");      //Prints " rpm" and a new line
  39. }
  40. //--------------------------------------------
  41. float getTemp()                   //returns the temperature from one DS18S20 in DEG Celsius
  42. {
  43.   byte data[12];
  44.   byte addr[8];
  45.   if ( !ds.search(addr))          //no more sensors on chain, reset search
  46.   {
  47.     ds.reset_search();
  48.     return -1000;
  49.   }
  50.   if ( OneWire::crc8( addr, 7) != addr[7])
  51.   {
  52.     Serial.println("CRC is not valid!");
  53.     return -1000;
  54.   }
  55.   if ( addr[0] != 0x10 && addr[0] != 0x28)
  56.   {
  57.     Serial.print("Device is not recognized");
  58.     return -1000;
  59.   }
  60.   ds.reset();
  61.   ds.select(addr);
  62.   ds.write(0x44, 1);                    // start conversion, with parasite power on at the end
  63.   byte present = ds.reset();
  64.   ds.select(addr);
  65.   ds.write(0xBE);                       // Read Scratchpad
  66.   for (int i = 0; i < 9; i++)           // we need 9 bytes
  67.   {
  68.     data[i] = ds.read();
  69.   }
  70.   ds.reset_search();
  71.   byte MSB = data[1];
  72.   byte LSB = data[0];
  73.   float tempRead = ((MSB << 8) | LSB);    //using two's compliment
  74.   float TemperatureSum = tempRead / 16;
  75.   return TemperatureSum;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement