problems with passing and using a global variable correctly

Hey all,

I am kind of stuck with a little detail of my code, where I want to use a global variable to be able to be used and changed by a function with several if-else-statements. I have several Capacitive Sensors that each have a letter prescribed to them that gets printed out by the Serial Monitor.
I have the variable char prev for the previous letter that the function updateCapacitiveSensors printed and I want it to only print the new letter, if it is a different one than the one before, so that if I touch one Sensor a little longer, it does not print it more than once.
With comparing the prev variable with the new letter I wanted to archieve this, but I think that it only changes the variable locally. So I changed the void updateCapacitiveSensors function to a char updateCapacitiveSensors function in order to pass the value for prev, but I think I did it wrong, but don't know where exactly

this is my complete code:

#include <CapacitiveSensor.h>
#include <arduino.h>


int main() {
  init();

  
  Serial.begin(9600);

//general loop for the sensors for the different regions
  static char prev='x';
  while (true) {
    prev= updateCapacitiveSensors(prev);
    delay(300);
  }
}

//decades
  int inpin = A0; // input pin for potentiometer
  int in = 0;
  
//regions
  int threshold = 60;
  

//1 capacitive sensor per region
  CapacitiveSensor cs_a5_a1 = CapacitiveSensor(A5, A1); //define region here  // GERMANY          10M resistor between pins A5 & 0, pin 0 is sensor pin
  CapacitiveSensor cs_a5_a2 = CapacitiveSensor(A5, A2); //define region here  // FRANCE           10M resistor between pins A5 & 1, pin 1 is sensor pin
  CapacitiveSensor cs_a5_2 = CapacitiveSensor(A5, 2); //define region here    // GREAT BRITAIN    10M resistor between pins A5 & 2, pin 2 is sensor pin
  CapacitiveSensor cs_a5_3 = CapacitiveSensor(A5, 3); //define region here    // SPAIN + PORTUGAL 10M resistor between pins A5 & 3, pin 3 is sensor pin
  CapacitiveSensor cs_a5_4 = CapacitiveSensor(A5, 4); //define region here    // ITALY            10M resistor between pins A5 & 4, pin 4 is sensor pin
  CapacitiveSensor cs_a5_5 = CapacitiveSensor(A5, 5); //define region here    // AFRICA           10M resistor between pins A5 & 5, pin 5 is sensor pin 
  CapacitiveSensor cs_a5_6 = CapacitiveSensor(A5, 6); //define region here    // SCANDINAVIA      10M resistor between pins A5 & 6, pin 6 is sensor pin
  CapacitiveSensor cs_a5_7 = CapacitiveSensor(A5, 7); //define region here    // ASIA             10M resistor between pins A5 & 7, pin 7 is sensor pin
  CapacitiveSensor cs_a5_8 = CapacitiveSensor(A5, 8); //define region here    // AUSTRALIA        10M resistor between pins A5 & 8, pin 8 is sensor pin
  CapacitiveSensor cs_a5_9 = CapacitiveSensor(A5, 9); //define region here    // SOUTH EAST       10M resistor between pins A5 & 9, pin 9 is sensor pin
  CapacitiveSensor cs_a5_10 = CapacitiveSensor(A5, 10); //define region here  // RUSSIA           10M resistor between pins A5 & 10, pin 10 is sensor pin
  CapacitiveSensor cs_a5_11 = CapacitiveSensor(A5, 11); //define region here  // SOUTH AMERICA    10M resistor between pins A5 & 11, pin 11 is sensor pin
  CapacitiveSensor cs_a5_12 = CapacitiveSensor(A5, 12); //define region here  // USA + CANADA     10M resistor between pins A5 & 12, pin 12 is sensor pin
  CapacitiveSensor cs_a5_13 = CapacitiveSensor(A5, 13); //define region here  // INDIA            10M resistor between pins A5 & 13, pin 13 is sensor pin
  

char updateCapacitiveSensors(char &prev) {


//read sensor input from the cap. sensors

  long sense0 = cs_a5_a1.capacitiveSensor(30); //GERMANY
  long sense1 = cs_a5_a2.capacitiveSensor(30); //FRANCE
  long sense2 = cs_a5_2.capacitiveSensor(30); //GREAT BRITAIN
  long sense3 = cs_a5_3.capacitiveSensor(30); //SPAIN + PORTUGAL
  long sense4 = cs_a5_4.capacitiveSensor(30); //ITALY
  long sense5 = cs_a5_5.capacitiveSensor(30); //AFRICA
  long sense6 = cs_a5_6.capacitiveSensor(30); //SCANDINAVIA
  
  long sense7 = cs_a5_7.capacitiveSensor(30); //ASIA
  long sense8 = cs_a5_8.capacitiveSensor(30); //AUSTRALIA
  long sense9 = cs_a5_9.capacitiveSensor(30); //SOUTH EAST
  long sense10 = cs_a5_10.capacitiveSensor(30); //RUSSIA
  long sense11 = cs_a5_11.capacitiveSensor(30); //SOUTH AMERICA
  long sense12 = cs_a5_12.capacitiveSensor(30); //USA +CANADA
  long sense13 = cs_a5_13.capacitiveSensor(30); //INDIA
  
//read sensor input from the potentiometer

in= analogRead(inpin);
int printin= 10;

if (in<100){
  printin=0;
}
else if (in<200){
  printin=1;
}
else if (in<300){
  printin=2;
}
else if (in<400){
  printin=3;
}
else if (in<500){
  printin=4;
}
else if (in<600){
  printin=5;
}
else if (in<700){
  printin=6;
}
else if (in<800){
  printin=7;
}
else if (in<900){
  printin=8;
}
else{
  printin=9;
}


 if (sense0 > threshold) {
    if (prev =='g'){
    }
    else{
    Serial.println(printin);
    Serial.println('g');
    return 'g';
    }
  }
  
  if (sense1 > threshold) {
    if  (prev =='f'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('f');
    return 'f';
    }
  }
  
  else if (sense2 > threshold) {
    if  (prev =='b'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('b');
    return 'b';
    }
  }
  
  else if (sense3 > threshold) {
    if  (prev =='s'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('s');
    return 's';
    }
  }
  
  else if (sense4 > threshold) {
    if  (prev =='i'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('i');
    return prev='i';
    }
  }
  
  else if (sense5 > threshold) {
    if  (prev =='o'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('a');
    return 'a';
    }
  }
  
  else if (sense6 > threshold) {
    if  (prev =='v'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('v');
    return 'v';
    }
  }
  
  else if (sense7 > threshold) {
    if  (prev =='c'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('c');
    return prev='c';
    }
  }
  
  else if (sense8 > threshold) {
    if  (prev =='t'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('t');
    return 't';
    }
  }
  
  else if (sense9 > threshold) {
    if  (prev =='e'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('e');
    return 'e';
    }
  }
  
  else if (sense10 > threshold) {
    if  (prev =='r'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('r');
    return 'r';
    }
  }
  
  else if (sense11 > threshold) {
    if  (prev =='o'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('o');
    return 'o';
    }
  }
  
  else if (sense12 > threshold) {
    if  (prev =='u'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('u');
    return 'u';
    }
  }
  
  else if (sense13 > threshold) {
    if  (prev =='n'){ 
    }
    else{
    Serial.println(printin);
    Serial.println('n');
    return 'n';
    }
  }

}

I would be glad if someone could point me in the right direction :slight_smile:
Thank you!

  while (true) {
    prev= updateCapacitiveSensors(prev);
    delay(300);
  }

How is this while loop ever going to end?

Why are you implementing your own main() function?

    if (prev =='g'){
    }
    else{
    Serial.println(printin);
    Serial.println('g');
    return 'g';
    }

If you are not going to do anything if the value hasn't changed,

    if (prev != 'g')
    {
       Serial.println(printin);
       Serial.println('g');
       return 'g';
    }

If you turned warnings on, you'd see that not all paths through updateCapacitiveSensors() return a value.

It is those paths that do not return a value that are causing you problems.

If you turned warnings on, you'd see that not all paths through updateCapacitiveSensors() return a value.

It is those paths that do not return a value that are causing you problems.

Thank you for your help! That makes sense, it works now. I did not know about warnings, but that helped a lot!

How is this while loop ever going to end?
Why are you implementing your own main() function?

I haven't decided when the while loop is going to end, but I will implement that later.
And I learned it that way, to implement my own main function, and now I am just used to doing i guess.

There is no need to "pass" global variables because they are accessible in every part of the program. This can be an advantage or a disadvantage.

...R