Go Down

Topic: setup() keeps repeating for no reason (Read 122 times) previous topic - next topic

Tryptamine

Dec 10, 2019, 08:45 pm Last Edit: Dec 10, 2019, 08:48 pm by Tryptamine
Hey guys,

Problem is pretty much the title.
I never had such a behaviour and i'm completely clueless why i have it here.


My Code is
Code: [Select]

long m1;
long m2;
long m3;
long mav;
long minimum;
long revvolt = 0;
long zellen[0][1];
int zellenzahl=0;

void setup() {
  Serial.begin(9600);
 
  Serial.println("SETUP START");
 
 
  for (int i = 20; i <= 53; i++) {
    pinMode (i, OUTPUT);
    digitalWrite(i, LOW);
  }
  pinMode (A13, INPUT);
  pinMode (A14, INPUT);
 
 
  delay(100);
  revvolt = messen(30, A14);
  Serial.println(revvolt);
 
  delay(100);
 

  for (int i=0; i<=5; i++){
    if (messen((42+2*i), A14) >= 1000){
      zellenzahl=zellenzahl+1;
    }
  }
 
  Serial.println("");
  Serial.print("Zellenzahl:");
  Serial.println(zellenzahl);
 
 
  long zellen[zellenzahl-1][0];
 
  for (int i=0; i<=zellenzahl-1; i++){
    zellen[i][0] = messen((42+2*i), A14);
    digitalWrite((43+2*i), HIGH);
    delay(200);
    digitalWrite((43+2*i), LOW);
    Serial.print("Zelle "+String(i+1)+": ");
    Serial.println(3.292/revvolt*zellen[i][0], 3);
  }
 
  Serial.println("SETUP END");
  delay(1000);
}




void loop() {
 
  Serial.println("LOOP START");
 
  for (int i=0; i<=zellenzahl-1; i++){
    Serial.println("LOOP 1");
    zellen[i][0] = messen((42+2*i), A14);
    Serial.println("LOOP 2");
    Serial.print("Zelle "+String(i+1)+": ");
    Serial.println(3.292/revvolt*zellen[i][0], 3);
    delay(100);
  }
  Serial.println("LOOP END"); 
}

long messen(int trigger, int messpin){
  digitalWrite(trigger, HIGH);
  delay(100);
  long wert = 0;
  for (int i = 0; i <=20 ; i++) {
    wert = wert+ analogRead(messpin);
    delay(5);
  }
  digitalWrite(trigger, LOW);
  delay(10);
  return(wert);
}





Serial Output is below.

Note how the "LOOP 1"-marker is reached, but "LOOP 2" is not.
Apparently

Code: [Select]
zellen[i][0] = messen((42+2*i), A14);

somehow restarts the setup.



Code: [Select]

SETUP START
14733

Zellenzahl:3
Zelle 1: 3.828
Zelle 2: 3.826
Zelle 3: 3.828
SETUP END
LOOP START
LOOP 1
SETUP START
14731

Zellenzahl:3
Zelle 1: 3.827
Zelle 2: 3.827
Zelle 3: 3.829
SETUP END




Why so?

Help is greatly appreciated.

TheMemberFormerlyKnownAsAWOL

#1
Dec 10, 2019, 08:48 pm Last Edit: Dec 10, 2019, 08:50 pm by TheMemberFormerlyKnownAsAWOL
zellen has zero rows of one element?
Edit: just noticed you have a second zellen array.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

david_2018

zellen has zero rows of one element?
Edit: just noticed you have a second zellen array.

The 2nd zellen array is a number of rows each with zero elements, so same problem, it occupies no space and has no element [ 0 ]

Tryptamine

#3
Dec 10, 2019, 09:08 pm Last Edit: Dec 10, 2019, 09:17 pm by Tryptamine
I was confused because the indices start with 0 but the numer of entrys with 1 (obviously).

I changed line 7 and 42 from
Code: [Select]

long zellen[0][1];

  long zellen[zellenzahl-1][0];


to
Code: [Select]

long zellen[1][1];

  long zellen[zellenzahl][1];



the output is even more weird now, and inconsistent:
Code: [Select]

SETUP START
14934

Zellenzahl:3
Zelle 1: 3.807
Zelle 2: 3.812
Zelle 3: 3.808
SETUP END
LOOP START
LOOP 1
LOOP 2
Zelle 1: 3.805
LOOP 1
SETUP START
14869

Zellenzahl:3
Zelle 1: 3.833
Zelle 2: 3.829
Zelle 3: 3.832
SETUP END


wildbill

Zellen has one row of one element, which begs the question of why it is an array at all.

zellenzahl may be as high as five, which may explain why things go a bit squirrely when you run this loop over your one element zellen array:
Code: [Select]

  for (int i=0; i<=zellenzahl-1; i++){
    zellen[i][0] = messen((42+2*i), A14);

gianafrancisco

it won't work, because in the line 42 you are using zellen array and it is visible only inside to the setup() function.
When you are in the loop() function the zellen array used is the global variable and that array only has one element and that is the problem.
I recommend you rename the line 7 for the next line

long zellen[MAX_ENTRY]; (with MAX_ENTRY as a static number for instance 3).

And comment the line 42, also when you set a value or read a value from the array use zellen[ i ] and not zellen[ i ][ 0 ], for instance:

Set its value: zellen[ i ] = 1000;
Read its value: long val = zellen[ i ];


Robin2

Quote
setup() keeps repeating for no reason
You may be 100% sure there is a reason .

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Tryptamine

it won't work, because in the line 42 you are using zellen array and it is visible only inside to the setup() function.
When you are in the loop() function the zellen array used is the global variable and that array only has one element and that is the problem.
EXACTLY that was it.
Thanaks a lot!

I did it like this, because i wanted the "zellen" (cell voltage) array to only have as many entrys as the "zellenzahl" (number of cells)

now my array has 0 entrys, which actually is no problem at all.
The array is zellen[j], because there are more entrys to come, like the difference to the minimum voltage of each cell.

Is this a problem? Otherwise i just create multiple arrays

Go Up