Help with pushbutton

Hi ,

I have the following code for use with a pushbutton but its not working after I added the millis function to set the timer. Could you please have a look at my code and see if you can tell me whats wrong. The code uploads fine but when I press the pushbutton nothing happens. Before I added the millis function the servo was turning ok when I pushed the button.

#include <Servo.h>

Servo myservo;
int ledPin = 13; // choose the pin for the LED
int toiletPin = 6; // choose the input pin (for a pushbutton)
//int bathPin = 5;
int val;
int val2 = 0; //used to read toilet button pin
int toiletTurn = 115; // amount to turn toilet valve
int tOff = 5; // off position of servo for toilet
unsigned long start;
unsigned long now;
unsigned long ontime;
unsigned long howlong = 5000; //how long to keep valve open; time in milliseconds
boolean activet = false; //used to activate counter routine

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(toiletPin, INPUT); // declare pushbutton as input
myservo.attach(5);
myservo.write(5);
delay(500);
}

void loop(){

val2 = digitalRead(toiletPin);
//toilet shattaf code for push button
if (val2 == HIGH) { // check if the input is HIGH (button released)
//digitalWrite(ledPin, LOW); // turn LED OFF
} else if (val2 == LOW && activet == true){
myservo.write(tOff);
delay(1000);
activet = false;
} else {
//digitalWrite(ledPin, HIGH); // turn LED ON
myservo.write(toiletTurn);
start = millis();
activet = true;
}
if(activet== true){
now = millis();
ontime = start + howlong;
if(now > ontime){
myservo.write(tOff);
delay(1000);
activet = false;
}
}

}

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(toiletPin, INPUT);    // declare pushbutton as input
  myservo.attach(5);
  myservo.write(5);
  delay(500);
}

You should be setting the position before attaching the servo.

The commented out code should be deleted. If you don't want it to happen, we don't want to see it. Put each { and } on its own line, and use Tools + Auto Format to make your code easier to read.

You are not using the internal pullup resistor, so you must have external pullup or pulldown resistors with the switches. Do you? How are the switches wired?

You are not causing action only at the transition from released to pressed, or pressed to released. Typically, this is what people want to do.

To do this, of course, you need to compare the current state to the previous state, which means that you need a place to store the previous state and you actually have to save the previous state.

    ontime = start + howlong;

Wrong. The OFF time is based on the start time and how long the thing should be on.

    delay(1000);

WTF? The whole idea is to get rid of delay.

The code uploads fine but when I press the pushbutton nothing happens.

The two actions have absolutely nothing to do with each other. Crappy code will upload as well as good code. The ability to upload code says nothing about the correctness of the logic of the code.

hi,

thanks for your help. problem solved. The last button state code needed to be added. all is working fine now.