Go Down

### Topic: ADC to 8Bit Pins (Read 4782 times)previous topic - next topic

#### kkshi1123

##### Oct 01, 2012, 11:57 am
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?

#### PaulS

#1
##### Oct 01, 2012, 12:04 pm
Quote
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.

#### terryking228

#2
##### Oct 01, 2012, 02:03 pm
Hi,

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

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

#### PaulS

#3
##### Oct 01, 2012, 02:07 pm
Quote
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?

#### Steph

#4
##### Oct 01, 2012, 02:14 pm
Wouldn't the easiest thing be to just bitshift the result two bits to the right?

#### fungus

#5
##### Oct 01, 2012, 02:31 pm

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;
digitalWrite(1, n>(0*scale));
digitalWrite(2, n>(1*scale));
digitalWrite(3, n>(2*scale));
...etc
digitalWrite(8, n>(7*scale));

(assuming your LEDS are on pins 1 to
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

#### terryking228

#6
##### Oct 01, 2012, 02:32 pm
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...
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

#### fungus

#7
##### Oct 01, 2012, 03:34 pm

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.

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

#8
##### Oct 01, 2012, 05:40 pm
Quote
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
Rob Gray aka the GRAYnomad www.robgray.com

#### Steph

#9
##### Oct 01, 2012, 06:56 pm
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:
Code: [Select]
`int value=analogRead(0); // read the portvalue = value >> 2; // go from 10 bits to 8 bitsPORTD = lowByte(value); // set digital pins 0 to 7 accordingly`

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

#### AWOL

#10
##### Oct 01, 2012, 09:27 pm
Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### fungus

#11
##### Oct 01, 2012, 09:40 pm

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.

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

#### kkshi1123

#12
##### Oct 01, 2012, 10:29 pm
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

#13
##### Oct 02, 2012, 01:56 am
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.

Quote
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
Rob Gray aka the GRAYnomad www.robgray.com

#### Runaway Pancake

#14
##### Oct 02, 2012, 03:46 amLast Edit: Oct 02, 2012, 03:49 am by Runaway Pancake Reason: 1

...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")

* * * * * *
Code: [Select]
`bargraph9 output states:000000000000000100000011000001110000111100011111001111110111111111111111`
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

Go Up

Please enter a valid email to subscribe