Mexico
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« on: October 01, 2012, 04:57:17 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?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 315
Posts: 35519
Seattle, WA USA
|
 |
« Reply #1 on: October 01, 2012, 05:04:11 am » |
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
Online
Edison Member
Karma: 11
Posts: 1365
... in The Woods In Vermont
|
 |
« Reply #2 on: October 01, 2012, 07:03:47 am » |
Hi,
See the MAP function... the examples shows mapping 0..1024 (10 bits) to 0..255 (8 bits)
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 315
Posts: 35519
Seattle, WA USA
|
 |
« Reply #3 on: October 01, 2012, 07:07:43 am » |
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
Jr. Member
Karma: 1
Posts: 81
Frequently Befuddled
|
 |
« Reply #4 on: October 01, 2012, 07:14:29 am » |
Wouldn't the easiest thing be to just bitshift the result two bits to the right?
|
|
|
|
|
Logged
|
|
|
|
|
Valencia, Spain
Offline
Edison Member
Karma: 65
Posts: 2262
|
 |
« Reply #5 on: October 01, 2012, 07:31:58 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?
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 
|
|
|
|
|
Logged
|
|
|
|
|
Topsham, Vermont USA
Online
Edison Member
Karma: 11
Posts: 1365
... in The Woods In Vermont
|
 |
« Reply #6 on: October 01, 2012, 07:32:30 am » |
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
|
|
|
|
|
Valencia, Spain
Offline
Edison Member
Karma: 65
Posts: 2262
|
 |
« Reply #7 on: October 01, 2012, 08:34:31 am » |
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
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6830
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #8 on: October 01, 2012, 10:40:48 am » |
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
|
|
|
|
|
Canada
Offline
Jr. Member
Karma: 1
Posts: 81
Frequently Befuddled
|
 |
« Reply #9 on: October 01, 2012, 11:56:46 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: October 01, 2012, 02:27:09 pm » |
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.
|
|
|
|
Valencia, Spain
Offline
Edison Member
Karma: 65
Posts: 2262
|
 |
« Reply #11 on: October 01, 2012, 02:40:45 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.
|
|
|
|
|
Logged
|
|
|
|
|
Mexico
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #12 on: October 01, 2012, 03:29:22 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
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6830
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #13 on: October 01, 2012, 06:56:49 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 23
Posts: 1381
Now, More Than Ever
|
 |
« Reply #14 on: October 01, 2012, 08:46:39 pm » |
...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
|
|
|
|
« Last Edit: October 01, 2012, 08:49:44 pm by Runaway Pancake »
|
Logged
|
Don't Be Upset By The Results You Didn't Get With The Work You Didn't Do
|
|
|
|
|