Go Down

Topic: [Solved] Arduino ignoring the truth? (Read 416 times) previous topic - next topic

bradfordb

Jul 05, 2008, 12:37 am Last Edit: Jul 05, 2008, 07:24 am by bradfordb Reason: 1
Hey all,

Its my first posting so be nice  :P
I'm having a strange issue. I'm trying to get my arduino to recieve a signal from serial and convert it to binary and display that binary using led's. I have two methods that do most of the work.

Code: [Select]
boolean bit_check (int mask, int number)
{
 if ((mask & number) == mask)
 {
   return true;
 }
 else
 {
   //Serial.println("BIT CHECK RETURNING FALSE");
   return false;
 }
}


This logical ands the inputted number with the mask (which is always a power of two) and checks if the and is equal to the mask (meaning that bit was on in the number).

Next method is the one having the issue.

Code: [Select]
void convert_to_binary(int val)
{
 for (i = 0; i < 8; i++)
 {
   if (bit_check(mask[i], val)==true)
   {
     Serial.print("LED ");
     Serial.print(i);
     Serial.println(" is high");
     digitalWrite(ledpins[i], HIGH);
   }  
   else
   {
     Serial.print("LED ");
     Serial.print(i);
     Serial.println(" is high");
     digitalWrite(ledpins[i], LOW);
   }
 }
}


This should call bit_check for each of the 8 bits in the byte val. I only use int because Serial.read does too. Everytime I run this program I get all ledpins high. With the debug line in bit_check I can see that some bits are not on (ie it returns false). I can't figure out for the life of me why it's doing this....

Any help is appreciated.

bens

#1
Jul 05, 2008, 01:12 am Last Edit: Jul 05, 2008, 01:13 am by bens Reason: 1
Both your if clause and else clause in convert_to_binary() serially transmit "LED {x} is high"...

Is this the source of your problem?

If not, you should provide the values that are stored in your mask array.

- Ben

bradfordb

Quote
Both your if clause and else clause in convert_to_binary() serially transmit "LED {x} is high"...

Is this the source of your problem?
- Ben


Yep.  :-[ Thought I checked for all of those issues. Oh well. Thanks for the quick reply!

bens

No problem.  When you have a bug in your program, I suggest you start by looking for the obvious stuff, then work your way to the more complicated details.

- Ben

Go Up