Hello everyone,
this is my first post here on the arduino forums. I just got my first arduino a week ago and i have almost no programming knowledge so bear with me.
i am aware that there is a forum for multiplexing and multiple leds, but my question is not so much about that, so thats why i have it here.
i know you are going to laugh when you see my code, but basically i have my cube set up like this:
i built a 3x3 cube and i have it so that each layer, or you could say plane, has a ground that is separate from the other layers. then, i have all of the anodes are connected to in collums. so basically i have 3 negative contacts and 9 positive contacts. since there is only one ground on the arduino, i used three transistors each connected to a different pin, to switch the ground between layers. i have the 9 columns connected directly to the 9 remaining pins on the arduino.
here is a diagram of my setup
and here is a picture
based on this setup, here is how my code turned out in order to light up the bottom layer of leds
digitalWrite(lev1,HIGH); //lev1,lev2, and lev3 refer to the pins that are connected to the transistors
digitalWrite(lev2,LOW); // to provide a ground to each of the layers
digitalWrite(lev3,LOW);
digitalWrite(led1,HIGH); //led1 to led9 are the pins that connect to each column
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(led5,HIGH);
digitalWrite(led6,HIGH);
digitalWrite(led7,HIGH);
digitalWrite(led8,HIGH);
digitalWrite(led9,HIGH);
with this code you can figure that the the entire bottom plane of leds would light up.
but, lets say i wanted one led in the bottom level to light up and an led on the second level to light up that is in a different column. well, with my setup i would have to turn on the ground to both level on and two and if i turn on any of the led pins, all of the leds in that column would light up.
so, here is what i did
digitalWrite(lev1,HIGH);
digitalWrite(lev2,LOW);
digitalWrite(lev3,LOW);
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
delay(5);
digitalWrite(lev1,LOW);
digitalWrite(lev2,HIGH);
digitalWrite(lev3,LOW);
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,LOW);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
delay(5);
the leds light up individually with out all of the leds in the column lighting up. and with a delay that short you dont notice it flash back and forth
this is were my problems lie:
i have each pattern defined as a function, for example sequence1() is a pattern sequence2() is a different pattern.
i want my arduino to cycle through the patterns (after a set time, not by user input) and i need it to repeat the pattern/function until it goes to the next one.
here is what i have tried
unsigned long time=millis();
unsigned long lasttime=0;
unsigned long x; //the difference between time and lasttime will be stored in x
int seq; // this will tell the loop which function/sequence to run
void setup()
{
seq=1; //i want to start with the first sequence
lasttime=0 //last time will be 0
//here is where i have all of my patterns
void sequence4()
{
digitalWrite(lev1,HIGH); // this is an example of one pattern
digitalWrite(lev2,HIGH);
digitalWrite(lev3,HIGH);
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,HIGH);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,HIGH);
delay(val);
digitalWrite(lev1,HIGH);
digitalWrite(lev2,HIGH);
digitalWrite(lev3,HIGH);
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
digitalWrite(led5,HIGH);
digitalWrite(led6,LOW);
digitalWrite(led7,LOW);
digitalWrite(led8,HIGH);
digitalWrite(led9,LOW);
delay(val);
digitalWrite(lev1,HIGH);
digitalWrite(lev2,HIGH);
digitalWrite(lev3,HIGH);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,HIGH);
digitalWrite(led4,LOW);
digitalWrite(led5,HIGH);
digitalWrite(led6,LOW);
digitalWrite(led7,HIGH);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
delay(val);
digitalWrite(lev1,HIGH);
digitalWrite(lev2,HIGH);
digitalWrite(lev3,HIGH);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,HIGH);
digitalWrite(led5,HIGH);
digitalWrite(led6,HIGH);
digitalWrite(led7,LOW);
digitalWrite(led8,LOW);
digitalWrite(led9,LOW);
delay(val);
}
}
void loop() // i think this is where my problem is
{
x=time-lasttime;
if(x==3000)
{
seq=seq+1; //after 3 seconds, increase seq by 1, and make lasttime equal to the current time
lasttime=time;
}
if (seq==1)
{
sequence1();
}
if (seq=3)
{
sequence3();
}
}
what am i doing wrong?