I don't know what's wrong

I’m new with arduino and I wrote one of my first code. This code is meant to change the powered LEDs combinations (for example 1,2 on, 3,4 off) but it doesn’t work… Do you know what’s the problem?

The code:

int pin = 0;
// the Voids named with letters are actually combinations
// a for example lights on only the first LED
// n lights on first and last LED
//and so on
void a(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(1, HIGH);
}
void a2(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(2, HIGH);
}
void c(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(1, HIGH);
pinMode(2, HIGH);
}
void e(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(3, HIGH);
}
void h(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(1, HIGH);
pinMode(3, HIGH);
}
void i(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(2, HIGH);
pinMode(3, HIGH);
}
void l(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(1, HIGH);
pinMode(2, HIGH);
pinMode(3, HIGH);
}
void m(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(4, HIGH);
}
void n(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(1, HIGH);
pinMode(4, HIGH);
}
void o(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(2, HIGH);
pinMode(4, HIGH);
}
void r(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(1, HIGH);
pinMode(2, HIGH);
pinMode(4, HIGH);
}
void t(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(3, HIGH);
pinMode(4, HIGH);
}
void w(){
for (int i;i<5;i++) pinMode(i, LOW);
pinMode(1, HIGH);
pinMode(3, HIGH);
pinMode(4, HIGH);
}
void setup() {
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, INPUT); //here is the button for changing
Serial.begin(9600);
}

void loop() {
Serial.println(digitalRead(7));
if (digitalRead(7) == 1){
if (pin==0)
for (int i;i<10;i++){
pinMode(1, HIGH);
pinMode(2, HIGH);
pinMode(3, HIGH);
pinMode(4, HIGH);
delay(1000);
pinMode(1, LOW);
pinMode(2, LOW);
pinMode(3, LOW);
pinMode(4, LOW);
delay(1000);
}
if (pin==1) a;
if (pin==2) a2;
if (pin==3) c;
if (pin==4) e;
if (pin==5) h;
if (pin==6) i;
if (pin==7) l;
if (pin==8) m;
if (pin==9) n;
if (pin==10) o;
if (pin==11) r;
if (pin==12) t;
if (pin==13) w;
if (pin==14) pin = 0;
pin++;
delay(2000);
}
}

Hi alcolicu22

Most of the places where you use pinMode, you should be using digitalWrite.

Also, when you post code, please put it between [nobbc] [/nobbc] tags. It makes it much easier for people to read.

Thanks

Ray

Between “code” tags : [code][/code]

if (pin==1) a; "a;" is is not a function call. Use a(); instead.

Note: use of the term "void" with a function specifies that the function returns no value.

Note: Pin 0 and Pin 1 double as the Serial pins. You can’t use them for digital I/O and also use them for Serial. Pick different pins.

In your ‘for’ loops you should set the initial value of ‘i’. Global variables get initialized to 0 but local variables don’t get initialized unless you specifically initialize them. They pick up whatever garbage is in memory. Assuming you switch to pins 2,3,4,and 5 you should change all your:

 for (int i;i<5;i++) pinMode(i, LOW);

to

 for (int i=2;i<6;i++) digitalWrite(i, LOW);

but it doesn't work..

Oh, how I hate that phrase.

ok, thanks for your help so far, I did some changes but the phrase “doesn’t work” is still avaible :frowning:

int pin = 0;
// the Voids named with letters are actually combinations 
// a for example lights on only the first LED
// n lights on first and last LED
//and so on
void a(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(1, HIGH);
}
void a2(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
}
void c(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(1, HIGH);
  digitalWrite(2, HIGH);
}
void e(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(3, HIGH);
}
void h(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(1, HIGH);
  digitalWrite(3, HIGH);
}
void i(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
}
void l(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(1, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
}
void m(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(4, HIGH);
}
void n(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(1, HIGH);
  digitalWrite(4, HIGH);
}
void o(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
}
void r(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(1, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
}
void t(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
}
void w(){
  for (int i=1;i<5;i++) digitalWrite(i, LOW);
  digitalWrite(1, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
}
void setup() {
   pinMode(1, OUTPUT);
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(7, INPUT); //here is the button for changing
   Serial.begin(9600);
}

void loop() {
  Serial.println(digitalRead(7));
 if (digitalRead(7) == 1){  
   if (pin==0)
       for (int i=1;i<10;i++){
         digitalWrite(1, HIGH);
         digitalWrite(2, HIGH);
         digitalWrite(3, HIGH);
         digitalWrite(4, HIGH);
         delay(1000);
         digitalWrite(1, LOW);
         digitalWrite(2, LOW);
         digitalWrite(3, LOW);
         digitalWrite(4, LOW);
         delay(1000);
       }
   if (pin==1) a();
   if (pin==2) a2();
   if (pin==3) c();
   if (pin==4) e();
   if (pin==5) h();
   if (pin==6) i();
   if (pin==7) l();
   if (pin==8) m();
   if (pin==9) n();
   if (pin==10) o();
   if (pin==11) r();
   if (pin==12) t();
   if (pin==13) w();
   if (pin==14) pin = 0;
   pin++;
   delay(2000);
 }
}

the phrase "doesn't work" is still avaible

OK, explain what "does work" means to you in this case, and how it differs from what you observe, which, presumably is your definition of "doesn't work"

It works!!!.. I've done some changes and it works, thank you very much guys!

If you are interested it’s just numbers noted as letters (a=1,b=2,…,o=15,p=0) that are “write” in the binary code using 4 LEDs.

int pin = 1;
// the Voids named with letters are actually combinations 
// a for example lights on only the first LED
// n lights on first and last LED
//and so on
void a(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
}
void a2(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(3, HIGH);
}
void c(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
}
void e(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(4, HIGH);
}
void h(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
}
void i(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
}
void l(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
}
void m(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(5, HIGH);
}
void n(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(5, HIGH);
}
void o(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(5, HIGH);
}
void r(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(5, HIGH);
}
void t(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
}
void w(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
}
void x(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
}
void y(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
}
void z(){
  for (int i=0;i<=5;i++) digitalWrite(i, LOW);
}
void setup() {
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(7, INPUT); //here is the button for changing
   Serial.begin(9600);
}

void loop() {
  Serial.println(digitalRead(7));
 int starebuton=digitalRead(7);
 if (starebuton == 1){  
   if (pin==1) a();
   if (pin==2) h();
   if (pin==3) a2();
   if (pin==4) e();
   if (pin==5) c();
   if (pin==6) {
     z();
     pin = 0;
   }
   pin++;
   delay(300);
 }
}

Thanks again!

The circuit is like in the picture attached, but without resistors (It’s just a starter pack ;D )

led5_bb.png

The circuit is like in the picture attached, but without resistors

If you have the LEDs connected directly from Arduino pins to GND without resistors, you run the risk of damaging your Arduino and / or the LEDs. You need the resistors to limit the amount of current flowing through them when the Arduino switches them on.

Hackscribble:
If you have the LEDs connected directly from Arduino pins to GND without resistors, you run the risk of damaging your Arduino and / or the LEDs.

Yeah, i know. That’s why tomorrow I’m going to buy some.