Yet another button push question

Hello All,

I know, I know, I've looked for an answer to this question but I haven't found any satisfactory answers; maybe I'm not searching with the correct terms.
In the void loop I check to see if my button is pressed (Trig_1 == LOW) however if I hold the button down, the button is LOW every time through the loop. How do I make sure that holding down the button will only cause one cycle through the void loop until the button is released and pressed again?

Thank you,
Muggs

//#include <Stepper.h>
/*
  Muggs
*/

// Define Constants
const int X_Home = 6; // Set X-Home limit switch to pin 6
const int Y_Home = 7; // Set Y-Home limit switch to pin 7
const int Trg_1 = 8; // Set Trigger_1 switch to pin 8
const int Trg_2 = 9; // Set Trigger_2 switch to pin 9
const int LASER = 10; // Set LASER to pin 10
const int LASER_Cut_Power = 5; // Set LASER Cutting Power
const int Home_Speed = 2; // Set speed at which motor moves to the Home position; Higher # = slower speed
const int Move_Speed = 2; // Set speed at which motor moves to position; Higher # = slower speed
const int Cut_Speed = 15; // Set speed at which LASER makes cut; Higher # = slower speed
const int Cut_Length = 13; // Set Length of LASER cut
const int Cut_Gap = 4; // Gap between cuts, set in MM

// Connections to A4988
const int X_stepPin = 3; // X Step
const int X_dirPin = 2;  // X Direction
const int Y_stepPin = 5; // Y Step
const int Y_dirPin = 4;  // Y Direction

// Motor steps per rotation
const int STEPS_PER_REV = 200;
const int MM = STEPS_PER_REV / 40;

// Define Variables
boolean hasRun = false;
int sensorValue_X = digitalRead(X_Home);
int sensorValue_Y = digitalRead(Y_Home);
int Plus_dir = HIGH;
int Minus_dir = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(X_stepPin, OUTPUT);
  pinMode(X_dirPin, OUTPUT);
  pinMode(Y_stepPin, OUTPUT);
  pinMode(Y_dirPin, OUTPUT);
  pinMode(X_Home, INPUT_PULLUP);
  pinMode(Y_Home, INPUT_PULLUP);
  pinMode(Trg_1, INPUT_PULLUP);
  pinMode(Trg_2, INPUT_PULLUP);
  pinMode(LASER, OUTPUT);

  hasRun = false;
  Home();
}

void loop() {
  Serial.println(sensorValue_X);
  Serial.println(sensorValue_Y);

  if (digitalRead(Trg_1) == LOW) {
    Move_Y(Plus_dir, 15, Move_Speed); // Move to position
    Move_X(Plus_dir, 11, Move_Speed);// Move to position
    Cut_1();// Turn on LASER and cut based on Trg_1 input
  }

  if (digitalRead(Trg_2) == LOW) {
    Move_Y(Plus_dir, 132, Move_Speed); // Move to position
    Move_X(Plus_dir, 11, Move_Speed);// Move to position
    Cut_2();// Turn on LASER and cut based on Trg_2 input
  }
}
void Home() {

  while (digitalRead(X_Home) != LOW)  // Move until you hit the switch
  {
    StepTowards_X_Switch();
  }
  for (int xi = 0; xi < 20; xi++)  // back off 20 steps
  {
    StepAwayFrom_X_Switch();
  }
  while (digitalRead(X_Home) != LOW) // Approach the switch slowly until it closes
  {
    StepTowards_X_Switch();
    delay(10);
  }
  while (digitalRead(Y_Home) != LOW)
  {
    StepTowards_Y_Switch();
  }
  for (int yi = 0; yi < 20; yi++)
  {
    StepAwayFrom_Y_Switch();
  }
  while (digitalRead(Y_Home) != LOW)
  {
    StepTowards_Y_Switch();
    delay(10);
  }
  hasRun = true;
}

void StepTowards_X_Switch() {
  digitalWrite(X_dirPin, Minus_dir);
  digitalWrite(X_stepPin, HIGH);
  delay(Home_Speed);
  digitalWrite(X_stepPin, LOW);
  delay(Home_Speed);
}

void StepTowards_Y_Switch() {
  digitalWrite(Y_dirPin, Minus_dir);
  digitalWrite(Y_stepPin, HIGH);
  delay(Home_Speed);
  digitalWrite(Y_stepPin, LOW);
  delay(Home_Speed);
}

void StepAwayFrom_X_Switch() {
  digitalWrite(X_dirPin, Plus_dir);
  digitalWrite(X_stepPin, HIGH);
  delay(Home_Speed);
  digitalWrite(X_stepPin, LOW);
  delay(Home_Speed);
}

void StepAwayFrom_Y_Switch() {
  digitalWrite(Y_dirPin, Plus_dir);
  digitalWrite(Y_stepPin, HIGH);
  delay(Home_Speed);
  digitalWrite(Y_stepPin, LOW);
  delay(Home_Speed);
}

void Move_X(int dir, int x, int speed) {
  int t = 0;
  while (t < x) {
    for (int i = 0; i < MM; i++) {
      digitalWrite(X_dirPin, dir);
      digitalWrite(X_stepPin, HIGH);
      delay(speed);
      digitalWrite(X_stepPin, LOW);
      delay(speed);
    }
    t++;
  }
}
void Move_Y(int dir, int x, int speed) {
  int t = 0;
  while (t < x) {
    for (int i = 0; i < MM; i++) {
      digitalWrite(Y_dirPin, dir);
      digitalWrite(Y_stepPin, HIGH);
      delay(speed);
      digitalWrite(Y_stepPin, LOW);
      delay(speed);
    }
    t++;
  }
}
void Cut_1() {
  analogWrite(LASER, LASER_Cut_Power); // Turn On LASER
  delay(500);
  Move_Y(Minus_dir, Cut_Length, Cut_Speed); // Move to Cut_1 Start position
  Move_X(Plus_dir, Cut_Gap, Move_Speed);
  Move_Y(Plus_dir, Cut_Length, Cut_Speed);
  analogWrite(LASER, 0); // Turn Off LASER
  Home();
}

void Cut_2() {
  analogWrite(LASER, LASER_Cut_Power); // Turn On LASER
  delay(500);
  Move_Y(Plus_dir, Cut_Length, Cut_Speed); // Move to Cut_2 Start position
  Move_X(Plus_dir, Cut_Gap, Move_Speed);
  Move_Y(Minus_dir, Cut_Length, Cut_Speed);
  analogWrite(LASER, 0); // Turn Off LASER
  Home();
}

aarg,

Thank you that was helpful.
So now when I press the button momentarily, the laser moves twice; if I hold the button down, the laser moves only once, but then moves again when I release it. Can't quit figure out why.

/*
  Muggs
*/

// Define Constants
const int X_Home = 6; // Set X-Home limit switch to pin 6
const int Y_Home = 7; // Set Y-Home limit switch to pin 7
const int Trg_1 = 8; // Set Trigger_1 switch to pin 8
const int Trg_2 = 9; // Set Trigger_2 switch to pin 9
const int LASER = 10; // Set LASER to pin 10
const int LASER_Cut_Power = 5; // Set LASER Cutting Power
const int Home_Speed = 2; // Set speed at which motor moves to the Home position; Higher # = slower speed
const int Move_Speed = 2; // Set speed at which motor moves to position; Higher # = slower speed
const int Cut_Speed = 15; // Set speed at which LASER makes cut; Higher # = slower speed
const int Cut_Length = 13; // Set Length of LASER cut
const int Cut_Gap = 4; // Gap between cuts, set in MM

// Connections to A4988
const int X_stepPin = 3; // X Step
const int X_dirPin = 2;  // X Direction
const int Y_stepPin = 5; // Y Step
const int Y_dirPin = 4;  // Y Direction

// Motor steps per rotation
const int STEPS_PER_REV = 200;
const int MM = STEPS_PER_REV / 40;

// Define Variables
boolean hasRun = false;
int sensorValue_X = digitalRead(X_Home);
int sensorValue_Y = digitalRead(Y_Home);
int Plus_dir = HIGH;
int Minus_dir = LOW;
int Trg_1_State;
int previousTrg_1_State = HIGH;
int Trg_2_State;
int previousTrg_2_State = HIGH;

void setup() {
  Serial.begin(9600);
  pinMode(X_stepPin, OUTPUT);
  pinMode(X_dirPin, OUTPUT);
  pinMode(Y_stepPin, OUTPUT);
  pinMode(Y_dirPin, OUTPUT);
  pinMode(X_Home, INPUT_PULLUP);
  pinMode(Y_Home, INPUT_PULLUP);
  pinMode(Trg_1, INPUT_PULLUP);
  pinMode(Trg_2, INPUT_PULLUP);
  pinMode(LASER, OUTPUT);

  hasRun = false;
  Home();
}

void loop() {
  Serial.println(sensorValue_X);
  Serial.println(sensorValue_Y);

  Trg_1_State = digitalRead(Trg_1);  // read the Trig_1 button state input pin:
  Trg_2_State = digitalRead(Trg_2);  // read the Trig_1 button state input pin:

  if (Trg_1_State != previousTrg_1_State) {
    if (Trg_1_State == LOW)
      delay(50);
    Move_Y(Plus_dir, 15, Move_Speed); // Move to position
    Move_X(Plus_dir, 11, Move_Speed);// Move to position
    Cut_1();// Turn on LASER and cut based on Trg_1 input
  }
  previousTrg_1_State = Trg_1_State;
  
  if (digitalRead(Trg_2) == LOW) {
    Move_Y(Plus_dir, 132, Move_Speed); // Move to position
    Move_X(Plus_dir, 11, Move_Speed);// Move to position
    Cut_2();// Turn on LASER and cut based on Trg_2 input
  }
}
void Home() {

  // Move until you hit the switch
  while (digitalRead(X_Home) != LOW)
  {
    StepTowards_X_Switch();
  }
  // back off 20 steps
  for (int xi = 0; xi < 20; xi++)
  {
    StepAwayFrom_X_Switch();
  }
  // Approach the switch slowly until it closes
  while (digitalRead(X_Home) != LOW)
  {
    StepTowards_X_Switch();
    delay(10);
  }

  while (digitalRead(Y_Home) != LOW)
  {
    StepTowards_Y_Switch();
  }
  // back off 20 steps
  for (int yi = 0; yi < 20; yi++)
  {
    StepAwayFrom_Y_Switch();
  }
  // Approach the switch slowly until it closes
  while (digitalRead(Y_Home) != LOW)
  {
    StepTowards_Y_Switch();
    delay(10);
  }
  hasRun = true;
}

void StepTowards_X_Switch() {
  digitalWrite(X_dirPin, Minus_dir);
  digitalWrite(X_stepPin, HIGH);
  delay(Home_Speed);
  digitalWrite(X_stepPin, LOW);
  delay(Home_Speed);
}

void StepTowards_Y_Switch() {
  digitalWrite(Y_dirPin, Minus_dir);
  digitalWrite(Y_stepPin, HIGH);
  delay(Home_Speed);
  digitalWrite(Y_stepPin, LOW);
  delay(Home_Speed);
}

void StepAwayFrom_X_Switch() {
  digitalWrite(X_dirPin, Plus_dir);
  digitalWrite(X_stepPin, HIGH);
  delay(Home_Speed);
  digitalWrite(X_stepPin, LOW);
  delay(Home_Speed);
}

void StepAwayFrom_Y_Switch() {
  digitalWrite(Y_dirPin, Plus_dir);
  digitalWrite(Y_stepPin, HIGH);
  delay(Home_Speed);
  digitalWrite(Y_stepPin, LOW);
  delay(Home_Speed);
}

void Move_X(int dir, int x, int speed) {
  int t = 0;
  while (t < x) {
    for (int i = 0; i < MM; i++) {
      digitalWrite(X_dirPin, dir);
      digitalWrite(X_stepPin, HIGH);
      delay(speed);
      digitalWrite(X_stepPin, LOW);
      delay(speed);
    }
    t++;
  }
}
void Move_Y(int dir, int x, int speed) {
  int t = 0;
  while (t < x) {
    for (int i = 0; i < MM; i++) {
      digitalWrite(Y_dirPin, dir);
      digitalWrite(Y_stepPin, HIGH);
      delay(speed);
      digitalWrite(Y_stepPin, LOW);
      delay(speed);
    }
    t++;
  }
}
void Cut_1() {
  analogWrite(LASER, LASER_Cut_Power); // Turn On LASER
  delay(500);
  Move_Y(Minus_dir, Cut_Length, Cut_Speed); // Move to Cut_1 Start position
  Move_X(Plus_dir, Cut_Gap, Move_Speed);
  Move_Y(Plus_dir, Cut_Length, Cut_Speed);
  analogWrite(LASER, 0); // Turn Off LASER
  Home();
}

void Cut_2() {
  analogWrite(LASER, LASER_Cut_Power); // Turn On LASER
  delay(500);
  Move_Y(Plus_dir, Cut_Length, Cut_Speed); // Move to Cut_2 Start position
  Move_X(Plus_dir, Cut_Gap, Move_Speed);
  Move_Y(Minus_dir, Cut_Length, Cut_Speed);
  analogWrite(LASER, 0); // Turn Off LASER
  Home();
}

Please post a wiring diagram for the switches.

aarg,

Just running pin 8 on the arduino through the microswitch to ground, using INPUT_PULLUP to set the pin HIGH in the void setup.
Trg_2 is on pin 9, same setup.
I was advised to do it this way so any broken wire would not result in triggering the laser.

Maybe you just forgot to enclose your block in braces?

if (Trg_1_State != previousTrg_1_State) {
    if (Trg_1_State == LOW)
      {
      delay(50);
      Move_Y(Plus_dir, 15, Move_Speed); // Move to position
      Move_X(Plus_dir, 11, Move_Speed);// Move to position
      Cut_1();// Turn on LASER and cut based on Trg_1 input
      }
  }
  previousTrg_1_State = Trg_1_State;

?

There is a very good reason why people ask for schematics and good images of the ‘actual’ wiring.

Please, if you expect people to help ‘you’, respond with the information requested.

Here is the example code for mechButton. I think it does exactly what you are looking for. Well, actually no.. It prints out a true or false on the serial monitor when the button changes state. The LED just is on or off as you hold or release the button (Shows if its pressed or not.)

#include <mechButton.h>
#include <idlers.h>

#define BUTTON_PIN1  2     // Pin we'll hook the button to. The other side hooks to ground.
#define LED_PIN      13    // Usual pin number for built in LED.


mechButton button1(2);  // Set button one to pin 2.


// Your standard sketch setup()
void setup() {
   
   Serial.begin(57600);						// Fire up our serial monitor thing.
   pinMode(LED_PIN,OUTPUT);				// Set up the LED pin for output.
   button1.setCallback(myCallback);		// Set up our callback. (Also calls hookup() for idling.)
}


// This is the guy that's called when the button changes state.
void myCallback(void) {

   Serial.print("Button just became ");
   if (button1.trueFalse()) {
      Serial.println("true!");
   } else {
      Serial.println("false!");
   }
}


// Your standard sketch loop()
void loop() {
   
   bool	buttonState;
   
   idle();											// Let all the idlers have time to do their thing.
   buttonState = button1.trueFalse();		// Have a look at what the current button state is.
   digitalWrite(LED_PIN,!buttonState);		// Since the button grounds when pushed, invert logic with !
}

If you want to try it, install LC_baseTools from the library manager.

-jim lee

larryd,

Thank you, i hope that the attached image will show clearly that in fact the D8 pin on the Arduino goes through the micro switch to ground.

JimLee, Thanks I'll have a look at that.

Good, please show a good ‘actual’ wiring image too.

No comment on reply #5?

aarg,

ARHHH I totally missed that, sorry! YES that worked a treat. It always seems to be the most basic stuff. Thank you so much, I really appreciate your help.

Muggs

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.