Code not working as planned, please help debug

So a quick overview of what the code is supposed to do

  • upon button push it should run code
  • output voltage to pin 13
  • hold for 5000ms
  • output voltage to pin 13
  • hold for 5000ms
  • stop

What its actually doing

  • starting as soon as I upload code
  • outputs voltage to pin 13
  • outputs voltage to pin 12
  • delays 5000ms
  • turns off pin 12
  • delays 5000ms
  • turns on pin 12
    and just keeps looping

Not sure why the button press isnt working, why pin 13 is always energized, and why it keeps looping.

Thanks for any help!

const int button = 2;
const int handpiece = 13;
const int valve = 12;
const int delay1 = 5000; //time to leave the handpiece on
const int delay2 = 5000; //time to leave the valve on
int buttonstate = 0;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize
    pinMode(button,INPUT);
    pinMode(handpiece, OUTPUT);     
    pinMode(valve, OUTPUT);
    Serial.begin(9600);
    digitalWrite(button, HIGH); //enable internal pull-up resistor
    digitalWrite(valve, LOW);    // turn the valve off 
    digitalWrite(handpiece, LOW);    // turn the handpiece off 
  }

void loop() {
  buttonstate = digitalRead(button);
  if (buttonstate == HIGH) {
  digitalWrite(handpiece, HIGH);   // turn the handpiece on 
  delay(delay1);               // time to leave handpiece on
  //int tach = analogRead(A1);
  //float voltage = tach * (5.0 / 1023.0);
  //Serial.println(voltage);
  digitalWrite(valve, HIGH);   // turn the valve on 
  delay(delay2);               // time to leave valve on
  digitalWrite(valve, LOW);    // turn the valve off 
  digitalWrite(handpiece, LOW);    // turn the handpiece off
} 
  else{
  digitalWrite(valve, LOW);    // turn the valve off 
  digitalWrite(handpiece, LOW);    // turn the handpiece off
}
}
  • upon button push it should run code

I'm not even going to bother looking at the code. I'm 99.99% certain that you have not got the switch wired correctly. A wiring diagram MUST be provided.

The reason your code immediately starts is because of this line:

digitalWrite(button, HIGH); //enable internal pull-up resistor

You activivate the internal pull-up when you declare the pin an input like this:

pinMode(button,INPUT_PULLUP);

This is documented here: InputPullups

The rest of your code isn't the cleanest way to achieve your functional goal but it looks like it should work.

PaulS:
I'm not even going to bother looking at the code. I'm 99.99% certain that you have not got the switch wired correctly. A wiring diagram MUST be provided.

I don't even have the switch hooked up yet

I don't even have the switch hooked up yet

But you are trying to read from the pin the switch is (not) connected to. And, the code isn't doing what you want. What a surprise.

Spoolx:
So a quick overview of what the code is supposed to do

  • upon button push it should run code
  • output voltage to pin 13
  • hold for 5000ms
  • output voltage to pin 13
  • hold for 5000ms
  • stop

This is a poor description.

It is exactly the same as

  • upon button push it should run code
  • output voltage to pin 13
  • hold for 10000ms
  • stop

and I have no idea what "stop" is supposed to mean. If you want the pin to go LOW you should say so.

If you only want this sequence to happen once and never again you should put the code in setup() rather than in loop(). There is no problem having loop empty - but it must be there

void loop() {
}

...R