Problems with debounce function lilypad

Hi everyone again,

This is my first time using arduino and I am having some trouble. I hope someone can help me with this. I am using lilypad arduino. I have one push button and several sensors that I want to calibrate when the button is pushed. By now, I have a code and I can’t make it work. Without pressing the button, the program is jumping to calibration. This is the program:

const int digitalPin3 = 3;
const int analogPin4 = 4;
const int analogPin3 = 3;
const int analogPin0 = 0;
const int analogPin1 = 1;
const int analogPin2 = 2;
int sensorShoulderL;
int sensorShoulderR;
int sensorBelly;
int sensorBack;
int sensorAss;

// inputs
int buttonPressed = HIGH;
boolean calibrationDone = false;
// setup
int buttonState;
int lastButtonState = HIGH;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
boolean firstTime = false;

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

pinMode(analogPin4, INPUT);
pinMode(analogPin3, INPUT);
pinMode(analogPin0, INPUT);
pinMode(analogPin1, INPUT);
pinMode(analogPin2, INPUT);
pinMode(digitalPin3, INPUT);
}
//loops is done all the time, looping
void loop (){
// calibrating until button is pressed
if (firstTime == false)
{
while(buttonPressed == false)
{
buttonPressed = debounceButton();
firstTime=true;
Serial.print("buttonPressed: ");
Serial.println(buttonPressed);
delay(100);
}
// Here calibration will start
}
}
/////////////////////////
boolean debounceButton()
{
Serial.print(“In debounce function”);
int buttonState = digitalRead(digitalPin3);
if (buttonState != lastButtonState) //if the button is still bouncing (changing the value)
{
lastDebounceTime = millis(); //save the time
}
if((millis() - lastDebounceTime) > debounceDelay) {
buttonPressed = buttonState;
Serial.print(“buttonState:”);
Serial.print(buttonState); //save the last button value
}
return true;
}

If anyone could take a look and tell me if they see some errors, it would be great. The button works with the debounce function that is in the examples, so it shouldn’t be a hardware problem.

Thank you so much in advance.
Regards

Button pressed is always going to be returned as true from that debounce routine, no matter what the state of the button. Because of:- return true;

Do you actually need to use debounce in this situation anyway as you are only going to do it once?

Post code using the # icon in future.

Add to this the small detail that debounceButton will be never reached because:

int buttonPressed = [glow]HIGH[/glow];
...
while(buttonPressed == [glow]false[/glow])

The whole code looks like you aren't completely sure why you want to use all those tests. Think you logic over again, best with a pen on paper.

Korman

Thank you for your answers! You were right, it is not necessary to use the debounce function. Now the button works perfect. I have another question, my program is supposed to read a sensor value when a button is pressed, then stored and whenever a given deviation value is exceeded, a vibration motor is triggered. What happens now is that the motors do not behave as they are supposed. I have tried different ways of declaring them but in some cases they don't work, others they seem to have an influence on the value read by the sensors. This is the last code I came up with:

const int devBack = 10;
const int devShoulderL = 3;

int offsetBack;
int offsetShoulderL;

int sensorBack;
int sensorShoulderL;

int digital8 = 8;
int digital5 = 5;


int buttonPin;
boolean calibrationDone = false;

void setup (){
  Serial.begin(9600);
  pinMode(digitalRead(8), OUTPUT);
  pinMode(digitalRead(5), OUTPUT);
  pinMode(digitalRead(3),INPUT);
  pinMode(digitalRead(13), OUTPUT);
}

void loop(){
  buttonPin = digitalRead(3);
  
  if (buttonPin == 1 && calibrationDone == false)
  {
    Serial.println("now it shoulde calibrate");
    calibrate();
    digitalWrite(13, HIGH);
  }
  if ( buttonPin ==1 || buttonPin == 0 && calibrationDone == true)
  checkBack();
  checkShoulderL();

}

///////////////
void checkShoulderL(){
  sensorShoulderL = analogRead(A4);
  if (abs(sensorShoulderL - offsetShoulderL) > devShoulderL){
    digitalWrite(5, LOW);
    Serial.print(sensorShoulderL, DEC);
    Serial.print("  correction: ");
    Serial.println(abs(sensorShoulderL-offsetShoulderL));
  }
  else
  {
    digitalWrite(5, HIGH);
    Serial.print(sensorShoulderL, DEC);
    Serial.print("  Good:  ");
    Serial.println(abs(sensorShoulderL - offsetShoulderL));
  }
}

///////////////

void checkBack(){
sensorBack = analogRead(A1);
  if (abs(sensorBack - offsetBack) >devBack){
    digitalWrite(digital8, HIGH);
    Serial.print(sensorBack, DEC);
    Serial.print("  correction: ");
    Serial.println(abs(sensorBack-offsetBack));
  }
  else
  {
    digitalWrite(digital8, LOW);
    Serial.print(sensorBack, DEC);
    Serial.print("  good: ");
    Serial.println(abs(sensorBack - offsetBack));
  }
}

    /////////////
  void calibrate(){
    Serial.println("in calibration function");
    offsetBack = analogRead(A1);
    Serial.print("Back: ");
    Serial.println(offsetBack);
    
    offsetShoulderL = analogRead(A4);
    Serial.print("ShoulderL: ");
    Serial.println(offsetShoulderL);
    
    calibrationDone = true;
  }

I think there is a problem with the way I am declaring the vibration motors. Any idea why this could be?

I really appreciate your replies!