Arduino if statement order im stuck! touch proximity sensor

Hello, I am trying to alter my sketch so that the output behaves as an sample and hold rather than going back to 0v.

I am using a touch sensor module to messure proximity data on 12 copper plates.

What is happening now is that when i release a node on my touch sensor board the serial the value of ''scaledProximity'' stops plotting on the serial plotter of arduino ide.

The output of analogwrite however goes back to 0v.. where i want it to be held at the last proximity value.

When i release a copper node the lastTouched node in the serial monitor goes to -1.

I think i am taking advantage of this added function.

Although the ''scaledProximity'' value is behing helt at the last touched value in the serial plotter.. I cannot get the analogwrite funtion to behave the same.

To be precise, the plotter does not contineu to plot when i release a copper touch node. It stops plotting and continues plotting when i touch another node (or the same) but it never drops like the analogWrite(A1, scaledProximity) does. I think it might have to do with the fact that it stops plotting.

Or it could be the order of things.

void loop() {
  int a1 = 0;                                                                                    // variable to store output of analog pin 1
  float voltage = 0;                                                                             // variable to store output voltage
  int scaledProximity;                                                                           // variable to store scaled proximity readings
  

                                                                                                 // check if any node is currently being touched
  int touched = cap.touched();
  int lastTouched = -1;                                                                          // variable to store last touched node
  for (int i = 0; i < 12; i++) {
    if (touched & (1 << i)) {
      lastTouched = i;
      break;
    }
  }

                                                                                         // output the filtered data of the last touched node on analog pin 1 as a voltage
  if (lastTouched != -1) { 
    if (touched & (1 << lastTouched)) {                                                  // check if the last touched node is still being touched
      int proximity = cap.filteredData(lastTouched);
      Serial.println("Proximity: " + String(proximity));
      proximity = constrain(proximity, 7, 60);                                           // filter out values above 120 and below 25
      int scaledProximity;                                                               // variable to store scaled proximity readings
      if (lastTouched >= 0 && lastTouched <= 11) {                                       // only update scaledProximity if lastTouched is in the range 0-11
        scaledProximity = map(proximity, 7, 60, 255, 0);                                 // scale proximity readings to a range of 0-255
      }
      voltage = (float)scaledProximity / 255 * 5; // convert to volts
      analogWrite(A1, scaledProximity);
    }
  } else {                                                                               // if lastTouched is -1, keep the last value of scaledProximity
    scaledProximity = analogRead(A1);
  }

  // print the output of analog pin 1 and the index of the last touched node
  Serial.println("Output of analog pin 1: " + String(voltage) + " V | Last touched node: " + String(lastTouched));
  delay(300);
}

Please post a complete sketch to save us all guessing what library you have used and what is in your setuo() function and, well, everything.

Please post a schematic diagram showing all components and how they are connected with attention to how power is being provided.

A make and model of the touching device(s) would not hurt.

Yes, maybe someone will carch your error(s). But many more ppl will even look for them if you follow general forum advices.

TIA

a7

Excuse me here is the full sketch. proximity module is mpr121

#include <Adafruit_MPR121.h>

#define MPR121_ADDRESS 0x5A // I2C address of MPR121
Adafruit_MPR121 cap = Adafruit_MPR121();

int lastTouched = -1; // variable to store last touched node

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

  if (!cap.begin(MPR121_ADDRESS)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }

  Serial.println("MPR121 found!");

  cap.setThresholds(20, 10); // default 12 and 6 set threshold value
}


void loop() {
  int a1 = 0;                                                                                    // variable to store output of analog pin 1
  float voltage = 0;                                                                             // variable to store output voltage
  int scaledProximity;                                                                           // variable to store scaled proximity readings
  

                                                                                                 // check if any node is currently being touched
  int touched = cap.touched();
  int lastTouched = -1;                                                                          // variable to store last touched node
  for (int i = 0; i < 12; i++) {
    if (touched & (1 << i)) {
      lastTouched = i;
      break;
    }
  }

                                                                                         // output the filtered data of the last touched node on analog pin 1 as a voltage
  if (lastTouched != -1) { 
    if (touched & (1 << lastTouched)) {                                                  // check if the last touched node is still being touched
      int proximity = cap.filteredData(lastTouched);
      Serial.println("Proximity: " + String(proximity));
      proximity = constrain(proximity, 7, 60);                                           // filter out values above 120 and below 25
      int scaledProximity;                                                               // variable to store scaled proximity readings
      if (lastTouched >= 0 && lastTouched <= 11) {                                       // only update scaledProximity if lastTouched is in the range 0-11
        scaledProximity = map(proximity, 7, 60, 255, 0);                                 // scale proximity readings to a range of 0-255
      }
      voltage = (float)scaledProximity / 255 * 5; // convert to volts
      analogWrite(A1, scaledProximity);
    }
  } else {                                                                               // if lastTouched is -1, keep the last value of scaledProximity
    scaledProximity = analogRead(A1);
  }

  // print the output of analog pin 1 and the index of the last touched node
  Serial.println("Output of analog pin 1: " + String(voltage) + " V | Last touched node: " + String(lastTouched));
  delay(300);
}

Turns out i had to move the ''int statements'' out of the void loop. Can anyone explain why? And maybe spot some oddities in my sketch.

not sure exactly what you mean by that, possibly that instead of using local variables you made global variables.

if you don't know what this changes, then it's time to read about scope

Here is the code:

All i did was move float voltage = 0; variable outside of the void loop.
Not sure but somehow if i declaired voltage inside of the loop it would jump back to 0.. now it keeps its last touched value active. This was what i wanted but i would like to know why. I would like to add a toggle switch to the arduino to switch between these function.

#include <Adafruit_MPR121.h>

#define MPR121_ADDRESS 0x5A // I2C address of MPR121
Adafruit_MPR121 cap = Adafruit_MPR121();


float voltage = 0;                                                                             // variable to store output voltage


// should i move int out of void loop? why??


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

  if (!cap.begin(MPR121_ADDRESS)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }

  Serial.println("MPR121 found!");

  cap.setThresholds(20, 10); // default 12 and 6 set threshold value
}

void loop() {
  
  //int lastTouched = -1;                                                                          // variable to store last touched node
  int scaledProximity;                                                                           // variable to store scaled proximity readings
  int a1 = 0;                                                                                    // variable to store output of analog pin 1
  
                                                                                                 // check if any node is currently being touched
  int touched = cap.touched();
  int lastTouched = -1;                                                                          // variable to store last touched node
  for (int i = 0; i < 12; i++) {
    if (touched & (1 << i)) {
      lastTouched = i;
      break;
    }
  }

                                                                                         // output the filtered data of the last touched node on analog pin 1 as a voltage
  if (lastTouched != -1) { 
    if (touched & (1 << lastTouched)) {                                                  // check if the last touched node is still being touched
      int proximity = cap.filteredData(lastTouched);
      Serial.println("Proximity: " + String(proximity));
      proximity = constrain(proximity, 7, 60);                                           // filter out values above 120 and below 25
      int scaledProximity;                                                               // variable to store scaled proximity readings
      if (lastTouched >= 0 && lastTouched <= 11) {                                       // only update scaledProximity if lastTouched is in the range 0-11
        scaledProximity = map(proximity, 7, 60, 255, 0);                                 // scale proximity readings to a range of 0-255
      }
      voltage = (float)scaledProximity / 255 * 5; // convert to volts
      analogWrite(A1, scaledProximity);
    }
  } else {                                                                               // if lastTouched is -1, keep the last value of scaledProximity
    scaledProximity = analogRead(A1);
  }

  // print the output of analog pin 1 and the index of the last touched node
  Serial.println("Output of analog pin 1: " + String(voltage) + " V | Last touched node: " + String(lastTouched));
  delay(300);
}

did you read the link about scope and understand the difference between local versus global variable? (if not, why am I loosing time providing answers and links?)

I read it. so making voltage so its being seen by the whole void loop made my sketch work. Im sure i made a bit of a mess of the sketch trying to make this work. Is the else function still needed? and also wondering if i should move analogwrite outside of the void loop

looks like you also moved the declaration and initialization of "lastTouched" inside the function which may also change behavior

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.