DS18B20 to PWM

Hello! I can´t get the kode to work when im trying to use the DS18B20 sensor to work with pwm…

DS18B20SensortoPWM.ino (2.06 KB)

// DS18B20 Read to 128x64 OLED Display

// Include the OneWire library
#include <OneWire.h>

// Include the Dallas Temperature library
#include <DallasTemperature.h>
#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE, 6);

char str[10];




// Setup the OneWire bus on Pin 2
OneWire bus(2);

// Setup DallasTemperature to work on the OneWire bus
DallasTemperature sensors(&bus);

// Include the U8glib Library
#include <U8glib.h>

// Configure the OLED Screen (I2C)
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST);

float temperature;
char temperatureString[6] = "-";

void drawTest(void) {
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 20, "Penis!");
}



void setup() {
  // Start the sensors
  sensors.begin();
 dht.begin();
  u8g.firstPage();  
  do {
    drawTest();
  } while( u8g.nextPage() );
  pinMode(9,OUTPUT);
}

void loop() {

  float temperature = getTemperature();
  int voltage = temperatureToPWM(temperature);
  analogWrite(9,voltage);

  int temperatureToPWM(float temperature)
{
  float temp=0;
  float voltage = 0;
  temp = temperature*10;
  voltage = map(temp,MIN_TEMP*10,MAX_TEMP*10,0,250);
  return voltage;
}
  
  // Update the sensor readings
  sensors.requestTemperatures();
  
  // Read Temperature
  temperature = sensors.getTempCByIndex(0);
  dtostrf(temperature, 2, 0, temperatureString);

  // Read temperature as Celsius
  float t = dht.readTemperature();
 
 // picture loop
  u8g.firstPage();  
  do {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_helvB10);
  u8g.drawStr( 30, 15, "Panntemp:");
  u8g.setFont(u8g_font_helvB12r);
  u8g.drawStr( 50, 32, temperatureString);
  u8g.setFont(u8g_font_helvB12);
  u8g.drawStr(68,32,"\260C");
  u8g.setFont(u8g_font_helvB10);
  u8g.drawStr( 10, 47, "Temperatur inne:");
  u8g.setFont(u8g_font_helvB12r);
  u8g.drawStr( 50, 62, dtostrf(t, 2, 0, str));
  u8g.setFont(u8g_font_helvB12);
  u8g.drawStr(68,62,"\260C");
   
   
   } while( u8g.nextPage() );

   
  // Delay for 2 seconds between readings
  delay(500);
}

You have tried to declare your temperatureToPWM function inside the loop function. Move it out.