Push Button for Step counter

Hi Everyone,

I’m using potentiometers to simulate an accelerometer measuring the movements of a user. So far with my code I have been able to track the number of steps that the user is taking and need to convert this into a distance. I must use a push button to change the stride length of the user, using 6 preset values. However when I have come to simulating my new code that includes the push button the number of button presses always stays at 1 no matter how many times it has been pressed. Can anyone see the problem with my code that is causing the push button presses to not be registered.

Thanks in advance.

Accel_Distance_calc.ino (5.04 KB)

More members will see your code if you post your code as described in the forum guidelines.

OPs code:

// Make sure these two variables are correct for your setup
int scale = 3; // 3 (±3g) for ADXL337, 200 (±200g) for ADXL377
  
void setup()
{
    const int buttonPin = 8;
    // initialize the button pin as an input:
    pinMode(buttonPin, INPUT);
    // Initialize serial communication at 115200 baud
    Serial.begin(9600);
}
 
// Read, scale, and print accelerometer data
void loop()
{

float threshhold=100;

int steps,flag=0;

 
  // Get raw accelerometer data for each axis
 
  int rawX = analogRead(A0);
  int rawY = analogRead(A1);
  int rawZ = analogRead(A2);

  delay(500); // Minimum delay of 0.5 milliseconds between sensor reads (500 Hz)

    // Get raw accelerometer data for each axis
 
  int rawX1 = analogRead(A0);
  int rawY1 = analogRead(A1);
  int rawZ1 = analogRead(A2);

  delay(500); // Minimum delay of 0.5 milliseconds between sensor reads (500 Hz)

    // Get raw accelerometer data for each axis
 
  int rawX2 = analogRead(A0);
  int rawY2 = analogRead(A1);
  int rawZ2 = analogRead(A2);
  
  delay(500); // Minimum delay of 0.5 milliseconds between sensor reads (500 Hz)

    // Get raw accelerometer data for each axis
 
  int rawX3 = analogRead(A0);
  int rawY3 = analogRead(A1);
  int rawZ3 = analogRead(A2);

  delay(500); // Minimum delay of 0.5 milliseconds between sensor reads (500 Hz)

    // Get raw accelerometer data for each axis
 
  int rawX4= analogRead(A0);
  int rawY4 = analogRead(A1);
  int rawZ4 = analogRead(A2);

  delay(500); // Minimum delay of 0.5 milliseconds between sensor reads (500 Hz)

  // Find average value
 int avgX = {(rawX + rawX1 + rawX2 + rawX3 + rawX4)/5};
 int avgY = {(rawY + rawY1 + rawY2 + rawY3 + rawY4)/5};
 int avgZ = {(rawZ + rawZ1 + rawZ2 + rawZ3 + rawZ4)/5};

  // Print out avgX value
  Serial.print("avgX; "); Serial.println(avgX);
  Serial.print("avgY; "); Serial.println(avgY);
  Serial.print("avgZ; "); Serial.println(avgZ);
   
   delay(500); // Minimum delay of 0.5 milliseconds between sensor reads (500 Hz)

   // Find total average
   int totalAVG = {avgX + avgY + avgZ};

    Serial.print("totalAVG; "); Serial.println(totalAVG);

    
   delay(500); // Minimum delay of 0.5 milliseconds between sensor reads (500 Hz)

  // Print steps


//cal steps 
if (totalAVG >threshhold && flag==0)
{
  steps=steps+1;
  flag=1;
 
}
 else if (totalAVG > threshhold && flag==1)
{
//do nothing 
}
  if (totalAVG <threshhold  && flag==1)
  {flag=0;}
  Serial.println('\n');
  Serial.print("steps=");
  Serial.println(steps);


// Calculate distance
{
 
// this constant won't change:
const int  buttonPin = 8;    // the pin that the pushbutton is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

  buttonState = digitalRead(buttonPin);  // read the pushbutton input pin:

  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH)
    {
      buttonPushCounter++;
      Serial.println("on");     // if the current state is HIGH then the button went from off to on:
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else
    {
      Serial.println("off");        // if the current state is LOW then the button went from on to off
    }
  }
    if (buttonPushCounter == 7)
  {
    buttonPushCounter == 1;
  }
  lastButtonState = buttonState;  // save the current state as the last state for next time through the loop

void  SlengthA();
void SlengthB();
void SlengthC();
void SlengthD();
void SlengthE();
void SlengthF();
void reset();


void SlenghtA();
{

if (buttonPushCounter == 1)
{
  float SlengthA = 0.4;
  float  DistanceA = SlengthA * steps;
    Serial.print("DistanceA= m");
  Serial.println(DistanceA);
}
}

void SlenghtB();
{

if (buttonPushCounter == 2)
{
  float SlengthB = 0.5;
  float  DistanceB = SlengthB * steps;
    Serial.print("DistanceB= m");
  Serial.println(DistanceB);
}
}

void SlenghtC();
{

if (buttonPushCounter == 3)
{
  float SlengthC = 0.6;
  float  DistanceC = SlengthC * steps;
    Serial.print("DistanceC= m");
  Serial.println(DistanceC);
}
}

void SlenghtD();
{
  
if (buttonPushCounter == 4)
{
  float SlengthD = 0.7;
  float  DistanceD = SlengthD * steps;
    Serial.print("DistanceD= m");
  Serial.println(DistanceD);
}
}

void SlenghtE();
{
 
if (buttonPushCounter == 5)
{
  float SlengthE = 0.8;
  float  DistanceE = SlengthE * steps;
    Serial.print("DistanceE= m");
  Serial.println(DistanceE);
}
}

void SlenghtF();
{
  
if (buttonPushCounter == 6)
{
  float SlengthF = 0.9;
  float  DistanceF = SlengthF * steps;
    Serial.print("DistanceF= m");
  Serial.println(DistanceF);
}
}

void reset();
{

  if (buttonPushCounter == 7)
  {
    buttonPushCounter == 1;
  }
}

delay(500); // Minimum delay of 0.5 milliseconds between sensor reads (500 Hz)
}
}

Without looking at anything else in your code: You initialize your button pin with:

pinMode(buttonPin, INPUT);

and look for a HIGH level in your code to indicate a press. Do you have a pull-down resistor to control the pin when the switch is open? If not, the pin may be drifting to a high level producing odd results.

It’s much more common to wire the switch to ground the pin and so set up the pin with:

pinMode(buttonPin, INPUT_PULLUP);

and then register a LOW as a press.

For your need you should also look into change or state vs acting on a level.