latching button

i am trying to write a program with the inputs up down and stop to control two outputs.
i want the up output to come on and stay on until the stop input is pressed and the same for the down input. also only one output can be active at a time. i would like to have it have a delay between say up, stop, and down. i would also like to add and encoder to turn off outputs at set limit. here is what i have so far.

int down = 6; //the pin where we connect the button
int mtrdown = 13; //the pin we connect the LED
int up = 5; //the pin where we connect the button
int mtrup = 12; //the pin we connect the LED
int stopsw = 4; //the pin where we connect the button
long debounce = 200;
boolean latch = true; // if true then the relay remains closed

void setup() {
pinMode(down, INPUT_PULLUP); //set the button pin as INPUT
pinMode(mtrdown, OUTPUT); //set the LED pin as OUTPUT
pinMode(up, INPUT_PULLUP); //set the button pin as INPUT
pinMode(mtrup, OUTPUT); //set the LED pin as OUTPUT
pinMode(stopsw, INPUT_PULLUP); //set the button pin as INPUT

}

void loop() {

int stateButton1 = digitalRead(down); //read the state of the button
if (stateButton1 == LOW) //if is pressed
{
digitalWrite(mtrup, LOW);
digitalWrite(mtrdown, HIGH);
}

int stateButton2 = digitalRead(up); //read the state of the button
if (stateButton2 == LOW) //if is pressed
{
digitalWrite(mtrup, HIGH); //write 1 or HIGH to led pin
digitalWrite(mtrdown, LOW);
}

int stateButton3 = digitalRead(stopsw); //read the state of the button
if (stateButton3 == LOW); //if is pressed
{
digitalWrite(mtrup, LOW); //write 1 or HIGH to led pin
digitalWrite(mtrdown, LOW);
}

}

thanks

Please use code tags when posting.

i want the up output to come on and stay on until the stop input is pressed

For this sort of application, a state machine works well. You will have variables that keep track of output states and change when certain inputs are detected.

Google "arduino state machine" for tutorials.

Andrew530:
i am trying to write a program with the inputs up down and stop to control two outputs.
i want the up output to come on and stay on until the stop input is pressed and the same for the down input. also only one output can be active at a time. i would like to have it have a delay between say up, stop, and down. i would also like to add and encoder to turn off outputs at set limit. here is what i have so far.

boolean pauseActive = false;
uint32_t pauseStart;
enum Dir { STOPPED, UP, DOWN};

Dir dir = STOPPED;

loop() {
  if the stop, up, or down button is pressed, then
    stop the elevator (if necessary)
    dir = STOPPED, UP, or DOWN (depending on the button)
    pauseActive = true;
    pauseSTart = millis();
  end

  if pauseActive AND the pause has timed out now, then
      pauseActive = false;

      if dir = UP, then start moving up
      else if dir = DOWN, then start moving down
      else, no nothing
  end if
}

i have this code. it will latch on the first input that is HIGH but the rest are ignored until reset

int buttonPin0 = 4;
int buttonPin1 = 5;
int buttonPin2 = 6;
int mtrUp = 12;
int mtrDown = 13;
int buttonStatus1 = LOW;
int buttonStatus2 = LOW;
int buttonStatus0 = LOW;

void setup() {
pinMode(mtrUp, OUTPUT);
pinMode(mtrDown, OUTPUT);
pinMode(buttonPin0, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
buttonStatus1 = digitalRead(buttonPin1);
buttonStatus2 = digitalRead(buttonPin2);
buttonStatus0 = digitalRead(buttonPin0);

if (buttonStatus1 == HIGH)
{
digitalWrite(mtrUp, HIGH);
digitalWrite(mtrDown, LOW);

if (buttonStatus1 == HIGH)
{
digitalWrite(mtrUp, LOW);
digitalWrite(mtrDown, HIGH);

if (buttonStatus0 == HIGH)
{
digitalWrite(mtrUp, LOW);
digitalWrite(mtrDown, LOW);

}
}
}
}

Please read the first line in the first reply. Please note how others post code.

Read the stickies at the top of each forum section; one or more of them will tell you how to post code.

 if (buttonStatus1 == HIGH)
  {
    digitalWrite(mtrUp, HIGH);
    digitalWrite(mtrDown, LOW);
    if (buttonStatus1 == HIGH)
    {
      digitalWrite(mtrUp, LOW);
      digitalWrite(mtrDown, HIGH);

You set 2 outputs if buttonStatus1 is HIGH then test whether buttonStatus1 is HIGH again, which it will be If it is HIGH then you change the same 2 outputs but to different values thus undoing what you previously did

Is that what you meant to do ?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

that was the problem. i now have this but i can't get it to turn off by making pin 4 high.

int buttonPin0 = 4;
int buttonPin1 = 5;
int buttonPin2 = 6;
int mtrUp = 12;
int mtrDown = 13;
int buttonStatus1 = LOW;
int buttonStatus2 = LOW;
int buttonStatus0 = HIGH;


void setup() {
  pinMode(mtrUp, OUTPUT);
  pinMode(mtrDown, OUTPUT);
  pinMode(buttonPin0, INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}
void loop() {
  buttonStatus1 = digitalRead(buttonPin1);
  buttonStatus2 = digitalRead(buttonPin2);
  buttonStatus0 = digitalRead(buttonPin0);

  if (buttonStatus1 == HIGH)
  {
    digitalWrite(mtrUp, HIGH);
    digitalWrite(mtrDown, LOW);

    if (buttonStatus2 == HIGH)
    {
      digitalWrite(mtrUp, LOW);
      digitalWrite(mtrDown, HIGH);

      if (buttonStatus0 == HIGH)
      {
        digitalWrite(mtrUp, LOW);
        digitalWrite(mtrDown, LOW);


      }
    }
  }
}

The reason it won't turn off is because your if statements are nested (they are all inside each other). So to turn it off you would have to hold down all three buttons at once.

int buttonPin0 = 4;
int buttonPin1 = 5;
int buttonPin2 = 6;
int mtrUp = 12;
int mtrDown = 13;
int buttonStatus1 = LOW;
int buttonStatus2 = LOW;
int buttonStatus0 = HIGH;


void setup() {
    pinMode(mtrUp, OUTPUT);
    pinMode(mtrDown, OUTPUT);
    pinMode(buttonPin0, INPUT);
    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);
}
void loop() {
    buttonStatus1 = digitalRead(buttonPin1);
    buttonStatus2 = digitalRead(buttonPin2);
    buttonStatus0 = digitalRead(buttonPin0);

    if (buttonStatus1 == HIGH)
    {
        digitalWrite(mtrUp, HIGH);
        digitalWrite(mtrDown, LOW);
    }
    if (buttonStatus2 == HIGH)
    {
        digitalWrite(mtrUp, LOW);
        digitalWrite(mtrDown, HIGH);
    }
    if (buttonStatus0 == HIGH)
    {
        digitalWrite(mtrUp, LOW);
        digitalWrite(mtrDown, LOW);
    }
}

I think this is probably what you mean to do.

Is there a way I can have it delay between switching motor direction. Say the motor is going up and the down button is pressed I would like it to stop the motor for a few seconds then go down

Thanks

Is there a way I can have it delay between switching motor direction.

Of course there is

Have you got the original version working yet ?

Almost I was going to try what the previous guy posted. It seems to make sense

Get the base version working first before changing it.

Note, that adding features is likely to mean a considerable amount of changes that could have been avoided if the code were written from scratch to meet the revised specification.

Could I have it write one pin low. Then a delay. Then write the opposite pin high.

Andrew530:
Could I have it write one pin low. Then a delay. Then write the opposite pin high.

See post #12

i have everything working pretty good. i added and encoder as a limit switch. the only problem i have is when i first power it up both outputs pin 11 and 12 or on.

#include <RotaryEncoder.h>

RotaryEncoder encoder(2, 3);

// These constants won't change:
const int encoderPin = (2, 3);      
boolean toggle = true;
int buttonPin0 = 4;
int buttonPin1 = 5;
int buttonPin2 = 6;
int mtrUp = 12;
int mtrDown = 11;
int buttonStatus1 = LOW;
int buttonStatus2 = LOW;
int buttonStatus0 = LOW;


// These variables will change:
int encoderMin = -10;  // minimum sensor value
int encoderMax = 10;   //maximum sensor value  
int encoderValue = 0;        
int press = 0;


void setup()
{
  Serial.begin(57600);
  Serial.println("SimplePollRotator example for the RotaryEncoder library.");

  // Read the current position of the encoder and print out when changed.

  // set the LED pins as outputs and the switch pin as input:
  pinMode(mtrUp, OUTPUT);
  pinMode(mtrDown, OUTPUT);
  pinMode(buttonPin0, INPUT_PULLUP);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}

void loop()
{

  buttonStatus1 = digitalRead(buttonPin1);
  buttonStatus2 = digitalRead(buttonPin2);
  buttonStatus0 = digitalRead(buttonPin0);

  if (buttonStatus1 == LOW)
  {
    digitalWrite(mtrDown, HIGH);
    delay(1000);
    digitalWrite(mtrUp, LOW);

  }
  if (buttonStatus2 == LOW)
  {
    digitalWrite(mtrUp, HIGH);
    delay(1000);
    digitalWrite(mtrDown, LOW);
  }
  if (buttonStatus0 == LOW)
  {
    digitalWrite(mtrUp, HIGH);
    digitalWrite(mtrDown, HIGH);
  }

  static int pos = 0;
  encoder.tick();

  int newPos = encoder.getPosition();

  if (pos != newPos)
  {
    Serial.print(newPos);
    Serial.println();
    pos = newPos;
  }

  if (newPos <= encoderMin)
  {
    digitalWrite(mtrDown, HIGH);
  }

  if (newPos >= encoderMax)
  {
    digitalWrite(mtrUp, HIGH);
  }

}

when i first power it up both outputs pin 11 and 12 or on.

Why not set them to the required state in setup() ?

I have tried a couple ways but can’t seem to get it to do that. What do you suggest?

Thanks

 pinMode(mtrUp, OUTPUT);
  digitalWrite(mtrUp, LOW);  //or HIGH if that is what you want

Ok I’ll try that

Thanks