programming

profilereda123

I am working on a project using Arduino. I am using tilt switch to count steps taking. My problem is the code I am using increment without stoping. What I want is to increment only once when it is low and increment only once when it is high. the code is below



#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


const int tiltSensor = 8;

int steps = 0;

int switchState = HIGH;

int prevSwitchState = LOW;


void setup() {

  lcd.begin(16,2);

Serial.begin(9600);

  pinMode(tiltSensor, INPUT);


  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("Ready to Walk?");

  lcd.setCursor(0, 1);

  lcd.print("Start Walking!");

}


void loop() {

 switchState = digitalRead(tiltSensor);


  if (switchState != prevSwitchState) {


    if (switchState == LOW) {

      steps = steps + 1;

      lcd.clear();

      lcd.setCursor(0, 0);

      lcd.print("Steps:");

      lcd.setCursor(0, 1);

      lcd.print(steps);

      Serial.print(steps); 

      

     

    

    }


    

  }

  prevSwitchState = switchState;

}

   

    • 8 years ago
    • 3
    Answer(0)