Been at this for days. I can't get the second IF statement to execute. Rank novice at this.
What I want: go to first IF statement. Read inputs, if condition is correct, run the first IF (homeRoutine A0) once only. Then go to the second IF statement. If the inputs are true, execute rotateRoutine every time d13 is high.
What it does: It seems to run the first routine in the loop. Then nothing else. The homing routine works perfectly as does the rotate routine just not consecutively. Also, If the input is already high before i reset the Nano, it will run that routine (one or the other but not both)
What I have tried. among many things, I put the home routine in setup() thinking it will only run once. It runs it but skips the loop. I tried setting up rotate as an interrupt my gross inexperience was a problem. Numerous other things. This is a condensed post since I clicked off the page once and it deleted everything. >:(
/*
R2 Homing works and slow blinks but need to make fast blink if failed home...R4
R3 Homing does not initiate with M4, always runs. fix this. also ledbuiltin is inop now...R5
R4 Now waits for M4 command - ok
R5 enable blink codes on both aux led and led_builtin
R6 time to rotate drop 3 pellets
R7 rotate routine works - Every M8 command rotates, TESTED WITH GRBL
R8 Added home to setup() it works but rotate in loop() doesnt. close?
tried moving rotate to if while. it only works after a reset if both ROTATE and HOMESW are HIGH
*/
#include <SpeedyStepper.h>
//BELOW IS EXPANDED GRBL I/O FOR NANO*************************************
const int LED_PIN = LED_BUILTIN; // D13
const int EN = 2; //eventually connect aux DRV8825 to grbl board enable pins
const int MOTOR_DIRECTION_PIN = 3;
const int MOTOR_STEP_PIN = 4;
const int LIMIT_SWITCH_PIN = 5;
const int HOMESW = A0; //output command //SPINDLE M3/M4^ HOMING ROUTINE
const int ROTATE = A3; //output command //COOLANT M8^/M9 SPINDLE STEP
const int ROTATE_INDICATOR = 7; //COOLANT M8^/M9
const int HOME_INDICATOR = 9; //SPINDLE M3/M4^
SpeedyStepper stepper;
void setup() //********************************SETUP**************************
{
pinMode(LED_PIN, OUTPUT); //led_builtin to indicate home status
pinMode(HOME_INDICATOR, OUTPUT);
pinMode(ROTATE_INDICATOR, OUTPUT);
pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP);
Serial.begin(9600);
stepper.connectToPins(MOTOR_STEP_PIN, MOTOR_DIRECTION_PIN);
}
void loop() //************************************LOOP*****************************
{
if (digitalRead (HOMESW) == HIGH && digitalRead (ROTATE) == LOW)
{
homeRoutine();
digitalWrite (HOME_INDICATOR, HIGH);
}
else if (digitalRead (HOMESW) == LOW && digitalRead (ROTATE) == HIGH)
{
rotateRoutine();
digitalWrite (ROTATE_INDICATOR, HIGH);
}
}
void rotateRoutine() //*******************************ROTATE***********************
{
stepper.setCurrentPositionInMillimeters(0);
stepper.setStepsPerRevolution(50 * 1); // 1x microstepping
stepper.setSpeedInRevolutionsPerSecond(5); //set speed
stepper.setAccelerationInMillimetersPerSecondPerSecond(100.0); //set accel
stepper.moveToPositionInSteps(50); // 50 steps (200/4=50 steps/360 degrees
stepper.setCurrentPositionInMillimeters(0);
return;
}
void homeRoutine() //**********************************HOME****************************
{
stepper.setStepsPerRevolution(400 * 1); // 1x microstepping
stepper.setSpeedInRevolutionsPerSecond(5); //set speed
stepper.setAccelerationInMillimetersPerSecondPerSecond(10.0); //set accel
const float homingSpeedInRevolutions = 1.0; //find home
const float maxHomingDistanceInRevolutions = 1; // 1/2 revoution to fine home. If no find, then fault
const int directionTowardHome = -1; // direction to move toward limit switch: 1 goes positive direction, -1 backward
if (stepper.moveToHomeInRevolutions(directionTowardHome, homingSpeedInRevolutions, maxHomingDistanceInRevolutions, LIMIT_SWITCH_PIN) != true)
{
while (true)
{
blinkFast();
}
}
delay(500);
stepper.setCurrentPositionInMillimeters(0);
while (true) {
blinkSlow();
}
return;
}
void blinkFast() //****************************FAIL angryLed :-(
{
digitalWrite (HOME_INDICATOR, HIGH);
digitalWrite (LED_BUILTIN, HIGH);
delay (100);
digitalWrite (HOME_INDICATOR, LOW);
digitalWrite (LED_BUILTIN, LOW);
delay (100);
return;
}
void blinkSlow() //**************************SUCCESS happyLed : -)
{
digitalWrite (HOME_INDICATOR, HIGH);
digitalWrite (LED_BUILTIN, HIGH);
delay (1000);
digitalWrite (HOME_INDICATOR, LOW);
digitalWrite (LED_BUILTIN, LOW);
delay (1000);
return;
}
