Advertisement
insaneisnotfree

Soplando la vela arduino

Sep 23rd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. //EL CODIGO NO ESTA COMENTADO Y NO ESTA TABULADO, sorry
  2. #define DHT11_PIN 0      // ADC0
  3.   int coso=0;
  4. int arranca(int num)
  5. {
  6.   if (num==1) {
  7. //Reiniciando la lectura de sensor dth
  8.   DDRC |= _BV(DHT11_PIN);
  9.   PORTC |= _BV(DHT11_PIN);
  10.   }
  11. }
  12.  
  13. byte read_dht11_dat()
  14. {
  15.   byte i = 0;
  16.   byte result=0;
  17.   for(i=0; i< 8; i++)
  18.   {
  19.     while(!(PINC & _BV(DHT11_PIN)));  // wait for 50us
  20.     delayMicroseconds(30);
  21.  
  22.     if(PINC & _BV(DHT11_PIN))
  23.       result |=(1<<(7-i));
  24.     while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
  25.   }
  26.   return result;
  27. }
  28.  
  29.   int relay=10;
  30. void setup() {
  31.  
  32.   pinMode(relay,OUTPUT);
  33.   DDRC |= _BV(DHT11_PIN);
  34.   PORTC |= _BV(DHT11_PIN);
  35.  
  36.   Serial.begin(9600);
  37.   Serial.println("Ready");
  38. }
  39.  
  40. void loop()
  41. {
  42.  
  43.   byte dht11_dat[5];
  44.   byte dht11_in;
  45.   byte i;
  46.   int relay=10;
  47.   // start condition
  48.   // 1. pull-down i/o pin from 18ms
  49.   PORTC &= ~_BV(DHT11_PIN);
  50.   delay(18);
  51.   PORTC |= _BV(DHT11_PIN);
  52.   delayMicroseconds(40);
  53.  
  54.   DDRC &= ~_BV(DHT11_PIN);
  55.   delayMicroseconds(40);
  56.  
  57.   dht11_in = PINC & _BV(DHT11_PIN);
  58.  
  59.   if(dht11_in)
  60.   {
  61.     int coso=coso+1;
  62.     digitalWrite(relay, LOW);
  63.     delay(500);
  64.     digitalWrite(relay, HIGH);
  65.     delay(500);
  66.     Serial.println("El sensor no esta conectado...");
  67.  
  68.     arranca(coso);
  69.     return;
  70.   }
  71.   delayMicroseconds(80);
  72.  
  73.   dht11_in = PINC & _BV(DHT11_PIN);
  74.  
  75.   if(!dht11_in)
  76.   { int coso=coso+1;
  77.     digitalWrite(relay, LOW);
  78.     delay(500);
  79.     digitalWrite(relay, HIGH);
  80.     delay(500);
  81.     Serial.println("dht11 start condition 2 not met");
  82.     arranca(coso);
  83.     return;
  84.  
  85.  
  86.   }
  87.   delayMicroseconds(80);
  88.   // now ready for data reception
  89.   for (i=0; i<5; i++)
  90.     dht11_dat[i] = read_dht11_dat();
  91.  
  92.   DDRC |= _BV(DHT11_PIN);
  93.   PORTC |= _BV(DHT11_PIN);
  94.  
  95.   byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
  96.   // check check_sum
  97.   if(dht11_dat[4]!= dht11_check_sum)
  98.   {
  99.     Serial.println("DHT11 checksum error");
  100.   }
  101.  
  102.   Serial.print("Humedad = ");
  103.   Serial.print(dht11_dat[0], DEC);
  104.   Serial.print(".");
  105.   Serial.print(dht11_dat[1], DEC);
  106.   Serial.print("%  ");
  107.   Serial.print("Temperatura = ");
  108.   Serial.print(dht11_dat[2], DEC);
  109.   Serial.print(".");
  110.   Serial.print(dht11_dat[3], DEC);
  111.   Serial.println("C  ");
  112.  
  113.   delay(1000);
  114.   if (dht11_dat[0]<=60) {
  115.    digitalWrite(relay, LOW);
  116.  } else {
  117.    digitalWrite(relay, HIGH);
  118.  }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement