Serial.begin() is screwing up cd4511 decoder and 7 segment display.

If I comment out serial.begin(), the 7 segment display and the cd4511 decoder work perfectly but there is no output text in serial monitor, and if I don’t comment out serial.begin() in order to output the texts, the display will flash 8 and 9 at some weird interval. I build it all on tinkercad, i haven’t tried it on the real device yet. Do you think it’s a tinkercad problem?

Here is the link: Circuit design CD4511 project | Tinkercad

int bin[4];

int W_PIN, X_PIN; 
int Y_PIN, Z_PIN;

int w_value,x_value,y_value,z_value;
int counter;

void printOutput();

void setup() {
  pinMode(W_PIN=13, OUTPUT);
  pinMode(X_PIN=12, OUTPUT);
  pinMode(Y_PIN=11, OUTPUT);
  pinMode(Z_PIN=10, OUTPUT);
  
  for(int x = 0; x < 4; x++){
    pinMode(x, OUTPUT);
  }
 Serial.begin(9600);
}

void loop(){
  for (counter=0;counter<=9;counter++){
      int i=0;
      int n=counter;
      
      while(i<4){
       bin[i]=n%2;
        n/=2;
        i++;
      }
      
      for(int i = 0; i < 4; i++){
        int x=3-i;
        digitalWrite(i, bin[x]);
      }
    
      
      w_value=bin[3];
      x_value=bin[2];
      y_value=bin[1];
      z_value=bin[0];
      
      
      digitalWrite(W_PIN, bin[3]);
      digitalWrite(X_PIN, bin[2]);
      digitalWrite(Y_PIN, bin[1]);
      digitalWrite(Z_PIN, bin[0]);
      
      printOutput();
      delay(1000);
    }
}
void printOutput()
{
  String outputString = "Counter = ";
  outputString += counter;
  outputString += ", WXYZ = ";
  outputString += w_value;
  outputString += x_value;
  outputString += y_value;
  outputString += z_value;
  outputString += ", W = ";
  outputString += w_value;
  outputString += ", X = ";
  outputString += x_value;
  outputString += ", Y = ";
  outputString += y_value;
  outputString += ", Z = ";
  outputString += z_value;
  Serial.println(outputString);
  return;
}

verywhiteshark:
I build it all on tinkercad, i haven’t tried it on the real device yet. Do you think it’s a tinkercad problem?

Well done to post the code properly. You must have the unusual habit of reading instructions. :astonished:

You have not used pins 0 or 1 in your project so that is not the mistake.

You have however, used “String” variables which is very likely to cause the program to crash at some time.

And I would certainly be more interested if the problem actually showed on “the real device”. :grinning:

Paul__B:
You have not used pins 0 or 1 in your project so that is not the mistake.

Wait… pins 0 and 1 are connected to pins 6 and 2 of the 4511, and this code

  for(int x = 0; x < 4; x++){
    pinMode(x, OUTPUT);
  }

sets both to OUTPUT!

Erik_Baas: Wait... pins 0 and 1 are connected to pins 6 and 2 of the 4511,

Ah! Didn't look, just at the code. :roll_eyes:

Obvious foul-up to start with. :astonished:

Problem solved. :grinning:

Okay, I think I found the problem, it's because i use pin 0 and 1... so without using those 2 pins everything works well as I expect. Do you guys have any idea why it's causing that problem when you declare the Serial.begin?

Thanks for the reply guys.

Sure.

Pins 0 (RX) and 1 (TX) are the serial port.

Enabling the serial port will screw up things when something else is connected to those pins. So always avoid using those pins unless you know how to cope with it, and there's really no other pins left to use.