简体   繁体   中英

How do i change variable after each 2-3 loops?

I have a loop that constantly goes on. I would like to change the variable after each 2-3 loops. Right now, after running the loop, the variable would stay always the same (if I don't change it) or it changes every time. My code:

#include "VernierLib.h"
VernierLib Vernier;
#define led 13
float sensorReading;
void setup() {
  Serial.begin(9600);
  Serial.println(Vernier.sensorName());
  Vernier.autoID();
}
void loop() { 
  float base_temp = Serial.parseFloat();
  sensorReading = Vernier.readSensor();
  Serial.print(sensorReading);
  Serial.print(" ");
  Serial.println(Vernier.sensorUnits());
  Serial.print("Želena temperatura: ");
  Serial.println(base_temp);
  if (base_temp > sensorReading) {
    digitalWrite(led, HIGH);
  } else {
    digitalWrite(led, LOW);
  }
  delay(2300);
}

Use some kindof tracker variable to check how many iterations you have done so far.

Something like

#include "VernierLib.h"
VernierLib Vernier;
#define led 13
float sensorReading;
int tracker=0;
int numLoops=3; //You can change this as you need to
void setup() {
  Serial.begin(9600);
  Serial.println(Vernier.sensorName());
  Vernier.autoID();
}
void loop() { 
  tracker++;
  if(tracker<=numLoops){
    //Put variable change code here
  }
  float base_temp = Serial.parseFloat();
  sensorReading = Vernier.readSensor();
  Serial.print(sensorReading);
  Serial.print(" ");
  Serial.println(Vernier.sensorUnits());
  Serial.print("Želena temperatura: ");
  Serial.println(base_temp);
  if (base_temp > sensorReading) {
    digitalWrite(led, HIGH);
  } else {
    digitalWrite(led, LOW);
  }
  delay(2300);
}

I'll extend Spyre answer a little. You can do that by checking modulo (the remainder after division) like this:

// Index hold information about "how many times did the loop run already"
int index = 0;
// NumLoop says "after how many 'loops' do I want to do the action"
// The word 'const' indicates, that this value can never be changed
const int numLoops = 3;

void loop() {
  if(++index % numLoops == 0) {
    //Put what you want there
  }
}

What the code does? You declare variable index set initially to 0 and the constant numLoops set to number of loops after which the code should be invoked.

Inside loop you check with if() statement result of modulo operation, if index % numLoops is equal to 0 => if(index % numLoops == 0) . And also you must increment index by 1 each loop. Short notation for index = index + 1 is ++index .

index % numLoops produce these results (operation is called Modulo ):

index   % numLoops = result             
0       %    3     =  0
1       %    3     =  1
2       %    3     =  2
3       %    3     =  0
4       %    3     =  1
5       %    3     =  2
6       %    3     =  0
etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM