ADC to 8Bit Pins

Hi, i'm new in programming with arduino and i'm having trouble with my proyect, i need to read an analog imput and get an output un binary, but in 8 different pins, meaning that if a have 5 volts on the analog input i should have all 8 pins HIGH ando if i have 0v all mus be LOW, i've been tryin several methods to turn the decimal output of analogRead() into binary and separate it into the outputs with no positive results. What should i do?

What should i do?

You should explain what the "several methods to turn the decimal output of analogRead() into binary and separate it into the outputs" were, and what the code looked like.

So far, all that you have described is that a value of 0 should turn all 8 pins off, and a value of 1023 should turn all 8 pins on. Now, you need to describe what the other 1022 values should do.

If, for instance, you want to turn one pin on when the analogRead() value is between 0 and 127, and two pins on when the value is between 128 and 254, etc., that is trivial. A series of if/else if/else statements will accomplish that.

Hi,

See the MAP function... the examples shows mapping 0..1024 (10 bits) to 0..255 (8 bits)

See the MAP function... the examples shows mapping 0..1024 (10 bits) to 0..255 (8 bits)

To divide by 4? Don't you think that's overkill?

Wouldn't the easiest thing be to just bitshift the result two bits to the right?

kkshi1123:
Hi, i'm new in programming with arduino and i'm having trouble with my proyect, i need to read an analog imput and get an output un binary, but in 8 different pins, meaning that if a have 5 volts on the analog input i should have all 8 pins HIGH ando if i have 0v all mus be LOW, i've been tryin several methods to turn the decimal output of analogRead() into binary and separate it into the outputs with no positive results. What should i do?

const int scale=128, offset=64;
int n = analogRead(A0)-offset; // Or whatever your pin is
digitalWrite(1, n>(0scale));
digitalWrite(2, n>(1
scale));
digitalWrite(3, n>(2scale));
...etc
digitalWrite(8, n>(7
scale));

(assuming your LEDS are on pins 1 to 8)

Hi, I think the purpose is to help the OP accomplish what he wants to do.

We may think in binary, and see shift as the obvious solution, but newbies don't...

terryking228:
Hi, I think the purpose is to help the OP accomplish what he wants to do.

We may think in binary, and see shift as the obvious solution, but newbies don't...

I'm also pretty sure that bit shifting won't solve the problem. He doesn't want an eight bit number, he wants to light up his LEDs in sequence.

he wants to light up his LEDs in sequence.

And yet I read that he wants a binary output, PaulS suggested a bar is some option.

kkshi1123, what exactly is the required format of the LEDs?


Rob

I suppose we do need to know more info from the OP to understand what he's actually aiming for.

I was thinking something along these lines would toggle 8 pins according to the value of an analog read:

int value=analogRead(0); // read the port
value = value >> 2; // go from 10 bits to 8 bits
PORTD = lowByte(value); // set digital pins 0 to 7 accordingly

Of course that makes a great deal of assumptions as to what is actually required.

Wouldn't the easiest thing be to just bitshift the result two bits to the right?

I'd like to think that the compiler's strength-reduction optimization will replace a divide by a constant four with a shift right by two. Easier to read.

AWOL:
I'd like to think that the compiler's strength-reduction optimization will replace a divide by a constant four with a shift right by two. Easier to read.

I'm sure it will.

About the 8 bits i'm going to bitshift to the left and take the 8 more significant digits. i'll be using pins 2 to 9 and again it would be if the input voltage is 2.5 volts, tha output should be 00001111, 5v 11111111, 0v 00000000, and so on, so what i was trying to do is to get the decimal number form analogRead and turn it to binary like 750=1011101110 and just get 10111011 on the pins leaving the last 1 and 0 out, i'm sorry if i can't explain myself better, english is not my first language

Ok, so it seems that you just need a binary output, which by good fortune because that is exactly what you are getting from your analog input.

get the decimal number form analogRead and turn it to binary

The analogRead() function does not return "750" as such, it returns 001011101110, ALL data on a processor is binary, we may choose to think of it in HEX, DEC or whatever but it's all binary.

So take your value, >> 2 or / 4, and write it to a port.

Now if you are using a small Arduino there is not a contiguous 8 pins in a single port so probably a series of digitalWrite()s will be required.


Rob

kkshi1123:
...if the input voltage is 2.5 volts, tha output should be 00001111, 5v 11111111, 0v 00000000...

That is a bargraph.
(IF 5v = 1111 1111, 2.5V = 1000 0000, 0V = 0000 0000 THEN that'd be "binary")


bargraph
9 output states:
00000000
00000001
00000011
00000111
00001111
00011111
00111111
01111111
11111111

That is a bargraph.

Oops you're right RP, I'd only had one cup of coffee :slight_smile:


Rob

The analogRead() function does not return "750" as such, it returns 001011101110, ALL data on a processor is binary, we may choose to think of it in HEX, DEC or whatever but it's all binary.

Well, in the reference page says that analogRead returns an integer between 0 and 1023, and thats the source of my problem, didn't know it was binary to begin with, so ti would be easier to use in several digitalWrite for the outputs, i'll try it.

That is a bargraph.
(IF 5v = 1111 1111, 2.5V = 1000 0000, 0V = 0000 0000 THEN that'd be "binary")

I'm sorry, i was in a hurry and wrote it wrong, it's not a map 2.5v would be 10000000 not 00001111, so it's binary.