Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Arduino bcd problem.

Status
Not open for further replies.

duffman

Member
Hi, I've been on here for years, but this is the first time posting. I am getting into arduino and I am running into a silly problem which has blindsided me. I am trying to make a BCD display to show some integer displayed in a variable. In this case it's TempVar. For some reason, the serial display seems to show that TempVar just shows 9 continually. No other number. It should show 2. What is going on here?

int BCD1 = 2;
int BCD2 = 3;
int BCD3 = 4;
int BCD4 = 5;
int TempVar=2;


// the setup routine runs once when you press reset:
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(BCD1, OUTPUT);
pinMode(BCD2, OUTPUT);
pinMode(BCD3, OUTPUT);
pinMode(BCD4, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()
{


delay(1); // delay in between reads for stability

if (TempVar = 0)
{
digitalWrite(BCD1, LOW);
digitalWrite(BCD2, LOW);
digitalWrite(BCD3, LOW);
digitalWrite(BCD4, LOW);
}


if (TempVar = 1)
{
digitalWrite(BCD1, LOW);
digitalWrite(BCD2, LOW);
digitalWrite(BCD3, LOW);
digitalWrite(BCD4, HIGH);
}

if (TempVar = 2)
{
digitalWrite(BCD1, LOW);
digitalWrite(BCD2, LOW);
digitalWrite(BCD3, HIGH);
digitalWrite(BCD4, LOW);
}

if (TempVar = 3)
{
digitalWrite(BCD1, LOW);
digitalWrite(BCD2, LOW);
digitalWrite(BCD3, HIGH);
digitalWrite(BCD4, HIGH);
}

if (TempVar = 4)
{
digitalWrite(BCD1, LOW);
digitalWrite(BCD2, LOW);
digitalWrite(BCD3, HIGH);
digitalWrite(BCD4, HIGH);
}

if (TempVar = 5)
{
digitalWrite(BCD1, LOW);
digitalWrite(BCD2, HIGH);
digitalWrite(BCD3, LOW);
digitalWrite(BCD4, LOW);
}

if (TempVar = 6)
{
digitalWrite(BCD1, LOW);
digitalWrite(BCD2, HIGH);
digitalWrite(BCD3, LOW);
digitalWrite(BCD4, HIGH);
}

if (TempVar = 7)
{
digitalWrite(BCD1, LOW);
digitalWrite(BCD2, HIGH);
digitalWrite(BCD3, HIGH);
digitalWrite(BCD4, LOW);
}

if (TempVar = 8)
{
digitalWrite(BCD1, LOW);
digitalWrite(BCD2, HIGH);
digitalWrite(BCD3, HIGH);
digitalWrite(BCD4, HIGH);
}

if (TempVar = 9)
{
digitalWrite(BCD1, HIGH);
digitalWrite(BCD2, LOW);
digitalWrite(BCD3, LOW);
digitalWrite(BCD4, LOW);
}

Serial.println(TempVar) ;



}
 

misterT

Well-Known Member
Most Helpful Member
One equal sign is an assignment. Two equal signs is a comparison.
In C-language all values other than zero are true. Assignments are evaluated first, then the result is compared against zero. So, in your code all the ifs are evaluated as "true" (except the first one).
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top