# Control output pins using 8-bit number

I would like to know how to control eight output pins using an 8-bit binary number (ie. 00001100 would turn outputs 2 and 3 on). In actual fact, it would be even better if I could control them with a decimal number (0-255) - depending on the state of a potentiometer. But they must output a binary value: my project involves reading an analog sensor (eventually, it will be serial data coming from the computer (in decimal format)), and creating another analog voltage using an Arduino and a DAC0800.

I am going to connect it like it is attached to the ROM in the following diagram:
http://www.cgs.synth.net/modules/pic/schem_cgs02_wavetable.gif

Do you know what the output rate would be (ie. changes possible per second)? I am hoping for about 100 if possible. The more the better.

Thanks.

Easy way is to use bitRead.
More difficult is direct port manipulation.
100Hz should be easy.

when you use this syntax to send the data to the dac:
(assuming that the dac is on portd == pins 0 through 7) it works fast and simple:
setup(){

DDRD=0xFF; //set portd as output
PORTD = 0; //set dac to 0
}

loop{

int potmeter = 0;

potmeter = analogRead(0); //pick a analog port..
potmeter &= 0xFF;
PORTD = portmeter;
}

Good luck!

``````potmeter = analogRead(0); //pick an analog port..
potmeter &= 0xFF;
``````

maybe "potmeter /=4;" would be a little less erratic.

potmeter /=4; is what you want to use for another reason also. If you and with 0xFF, every time the pot goes over that value, it loops and starts counting on the pins again. Divide by 4 will give you 255 steps between 0v and 5v. However, you don't actually need the variable there:

will work just fine. You may not need the byte().

Sorry,
Guys tour write, dividing is better than chopping bits off

Hi, thanks for the replies. However, the only problem is that I would like the DAC to be on pins 2-9, so I have access to the Serial Tx/Rx lines (pins 0 and 1). Is this possible to do? Any ideas? Unfortunately, there are only six data lines on Port B.
Thanks.

Your simplest option is probably 'bitRead', or if you're feeling advanced, a bit of port manipulation for the bulk of the bits on a single port, and bitRead for the remainder.

If I understand the question correctly something along the lines of:

``````byte    pins[2, 3, 4, 5, 6, 7, 8, 9];
byte*   p = pins;

do
{
digitalWrite(*p++, (value & mask_bit) ? HIGH : LOW);
} while ( mask_bit <<= 1 );
``````

EDIT: Changed per below

Thanks I thought I'd corrected that before the post but apparently not.

lloyddean and Groove, could you please format your example below in a way a beginner like me can use it

I presume it's something like this:

``````void setup() {
Serial.begin(9600);
}

void loop() {

byte pins[2, 3, 4, 5, 6, 7, 8, 9]; //an array

do
{
digitalWrite(*p++, (value & mask_bit) ? HIGH : LOW);
}
while ( mask_bit <<= 1 );
}
``````

Unfortunately, the compiler doesn't like this. Could you help? I just want to read a pot, and output it's value to the DAC for now.

I would eventually like to control the DAC through MATLAB as seen at Legacy MATLAB and Simulink Support for Arduino: Slides and Examples - File Exchange - MATLAB Central

Thanks.

Your pins array declaration/initialisation is incorrect.
Look for some examples to see how it should be.
also I don't know what your doing withe the *p++, but you should be indexing the pins array, not trying to dereference a non-existent pointer.
Sorry, on train, posting via phone.

``````const  byte pins [8] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
Serial.begin(9600);
}

void loop() {
byte p = analogRead(0) / 4;
for (int i = 0, unsigned byte mask_bit = 1; i < 8; ++i, mask_bit<<=1) {  {