Go Down

Topic: int question (Read 1 time) previous topic - next topic



Still trying to get the hang of programing so here is my noobie question.

Can I define a list of int's followed by a list of int's that are made of various combinations of the first set? A mouthful I know .

here is a simple example for dice, that does not work, of what i am trying to do:
Code: [Select]
int A = 3;
int B = 4;
int C = 5;
int D = 6;

int roll1 = A;
int roll2 = B;
int roll3 = A & B;
int roll4 = B & C;
int roll5 = A & B & C;
int roll6 = B & C & D;

void setup() {
 pinMode(A, OUTPUT);
 pinMode(B, OUTPUT);
 pinMode(C, OUTPUT);
 pinMode(D, OUTPUT);

void loop() {
 digitalWrite(roll1, HIGH);
 digitalWrite(roll1, LOW);
 digitalWrite(roll6, HIGH);
 digitalWrite(roll6, LOW);

It verifies just fine but does not do what i want it to.  I tried a couple of different variations however none worked.... obviously. ;)



May 30, 2008, 10:48 pm Last Edit: May 30, 2008, 10:49 pm by tehboii Reason: 1
You can't do it this way. If you want to write a function changing the status of multiple pins, you can use a bitmask. The & operator is the reference pointer operator.

A bitmask can be, for instance, a byte, of which every bit refers to a different pin, starting from a defined one. If the bit is at 1, it will, ie, sets the pin to HIGH, or to LOW if it is at 0.

Examples :

255d = 11111111b
0d = 00000000b
145d = 10010001
(Here, d stands for decimal, b for binary)

You'll need to understand how you can convert a decimal number to a binary one, or use bitwise operators - They are easy.

Sorry I don't have time to write a code sample, and my answer is very quick. Just look at bitwise function in the extended references and try. But remember you'll HAVE to write a function to do this.


Are you wanting to do bit AND (&) or just add A and B (+)


I understand he is trying to do some kind of bitmask operations to change multiple pins with a single digitalWrite, which is impossible. If this is not what you were trying to do, please just ignore my answer.


I was looking to change the status of multiple pins with a single digitalWrite.  I'll read up on bitmask operations and adjust my code accordingly.

Thanks for the push in the right direction.  


digitalWrite cannot affect multiple pins, but the mega168 allows for this by having its I/O pins on ports.  To set multiple pins at once, you first need to convert the Arduino pin enumeration into port pins (pins 0 - 7 are bits 0 - 7 of port D, pins 8 - 13 are bits 0 - 5 of port B, and analog inputs 0 - 5 are bits 0 - 5 of port C).  You can then do bitmask operations on the port registers, which are each single bytes.  For example, to drive Arduino pins 1, 2, and 5 high, I would do:

DDRD |= (1 << 1) | (1 << 2) | (1 << 5);  // set pins 1, 2, and 5 as digital outputs
PORTD |= (1 << 1) | (1 << 2) | (1 << 5);  // drive pins 1, 2, and 5 HIGH

PORTD &= ~(1 << 1) & ~(1 << 2) & ~(1 << 5);  // drive pins 1, 2, and 5 LOW

- Ben


Yes the BIT AND operator is a confusion item for you and I understand.  I think raw port programming is not for the faint of heart either.  I just threw together a dice rolling program tonight and I think this little function call will make it easy for you.  I plan on posting some software and hardware layouts at a latter time but for now here is my showDigit function call with variables changed to match your sample code, just do away with digit1 ... digit6 and add code to call showDigit(5) or showDigit(random(1,7)).

void showDigit(int digit) {
   digitalWrite(A, (digit & 1   ? HIGH : LOW));  // turn LED ON/OFF
   digitalWrite(B, (digit >= 2 ? HIGH : LOW));  // turn LED ON/OFF
   digitalWrite(C, (digit >= 4 ? HIGH : LOW));  // turn LED ON/OFF
   digitalWrite(D, (digit == 6 ? HIGH : LOW));  // turn LED ON/OFF

Freeduino SB


I think raw port programming is not for the faint of heart either.

In general, I strongly encourage people who are moderately experienced at AVR programming to understand what's going on internally as their code executes.  Hiding away the complexities might make AVR programming more approachable to the beginner, but in the end I think it can become a hinderance to those who want to start accomplishing increasingly complicated tasks or who start focusing on achieving high levels of performance.  It also makes you much more selfreliant should you need to do something that doesn't already have a library devoted to it.

- Ben

Go Up