# Analog output value control

I am using a Sharp IR sensor which has an analog output. The outputs it gives are not the integer values that I want.

using analogRead and Serial.println I can see that it is reading a value of 270 +/- 3.

I want this to put a value of 0 into my little bits number counter when it is on the value setting. It gives me a value of 1.

This is my first small hurdle to get over. The next hurdle is to get the incremental analogRead values to give integer outputs ranging from 30 - 0.

I need to know if I can take a range of analogRead values to all go to one integer output. I need to know what lines of code I use to make this happen.

I am in desperate need of help on this, please assist.

If it is linear, look at the Map() function.

You want 270 to be 1, what will 0 and 30 be?

Griever1488: I want this to put a value of 0 into my little bits number counter when it is on the value setting. It gives me a value of 1.

Don't you think it would be a lot easier to help you out if we could see the code and could see what the hell you're talking about? You can't make analogRead return anything but the 10 bit number it already does. You can however do whatever sort of math on that number after you get it that you like. If you don't know the lines of code, can you describe the math you want to have happen in math terms instead of code?

Don't you think it would be a lot easier to help you out if we could see the code and could see what the hell you're talking about? You can't make analogRead return anything but the 10 bit number it already does. You can however do whatever sort of math on that number after you get it that you like. If you don't know the lines of code, can you describe the math you want to have happen in math terms instead of code?

LittleBits is a company that makes all sorts of little modules for basic (BASIC) electronic play. http://littlebits.cc/

Now forgive me for the rest of my first post. I am about as new to this as a caveman. I did some more toying with the coding and made some things work.

const int ledPin = 6; const int sensorPin = 0;

const long referenceMv = 50000;

void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(ledPin, OUTPUT); }

void loop() { // put your main code here, to run repeatedly: int val = analogRead(sensorPin); int mV = (val * referenceMv) / 1023; Serial.println(mV); delay(100); analogWrite(ledPin, val / 4);

My sensor is reading and giving me values which for the time being I will consider arbitrary. I am thinking I will need a capacitor in the circuit to smooth out the noise that I am getting since there is a quick spike every few seconds when the distance to the sensor hasn't changed. (the spike will jump the value I have printing up by 4-5 thousand)

So my biggest question is how do I make a conditional function based on a range of values? For example if I wanted it to say (pardon me for the layman terms)

If the sensor reads between 14000-14500, make the output on pin 6 read 0.

if (sensor > 14000 && sensor < 14500) DigitalWrite(6,LOW);

``````const long referenceMv = 50000;
``````
``````const long referenceMv = 5000;
``````

Okay so my next noob question is why won't the loop actually run a loop? When I upload it, it starts at 0. The sensor reads a distance and gives a value. The first value that it returns seems to be the only value it writes. It seems like once it latches onto a value it stops doing anything. The sensor continues to read values and print them, but it won't change the output. I'm looking through the book I have and I can't find a line of code to make it repeat constantly. I was under the understanding that if it was in the void loop that it would continue to query the sensor and then follow instructions based on the return from the sensor. Not sure what I am doing wrong here...

EDIT: To hopefully help clarify what I am doing here. I want to read the sensor and convert its values.

so if i have a range of 1400-1450 I want the output to be 10 if I have a range of 1450-1500 I want the output to be 11 and on and on going up value ranges of 50 and then the output goes up 1. I can set up all the value thresholds but it seems to just stick to the first value it finds and then stops trying to read or do anything.

So how about posting your current code? The only code I can see doesn't do any value conversions like you describe.

BTW what EXACTLY is connected to pin 6? I have a feeling the name "ledPin" is really misleading.

Steve

const int output = 6;
const int sensorPin = 0;

const long referenceMv = 5000;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(output, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
int mV = (val * referenceMv) / 1023;
Serial.println(mV);
analogWrite(output, val / 15);
if (val > 1400 && val < 1450) analogWrite(output, 10);
if (val > 1450 && val < 1500) analogWrite(output, 11);
delay(100);
}

I changed up the names of some pins to make it more generic. I had copied the ledPin off an example I had found.

Hi,

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

What model Arduino are you using. Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What do you expect to get out of pin6 with analogWrite(output, val / 15); ?

Thanks... Tom.. :)