Pages: [1] 2   Go Down
Author Topic: ADC to 8Bit Pins  (Read 2704 times)
0 Members and 1 Guest are viewing this topic.
Mexico
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50899
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 33
Posts: 1940
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

Logged

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50899
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Canada
Offline Offline
Jr. Member
**
Karma: 1
Posts: 81
Frequently Befuddled
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 152
Posts: 5750
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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>(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 smiley-cool
Logged

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

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 33
Posts: 1940
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

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

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 152
Posts: 5750
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8620
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Canada
Offline Offline
Jr. Member
**
Karma: 1
Posts: 81
Frequently Befuddled
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 152
Posts: 5750
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

Mexico
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8620
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Faraday Member
**
Karma: 66
Posts: 2588
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...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:
bargraph
9 output states:
00000000
00000001
00000011
00000111
00001111
00011111
00111111
01111111
11111111
« Last Edit: October 01, 2012, 08:49:44 pm by Runaway Pancake » Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Pages: [1] 2   Go Up
Jump to: