Array created by loop

Hi.

I've created a loop for a traffic-light system, and everytime it runs, it gives the variable 'loopTal' a unique number (overrides).
Now I've also told it to do change the variable 'sensor2' to 1, if 'knap1' (a button) in that exact loop was turned on, and remain 0 if it wasnt.

This gives me 2 numbers each time the loop runs. Now i would like to put these data into an Array, so that i can access them to check what the sensor2-value was, at a certain loopTal-value. How do i do this?

This is my code: blabla - Pastebin.com

At line 94, i have the 1 or 0 expression.

At line 104 is where i want to go into the array and check the sensor2 value. Dont mind the code on that line.

At line 137 i tried creating an array, but i'm not sure its the right way:
int tabel[loopTal] = {sensor2}; - meaning that the amount of rows, will be as long as the loop runs, and the colums will be the sensor2 data.

Now i want it to see what the sensor2-value was at the loop before the current one. If it was 1, do nothing - and if it was 0 - do what's listed in the else statement from line 110.

Might need a better explanation, but i'm kind of green at programming.

Thanks in advance, Jeppe :slight_smile:

This is my code

That is NOT the way to post code. There are two stickies at the top of the forum that you are supposed to read before posting. I suggest that you read them now.

I actually did read them, but i figured this would give the thread a better overview, since there are no line-numbers like on pastebin, but here's the code:

//Inputs til lyskasse1
int lysgr1 = 13;
int lysgl1 = 12;
int lysrd1 = 11;

//Inputs til lyskasse2
int lysgr2 = 10;
int lysgl2 = 9;
int lysrd2 = 8;

//Inputs til lyskasse3
int lysgr3 = 7;
int lysgl3 = 6;
int lysrd3 = 5;

//Inputs til Knap1 og Ambulance-knap
int ambu = 4;
int knap1 = 3;

//Tid mellem loops
int tid = 1000;
int tidgul = 2000;

int loopTal = 0;

int sensor2 = 0;

//Tilstand 1


/*Setup, hvor vi fortæller programmet, 
at de forskellige pins er outsputs.
*/

void setup(){
  pinMode(lysgr1, OUTPUT);
  pinMode(lysgl1, OUTPUT);
  pinMode(lysrd1, OUTPUT);
  
  pinMode(lysgr2, OUTPUT);
  pinMode(lysgl2, OUTPUT);
  pinMode(lysrd2, OUTPUT);
  
  pinMode(lysgr3, OUTPUT);
  pinMode(lysgl3, OUTPUT);
  pinMode(lysrd3, OUTPUT);
  
  pinMode(knap1, INPUT);
  pinMode(ambu, INPUT);
  
 //Opstarter serieforbindelse - Bits/tastetryk per sekund (Baud) - USB 2.0 hastighed
 Serial.begin(9600);
 
 //Printer en titel og laver et linebreak:
 Serial.println("Dette er vores lyskrydsprogram!");
}
 
// Vores endelige loop, som kører i ring til evig tid.

 
 void loop(){
   loopTal = loopTal +1;   

   Serial.println(loopTal);

//Koden her svarer til hvis "Er ambulance tilstand til?" JA
   if (digitalRead(ambu) == HIGH) { //hvis ambulanceknappen = tændt (knappen er trykket)
       digitalWrite(lysrd1 && lysrd2 && lysrd3, HIGH); //tænd de røde
       digitalWrite(lysgr1 && lysgr2 && lysgr3, LOW); //sluk de grønne
       digitalWrite(lysgl1 && lysgl2 && lysgl3, LOW); //sluk de gule
       delay(tid);                                    
/*
Venter 10 sekunder. 
Når en if statement er sand, så springer den selvfølgelig else-statement over.        
Så den hopper ned til hvor else {} slutter, hvilket er ved enden af loopet. 
Vi ender derfor med en if/else statement inde i en if/else statement (og måske flere)
*/

   } else {
// Koden her svarer til hvis "Er ambulance tilstand til?" NEJ
// Dette er egentlig vores standard tilstand, hvor der konstant skal være grønt på 'hovedvejen'.
       digitalWrite(lysgr1 && lysgr3, HIGH); 
       digitalWrite(lysrd2, HIGH);
       digitalWrite(lysgl1 && lysgl2 && lysgl3, LOW);
       digitalWrite(lysrd1 && lysrd3, LOW); 
       digitalWrite(lysgr2, LOW);
              
/* 
Der er som sagt i forvejen grønt, og nu tjekkes der om sensor-knappen er tændt. 
Hvis den er tændt, skal den give variablen sensor2 værdien 1, og hvis slukket 0.
Hvis vi så i næste loop har den slukket, vil vi kunne se at sensor2 = 0 i forrige loop.
Dette er så vi kan forhindre at få 2 loops på 10 sekunder i streg, hvor der kun er grønt ved lysgr2.
*/
       if (digitalRead(knap1) == HIGH){
         sensor2 = 1; 
       } else {
         sensor2 = 0; 
       }
/*
Hvis loopTal -1 (forrige loop) havde en sensorværdi på 1 (den opfangede noget)
så må den ikke tænde for lysgr2 igen. Så skal de bare springe ned og vente 10sek.
ELLERS, skal den køre den proces, hvor den tænder gule lys og skifter mellem kryds.
*/
       if (loopTal -1) == sensor2 = 1{
         //OVENSTÅENDE SKAL SKRIVES RIGTIGT. 
         //KAN DEN 'GEMME' INFORMATION FRA FORRIGE LOOP???
         
         int getIntArray();
         
       } else {
         digitalWrite(lysgr1 && lysgr3, LOW);
         digitalWrite(lysgl1 && lysgl3, HIGH);
         delay(tidgul);
         digitalWrite(lysgl1 && lysgl3, LOW);
         digitalWrite(lysrd1 && lysrd3, HIGH);
         digitalWrite(lysgl2, HIGH);
         delay(tidgul);
         digitalWrite(lysrd2, LOW);
         digitalWrite(lysgl2, LOW);
         digitalWrite(lysgr2, HIGH);
         delay(8000);
         digitalWrite(lysgr2, LOW);
         digitalWrite(lysgl2, HIGH);
         digitalWrite(lysgl1 && lysgl3, HIGH);
         delay(tidgul);
         digitalWrite(lysgl2, LOW);
         digitalWrite(lysrd2, HIGH);
         digitalWrite(lysgl1 && lysgl3, LOW);
         digitalWrite(lysrd1 && lysrd3, LOW);
         digitalWrite(lysgr1 && lysgr3, HIGH);
       }
   }
   Serial.println(sensor2);
   delay(tid);   
}

   int tabel[loopTal] = {sensor2};
   Serial.println(tabel[loopTal -1]);
         int getIntArray();

What is this? It isn't a function call. Defining a function prototype in the middle of a function is not normal. So, what is this?

   int tabel[loopTal] = {sensor2};
   Serial.println(tabel[loopTal -1]);

This code is not in a function. Where is it supposed to be? You can't create a global array with a variable size, unless the variable is const.

PaulS:

         int getIntArray();

What is this? It isn't a function call. Defining a function prototype in the middle of a function is not normal. So, what is this?

Basically everyting in that if-statement is garbage, but i googled the expression getIntArray, and it's somewhat similar to what i wanna do - its just in another language i think.

That if-statement is supposed to ask. "If, the the previous loop had an "sensor2" value of 1, then do nothing - meaning it will jump to the bottom and just delay until the loop starts over.

PaulS:

   int tabel[loopTal] = {sensor2};

Serial.println(tabel[loopTal -1]);



This code is not in a function. Where is it supposed to be? You can't create a global array with a variable size, unless the variable is const.

This was my attempt to create an array with the values from loopTal and sensor2. I figured it needed to be outside the loop, in order not to get resat after each loop, and that it needed to be after loop, since the variables are changed inside and would just remain 0 as defined at the top. (again, im sort of green at programming).

I was just thinking.. Since i only need to keep record of the current and previous loop, could i then do it by changing the variable sensor2 in the case where the loop-number (loopTal) is even, and create another variable that is changed when the loop-number is uneven. Like this:

       if (loopTal % 2) == 0) {
         if (digitalRead(knap1) == HIGH){
         sensor2 = 1; 
       } else {
         sensor2 = 0; 
       }
       } else {
         if (digitalRead(knap1) == HIGH){
         sensor1 = 1; 
       } else {
         sensor1 = 0; 
       } 
       }

The loopTal-value will change even to uneven infintely, so maybe i can do something like - "If loopTal is even, then take the sensor2 value - but if its uneven, take the sensor1 value". Does that make sense?

I haven't bothered looking into the context for that code, but given that you have two sensor variables and want to decide which one to access based on another variable, holding those variables in an array seems like the most obvious approach.

If you're looking for compactness, you could perform the whole thing in a single statement.

sensor[loopTal % 2] = (digitalRead(knap1) == HIGH);

Does that make sense?

Yes, and that should work.

You could also use a global or static array, of size 2, and use loopTal as an index into the array, to define where to store the new value and where to read the value from.

I would love to the array thing, but i simply dont know how to approach it.. Anyways i gave the other thing a try: I removed leftovers and changed the variablenames to english:

int lysgr1 = 13;
int lysgl1 = 12;
int lysrd1 = 11;

int lysgr2 = 10;
int lysgl2 = 9;
int lysrd2 = 8;

int lysgr3 = 7;
int lysgl3 = 6;
int lysrd3 = 5;

int ambu = 4;
int sensorBUTTON = 3;

int time = 1000;
int timeYELLOW = 2000;

int loopNUMBER = 0;

int sensorEVEN = 0;
int sensorUNEVEN = 0;

void setup(){
  pinMode(lysgr1, OUTPUT);
  pinMode(lysgl1, OUTPUT);
  pinMode(lysrd1, OUTPUT);
  
  pinMode(lysgr2, OUTPUT);
  pinMode(lysgl2, OUTPUT);
  pinMode(lysrd2, OUTPUT);
  
  pinMode(lysgr3, OUTPUT);
  pinMode(lysgl3, OUTPUT);
  pinMode(lysrd3, OUTPUT);
  
  pinMode(sensorBUTTON, INPUT);
  pinMode(ambu, INPUT);

 Serial.begin(9600);
 
 Serial.println("This is my first program!");
}
 
void loop(){
   loopNUMBER = loopNUMBER +1;   

   Serial.println(loopNUMBER);

   if (digitalRead(ambu) == HIGH) {
       digitalWrite(lysrd1 && lysrd2 && lysrd3, HIGH); 
       digitalWrite(lysgr1 && lysgr2 && lysgr3, LOW);
       digitalWrite(lysgl1 && lysgl2 && lysgl3, LOW);
       delay(time);                                    

   } else {
       digitalWrite(lysgr1 && lysgr3, HIGH); 
       digitalWrite(lysrd2, HIGH);
       digitalWrite(lysgl1 && lysgl2 && lysgl3, LOW);
       digitalWrite(lysrd1 && lysrd3, LOW); 
       digitalWrite(lysgr2, LOW);
              
       if (loopNUMBER % 2) == 0) {
         if (digitalRead(sensorBUTTON) == HIGH){
         sensorEVEN = 1; 
       } else {
         sensorEVEN = 0; 
       }
       
       } else {
         if (digitalRead(sensorBUTTON) == HIGH){
         sensorUNEVEN = 1; 
       } else {
         sensorUNEVEN = 0; 
       } 
       }

       if (loopNUMBER % 2) == 0 {
         if sensorUNEVEN == 1 {
         } else{
         
         if sensorEVEN == 1 {
         }
         } else {
         digitalWrite(lysgr1 && lysgr3, LOW);
         digitalWrite(lysgl1 && lysgl3, HIGH);
         delay(timeYELLOW);
         digitalWrite(lysgl1 && lysgl3, LOW);
         digitalWrite(lysrd1 && lysrd3, HIGH);
         digitalWrite(lysgl2, HIGH);
         delay(timeYELLOW);
         digitalWrite(lysrd2, LOW);
         digitalWrite(lysgl2, LOW);
         digitalWrite(lysgr2, HIGH);
         delay(8000);
         digitalWrite(lysgr2, LOW);
         digitalWrite(lysgl2, HIGH);
         digitalWrite(lysgl1 && lysgl3, HIGH);
         delay(timeYELLOW);
         digitalWrite(lysgl2, LOW);
         digitalWrite(lysrd2, HIGH);
         digitalWrite(lysgl1 && lysgl3, LOW);
         digitalWrite(lysrd1 && lysrd3, LOW);
         digitalWrite(lysgr1 && lysgr3, HIGH);
       }
   }
   delay(time);   
}

It was very confusing writing and understanding that.. But does it make sense? Sadly i don't have the board with me, so i can't test it yet. :frowning:

       digitalWrite(lysrd1 && lysrd2 && lysrd3, HIGH);

What do you think this is doing? I don't see the value in setting pin 0 or pin 1 HIGH, which is what this code is ACTUALLY doing.

PaulS:

       digitalWrite(lysrd1 && lysrd2 && lysrd3, HIGH);

What do you think this is doing? I don't see the value in setting pin 0 or pin 1 HIGH, which is what this code is ACTUALLY doing.

lysrd1, lysrd2 and lysrd3 are connected to pin 5, 8 and 11 in the top. So the line is supposed to turn those 3 pins to HIGH. Is that wrong?

I don't see the value in setting pin 0 or pin 1 HIGH

They're all non-zero, so it's just the same as writing digitalWrite(1, HIGH);

So the line is supposed to turn those 3 pins to HIGH. Is that wrong?

Unless you've checked the box on the IDE that's labeled "Let me invent shortcuts", yes it is.

Seeing as how there is no such checkbox, you can't invent shortcuts. There is A function for turning A pin on or off. If you want to turn two or three pins on or off, you can create a new function that turns more than one pin on or off, or you can call the one existing function multiple times.

PaulS:

So the line is supposed to turn those 3 pins to HIGH. Is that wrong?

Unless you've checked the box on the IDE that's labeled "Let me invent shortcuts", yes it is.

Seeing as how there is no such checkbox, you can't invent shortcuts. There is A function for turning A pin on or off. If you want to turn two or three pins on or off, you can create a new function that turns more than one pin on or off, or you can call the one existing function multiple times.

Ah ok, so this would be the correct way unless i do the function?:

       digitalWrite(lysrd1, HIGH);
       digitalWrite(lysrd2, HIGH);
       digitalWrite(lysrd3, HIGH);

SUCCES! I cleaned the whole thing up and cleared the mistakes as you mentioned and it compiled with no further errors! Some brackets and ()'s and it compiled perfectly. I tried it in a simulator, and it seems to run smooth.
Thanks for your guidance! Ill come back if it turns out to not work with the whole thing wired up, hehe :slight_smile:

int lysgr1 = 13;
int lysgl1 = 12;
int lysrd1 = 11;

int lysgr2 = 10;
int lysgl2 = 9;
int lysrd2 = 8;

int lysgr3 = 7;
int lysgl3 = 6;
int lysrd3 = 5;

int ambu = 4;
int sensorBUTTON = 3;

int time = 1000;
int timeYELLOW = 2000;

int loopNUMBER = 0;

int sensorEVEN = 0;
int sensorUNEVEN = 0;

void setup(){
  pinMode(lysgr1, OUTPUT);
  pinMode(lysgl1, OUTPUT);
  pinMode(lysrd1, OUTPUT);
  
  pinMode(lysgr2, OUTPUT);
  pinMode(lysgl2, OUTPUT);
  pinMode(lysrd2, OUTPUT);
  
  pinMode(lysgr3, OUTPUT);
  pinMode(lysgl3, OUTPUT);
  pinMode(lysrd3, OUTPUT);
  
  pinMode(sensorBUTTON, INPUT);
  pinMode(ambu, INPUT);

 Serial.begin(9600);
 
 Serial.println("This is my first program!");
}
 
void loop(){
   loopNUMBER = loopNUMBER +1;   

   Serial.println(loopNUMBER);

   if (digitalRead(ambu) == HIGH) {
       digitalWrite(lysrd1, HIGH); 
       digitalWrite(lysrd2, HIGH); 
       digitalWrite(lysrd3, HIGH); 
       digitalWrite(lysgr1, LOW);
       digitalWrite(lysgr2, LOW);
       digitalWrite(lysgr3, LOW);
       digitalWrite(lysgl1, LOW);
       digitalWrite(lysgl2, LOW);
       digitalWrite(lysgl3, LOW);
       delay(time);                                    

   } else {
       digitalWrite(lysgr1, HIGH);
       digitalWrite(lysgr3, HIGH);
       digitalWrite(lysrd2, HIGH);
       digitalWrite(lysgl1, LOW);
       digitalWrite(lysgl2, LOW);
       digitalWrite(lysgl3, LOW);
       digitalWrite(lysrd1, LOW);
       digitalWrite(lysrd3, LOW); 
       digitalWrite(lysgr2, LOW);
              
       if ((loopNUMBER % 2) == 0) {
         if (digitalRead(sensorBUTTON) == HIGH){
         sensorEVEN = 1; 
       } else {
         sensorEVEN = 0; 
       }
       
       } else {
         if (digitalRead(sensorBUTTON) == HIGH){
         sensorUNEVEN = 1; 
       } else {
         sensorUNEVEN = 0; 
       } 
       }

       if ((loopNUMBER % 2) == 0) {
         if (sensorUNEVEN == 1) {
         } else{
         
         if (sensorEVEN == 1) {
         }
		 }}
         else {
         digitalWrite(lysgr1, LOW);
         digitalWrite(lysgr3, LOW);
         digitalWrite(lysgl1, HIGH);
         digitalWrite(lysgl3, HIGH);
         delay(timeYELLOW);
         digitalWrite(lysgl1, LOW);
         digitalWrite(lysgl3, LOW);
         digitalWrite(lysrd1, HIGH);
         digitalWrite(lysrd3, HIGH);
         digitalWrite(lysgl2, HIGH);
         delay(timeYELLOW);
         digitalWrite(lysrd2, LOW);
         digitalWrite(lysgl2, LOW);
         digitalWrite(lysgr2, HIGH);
         delay(8000);
         digitalWrite(lysgr2, LOW);
         digitalWrite(lysgl2, HIGH);
         digitalWrite(lysgl1, HIGH);
         digitalWrite(lysgl3, HIGH);
         delay(timeYELLOW);
         digitalWrite(lysgl2, LOW);
         digitalWrite(lysrd2, HIGH);
         digitalWrite(lysgl1, LOW);
         digitalWrite(lysgl3, LOW);
         digitalWrite(lysrd1, LOW);
         digitalWrite(lysrd3, LOW);
         digitalWrite(lysgr1, HIGH);
         digitalWrite(lysgr3, HIGH);
       }
   }
   delay(time);  
 }

It seems another issue came up.
The if-statement that asks if the loopNUMBER is an even number, is doing something weird. As you can see in the image, in the middle - loopNUMBER is in this specific case set to 11 (an un-even number).
The red boxes/brackets are for if the if-statement is true, meaning loopNUMBER is an even number - and the green boxes/brackets are what happens if loopNUMBER is an uneven number. The blue-line explains where i step by step am in the loop - but since the loopNUMBER value is uneven, why does it jump to the else-statement inside the outer if-statement, when it's supposed to jump to the outer else-statement? Does my question make sense? :smiley:

Does it have anything to do with how my lines are spaced, organized - that the program may read it differently than i want i to?

Thanks in advance. :slight_smile:

I don't know what emulator you are using, but I would have thought that the red boxes would have signified a failed test and the green ones a successful test. That makes more sense to me. Red = warning, green = OK. Are you sure that you are describing the meaning of the colours correctly ?

What value do you get if you set a variable to (loopNUMBER % 2) or print it before testing it ? Does the emulator handle the modulo operator correctly ?

UKHeliBob:
I don't know what emulator you are using, but I would have thought that the red boxes would have signified a failed test and the green ones a successful test. That makes more sense to me. Red = warning, green = OK. Are you sure that you are describing the meaning of the colours correctly ?

What value do you get if you set a variable to (loopNUMBER % 2) or print it before testing it ? Does the emulator handle the modulo operator correctly ?

The green/red boxes are made in paint by myself, not by the emulator, hehe.

Can i do what you're suggesting by defining: int k = {(loopNUMBER % 2)} in the top, and then just insert k in the if-statement and print k afterwards?

I tried that, and it prints k = 0 over and over. Meaning it will always be a true if-statement. Very good job there.. But that must mean that the ((loopNUMBER % 2) == 0) doesn't do what it's supposed to. Does anyone have a more specific explanation on the 'variable % 2'? Doesn't it mean - if variable is dividable by 2 or something? It's supposed to tell whether its an even or uneven number.. Any alternatives?

variable & 1

will tell you if the number is odd or even.

I find code like this:

if(loopNumber % 2 == 0)
{
}
else
{
}

far easier to see the logic/flow than:

if(loopNumber % 2 == 0) {
} else {
}

You might try that, to see if the layout is even correct.

The scroll bars in your picture don't work.