Help with simple logic problem

Hi all.

Rigged up an ldr to my arduino for light intensity so that when the analogpin reads in the value, it is displayed on an lcd.

All works fine, my simple problem is, the value displayed is obviously between 0 and 1024, my logic seems to have broken just now as i can't figure out how i would set it so that it's as a percentage between 0 and 100%.

Can anyone assist me?

I thought about taking the adc number and dividing by 100 but it didn't work the way i thought it would.

Please see Attached code:

//Arduino lcd experiments "Multi sensory device"
//Ross Gillie 23/8/09

//Version history

/*Version 0.1:
Backlight comes on
Startup ok message displayed
*/

/*version 0.2:
Reads value from ldr and prints to lcd.
Sampling issues to fix as numbers flicker like crazy
*/

/*version 0.21:
fixed sampling issue by adding in delay after analog read and before printing to lcd,doh.
*/


#include <LiquidCrystal.h>        //Include liquid crsytal library to control lcd

LiquidCrystal lcd(12,11,5,4,3,2);  //12-RS,11-ENABLE,5-D4,4-D5,3-D6,2-D7
int backlight = 9;                //Backlight controlled by pin 9
int backlightbutton = 8;          //backlight controlled by switch on pin 8
int mode = 7;                     //button for selecting mode of sensing
int ldr = 2;                      //ldr sensor connected to analog pin 2
int ldrVal = 0;                  //value to store ldr value

int LI;              //mode 1



void setup()              //setup stuff before main program
{
  //set rows of display
  lcd.begin(16,2);
  pinMode(backlight, OUTPUT);  //set backlight, pin 9 as output
  digitalWrite(backlight, HIGH);    //turn on backlight
  //Print startup msg
  lcd.print("Startup...OK");
  delay(4000);    //delay startup msg for 4 seconds before running main loop
  lcd.clear();      //clear lcd
  lcd.home();        //sets cursor back to home position
}

// Main program stuff below

void loop()
{
  //mode select system will go in here before anything
  //make each mode a function that will be selected by mode button on pin 7
  //data read out on lcd
  /*Mode 1: Light intensity
    Mode 2: EMF detector
    Mode 3: Capacitance touch sensor
    Mode 4: ?
    Mode 5: ?
    Mode 6: ?
  */
 
  LightIntensity();
}

//Functions go here

void LightIntensity()    //Mode 1: light intensity, LDR on analog0, read and delivers light intensity reading as %
{
                      
  lcd.print("Light Int:");       //Light intensity message
  ldrVal = analogRead (ldr);    //read value from ldr
  delay(3000);                  //attempt to sample every 2 seconds
  lcd.setCursor(11,0);          //set cursor to display value after text
  lcd.print(ldrVal);            //print value to lcd
}

Only begun code so nowhere nearfinished, just tryna get things done step by step.

Cunningly, someone's already done it for you:

between 0 and 1024

sp. "1023"

I thought about taking the adc number and dividing by 100 but it didn't work the way i thought it would.

1023 / 100 = 10 (integer division)

Take a look at this function and map your 0-1023 to 0-100

http://arduino.cc/en/Reference/Map

Psst, RoyK, read reply #1 :wink:

Ah, many thanks lads.

:slight_smile: