Advertisement
Guest User

kod/program

a guest
Jan 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 KB | None | 0 0
  1. [code]
  2. int led = 3;           // led wpiety do pina
  3. int brightness = 0;    // jasnosc led
  4.  
  5.  
  6. void setup() {
  7.   // deklaraccja jako wyjscie pinu numer 3 (!!!)
  8.   pinMode(led, OUTPUT);
  9. }
  10.  
  11. //petla nieskonczona
  12. void loop() {
  13.   // ustawiamy jasnosc pinu 3
  14. //tutaj podpinasz odczyt termometra i ustalasz wartoś
  15. {
  16.   int temperatura =
  17. // OneWire DS18S20, DS18B20, DS1822 Temperature Example
  18. //
  19. // http://www.pjrc.com/teensy/td_libs_OneWire.html
  20. //
  21. // The DallasTemperature library can do all this work for you!
  22. // http://milesburton.com/Dallas_Temperature_Control_Library
  23.  
  24. OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)
  25.  
  26. void setup(void) {
  27.   Serial.begin(9600);
  28. }
  29.  
  30. void loop(void) {
  31.   byte i;
  32.   byte present = 0;
  33.   byte type_s;
  34.   byte data[12];
  35.   byte addr[8];
  36.   float celsius, fahrenheit;
  37.  
  38.   if ( !ds.search(addr)) {
  39.     Serial.println("No more addresses.");
  40.     Serial.println();
  41.     ds.reset_search();
  42.     delay(250);
  43.     return;
  44.   }
  45.  
  46.   Serial.print("ROM =");
  47.   for( i = 0; i < 8; i++) {
  48.     Serial.write(' ');
  49.     Serial.print(addr[i], HEX);
  50.   }
  51.  
  52.   if (OneWire::crc8(addr, 7) != addr[7]) {
  53.       Serial.println("CRC is not valid!");
  54.       return;
  55.   }
  56.   Serial.println();
  57.  
  58.  
  59.  
  60.   // the first ROM byte indicates which chip
  61.   switch (addr[0]) {
  62.     case 0x10:
  63.       Serial.println("  Chip = DS18S20");  // or old DS1820
  64.       type_s = 1;
  65.       break;
  66.     case 0x28:
  67.       Serial.println("  Chip = DS18B20");
  68.       type_s = 0;
  69.       break;
  70.     case 0x22:
  71.       Serial.println("  Chip = DS1822");
  72.       type_s = 0;
  73.       break;
  74.     default:
  75.       Serial.println("Device is not a DS18x20 family device.");
  76.       return;
  77.   }
  78.  
  79.   ds.reset();
  80.   ds.select(addr);
  81.   ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  82.  
  83.   delay(1000);     // maybe 750ms is enough, maybe not
  84.   // we might do a ds.depower() here, but the reset will take care of it.
  85.  
  86.   present = ds.reset();
  87.   ds.select(addr);    
  88.   ds.write(0xBE);         // Read Scratchpad
  89.  
  90.   Serial.print("  Data = ");
  91.   Serial.print(present, HEX);
  92.   Serial.print(" ");
  93.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  94.     data[i] = ds.read();
  95.     Serial.print(data[i], HEX);
  96.     Serial.print(" ");
  97.   }
  98.   Serial.print(" CRC=");
  99.   Serial.print(OneWire::crc8(data, 8), HEX);
  100.   Serial.println();
  101.  
  102.   // Convert the data to actual temperature
  103.   // because the result is a 16 bit signed integer, it should
  104.   // be stored to an "int16_t" type, which is always 16 bits
  105.   // even when compiled on a 32 bit processor.
  106.   int16_t raw = (data[1] << 8) | data[0];
  107.   if (type_s) {
  108.     raw = raw << 3; // 9 bit resolution default
  109.     if (data[7] == 0x10) {
  110.       // "count remain" gives full 12 bit resolution
  111.       raw = (raw & 0xFFF0) + 12 - data[6];
  112.     }
  113.   } else {
  114.     byte cfg = (data[4] & 0x60);
  115.     // at lower res, the low bits are undefined, so let's zero them
  116.     if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  117.     else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  118.     else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  119.     //// default is 12 bit resolution, 750 ms conversion time
  120.   }
  121.   celsius = (float)raw / 16.0;
  122.   fahrenheit = celsius * 1.8 + 32.0;
  123.   Serial.print("  Temperature = ");
  124.   Serial.print(celsius);
  125.   Serial.print(" Celsius, ");
  126.   Serial.print(fahrenheit);
  127.   Serial.println(" Fahrenheit");
  128. }; //tutaj masz deklaracje z przypisaniem wartosci; zmianiasz tutaj
  129.  
  130.   analogWrite(led, brightness);
  131.  
  132.   //zmiana jasnoci w trakcie nastepnego przejscia petli:
  133.   if (temperatura=0)  
  134.      brightness = 80;
  135.  else if (temperatura >= 0 && temperatura <= 2)
  136.  brightness= 20;
  137.  else if (temperatura >= 2 && temperatura <= 4
  138.  brightness= 40;
  139.  else if (temperatura >= 4 && temperatura <= 6)
  140.  brightness= 60;
  141.  else if (temperatura >= 6 && temperatura <= 8)
  142.  brightness = 80;
  143.  else if (temperatura >= 8 && temperatura <= 10)
  144.  brightness = 140;
  145.  else if (temperatura >= 10 && temperatura <= 12)
  146.  brightness = 100;
  147.  else if (temperatura >= 12 && temperatura <= 14)
  148.  brightness = 120;
  149.  else if (temperatura >= 14 && temperatura <= 16)
  150.  brightness = 140;
  151.  else if (temperatura >= 16 && temperatura <= 18)
  152.  brightness = 160;
  153.  else if (temperatura >= 18 && temperatura <= 20)
  154.  brightness = 180;
  155.  else if (temperatura >= 20 && temperatura <= 22)
  156.  brightness = 200;
  157.  else if (temperatura >= 22 && temperatura <= 24)
  158.  brightness = 220;
  159.  else if (temperatura >= 24 && temperatura <= 26)
  160.  brightness = 240;
  161.  else if (temperatura > 26 && temperatura < 28)
  162.  brightness  = 260;
  163.  else if (temperatura >= 28 && temperatura <= 30)
  164.  brightness = 280;
  165.  else if (temperatura >=30  && temperatura <= 32)
  166.  brightness = 300;
  167.  else if (temperatura >= 32 && temperatura <= 34}
  168.  brightness = 320;
  169.  else if (temperatura >= 34 && temperatura <= 36)
  170.  brightness = 340;
  171.  else if (temperatura >= 36 && temperatura <= 37)
  172.  brightness = 360;
  173.   delay(30);
  174. }
  175.  
  176.  
  177. /* nastepne zadanie, na jutro bedziesz miał takie
  178.  *  zrobic tak aby zmienna brightness zamienic na temperature tj zrobic zalezność swiatła od wysokosci temperatury... tylko to chyba bedzie dość trudne net chwile mi daj
  179.  *  ustawiasz fade amount dla 20 stopni dla czujnika temp do max 36 (temperatura ciała człowieka)
  180.  *  czyli if (temperatura == 20 )
  181.  *          fadeAmount = 80;
  182.  *        if (temperatura == 24)
  183.  *          fadeAmount = 96;
  184.  *        if (temperatura == 28)
  185.  *          fadeoumnt  = 112;
  186.  *          
  187.  *          rozumiesz? na razie tak, potem zbudujemy tablice PWM w liczbach całkowityh aby była płynna regulacja
  188.  */
  189. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement