Transfer parameter-value from loop to function

Hello guys,

my current challenge is to transfer parameter values from loop to functions. For my understanding i need global parameters, but the "choosing" itself of the correct parameters do happen at the loop. The parameters should be declared, if a specific button at the touchscreen is pressed. For example: If button 1 is pressed, < int matchtime = 5000; > should be transfered. If button 2 is pressed, -> int matchtime = 2000; > should be transfered.

The goal is to use the value of "matchtime" in milliseconds at function void matching().
The function is doing all the time the same, only difference are the different time periods, which are settet before by the button click.

Whats the smartest way to declare the respective parameter by clicking the button?

Im really looking forward to your help. many thanks in advance!!

The code:

#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "URTouch.h"

#define TFT_DC 2
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

#define t_SCK 3
#define t_CS 4
#define t_MOSI 5
#define t_MISO 6
#define t_IRQ 7

#define BLUE 0x001F
#define GREEN 0x07E0
#define WHITE 0xFFFF
#define BLACK 0x0000
#define YELLOW 0xFFF0
#define RED 0xF800

char currentpage;
URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);
int x, y;

int matchtime = 100;
int vmax;
int vmin;

unsigned long previousMillis;

const uint8_t ledPins[4] = { A1, A2, A3, A4 };
const uint8_t lastIndex = sizeof(ledPins) - 1;
const uint16_t blinkDuration = 200; //time period for void ventilation
uint32_t lastBlink;
uint8_t currLed = lastIndex;


//////////////////////////////
void setup() {
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  Serial.begin(9600);
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(ILI9341_WHITE);
  ts.InitTouch();
  ts.setPrecision(PREC_EXTREME);
  currentpage = '0'; // standard starting page after booting is home screen
  drawhomescreen();
  for (auto pin : ledPins) {
    pinMode(pin, OUTPUT);
  }
}

void loop() {
  // home screen setup
  if (currentpage == '0') {
    // add function: set test variables to 0
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();

      if ((x >= 50) && (x <= 125) && (y >= 20) && (y <= 60)) { // check, if click is within coordinates of several type
        // set the type specific parameters, which are used in void matching e.d.
        int matchtime = 5000; // 5s testing time for this type
        vmin = 20;  // minvolume for this type
        vmax = 23;  // max volumen for this type

        currentpage = '1';
        drawventilate(); // display next page
      }
      //if((x>=200) && (x<=275) && (y>=20) && (y<=60)){ } add PARAMETER for 470
      //if((x>=50) && (x<=125) && (y>=90) && (y<=130)){ } add PARAMETER for 600
      //if((x>=50) && (x<=125) && (y>=160) && (y<=200)){ } add PARAMETER for 900
    }
  }


  if (currentpage == '1') {
    startventilate(); // making rail air-free and adjusting pressure
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();
      if ((x >= 0) && (x <= 320) && (y >= 0) && (y <= 240)) {
        digitalWrite(A1, LOW);
        digitalWrite(A2, LOW);
        digitalWrite(A3, LOW);
        digitalWrite(A4, LOW);
        currentpage = '2';
        leakcheck(); // display screen "start leak check"
      }
    }
  }

  if (currentpage == '2') { // leak check
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();
      if ( (x >= 0) && (x <= 320) && (y >= 0) && (y <= 240) ) {
        currentpage = '3';
        drawmatching();  display screen "start matching"
      }
    }
  }

  if (currentpage == '3') {
    matching();
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();
      if ( (x >= 0) && (x <= 320) && (y >= 0) && (y <= 240) ) {
        currentpage == '4';
      }
    }
  }

  if (currentpage == '4') {
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();

      drawresults();

      if ( (x >= 0) && (x <= 320) && (y >= 0) && (y <= 240) ) {

        currentpage == '0';
      }
    }
  }
}



// (ADD PROGRAM FOR PROOF IDLE MATCH / IDLE OPENING TIME)

void drawventilate() {

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(05, 20);
  tft.setTextSize(2);
  tft.print("STARTE PUMPE & EINSTELLEN:");
  tft.setCursor(05, 70);
  tft.setTextSize(2);
  tft.print("3,0 Bar Druck ");
  //fertig button
  tft.fillRect (90, 160, 140, 40, BLUE);
  tft.drawRect (90, 160, 140, 40, WHITE);
  tft.setCursor(100, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("FERTIG");
}

void drawhomescreen() {
  // creating home screen
  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(1);
  tft.setCursor(80, 5);
  tft.print("Duesengroesse waehlen");
  tft.fillRect (50, 20, 75, 40, BLUE);
  tft.drawRect (50, 20, 75, 40, BLACK);
  tft.setCursor(65, 30);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("400");

  tft.fillRect (200, 20, 75, 40, BLUE);
  tft.drawRect (200, 20, 75, 40, BLACK);
  tft.setCursor(215, 30);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("470");

  tft.fillRect (50, 90, 75, 40, BLUE);
  tft.drawRect (50, 90, 75, 40, BLACK);
  tft.setCursor(65, 100);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("600");

  tft.fillRect (50, 160, 75, 40, BLUE);
  tft.drawRect (50, 160, 75, 40, BLACK);
  tft.setCursor(65, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("900");
}


void startventilate() {
  uint32_t topLoop = millis();
  if (topLoop - lastBlink >= blinkDuration) {
    lastBlink = topLoop;
    digitalWrite(ledPins[currLed], LOW);  // switch off last injector
    if (++currLed > lastIndex) {
      currLed = 0;
    }
    digitalWrite(ledPins[currLed], HIGH); // turn next injector on
  }
}

void leakcheck() {
  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(50, 20);
  tft.setTextSize(3);
  tft.print("LEAK CHECK!");

  tft.setCursor(50, 50);
  tft.setTextColor(RED);
  tft.setTextSize(2);
  tft.print("ALLE DUESEN DICHT?");

  tft.setCursor(50, 80);
  tft.setTextSize(2);
  tft.print("VENTIL SCHLIESSEN!");

  //fertig button

  tft.fillRect (30, 160, 250, 40, BLUE);
  tft.drawRect (30, 160, 250, 40, WHITE);
  tft.setCursor(35, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("DUESEN DICHT");
}

void drawmatching() {

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(50, 20);
  tft.setTextSize(3);
  tft.print("Einstellen");
  tft.setCursor(50, 50);
  tft.setTextSize(2);
  tft.print("3,0 Bar Druck ");

  //fertig button einblenden
  tft.fillRect (90, 160, 140, 40, BLUE);
  tft.drawRect (90, 160, 140, 40, WHITE);
  tft.setCursor(100, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("FERTIG");
}

// CODE FOR MATCHING
void matching() {
  matchtime = digitalRead(matchtime);
  Serial.print("zeit=");
  Serial.print(matchtime);

}


void drawresults() {

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(RED);
  tft.setCursor(10, 30);
  tft.setTextSize(2);
  tft.print("Werte pruefen!");

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(10, 90);
  tft.setTextSize(3);
  tft.print("MINIMUM");
  tft.setCursor(10, 1300);
  tft.setTextSize(3);
  tft.print("MAXIMUM");

  tft.setCursor(150, 90);
  tft.setTextSize(3);
  tft.print('vmin');

  tft.setCursor(150, 130);
  tft.setTextSize(3);
  tft.print('vmax');

  //fertig button

  tft.fillRect (90, 160, 140, 40, BLUE);
  tft.drawRect (90, 160, 140, 40, WHITE);
  tft.setCursor(100, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("FERTIG");

}

Sounds like you need to read about C++ Function Parameters

If matchtime is a global variable then its value will be available throughout the sketch so there is no need to transfer its value to a function that you call.

However, in your sketch you have more than one variable named matchtime
In loop(), so only available in loop()

        int matchtime = 5000; // 5s testing time for this type

As a global

int matchtime = 100;

So available throughout the sketch

The global version will be set when you do this

void matching()
{
  matchtime = digitalRead(matchtime);
  Serial.print("zeit=");
  Serial.print(matchtime);
}

Note also that if you want to use matchtime as some kind of timing variable then setting its value to the state of a pin using digitalRead() is not going to be of much use

currentpage == '4';

You need to learn about the difference between "=" and "==" !

EDIT:

tft.print('vmin');

...and the difference between " and '

@PaulRB yeah man youre right, Im an bloody coding starter, so thanks for your input!

To the solution of the topic:

@killzone_kid Arguments seems like the keyword im searching for, thank you!
Based on the link Ive tried now the following, can you find the still existing bug?:

#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "URTouch.h"

#define TFT_DC 2
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

#define t_SCK 3
#define t_CS 4
#define t_MOSI 5
#define t_MISO 6
#define t_IRQ 7

#define BLUE 0x001F
#define GREEN 0x07E0
#define WHITE 0xFFFF
#define BLACK 0x0000
#define YELLOW 0xFFF0
#define RED 0xF800

char currentpage;
URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);
int x, y;

int matchtime;
int vmax;
int vmin;

unsigned long previousMillis;

const uint8_t ledPins[4] = { A1, A2, A3, A4 };
const uint8_t lastIndex = sizeof(ledPins) - 1;
const uint16_t blinkDuration = 200; //time period for void ventilation
uint32_t lastBlink;
uint8_t currLed = lastIndex;


//////////////////////////////
void setup() {
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  Serial.begin(9600);
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(ILI9341_WHITE);
  ts.InitTouch();
  ts.setPrecision(PREC_EXTREME);
  currentpage = '0'; // standard starting page after booting is home screen
  drawhomescreen();
  for (auto pin : ledPins) {
    pinMode(pin, OUTPUT);
  }
}

void loop() {
  // home screen setup
  if (currentpage == '0') {
    // add function: set test variables to 0
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();

      if ((x >= 50) && (x <= 125) && (y >= 20) && (y <= 60)) { // check, if click is within coordinates of several type
        // set the type specific parameters, which are used in void matching e.d.
        int matchtime = 5000; // 5s testing time for this type
        vmin = 20;  // minvolume for this type
        vmax = 23;  // max volumen for this type

        currentpage = '1';
        drawventilate(); // display next page
      }
      //if((x>=200) && (x<=275) && (y>=20) && (y<=60)){ } add PARAMETER for 470
      //if((x>=50) && (x<=125) && (y>=90) && (y<=130)){ } add PARAMETER for 600
      //if((x>=50) && (x<=125) && (y>=160) && (y<=200)){ } add PARAMETER for 900
    }
  }


  if (currentpage == '1') {
    startventilate(); // making rail air-free and adjusting pressure
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();
      if ((x >= 0) && (x <= 320) && (y >= 0) && (y <= 240)) {
        digitalWrite(A1, LOW);
        digitalWrite(A2, LOW);
        digitalWrite(A3, LOW);
        digitalWrite(A4, LOW);
        currentpage = '2';
        leakcheck(); // display screen "start leak check"
      }
    }
  }

  if (currentpage == '2') { // leak check
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();
      if ( (x >= 0) && (x <= 320) && (y >= 0) && (y <= 240) ) {
        currentpage = '3';
        drawmatching();  //display screen "start matching"
      }
    }
  }

  if (currentpage == '3') {
    matching(int matchtime);
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();
      if ( (x >= 0) && (x <= 320) && (y >= 0) && (y <= 240) ) {
        currentpage = '4';
      }
    }
  }

  if (currentpage == '4') {
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();

      drawresults();

      if ( (x >= 0) && (x <= 320) && (y >= 0) && (y <= 240) ) {

        currentpage == '0';
      }
    }
  }
}



// (ADD PROGRAM FOR PROOF IDLE MATCH / IDLE OPENING TIME)

void drawventilate() {

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(05, 20);
  tft.setTextSize(2);
  tft.print("STARTE PUMPE & EINSTELLEN:");
  tft.setCursor(05, 70);
  tft.setTextSize(2);
  tft.print("3,0 Bar Druck ");
  //fertig button
  tft.fillRect (90, 160, 140, 40, BLUE);
  tft.drawRect (90, 160, 140, 40, WHITE);
  tft.setCursor(100, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("FERTIG");
}

void drawhomescreen() {
  // creating home screen
  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(1);
  tft.setCursor(80, 5);
  tft.print("Duesengroesse waehlen");
  tft.fillRect (50, 20, 75, 40, BLUE);
  tft.drawRect (50, 20, 75, 40, BLACK);
  tft.setCursor(65, 30);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("400");

  tft.fillRect (200, 20, 75, 40, BLUE);
  tft.drawRect (200, 20, 75, 40, BLACK);
  tft.setCursor(215, 30);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("470");

  tft.fillRect (50, 90, 75, 40, BLUE);
  tft.drawRect (50, 90, 75, 40, BLACK);
  tft.setCursor(65, 100);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("600");

  tft.fillRect (50, 160, 75, 40, BLUE);
  tft.drawRect (50, 160, 75, 40, BLACK);
  tft.setCursor(65, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("900");
}


void startventilate() {
  uint32_t topLoop = millis();
  if (topLoop - lastBlink >= blinkDuration) {
    lastBlink = topLoop;
    digitalWrite(ledPins[currLed], LOW);  // switch off last injector
    if (++currLed > lastIndex) {
      currLed = 0;
    }
    digitalWrite(ledPins[currLed], HIGH); // turn next injector on
  }
}

void leakcheck() {
  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(50, 20);
  tft.setTextSize(3);
  tft.print("LEAK CHECK!");

  tft.setCursor(50, 50);
  tft.setTextColor(RED);
  tft.setTextSize(2);
  tft.print("ALLE DUESEN DICHT?");

  tft.setCursor(50, 80);
  tft.setTextSize(2);
  tft.print("VENTIL SCHLIESSEN!");

  //fertig button

  tft.fillRect (30, 160, 250, 40, BLUE);
  tft.drawRect (30, 160, 250, 40, WHITE);
  tft.setCursor(35, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("DUESEN DICHT");
}

void drawmatching() {

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(50, 20);
  tft.setTextSize(3);
  tft.print("Einstellen");
  tft.setCursor(50, 50);
  tft.setTextSize(2);
  tft.print("3,0 Bar Druck ");

  //fertig button einblenden
  tft.fillRect (90, 160, 140, 40, BLUE);
  tft.drawRect (90, 160, 140, 40, WHITE);
  tft.setCursor(100, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("FERTIG");
}

// CODE FOR MATCHING
void matching(int matchtime) {
  
  Serial.print("zeit=");
  Serial.print(matchtime);

}


void drawresults() {

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(RED);
  tft.setCursor(10, 30);
  tft.setTextSize(2);
  tft.print("Werte pruefen!");

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(10, 90);
  tft.setTextSize(3);
  tft.print("MINIMUM");
  tft.setCursor(10, 1300);
  tft.setTextSize(3);
  tft.print("MAXIMUM");

  tft.setCursor(150, 90);
  tft.setTextSize(3);
  tft.print('vmin');

  tft.setCursor(150, 130);
  tft.setTextSize(3);
  tft.print('vmax');

  //fertig button

  tft.fillRect (90, 160, 140, 40, BLUE);
  tft.drawRect (90, 160, 140, 40, WHITE);
  tft.setCursor(100, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("FERTIG");

}
[/code]rint("zeit=");
  Serial.print(matchtime);

}

@UKHeliBob The global definition was left from testing before. So the

 int matchtime = 100; 

can be "deleted".

Unless you actually delete it then the compiler will use its value and store values in it

I note that you still have more than one variable named matchtime, each with their own scope, one of which is global, and you have mangled the end of your code

Hello dear,

Ive done a little progress with your help. :slight_smile:
At the moment, the matching value of 5000 is passed to the function matching(), if button 1 is clicked. When we press button 2, the value of 2000 should be passed to the function matching.
The fault at this point is, that in case button 2 is clicked, the serial monitor still displays 5000.
So still pass the wrong variables with my code. :smiley:

For reference the current code, without mangling (:D):

#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "URTouch.h"

#define TFT_DC 2
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

#define t_SCK 3
#define t_CS 4
#define t_MOSI 5
#define t_MISO 6
#define t_IRQ 7

#define BLUE 0x001F
#define GREEN 0x07E0
#define WHITE 0xFFFF
#define BLACK 0x0000
#define YELLOW 0xFFF0
#define RED 0xF800

char currentpage;
URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);
int x, y;

int vmax;
int vmin;
int matchtime;


unsigned long previousMillis;

const uint8_t ledPins[4] = { A1, A2, A3, A4 };
const uint8_t lastIndex = sizeof(ledPins) - 1;
const uint16_t blinkDuration = 200; //time period for void ventilation
uint32_t lastBlink;
uint8_t currLed = lastIndex;


//////////////////////////////
void setup() {
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  Serial.begin(9600);
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(ILI9341_WHITE);
  ts.InitTouch();
  ts.setPrecision(PREC_EXTREME);
  currentpage = '0'; // standard starting page after booting is home screen
  drawhomescreen();
  for (auto pin : ledPins) {
    pinMode(pin, OUTPUT);
  }
}

void loop() {
  // home screen setup
  if (currentpage == '0') {
    // add function: set test variables to 0
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();

      if ((x >= 50) && (x <= 125) && (y >= 20) && (y <= 60)) { // button 1 clicked 
        // set the type specific parameters, which are used in void matching e.d.
        int matchtime = 5000; // 5s testing time for this type
        vmin = 20;  // minvolume for this type
        vmax = 23;  // max volumen for this type

        currentpage = '1';
        drawventilate(); // display next page
      }
      
      if((x>=200) && (x<=275) && (y>=20) && (y<=60)){ // button 2 clicked 
        // set the type specific parameters, which are used in void matching e.d.
        int matchtime = 2000; // 5s testing time for this type
        vmin = 20;  // minvolume for this type
        vmax = 23;  // max volumen for this type
        currentpage = '1';
        drawventilate(); // display next page        
        
        }
      //if((x>=50) && (x<=125) && (y>=90) && (y<=130)){ } add PARAMETER for 600
      //if((x>=50) && (x<=125) && (y>=160) && (y<=200)){ } add PARAMETER for 900
    }
  }


  if (currentpage == '1') {
    startventilate(); // making rail air-free and adjusting pressure
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();
      if ((x >= 0) && (x <= 320) && (y >= 0) && (y <= 240)) {
        digitalWrite(A1, LOW);
        digitalWrite(A2, LOW);
        digitalWrite(A3, LOW);
        digitalWrite(A4, LOW);
        currentpage = '2';
        leakcheck(); // display screen "start leak check"
      }
    }
  }

  if (currentpage == '2') { // leak check
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();
      if ( (x >= 0) && (x <= 320) && (y >= 0) && (y <= 240) ) {
        currentpage = '3';
        drawmatching();  //display screen "start matching"
      }
    }
  }

  if (currentpage == '3') {
    matching(5000);
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();
      if ( (x >= 0) && (x <= 320) && (y >= 0) && (y <= 240) ) {
        currentpage = '4';
      }
    }
  }

  if (currentpage == '4') {
    if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();

      drawresults();

      if ( (x >= 0) && (x <= 320) && (y >= 0) && (y <= 240) ) {

        currentpage == '0';
      }
    }
  }
}



// (ADD PROGRAM FOR PROOF IDLE MATCH / IDLE OPENING TIME)

void drawventilate() {

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(05, 20);
  tft.setTextSize(2);
  tft.print("STARTE PUMPE & EINSTELLEN:");
  tft.setCursor(05, 70);
  tft.setTextSize(2);
  tft.print("3,0 Bar Druck ");
  //fertig button
  tft.fillRect (90, 160, 140, 40, BLUE);
  tft.drawRect (90, 160, 140, 40, WHITE);
  tft.setCursor(100, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("FERTIG");
}

void drawhomescreen() {
  // creating home screen
  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(1);
  tft.setCursor(80, 5);
  tft.print("Duesengroesse waehlen");
  tft.fillRect (50, 20, 75, 40, BLUE);
  tft.drawRect (50, 20, 75, 40, BLACK);
  tft.setCursor(65, 30);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("400");

  tft.fillRect (200, 20, 75, 40, BLUE);
  tft.drawRect (200, 20, 75, 40, BLACK);
  tft.setCursor(215, 30);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("470");

  tft.fillRect (50, 90, 75, 40, BLUE);
  tft.drawRect (50, 90, 75, 40, BLACK);
  tft.setCursor(65, 100);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("600");

  tft.fillRect (50, 160, 75, 40, BLUE);
  tft.drawRect (50, 160, 75, 40, BLACK);
  tft.setCursor(65, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("900");
}


void startventilate() {
  uint32_t topLoop = millis();
  if (topLoop - lastBlink >= blinkDuration) {
    lastBlink = topLoop;
    digitalWrite(ledPins[currLed], LOW);  // switch off last injector
    if (++currLed > lastIndex) {
      currLed = 0;
    }
    digitalWrite(ledPins[currLed], HIGH); // turn next injector on
  }
}

void leakcheck() {
  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(50, 20);
  tft.setTextSize(3);
  tft.print("LEAK CHECK!");

  tft.setCursor(50, 50);
  tft.setTextColor(RED);
  tft.setTextSize(2);
  tft.print("ALLE DUESEN DICHT?");

  tft.setCursor(50, 80);
  tft.setTextSize(2);
  tft.print("VENTIL SCHLIESSEN!");

  //fertig button

  tft.fillRect (30, 160, 250, 40, BLUE);
  tft.drawRect (30, 160, 250, 40, WHITE);
  tft.setCursor(35, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("DUESEN DICHT");
}

void drawmatching() {

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(50, 20);
  tft.setTextSize(3);
  tft.print("Einstellen");
  tft.setCursor(50, 50);
  tft.setTextSize(2);
  tft.print("3,0 Bar Druck ");

  //fertig button einblenden
  tft.fillRect (90, 160, 140, 40, BLUE);
  tft.drawRect (90, 160, 140, 40, WHITE);
  tft.setCursor(100, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("FERTIG");
}

// CODE FOR MATCHING
void matching(int matchtime) {
  
  Serial.print("zeit=");
  Serial.print(matchtime);

}


void drawresults() {

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(RED);
  tft.setCursor(10, 30);
  tft.setTextSize(2);
  tft.print("Werte pruefen!");

  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(BLACK);
  tft.setCursor(10, 90);
  tft.setTextSize(3);
  tft.print("MINIMUM");
  tft.setCursor(10, 1300);
  tft.setTextSize(3);
  tft.print("MAXIMUM");

  tft.setCursor(150, 90);
  tft.setTextSize(3);
  tft.print('vmin');

  tft.setCursor(150, 130);
  tft.setTextSize(3);
  tft.print('vmax');

  //fertig button

  tft.fillRect (90, 160, 140, 40, BLUE);
  tft.drawRect (90, 160, 140, 40, WHITE);
  tft.setCursor(100, 170);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("FERTIG");

}

Why have you still got multiple different variables named matchtime ? By my count you have 4 of them, each with different scopes

Good morning,

yes there are 4 different scopes, I think the int matchtime before setup we dont need.

The second and third int matchtime i like to use to give the parameter matchtime an value, which one is called up at the function matchtime.

I do understand, that every loop, in which i give the parameter an value, is an different scope.

What i do not understand is, how i can make the value of the parameter visible for the function.

How i can assign a value to an parameter within scope 'A' and work with this value at a function, if the function is outside of this scope 'A'?

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  byte number = random(256); //a byte variable local to loop()
  showNumber(number); //pass the value to a function
  delay(1000);
}

void showNumber(byte theByte) //value passed is named theByte in the function
{
  Serial.print("value in the function : ");
  Serial.println(theByte);
}

Hello @UKHeliBob,

I found our missunderstanding:
My challenge is to declarate the arguments within an if statement WITHOUT calling up the function. The function should be later called up by another if statement and should use the arguments, which are declared by an former if statement.

EDIT: So I would like to define "global" variables WITHIN the Loop. Or find another way to get the same result, if this way is not possible :slight_smile:

Ive compressed it into a code snipped to make it more understandable:

#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "URTouch.h"
#define TFT_DC 2
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
#define t_SCK 3
#define t_CS 4
#define t_MOSI 5
#define t_MISO 6
#define t_IRQ 7
URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);
char currentpage;
int x, y;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  if (currentpage == '0') {
      if (ts.dataAvailable()) {
      ts.read();
      x = ts.getX();
      y = ts.getY();

      if ((x >= 50) && (x <= 125) && (y >= 20) && (y <= 60)) { // button 1 clicked 
        
        int matchtime = 5000; // 5s testing time for this type is setted here
          
     }
      
      if((x>=200) && (x<=275) && (y>=20) && (y<=60)){ // button 2 clicked 
        
        int matchtime =2000;// 2s testing time for this type is setted here
             
               }
         }
  }

//(...)

 if (currentpage == '3')
  matching(); //function with arguments is called up here. The function should now run with value of arguments, which are defined at line 15 or 19 (depending on which button was clicked before). 
  
}

void matching(int matchtime) { //function, which is working with previously assigned parameters
  
  Serial.print("zeit=");
  Serial.print(matchtime); //prooves, that correct argument is used at function

}
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  byte number; //a variable whose scope is the loop() function
  for (int count = 0; count < 8; count++) //another local variable
  {
    if (count == 3)
    {
      number = random(256); //give the variable a value
      showNumber(count, number); //pass the values to a function
      delay(1000);
    }
  }
}

void showNumber(int theCount, byte theByte)
{
  Serial.print("count is : ");
  Serial.println(theCount);
  Serial.print("the random number is : ");
  Serial.println(theByte);
}

Okay guys, found the solution.
If anybody is searching for the same issue, this is the easy understandable solution:

int value = 0; // set value to 0 at first


void setup() {
  Serial.begin(9600);
}

void loop() {

  if (generalcondition == true) {

    if (condition 1 == true) {

      value = 50; // overwrite variable "value" to 50

    }

    if (condition 2 == true) {

      value = 200; //overwrite variable "value" to 200

    }
  }

  if generalcondition2 == true) {

  dofunction (value);
  }

}

void dofunction ( int value) {

  Serial.print("acutual value=");
  Serial.print(value);

}
int value = 0; // set value to 0 at first
  dofunction (value);

You still don't get it

value is declared as a global so you don't need to pass it to the function. In fact, doing so can cause more confusion because you still have 2 different variables named value, one with global scope and one whose scope is in the function