Understanding Code

Hi Guys,

Trying to understand this code.

Anybody willing to add some sensible comments.

Regards
Peter

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int MIN_TEMP = 16;
int MAX_TEMP = 28;

void setup() {

pinMode(9,OUTPUT);
Serial.begin(9600);
sensors.begin();

}

void loop() {

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

float getTemperature()
{
  float temperature =0;
  sensors.requestTemperatures(); // Send the command to get temperatures
  temperature = sensors.getTempCByIndex(0);
  Serial.println(temperature);
  return temperature;
}

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;
}

Which parts don’t you understand?

This line probably does not need a comment:

  float temperature = getTemperature();

map uses integer-only arithmetic, so I don't know why you'd want to use it on a float.

pinMode(9,OUTPUT); // redundant; could be removed

Which parts don't you understand?

Yes, this is the first comment that comes to mind. :slight_smile:

AWOL thanks for the useful feedback.

I shall re-phrase my question.

Would somebody add comments to the code to help me understand, as its is confusing me at the moment.

Regards
Peter

Can you say what you are confused by?

Apart from not giving pin 9 a useful name, it looks pretty straightforward, if a little long.

This I understand :slight_smile:

const int analogInPin= 0; //Potentiometer wiped connect to pin A0
const int analogMeterPin= 9; //Analog output pin connected to the meter

int sensorValue= 0; //value read from the potentiometer
int outputValue= 0; //value output to the terminal D9 (analog out)

void setup ()
{
//nothing in setup
}

void loop()
{
sensorValue= analogRead(analogInPin); //this gives us the analog voltage read from the potentiometer
outputValue= map (sensorValue, 0, 1023, 0, 255); //scale for analog out value
analogWrite (analogMeterPin, outputValue); // write the analog out value
}

This I understand

But you can't seem to tell us what you don't understand.

//Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

//Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

//Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

//Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

//set the lower bounds
int MIN_TEMP = 16;
//set the upper bounds
int MAX_TEMP = 28;

void setup()
{
  //Not really needed
  pinMode(9, OUTPUT);
  //Set baud rate, start serial port
  Serial.begin(9600);
  //Start up the library
  sensors.begin();

}

void loop()
{
  //read the temperature
  float temperature = getTemperature();
  //convert temperature to an equivalent PWM value
  int voltage = temperatureToPWM(temperature);
  //send PMW converted value to a PWM pin
  analogWrite(9, voltage);
  //wait for 1/2 second
  delay(500);
}

float getTemperature()
{
  float temperature = 0;
  //Send the command to get temperatures
  sensors.requestTemperatures();
  //Get temperature, use the function ByIndex, from the first sensor.
  temperature = sensors.getTempCByIndex(0);
  //send to serial monitor
  Serial.println(temperature);
  //return value to calling routine
  return temperature;
}

int temperatureToPWM(float temperature)
{

  float temp = 0;
  float voltage = 0;
  
  //adjust to temperature keep the tenths digit
  temp = temperature * 10;
  //as mentioned should use 'integer'
  //See: https://www.arduino.cc/reference/en/language/functions/math/map/
  //filter values between lower to upper bounds, *10 as we did tenths decimal adjustment previously
  //convert to equivalent 0-250 for PWM
  voltage = map(temp, MIN_TEMP * 10, MAX_TEMP * 10, 0, 250);
  //return to calling routine the mapped conversion
  return voltage;
}

LarryD that helps me a great deal:)

Many thanks
Peter