Society of Robots - Robot Forum

Software => Software => Topic started by: blackbeard on October 24, 2010, 06:39:32 PM

Title: output binary to selected pins
Post by: blackbeard on October 24, 2010, 06:39:32 PM
ok so i have an led cube i'm trying to program. essentially i've got 2 8 bit dmux connected to 3 i/o on the arduino and the inputs connected to 2 other pins. my problem is that i don't always want to have this kind of thing in my code. i'd much rather just say a binary number to write to the pins that the mux is connected to

digitalWrite(muxS1, HIGH);
digitalWrite(muxS2, LOW);
digitalWrite(muxS3, LOW);

digitalWrite(muxS1, LOW);
digitalWrite(muxS2, HIGH);
digitalWrite(muxS3, LOW);


Title: Re: output binary to selected pins
Post by: madsci1016 on October 25, 2010, 07:44:21 AM
How about

for(int value = 0; value <=7; value++){

digitalWrite(muxS1, bitRead(value, 0));
digitalWrite(muxS2, bitRead(value, 1));
digitalWrite(muxS3, bitRead(value, 2));

}

That will set the three pins to the state described by the lower three bits in the value number. With the loop, it will cycle through all the outputs of your demux.
Title: Re: output binary to selected pins
Post by: blackbeard on October 25, 2010, 08:29:41 AM
How about

for(int value = 0; value <=7; value++){

digitalWrite(muxS1, bitRead(value, 0));
digitalWrite(muxS2, bitRead(value, 1));
digitalWrite(muxS3, bitRead(value, 2));

}

That will set the three pins to the state described by the lower three bits in the value number. With the loop, it will cycle through all the outputs of your demux.

thanks allot. that's allot better then the code i am using now which looks something like this it's random but that's just so i can test the whole cube.

  count = random(0,8);
  row = bin[count];
  mux1= row & 0x01;
  mux2 = (row>>1) & 0x01;
  mux3 = (row>>2) & 0x01;
  digitalWrite(7, mux1);
  digitalWrite(8, mux2);
 digitalWrite(9, mux3);