Go Down

Topic: Easy way to dim parallel LCD backlight with a photoresitor (Read 3537 times) previous topic - next topic

dtokez

Thanks DC, I don't understand exactly how the function is working but I understand the principles.

Also a bit confused as to what you mean by treating the open & short circuit cases separately?
I think maybe I need to add a case somehow for sensor reading = 0 and sensor reading = 1024?

Could I use an else - if function.. i.e. if reading is 0 = Lcd displays - sensor disconnected, else if reading is 1024 = Lcd displays - short circuit?

Thanks

Dan

dc42

Yes, that's what I had in mind, except that the max value you get from analogRead is 1023 not 1024. I was wrong when I mentioned sensor reading 1024 in my earlier post.

Rather than testing for == 0 and == 1023 you might want to test for <= 1 and >= 1022 or similar, because the ADC doesn't always produce exactly the reading you expect, especially if there is noise on the reference voltage.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

dtokez

Thanks again DC!

I am just testing it out at the moment, I have a probe outside and I think its just below 0 right now according to another therm, but the arduino is reporting a solid 0?

How would I set it to display a decimal point ie 0.6c?

Here is what I have so far :)

Code: [Select]
/*


The circuit:
* LDR connected to analog pin 0.
* LED / transistor connected from digital pin 3 to ground

* LCD RS pin to digital pin 7
* LCD EN pin to digital pin 8
* LCD D4 pin to digital pin 9
* LCD D5 pin to digital pin 10
* LCD D6 pin to digital pin 11
* LCD D7 pin to digital pin 12
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* LCD LED+ (15) pin transistor
* LCD LED- (16) to ground

* Thermisor divider 1 output to analog pin 1
* Thermisor divider 2 output to analog pin 2


*/

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the lDR is attached to
const int analogOutPin = 3; // Analog output pin that the LED is attached to

const int minLight = 100;        // at or below this light level, use minimum backlight intensity
const int maxLight = 900;       // at or above this light level, use maximum backlight intensity
const int minBacklight = 5;      // lowest backlight intensity to use
const int maxBacklight = 255;  // highest backlight intensity to use

// include the library code:
#include <LiquidCrystal.h>

#include <math.h>

double Thermister(int RawADC) {
  double Temp;
  Temp = log(((10240000/RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  Temp = Temp - 273.15;            // Convert Kelvin to Celcius

  return Temp;
}


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


int sensorValue = 0;        // value read from the LDR
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  lcd.begin(16, 2);
  delay(100);
  pinMode(13, OUTPUT); 

}

void loop() {
  // read the analog in value:
  analogRead(analogInPin);
  delay(100);
  sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  outputValue = map(constrain(sensorValue, minLight, maxLight), minLight, maxLight, minBacklight, maxBacklight );
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue); 


  // Temperature 1 print
  lcd.clear();

  lcd.setCursor(0,0);
  lcd.print ("Out: ");
  lcd.print(int(Thermister(analogRead(1))));


  lcd.print((char)223);
  lcd.print ("c");


  Serial.print ("Temp1=");
  Serial.println(int(Thermister(analogRead(1))));  // display temp

  // Temperature 2 print
  //lcd.setCursor(0,1);
  //lcd.print(int(Thermister(analogRead(2))));


  //lcd.print((char)223);
  //lcd.print ("c");

  //Serial.print ("Temp2=");
  //Serial.println(int(Thermister(analogRead(2))));  // display temp


  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(1000);                     
}











dtokez

#33
Feb 11, 2012, 02:03 am Last Edit: Feb 11, 2012, 03:04 am by dtokez Reason: 1
Been trying to get a decimal place by using fractions for a few hours, cant get it to compile though :(

Code: [Select]
/*


The circuit:
* LDR connected to analog pin 0.
* LED / transistor connected from digital pin 3 to ground

* LCD RS pin to digital pin 7
* LCD EN pin to digital pin 8
* LCD D4 pin to digital pin 9
* LCD D5 pin to digital pin 10
* LCD D6 pin to digital pin 11
* LCD D7 pin to digital pin 12
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* LCD LED+ (15) pin transistor
* LCD LED- (16) to ground

* Thermisor divider 1 output to analog pin 1
* Thermisor divider 2 output to analog pin 2


*/

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the lDR is attached to
const int analogOutPin = 3; // Analog output pin that the LED is attached to

const int minLight = 100;        // at or below this light level, use minimum backlight intensity
const int maxLight = 900;       // at or above this light level, use maximum backlight intensity
const int minBacklight = 5;      // lowest backlight intensity to use
const int maxBacklight = 255;  // highest backlight intensity to use

unsigned int frac;


// include the library code:
#include <LiquidCrystal.h>

#include <math.h>

double Thermister(int RawADC) {
 double Temp;
 Temp = log(((10240000/RawADC) - 10000));
 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
 Temp = Temp - 273.15;            // Convert Kelvin to Celcius

 return Temp;
}


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


int sensorValue = 0;        // value read from the LDR
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
 lcd.begin(16, 2);
 delay(100);
 pinMode(13, OUTPUT);  

}

void loop() {
 // read the analog in value:
 analogRead(analogInPin);
 delay(100);
 sensorValue = analogRead(analogInPin);            
 // map it to the range of the analog out:
 outputValue = map(constrain(sensorValue, minLight, maxLight), minLight, maxLight, minBacklight, maxBacklight );
 // change the analog out value:
 analogWrite(analogOutPin, outputValue);          

 // print the results to the serial monitor:
 Serial.print("sensor = " );                      
 Serial.print(sensorValue);      
 Serial.print("\t output = ");      
 Serial.println(outputValue);  


 // Temperature 1 print
 lcd.clear();

 lcd.setCursor(0,0);
 lcd.print ("Out: ");
 lcd.print(int(Thermister(analogRead(1))));
 lcd.print (".");
 
 frac = (Thermister(analogRead(1)) - int (Thermister(analogRead(1)) * 10;
 lcd.print(frac, DEC);


 lcd.print((char)223);
 lcd.print ("c");


 Serial.print ("Temp1=");
 Serial.println(int(Thermister(analogRead(1))));  // display temp

 // Temperature 2 print
 //lcd.setCursor(0,1);
 //lcd.print(int(Thermister(analogRead(2))));


 //lcd.print((char)223);
 //lcd.print ("c");

 //Serial.print ("Temp2=");
 //Serial.println(int(Thermister(analogRead(2))));  // display temp


 // wait 10 milliseconds before the next loop
 // for the analog-to-digital converter to settle
 // after the last reading:
 delay(1000);                    
}

dc42

Replace the line "lcd.print(int(Thermister(analogRead(1))));" by "lcd.print(Thermister(analogRead(1)), 1);".
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

dtokez

Thanks DC!

lcd.print(Thermister(analogRead(1)), 1);

Is the , 1 instructing to display to 1 decimal place? Or is it still using the fraction bits?

dc42

Yes, when you pass a float or double to print() or println() then the second parameter is the number of decimal places you want printed. See http://arduino.cc/en/Serial/Print.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

dtokez

excellent, thank you for the info.

I'm now trying to take an average temperature reading to try and eliminate some of the noise.

I am trying to get this code to compile but can't work out the problem, I might have it completely wrong I'm not sure :(

Code: [Select]
/*


The circuit:
* LDR connected to analog pin 0.
* LED / transistor connected from digital pin 3 to ground

* LCD RS pin to digital pin 7
* LCD EN pin to digital pin 8
* LCD D4 pin to digital pin 9
* LCD D5 pin to digital pin 10
* LCD D6 pin to digital pin 11
* LCD D7 pin to digital pin 12
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* LCD LED+ (15) pin transistor
* LCD LED- (16) to ground

* Thermisor divider 1 output to analog pin 1
* Thermisor divider 2 output to analog pin 2


*/

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the lDR is attached to
const int analogOutPin = 3; // Analog output pin that the LED is attached to

const int minLight = 100;        // at or below this light level, use minimum backlight intensity
const int maxLight = 900;       // at or above this light level, use maximum backlight intensity
const int minBacklight = 5;      // lowest backlight intensity to use
const int maxBacklight = 255;  // highest backlight intensity to use

// include the library code:
#include <LiquidCrystal.h>
#undef float
#define NUMSAMPLES 5
int samples[NUMSAMPLES];
#include <math.h>

double average(int RawADC) {
  double Temp;
  Temp = log(((10240000/RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  Temp = Temp - 273.15;            // Convert Kelvin to Celcius

  return Temp;
}


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


int sensorValue = 0;        // value read from the LDR
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  lcd.begin(16, 2);
  delay(100);
  pinMode(13, OUTPUT); 

}

void loop() {
  // read the analog in value:
  analogRead(analogInPin);
  delay(200);
  sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  outputValue = map(constrain(sensorValue, minLight, maxLight), minLight, maxLight, minBacklight, maxBacklight );
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(outputValue); 

uint8_t i;
  float average;

  // take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES; i++) {
   samples[i] = analogRead(1)), 1);
   delay(10);
  }

  // average all the samples out
  average = 0;
  for (i=0; i< NUMSAMPLES; i++) {
     average += samples[i];
  }
  average /= NUMSAMPLES;











  //Temperature 1 print
  lcd.clear();

  lcd.setCursor(0,0);
  lcd.print ("Out: ");
  lcd.print(average(analogRead(1)), 1);


  lcd.print((char)223);
  lcd.print ("c");


  //Serial.print ("Temp1=");
  //Serial.println(int(Thermister(analogRead(1))));  // display temp

  // Temperature 2 print
  //lcd.setCursor(0,1);
  //lcd.print(int(Thermister(analogRead(2, 1))));


  //lcd.print((char)223);
  //lcd.print ("c");

  //Serial.print ("Temp2=");
  //Serial.println(int(Thermister(analogRead(2))));  // display temp


  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(1000);                     
}



Go Up