Treat launcher: 1 digital pin keeps turning on and off.

Making a button activated treat launcher for my pupper using an Uno, 2 channel relay module, and a door lock actuator. Modified a simple code I found and am having issues making it work correctly. My intention is to press a button and have the first relay turn on to make the actuator extend, then off. Then have the second relay turn on to retract the actuator, then off. But it seems to just automatically, without button press, send voltage to pin 12 (extend) for 1 second, then turn off, then sends power to 12 again, and repeat continuously.

Still fairly new to arduino.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int pushPin =  12;      // the number of the push pin
const int pullPin =  11;      // the number of the pull pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(pushPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  digitalWrite (buttonPin, HIGH);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(pushPin, HIGH);
    delay(1000);
    digitalWrite(pushPin, LOW);
    delay(1000);
    digitalWrite(pullPin, HIGH);
    delay(100);
    digitalWrite(pullPin, LOW);
    
  } else {
    // turn LED off:
    digitalWrite(pushPin, LOW);
    digitalWrite(pullPin, LOW);
  }
}

How do you have the button wired?

In setup you set the buttonPin as an INPUT, then wrote a HIGH to it, which activates the internal pull-up resistor. Normally when someone does this, they have the button wired between the input pin and ground, so that the input is LOW when the button is pressed, and the internal pull-up resistor pulls the input HIGH when the button is released. But in your code, you are testing for a HIGH input to indicate the button is pressed, the exact opposite of how it would be functioning.

This code works perfectly for only for extending the actuator. If I change (buttonPin, HIGH) to LOW, it just keeps the relay on without a button press. I acquired the code and button wiring from an example originally made to turn on an LED.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int pushPin =  12;      // the number of the push pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(pushPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  digitalWrite (buttonPin, HIGH);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(pushPin, HIGH);
        
  } else {
    // turn LED off:
    digitalWrite(pushPin, LOW);
    
  }
}

This code works perfectly for only for extending the actuator. If I change (buttonPin, HIGH) to LOW, it just keeps the relay on without a button press. I acquired the code and button wiring from an example originally made to turn on an LED.

The wiring should give you a LOW input with the switch NOT pressed, and a HIGH input with the switch pressed, but there should be no reason for the digitalWrite to buttonPin in setup.

This code works perfectly for only for extending the actuator. If I change (buttonPin, HIGH) to LOW, it just keeps the relay on without a button press. I acquired the code and button wiring from an example originally made to turn on an LED.

Not really understanding your description - do you mean that the code works properly for turning the relay on and off when you have (buttinPin, HIGH), or that it only turns the relay on to extend the actuator, but never turns it off?

Also, do you know the exact model of the relay board that you have? Some relay boards are active low, which means that a LOW output turns the relay ON, and a HIGH turns it OFF.

Hi,
Do you have a DMM to check the wiring of the tactile switch?
To ensure you have the wiring correct, with the PULLUP activated try this.
Check your code for button press = LOW on the input pin.
The pin will be HIGH for button not pressed.
wiring (1).jpg

Tom… :slight_smile:

A lot of those relay boards are active low. Meaning with a low signal they close the NO contacts. You do not want a digitalWrite(buttonPin, HIGH) in your code as this is an input.

pinMode(buttonPin, INPUT);
  digitalWrite (buttonPin, HIGH);

Does the same thing as:

pinMode(buttonPin, INPUT_PULLUP);