Need Help - How do you store a variable declared in an If Statement

Hello everyone,

I am still a n00b programmer, and am working on a project utilizing a resistive touchscreen to take values from a potentiometer. All my components work, but I can’t “store the values” I take in from the buttons on touchscreen.

What I am having trouble with, is I programmed touch buttons via a series of “if” statements (so where it reads pressures on the screen and the if statements differentiate what part of screen it is on, and thus which button is being pressed)

Problem is… Inside the if statement (So imagine you click a button) it reads the analogvalue from the potentiometer. I want to use that value later on in the code, so OUTSIDE of the if statement, but it isn’t saved…

Looking at the code below, I am specifically having troubles with the char’s dx, dy, and dz… I want to do math with them on the "CurrentPage = ‘3’ " but the value is not stored from the other pages/ buttons… (i.e. from inside the other if statements) I try to print them out, and the variable acts as though it was never declared, even though the if statements are set up in such a way, that the variable HAD TO be read before getting to next button…

in the code below, I simply declared the dx, dy, and dz manually inside if statements, hoping to get some sort of math to happen when hitting fourth button, but no luck!! :frowning:

Look for ******************* to find the problem areas I mentioned

#include <Adafruit_BNO055.h> // Gyro sensor specific library
#include <Adafruit_ILI9341.h> // SPI color library
#include <Adafruit_STMPE610.h>
#include <Adafruit_GFX.h> // Graphics library
// Defining tft library and tft() function pins
#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
// Defining Sensor
#define GYRORST 19
Adafruit_BNO055 bno = Adafruit_BNO055(55);
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Defining touchscreen pressure parameters
#define TS_MINX 0
#define TS_MINY 0
#define TS_MAXX 3800
#define TS_MAXY 4000
// Global variables
char dz; //****declared as characters***********
char dy; //****declared as characters*********
char dx; //****declared as characters**********
char currentPage;
// Defining Button Sizes and placement
int d = 10; int w1 = 70; int b123xd = 140;
int b123xu = b123xd+w1; int b45xd = b123xu+2*d; int b45xu = b45xd+w1;
int b6xd = 250; int b6xu = 310; int b7xd = 220;
int b7xu = 300; int yh1 = 45; int b1yu = 60;
int b1yd = b1yu+yh1; int b2yu = b1yd+d; int b2yd = b2yu+yh1;
int b3yu = b2yd+d; int b3yd = b3yu+yh1; int yh2 = 60;
int b4yu = 60; int b4yd = b4yu+yh2; int b5yu = b4yd+d;
int b5yd = b5yu+yh2; int b6yu = 200; int b6yd = 230;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  !bno.begin();
  ts.begin();
  tft.begin();
  homeScreen(); // Draws the Home Screen
  currentPage = '0'; // Declare home screen as page 0
}

void loop() {
  // put your main code here, to run repeatedly:
  if (ts.bufferEmpty()){
    return;
  }
  TS_Point p = ts.getPoint();
  int y = tft.height()-map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
  int x = tft.width()-map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
    if (currentPage == '0') {
        if ((x>=b123xd) && (x<=b123xu) && (y<=b1yd) && (y>=b1yu)) {
          dz = 6;                 // *************MAnually made a number*************
          //double dz = analogRead(A6);
          button1();
          currentPage = '1';
        }
    }
    if (currentPage == '1') {
        if ((x>=b123xd) && (x<=b123xu) && (y<=b2yd) && (y>=b2yu)) {
          dy = 4;                      //*****************Manually input a number*********
          //double dy = analogRead(A7);
          button2();
        }
        if ((x<=320-b6xd) && (x>=320-b6xu) && (y<=b6yd) && (y>=b6yu)) {
          button6();
          homeScreen();
          currentPage = '0';
        }
    }
    if (currentPage == '2') {
        if ((x>=b123xd) && (x<=b123xu) && (y<=b3yd) && (y>=b3yu)) {
          dx = 4;                                    //*****************Again Manually**********
          //double dx = analogRead(A7);
          button3();
        }
        if ((x<=320-b6xd) && (x>=320-b6xu) && (y<=b6yd) && (y>=b6yu)) {
          button6();
          homeScreen();
          button1();
          currentPage = '1';
        }
    }
    if (currentPage == '3') {
        if ((x<=320-b45xd) && (x>=320-b45xu) && (y<=b4yd) && (y>=b4yu)) {
          gyrorst();
          sensors_event_t event;
          bno.getEvent(&event);
          float x_offset = event.orientation.x;
          float y_offset = event.orientation.y;
          float z_offset = event.orientation.z;
          Serial.println("DX = "); 
          Serial.println(dx);  //******** NOT PRINTING VALUE IS ZERO/not declared************
          Serial.println("DY = ");
          Serial.println(dy);  // *********NOT PRINTING VALUE IS ZERO/not declared************
          Serial.println("DZ = ");
          Serial.println(dz);  // ***********NOT PRINTING VALUE IS ZERO/not declared********
          float a = (atan2(dy,dz))-0.0873; 
          float d = sqrt((dx*dx)-(dy*dy)); // ******Thus all this math turns to zero in this statement :( **********
          float b = -(atan2(d,dz));
          double c = -M_PI/2;
        }
    }

}

Any help is greatly appreciated, I am really hoping to get this math working ASAP and know I am declaring my variables wrong or not storing them correctly… I don’t understand the if statement correctly, i assume… Thanks in advance for looking!

ArduinoHelp.ino (3.38 KB)

You don’t quote code.
You use code tags. </>

First, you need to read the Tutorials post by Nick Gammon at the top of this Forum, look for Scope under C++ Programming. Then read the other two posts by Nick, especially on the use of code tags for listing source code. I think you'll find them helpful.

I have searched for three weeks on this problem, and read through many many arduino threads... I thought I got it working, but it still isn't. None of the threads are the same as mine, and I am just having trouble understanding how to store a variable that was gathered from inside an IF statement.

Any help regarding that, is GREATLY appreciated!! Thank you!!!

And hope the code is more readable now, my apologies.

Where is 'currentPage' ever made equal to 2 or 3?

.

LarryD:
Where is 'currentPage' ever made equal to 2 or 3?

.

I just tried to cut out a bunch of stuff to make the code small, so I have all the buttons working..
. I didn't include the IF statement with the button that changes the "currentpage" just like how I didn't include the button() functions...

My question is only how do I store the DX dy & dz values INSIDE an IF statement... I need to use those values in the other IF statement for math calculations... I thought declaring them as Char's at first then later imputing the value from the analog potentiometer would work, but it is not :confused:

Tell us what you think the value of '1’ is.

.

char dz; //declared as characters*******
char dy; //declared as characters*****
char dx; //declared as characters******

Why not make these byte or int?

LarryD:
char dz; //declared as characters*******
char dy; //declared as characters*****
char dx; //declared as characters******

Why not make these byte or int?

Never used byte before… And I thought when using int , it needs a number attached right away. I obviously want to press the button and capture the value at that time to use later

A variable doesn't need a value right away, including int variables.

Not totally sure what you are doing but:

‘byte’ can hold numbers from 0 to 255
‘int’ can hold numbers from -32,768 to 32,767
These are initialized to 0 if not assigned when you declare them as global.

If, dz, dy or dz are always going to be positive and >=0 and <256 then use ‘byte’
If, you need plus and negative numbers in the range -32,768 to 32,767 then use ‘int’

This way when you print dx, dy or dz you will get a ‘number’ rather than a ‘character’ printed.

Edit:
If you send a 1 to the Arduino from the serial monitor you get 49 at the Arduino which is ASCII for 1.

.

Sorry, I think I gave over zealous code… I cut it down to exactly what I am asking… See how on page 0, I press a button (the if statement) and inside that statement I assign dz=6… Then I later get to page 3, in which I want to use dz and do math… But I just try to print that value, and nothing shows up, as if I never captured it!! So I imagine the IF statement is being treated as a void?? This is where I am unclear…

 if (currentPage == '0') {
        if ((x>=b123xd) && (x<=b123xu) && (y<=b1yd) && (y>=b1yu)) {
          dz = 6;
          //double dz = analogRead(A6);
          button1();
          currentPage = '1';
        }
    }
  
    }
  if (currentPage == '3') {
        if ((x<=320-b45xd) && (x>=320-b45xu) && (y<=b4yd) && (y>=b4yu)) {
         
          Serial.println("DZ = "); 
          Serial.println(dz);  //***** Now on page 3 I do not have dz =6, it is as if I never assigned it********
          
        }

Thanks for all the replies, I am sorry I am not explaining it better! This is my first time using a forum on the “need help” end. My apologies

What happens here?

char currentPage = '0';
byte dz = 0;

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

  if (currentPage == '0')
  {
    dz = 6;
    currentPage = '3';
  }

  if (currentPage == '3')
  {
    Serial.print("DZ = ");
    Serial.println(dz);
    
  }
}

Did you check any where else you are changing the value of dz, I can see functions like button1(), what it is doing?

I would say write a small program like something mentioned by @Larry in #12, the add the extra lines you intended and see what you are doing wrong.

LarryD:
What happens here?

Thank you Larry, it does now print the correct value in another IF statement! I will look more into the byte declaration, so I can understand why... Thank you so much!!!!!!

If dz is a ‘char’ and dz = 6 the ASCII table shows this is the [Acknowledge] code.
The Arduino says “this is a ‘char’ so it is already ASCII, so I will sent a 6 to the serial monitor”.
Therefore, you were trying to Serial.print an ‘Acknowledge code’ to the serial monitor, which is a non printable character from the ASCII table.

If dz is a ‘byte’ or an ‘int’ and dz = 6.
This time when you Serial.print, the Arduino says “this is a number so I have to convert it to ASCII first, and I will send a 54 to the serial monitor” which is the printable number 6 from the ASCII table.

.

In general, what you are asking about is called variable "scope" .

Variables declared outside a function live for the lifetime of the sketch - for as long as the Aruino is not reset.

Variables declared inside functions, or inside any {block} are visible only inside that block and "live" only while that block is executing … unless they are declared static.

There's more to it, of course. Google "C++ variable scope".