Pages: [1]   Go Down
Author Topic: Ughhhh!! - Array Issues...  (Read 481 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Faraday Member
**
Karma: 30
Posts: 2501
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()

  //PatternSeq1();
  if(LEDValue==7) LEDValue=0;
  Serial.print(LEDValue); 
  Serial.print(":");
//  Serial.println(
  Display(LEDValue);
  delay(4000);
  LEDValue++; 

}



int terminator(byte req[])
{
  int n = 0;
  int v;
  v = req[n];
   while (v!=0)
  {
  //  Serial.println(v);   //does not stop when it hits 0 in the array?!
    n++;
    v = req[n];
  }
  return n-1; 
}

void Display(byte Val)
{
  int items,V,n = 0;
  Clear();
//  Serial.print("Items: ");
//  Serial.print(items);
//  Serial.println("");
//  Serial.print("V: ");
//  Serial.println(V); 
   switch(Val)
   {
     case 0: { items=terminator(v0);  for(n=0; n<=items; n++) digitalWrite(v0[n],HIGH); }
     case 1: { items=terminator(v1);  for(int n=0; n<=items; n++) { digitalWrite(v1[n],HIGH);  }}     
     case 2: { items=terminator(v2);  for(int n=0; n<=items; n++) { digitalWrite(v2[n],HIGH);  }}
     case 3: { items=terminator(v3);  for(int n=0; n<=items; n++) { digitalWrite(v3[n],HIGH);  }}
     case 4: { items=terminator(v4);  for(int n=0; n<=items; n++) { digitalWrite(v4[n],HIGH);  }}
     case 5: { items=terminator(v5);  for(int n=0; n<=items; n++) { digitalWrite(v5[n],HIGH); }}
     case 6: { items=terminator(v6);  for(int n=0; n<=items; n++) { digitalWrite(v6[n],HIGH);  }}
//     case 7: { items=sizeof(v6); for(int n=0; n<=items; n++) digitalWrite(v7[n],HIGH); }         
   } 
     delay(5000);
         
}



void Clear()
{
  int n;
  for(n=2; n<=9;)
  {
    digitalWrite(n,LOW);
    delay(50);
    n++;
  }
}


sorry for the mess (been trying for hours to work this out)

basically, the code above is the issue, quite simple, it runs through an array of bytes, {2,3,4,5,0}  because i could not find a "how many in array function" i simply
appended 0 in the array.

1. how many elements in the array
2. switch on pins in that array.

Code:
int terminator(byte req[])
{
  int n = 0;
  int v;
  v = req[n];
   while (v!=0)
  {
  //  Serial.println(v);   //does not stop when it hits 0 in the array?!
    n++;
    v = req[n];
  }
  return n-1; 
}

is simply returning an invalid number eg 2,3,4,0 = 3 (meaning, pins 2,3,4 need to be switched on) the 0 does not seem to be found.

full code

Code:
byte v0[] = {2,3,0};
byte v1[] = {3,4,0};
byte v2[] = {5,6,7,0};
byte v3[] = {8,9,0};
byte v4[] = {2,0};
byte v5[] = {2,0};
byte v6[] = {2,0};

//byte v2[] = {8,9,5,2,3};
//byte v3[] = {2,3,4,5,6,7,8,9};

byte LEDValue = 0;

void setup()
{
 int n;
 Serial.begin(9600);
 for(n=2; n<=9; n++)
    pinMode(n,OUTPUT);   
}


void loop()

  //PatternSeq1();
  if(LEDValue==7) LEDValue=0;
  Serial.print(LEDValue); 
  Serial.print(":");
//  Serial.println(
  Display(LEDValue);
  delay(4000);
  LEDValue++; 

}



int terminator(byte req[])
{
  int n = 0;
  int v;
  v = req[n];
   while (v!=0)
  {
  //  Serial.println(v);   //does not stop when it hits 0 in the array?!
    n++;
    v = req[n];
  }
  return n-1; 
}

void Display(byte Val)
{
  int items,V,n = 0;
  Clear();
//  Serial.print("Items: ");
//  Serial.print(items);
//  Serial.println("");
//  Serial.print("V: ");
//  Serial.println(V); 
   switch(Val)
   {
     case 0: { items=terminator(v0);  for(n=0; n<=items; n++) digitalWrite(v0[n],HIGH); }
     case 1: { items=terminator(v1);  for(int n=0; n<=items; n++) { digitalWrite(v1[n],HIGH);  }}     
     case 2: { items=terminator(v2);  for(int n=0; n<=items; n++) { digitalWrite(v2[n],HIGH);  }}
     case 3: { items=terminator(v3);  for(int n=0; n<=items; n++) { digitalWrite(v3[n],HIGH);  }}
     case 4: { items=terminator(v4);  for(int n=0; n<=items; n++) { digitalWrite(v4[n],HIGH);  }}
     case 5: { items=terminator(v5);  for(int n=0; n<=items; n++) { digitalWrite(v5[n],HIGH); }}
     case 6: { items=terminator(v6);  for(int n=0; n<=items; n++) { digitalWrite(v6[n],HIGH);  }}
//     case 7: { items=sizeof(v6); for(int n=0; n<=items; n++) digitalWrite(v7[n],HIGH); }         
   } 
     delay(5000);
         
}



void Clear()
{
  int n;
  for(n=2; n<=9;)
  {
    digitalWrite(n,LOW);
    delay(50);
    n++;
  }
}

void PatternSeq1()
{
  int n;
  for(n=2; n<=9; n++)
  {
    digitalWrite(n,HIGH);
    delay(5500);
  }
 
 
 for(n=9; n>=2; n--)
  {
    digitalWrite(n,LOW);
    delay(500);
  }
 
 
}




* LEDface.ino (1.95 KB - downloaded 8 times.)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26522
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for(n=0; n<=items; n++)
If the function returns, say 3, then the valid subscripts are 0, 1 and 2 but not 3.
Code:
for(n=0; n<items; n++)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50434
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i could not find a "how many in array function" i simply appended 0 in the array.

Code:
byte array[] = {1, 3, 5, 7}

void loop()
{
   byte elemCnt = sizeof(array)/sizeof(array[0]);
   for(byte i=0; i<elemCnt; i++)
   {
   }
}

The sizeof() function will tell you the size of whatever you ask it to tell you the size of. The size of an array divided by the size of one element of the array will tell you how many items are in the array.

Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4814
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


1. how many elements in the array

The smart thing is to #define the array size or store it in a const byte (only uses 1 byte, int uses 2) and use that in your code. But otherwise sizeof(array) / sizeof(array element) will tell.

Quote
Code:
int terminator(byte req[])
{
  int n = 0;
  int v;
  v = req[n];
   while (v!=0)
  {
  //  Serial.println(v);   //does not stop when it hits 0 in the array?!
    n++;
    v = req[n];
  }
  return n-1; 
}

is simply returning an invalid number eg 2,3,4,0 = 3 (meaning, pins 2,3,4 need to be switched on) the 0 does not seem to be found.

Perhaps
Code:
   while (v!=0)
has something to do with not seeing 0 value printed?

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: