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!!!