I want to now use small Thermistor for my code, Im LOST!

For the discussion of Arduino related topics.

Moderator: phalanx

Post Reply
toxsickcity
Posts: 3
Joined: Thu Nov 09, 2017 6:34 am

I want to now use small Thermistor for my code, Im LOST!

Post by toxsickcity » Thu Nov 09, 2017 6:41 am

Hi All,

I am new here, I hope to get some help to make a NTC 10K thermistor to work with my arduino project...

I hope everyone is well to get some help on my code for project I am doing.
I have tried for days and I just don't get it. it may stand to you guys.

My project is using a program thats gets written to a programmable circuit (Arduino) that reads a sensor I didn't like the ONEWIRE sensor and now wish to use 10K type which are much much smaller and can better hide!

I have made my code from searching all over the internet and putting them together.. in the end I got it working but now which to use those 10k thermistor as they are tiny and can fit in smallest places..

Can you guys can read my program and help to see where I am going wrong. I have marked areas of interest with // areas.

Everytime I compile I am getting GetTemp4 was not declared in this scope Line 65
float temperature = constrain(getTemp4(), MIN_TEMP, MAX_TEMP);

thanks, Please read on as I attached other code I stole the thermistor program

Code: Select all

// LINE 6-17 = new code trying to make use of TEMP_PIN A0 which is a thermistor.
// LINE 41 added TEMP-PIN as an input
// Lines 57-58 I copied from another code.
// LINES 87-134 I have disabled to try and start using a new Thermistor line 6

#define TEMP_PIN A0
//int adc = 0;
//int blue = 0, red = 0;
double ReadThermistor(int adc) {
  double resistance = ((1024.0/adc) - 1);    //calculate from voltage divider, for 10k resistor
  double Temp4 = log(resistance);
  // calculate the temperature, in K, using 4 thermistor model/material specific parameters A, B, C, D
  // here we use the values for the Sparkfun/Hactronics version of the Vishay 10k NTC thermistor
  Temp4 = 1 / (0.003354016 + 0.0002569850 * Temp4 + 0.000002620131 * Temp4 * Temp4 + 0.00000006383091 * Temp4 * Temp4 * Temp4);
  Temp4 = Temp4 - 273.15;            // Convert Kelvin to Celsius
  return Temp4;
}


//#include "OneWire.h"
//#include "Streaming.h"

//const int DS18S20a_Pin = 2; //DS18S20 Signal pin on digital 2
#define MIN_TEMP 10
#define MAX_TEMP 45
#define RELAY1  4 // Relay on digital pin 4
//OneWire ds(DS18S20a_Pin);

#define SIZE    255
#define DELAY    0
#define HUE_MAX  6.0
#define HUE_DELTA 0.01
int firstPins [] = { 3,5};
long rgb[2];
long rgbval;
float hue=0.0, saturation=1, value=1;
long bright[2] = { 256, 256};
long k, temp_value;

void setup () {
    pinMode(TEMP_PIN, INPUT);

    
  randomSeed(analogRead(4));
  pinMode(RELAY1, OUTPUT);
  Serial.begin(57600);
  for (k=0; k<2; k++) {
    pinMode(firstPins[k], OUTPUT);
    rgb[k]=0;
     analogWrite(firstPins[k], rgb[k] * bright[k]/256);
  }
}


void loop() {
  adc = analogRead(TEMP_PIN);
  int temp4 = ReadThermistor(adc);
  
  pinMode(RELAY1, OUTPUT); //new


//This is to make use of temperature to setup lighting and activate relay switches based of heat etc
 // float temperature = constrain(getTemp(), MIN_TEMP, MAX_TEMP);    // REMOVED LINE TO NOW USE DIFFERENT SENSOR the gettemp is below and cannot work out how to attach new sensor
    float temperature = constrain(getTemp4(), MIN_TEMP, MAX_TEMP);   // I HAVE RENAMED Temp to Temp4 to make it clear as the code above Lines 11-16
    
    float deltaTemp = (MAX_TEMP - MIN_TEMP);
    float deltaHue = 4 - 0;

  hue = map((temperature - MIN_TEMP) * 100, 0, deltaTemp * 100, deltaHue * 100, 0) / 100.0;
  rgbval=HSV_to_RGB(hue, saturation, value);
  rgb[0] = (rgbval & 0x00FF0000) >> 16;
  rgb[1] = (rgbval & 0x0000ff00) >> 8;
            for (k=0; k<2; k++) {
    analogWrite(firstPins[k], rgb[k] * bright[k]/256);}

//SECTION CONTROL RELAY SWITCH
if (temperature < 40) {
digitalWrite(4,HIGH);
} else if (temperature > 36) {
digitalWrite(4,LOW);
}
}



//SENSORS SECTION

//     I DISABLED THIS PART OF CODE TO ALLOW USE OF MY NEW SENSOR NOT THE ONEWIRE TYPE.

//float getTemp(){
 //returns the temperature from one DS18S20 in DEG Celsius

 //byte data[12];
// byte addr[8];

// if ( !ds.search(addr)) {
//   //no more sensors on chain, reset search
//   ds.reset_search();
//   return -10;
// }
// if ( OneWire::crc8( addr, 7) != addr[7]) {
//   Serial.println("CRC is not valid!");
//   return -10;
// }
// if ( addr[0] != 0x10 && addr[0] != 0x28) {
//   Serial.print("Device is not recognized");
//   return -10;
// }

// ds.reset();
// ds.select(addr);
// ds.write(0x44,1); // start conversion, with parasite power on at the end

// byte present = ds.reset();
// ds.select(addr);  
// ds.write(0xBE); // Read Scratchpad

 
// for (int i = 0; i < 9; i++) { // we need 9 bytes
//  data[i] = ds.read();
// }
 
// ds.reset_search();
 
// byte MSB = data[1];
// byte LSB = data[0];

// float tempRead = ((MSB << 8) | LSB); //using two's compliment
// float TemperatureSum = tempRead / 16;
 
// return TemperatureSum;
//}
// End Sensors





//THIS SECTION IS CREATING COLOURS TO DISPLAY ON LED STRIP

long HSV_to_RGB( float h, float s, float v ) {
  /* modified from Alvy Ray Smith's site: http://www.alvyray.com/Papers/hsv2rgb.htm */
  // H is given on [0, 6]. S and V are given on [0, 1].
  // RGB is returned as a 24-bit long #rrggbb
  int i;
  float m, n, f;

  // not very elegant way of dealing with out of range: return black
  if ((s<0.0) || (s>1.0) || (v<1.0) || (v>1.0)) {
    return 0L;
  }

  if ((h < 0.0) || (h > 6.0)) {
    return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;
  }
  i = floor(h);
  f = h - i;
  if ( !(i&1) ) {
    f = 1 - f; // if i is even
  }
  m = v * (1 - s);
  n = v * (1 - s * f);
  switch (i) {
  case 6:
  case 0: 
    return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
  case 1: 
    return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
  case 2: 
    return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
  case 3: 
    return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
  case 4: 
    return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
  case 5: 
    return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);
  }
}

}
I have copy and pasted some code from this program which is simpler and use RGB.. I want only RedBlue and my main code has what I need.. as to why I want to make the sensor in this code work my mine

https://create.arduino.cc/projecthub/be ... led-6c8cdf

Code: Select all

#define TEMP_PIN A0
#define RED_PIN 9
#define GREEN_PIN 10
#define BLUE_PIN 11

int adc = 0;
int blue = 0, red = 0;

double ReadThermistor(int adc) {

  double resistance = ((1024.0/adc) - 1);    //calculate from voltage divider, for 10k resistor
  double Temp = log(resistance);

  // calculate the temperature, in K, using 4 thermistor model/material specific parameters A, B, C, D
  // here we use the values for the Sparkfun/Hactronics version of the Vishay 10k NTC thermistor
  Temp = 1 / (0.003354016 + 0.0002569850 * Temp + 0.000002620131 * Temp * Temp + 0.00000006383091 * Temp * Temp * Temp);
  Temp = Temp - 273.15;            // Convert Kelvin to Celsius
  return Temp;
}

void setLED(int blue, int red){
  analogWrite(BLUE_PIN, blue);
  analogWrite(RED_PIN, red);
}

void setup(){
  Serial.begin(9600);
  pinMode(BLUE_PIN, OUTPUT); 
  pinMode(RED_PIN, OUTPUT); 
  pinMode(GREEN_PIN, OUTPUT);  
  pinMode(TEMP_PIN, INPUT);
}

void loop(){
  adc = analogRead(TEMP_PIN);
  int temp = ReadThermistor(adc);
  Serial.println(temp);
  
  red = map(temp, 20, 40, 0, 255);
  blue = 255 - red;
  
  setLED(blue, red);
}
Cheers.
Shaun

jremington
Support Volunteer
Posts: 2275
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: I want to now use small Thermistor for my code, Im LOST!

Post by jremington » Thu Nov 09, 2017 2:57 pm

Everytime I compile I am getting GetTemp4 was not declared in this scope Line 65
float temperature = constrain(getTemp4(), MIN_TEMP, MAX_TEMP);
This is because you have not defined a function named getTemp4, which evidently reads the temperature from some sensor.

The only way to succeed in this and future projects is to go through a program line by line and make sure you understand (and approve of) what each line is doing.

Get rid of all the other lines. They just confuse you and us.

toxsickcity
Posts: 3
Joined: Thu Nov 09, 2017 6:34 am

Re: I want to now use small Thermistor for my code, Im LOST!

Post by toxsickcity » Thu Nov 09, 2017 4:33 pm

I dont know much at all im afraid.. I dont do programming at all!

you mention that I need to define it,
to be honest I dont know how the rgb lighting code down below gets its data..

I left in the code for the onewire to help you if you need to see why and what it did and hope we can solve it by removing it safely and using the important part of it.

I hope someone can guide me where and what i need to fix my code

jremington
Support Volunteer
Posts: 2275
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: I want to now use small Thermistor for my code, Im LOST!

Post by jremington » Thu Nov 09, 2017 5:27 pm

There must be thousands of tutorials on line to read a thermistor. Google "arduino thermistor" and start with a simpler example.

The real problem is that you want to run before you can crawl. Experiment with the basic examples that come with the Arduino IDE (software development package) and learn how to blink an LED, read the analog input, etc. before trying this project.

toxsickcity
Posts: 3
Joined: Thu Nov 09, 2017 6:34 am

Re: I want to now use small Thermistor for my code, Im LOST!

Post by toxsickcity » Thu Nov 09, 2017 8:43 pm

I reset my usable code to use onewire.

I just learnt with experimenting with my code that the onewire sensor is tied to the colour hue code at the bottom.

The sensor 1 that I blanked out has a section

Code: Select all

for (int i = 0; i < 9; i++) { // we need 9 bytes
//  data[i] = ds.read();
The int i is tied to the code at the bottom

Code: Select all

long HSV_to_RGB( float h, float s, float v ) {
  /* modified from Alvy Ray Smith's site: http://www.alvyray.com/Papers/hsv2rgb.htm */
  // H is given on [0, 6]. S and V are given on [0, 1].
  // RGB is returned as a 24-bit long #rrggbb
  int i;
  float m, n, f;

  // not very elegant way of dealing with out of range: return black
  if ((s<0.0) || (s>1.0) || (v<1.0) || (v>1.0)) {
    return 0L;
  }

  if ((h < 0.0) || (h > 6.0)) {
    return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;
  }
  i = floor(h);
  f = h - i;
  if ( !(i&1) ) {
    f = 1 - f; // if i is even
  }
  m = v * (1 - s);
  n = v * (1 - s * f);
  switch (i) {
  case 6:
  case 0: 
    return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
  case 1: 
    return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
  case 2: 
    return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
  case 3: 
    return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
  case 4: 
    return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
  case 5: 
    return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);
  }
I tried changing the h, s, v, m, n, i to h1, s1 and so on
The code would not compile until I made i to i1 in the sensor section

So looks like I'm up shits Creek..

I'm desperately trying to complete this project.. it goes in a massive wall art styled computer and it handles the effects..
I'm no developer by any means and learning this stuff is way beyond me.
I know little bits.
So please help.

Post Reply