LED Cube + Potentiometer

Hello everybody,

I am quite a beginner and tried to make my own 4x4x4 LED Cube using an ArduinoNano.

The Cube and the program is running smoothly so I wanted to add a Potentiometer to easily
switch between some programms I want the LED Cube todo.

I got the Potentiometer working as well. But due to the fact I am using all 14 digital pins i had to use some analog pins as well. So pin 0 to 13 plus A4 and A5 are the collumns and A0 to A3 are the layers of the cube.

A7 I want to use as my potentiometer analog input but and here comes the problem:

As soon as I activate serial reading with

Serial.begin(9600);

the 2 collumns which I put on A4 and A5 are not working properly anymore. Here is some example code I am testing it with (it's part of a code I found on youtube.

First the code which works for the LED Cube only:

int layer[4]={A3,A2,A1,A0}; //initializing and declaring led layers
int column[16]={13,12,11,10,9,8,7,6,5,4,3,2,1,0,A5,A4}; //initializing and declaring led rows
int time = 250;
 
void setup()
{  
  for(int i = 0; i<16; i++)   
  {
    pinMode(column[i], OUTPUT);  //setting rows to ouput
  }
  
  for(int i = 0; i<4; i++)
  {
    pinMode(layer[i], OUTPUT);  //setting layers to output
  }
  
  randomSeed(analogRead(10));  //seeding random for random pattern
}

void loop()
{
  turnEverythingOff();
  flickerOn();
  turnEverythingOn();
  delay(time);
}

//turn all off
void turnEverythingOff()
 {
   for(int i = 0; i<16; i++)
   {
     digitalWrite(column[i], 1);
   }
   for(int i = 0; i<4; i++)
   {
     digitalWrite(layer[i], 0);
   }
 }
 
//turn all on
void turnEverythingOn()
{
  for(int i = 0; i<16; i++)
  {
    digitalWrite(column[i], 0);
  }
  //turning on layers
  for(int i = 0; i<4; i++)
  {
    digitalWrite(layer[i], 1);
  }
}

//flicker on
void flickerOn()
{
  int i = 150;
  while(i != 0)
  {
    turnEverythingOn();
    delay(i);
    turnEverythingOff();
    delay(i);
    i-= 5;
  }
}

And here is the code I tried to run with the potentiometer (not the whole code):

const int sensorPin = A7;
int oldVal;

int layer[4]={A3,A2,A1,A0}; //initializing and declaring led layers
int column[16]={13,12,11,10,9,8,7,6,5,4,3,2,1,0,A5,A4}; //initializing and declaring led rows
int time = 250;

void setup()
{
  Serial.begin(9600); //<--this line causes me troubles
  
  for(int i = 0; i<16; i++)   
  {
    pinMode(column[i], OUTPUT);  //setting rows to ouput
  }
  
  for(int i = 0; i<4; i++)
  {
    pinMode(layer[i], OUTPUT);  //setting layers to output
  }
  
  randomSeed(analogRead(10));  //seeding random for random pattern
}
void loop()
{
  //Serial.begin(9600);             //<-- I also tried to use serial.begin here and serial.end
                                    //in the loop just before i would like to control the LED
                                    //pins. but this also didnt work. 
  int sensorVal = analogRead(sensorPin);

  int mappedVal = map(sensorVal,0,1023,0,500);  
  
  if(mappedVal != oldVal){
    oldVal=mappedVal;
    Serial.print("Sensor Value: "); //just to see if the Potentiometer actually works.
    Serial.println(mappedVal);
    delay(200);
  }

  //Serial.end(); //<-- that was my second guess to use serial.begin and serial.end before 
                  //controlling the LED cube pins but this also doesnt work.

  if(mappedVal<=150){
    turnEverythingOff();
    flickerOn();
    turnEverythingOn();
    delay(time);
  }
}

I wrote some comments in the lines were I used or wanted to use serial.begin and serial.end
I assume it doesnt like that I want to read from one analog pin while using the other analog pins as digital IN-/OUTPUT.
I could not find anything in the forum cos I could not extract proper search terms. I had no idea what to look for.
Is it even possible todo it my way? What am I missing. I would be very glad if someone could help me.
Thank you!!!

Digital pins 0 and 1 are used by the Serial interface so trying to use them to drive LEDs at the same time isn't going to work.

Why digital pins? I only got problems with the analog pins A4 and A5 while I simultaniously try to use A7 as a analog input pin.

So it is really not possible to use 2 analog pins as an output while reading from a 3rd analog pin?

If you're convinced it's nothing to do with using the Serial pins for column addressing then take the Serial calls out of the combined code and see what it does.

But I'd also check the wiring. I'm not sure the columns are connected where you think they are.

Steve

So it is really not possible to use 2 analog pins as an output while reading from a 3rd analog pin?

It is possible to do what you describe with the analogue pins.

Thank you everybody!
I solved it. My misstake was indeed the wiring. I went through everything and and it wasn't the A4 and A5 which caused the problem but Pin 0 and 1 and also just because I wanted to see the analog values on my screen. After just using the A7 value internally only and not starting any serial communication pins 0 and 1 were working properly again and my simple programm switch potentiometer is now working as well. Thank you very much.