Increase variable with each button press more cleanly?

Hey all. Trying to increase a variable when a button is pressed;

I have:

 {
    if (State <= 0 && State <= 20)
      xDis=30;
    }
   {
    if (State >= 0)
      yDis=1;
    }
   {
    if (State >= 1)
      yDis=2;
    }
     {
    if (State >= 2)
      yDis=3;
    }

But this seems tedious, and I want the yDis variable to go from 0-10 in 0.5 increments. Is there a better, simplier way to write this instead of 20 if statements?

Full code:

#include<Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//define State Var (for button)
int State;
//def xDis and yDis
int xDis;
float yDis; //needs to be float, since it goes up Y.YYft increments

const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
 
int minVal=265;
int maxVal=402;
 
double x;
double y;
double z;
 
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(57600);
//button setup
{
    Serial.begin(57600);
  pinMode(4,INPUT);
}
//new
  Serial.begin(57600);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Wire.setClock(400000);
  Wire.begin();
  delay(250);
  Wire.beginTransmission(0x68); 
  Wire.write(0x6B);
  Wire.write(0x00);
  Wire.endTransmission();
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
}}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
 
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI)-90;
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
 
Serial.print("AngleX= ");
Serial.println(x);
 
Serial.print("AngleY= ");
Serial.println(y);
 
Serial.print("AngleZ= ");
Serial.println(z);
Serial.println("-----------------------------------------");
// add delay if needed
delay(0);
//button
{
    int ButtonPress = digitalRead(4);
    if (ButtonPress==0)
    {
      State=State+1;
      Serial.println(State);
      delay(50);
    if (State  >= 20)
{
      State=0;
}
    }
}

  // Determine Distance

  //X=30ft and y increments
   {
    if (State <= 0 && State <= 20)
      xDis=30;
    }
   {
    if (State >= 0)
      yDis=0;
    }
   {
    if (State >= 1)
      yDis=0.5;
    }
     {
    if (State >= 2)
      yDis=1;
    }
     {
    if (State >= 3)
      yDis=1.5;
    }

 //display items and objectives (angles)
 display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  // Display distance text
  display.print("Distance:(");
  display.print(xDis);
  display.print(",");
   display.print(yDis);
  display.print(")ft");
  display.setCursor(0, 6);
  display.println("=-=---------------=-=");
  // Display Current Angle
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 16);
    display.println("Current Angle:");
  // Display Target Angle
  display.setCursor(0, 28);
  display.setTextSize(2);
  display.println(y);

  //
  float TargetAngle = 4.95;
  display.setCursor(0, 56);
  display.setTextSize(1);
  display.print("Target Angle:");
  display.print(TargetAngle);
  display.display(); 
}

Wait for push and release, then just increment pushCount++. Multiply push count by scale.

Couldn't agree more.
Post your code.

You posted no code that has anything to do with button pressing.

Please post complete sketches that you are working on, and say what they do that they should not, or don't do that they should.

Without a complete sketch it is hard to make a recommendation about how to fix the part you did share.

a7

I have posted it, sorry.

Ideally I could hold down the button, since pressing it 20 times may be inconvenient - the current sketch allows for that, but still has the tedious code.

Don't you hate it when that happens?
The IDE's auto format is highly recommended before posting.

I am not sure what you are referring to. I am an absoulute beginner and this is my first sketch. I know its probably not pretty, but again, I have no idea what the standard is.

Please do not open more than one topic regarding the same subject.

The wandering braces. (These are braces { } )

@hatcollector since State is an integer, and yDis is a float, then all you need is

  yDis = 0.5 * State;

as State goes from 0 to 20, yDis will follow along in 0.5 steps 0 to 10.0.

a7

Like i said in post #2, multiply push count by scale! LOL

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