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();
}
