Mapping voltages?

Hi, I am building an interface for a laser power meter. The meter outputs 1mv pew mW (Unit). I am using the internal 1.1v reference.

Basically, as it is, if you feed the ADC 500mv, it returns 512, if you feed it 1.1V, it returns 1024.

How can I map the numbers so the returned value is the same as the voltage input? So if I fed the ADC 500mv, it would return (After calculation), 500, or if I fed it 1.1V, it would return 1100 (Again, after calculation?)

Cheers, Dan :)

These two should get you what you need...

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

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1257716581

http://www.arduino.cc/playground/Main/Fscale

Hi, I have figured it out :slight_smile:

int val = 0;
int low = 0;
int high = 0;
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
 LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
  analogReference(DEFAULT);
  // set up the LCD's number of rows and columns: 
  lcd.begin(20, 4);
  Serial.begin(9600);
}

void loop() {
  val = analogRead(0);
  Serial.println(val);
  if (val > 996){
    analogReference(DEFAULT);
    delay(100);
    high = analogRead(0);
    high = high*.0048828125*1000;
    lcd.print(high);
  }
  if (val < 996){
    analogReference(INTERNAL);
    delay(100);
    low = analogRead(0);
    low = low*.0010752688*1000;
    lcd.print(low);
  }
  delay(500);
  lcd.clear();
 }

It works, though for some reason, once it switches to the if (val > 996) part, it flashes between the right value, but for some reason, also the if (val < 996) value??

What sensor is attached to pin 0? You have very short cycles, so the analogRead function gets called fairly often. The value returned could easily fluctuate between 995 and 997.

How come nothing is supposed to happen is val == 996?

Attached to pin 0 is a bench power supply and a DMM for testing, but will eventually be connected to a laser power meter.

Basically, I want 2 modes, one for values less than 1.1V, and one for values between 1.1V to 5V.

Because 0-1.1V is such a small range, the resolution is greater. The 1.1V-5V scale has a lower resolution, but can handle higher powers.

I want the code to select a scale accordingly.

ie, any voltage below 1.1V with use the internal reference, and if it goes over 1.1V, it switches to the default 5V reference.

As for the changing values, even if I push the input voltage up to 5V it still jumps between them. I think maybe the analogReference functions are screwing it up somehow.

I could be wrong, but I thought that a call to analogReference was persistent.

You make a call to analogRead, and then, depending on the value read (relative to what?) you call analogReference, and read again. Then the loop ends, and begins again.

The first call to analogRead returns a value relative to DEFAULT on one iteration, then INTERNAL on the next. If the value input to the pin hasn't changed, the output will, because the "relative to" value has.