Go Down

Topic: binary output for an R/2R ladder. (Read 1 time) previous topic - next topic

modius13

So, first post. I'm not sure if something like this is posted around here somewhere so here goes.

I'm trying to take an analog input from a pot (0-255) and send that to an 8-bit R/2R ladder connected to the digital pins. I know the easiest way to get the 0-255 value is to use:

map(potin, 0, 1023, 0, 255)

My question is, is there an easier way to do the binary output on the digital pins than using a switch/case with 256 cases?

-modius13

Coding Badly

The simplest way would be to put all the eight lines of the ladder on a single I/O port (like port C).  You would then be able to output the value with a simple assignment...

 PORTC = MyAnalogValue;

There's a description here that will hopefully get you started...

http://www.arduino.cc/en/Reference/PortManipulation

Good luck,
Brian

modius13

ah, that is cool. So, something along the lines of:

switch (potin)
{
  case 1:
  PORTD = B00000000
  break;

  case 2:
  PORTD = B00000001
  break;

  ...

  case 256:
  PORTD = B11111111
}

would be the way to go? Just want to make sure. I'm new to Arduino but have a basic grasp of C++

-modius13

RuggedCircuits

No, you're making it too complicated! What Coding Badly was coding properly was much simpler:
Code: [Select]
val = map(potin, 0, 1023, 0, 255);
PORTC = val;

The end. That's it! Assuming, of course, all your pins are on PORTC and you've configured all pins to be outputs.

The bigger question is: why? It sounds like you're trying to "reflect" an analog voltage set by a potentiometer to go somewhere else. Why not just use an op-amp in a noninverting buffer configuration? One little chip...

modius13

Ah, that makes a lot more sense. Thank you RuggedCircuits. As to why: It's going to be a part of something bigger. My own take on an Arduino synthesizer. I'll be sure to post results as I go along. Just waiting for my Arduino to arrive in the mail to implement things.

-modius13

Go Up