Go Down

Topic: Ughhhh!! - Array Issues... (Read 503 times) previous topic - next topic

cjdelphi

Code: [Select]

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: [Select]

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: [Select]

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);
  }

 
}





AWOL

Code: [Select]
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: [Select]
for(n=0; n<items; n++)
"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.

PaulS

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


Code: [Select]
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.


GoForSmoke



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: [Select]

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: [Select]
   while (v!=0)

has something to do with not seeing 0 value printed?

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

Go Up