Arduino Nano + DRV8825 + NEMA17 Error

I have made some progress with your suggestion. I am using an Arduino Nano, DRV8825 driver with a NEMA17 stepper, and 4 buttons to simulate ACS712 current sensors that will be used later. In short each button is attached to analog pins (A0,A1,A2,A3) and the analog ports are coded for specific degrees (0,45,225,270), After some work I was able to get the code to operate as wanted. The stepper response to the buttons and takes the shortest route to the next selection regardless of order of selection. As this is a work in progress, I am making changes one at a time such as adding coding to write a digital pin high for a relay and changing over to an array for portions. That’s where I am getting the Error compiling for board Arduino Nano.
I going to include 2 codes, one working and one that give the compile ERROR. I hope I have not violated any rules and have read some results that didn’t seem to apply. Any help would be appreciated .

`[code] THIS ONE GIVES THE ERROR ABOVE
//same as 1.0 but names changed to na=new angle & CA=current angle
// This one seem to work, had change some code to get it to work in all examples.
// Using buttons to simulate analog inputs for now.
// Adding led to represent vac relay on/off PIN 4

// defines pins numbers
const int stepPin = 3;
const int dirPin = 2;
const int enPin = 8;
const int vacPin = 4; //Just added this for vac

// Button asignments
//const int b1 = A0;
//const int b2 = A1;
//const int b3 = A2;
//const int b4 = A3;

const int PIN_COUNT = 4;
int inputPins[PIN_COUNT] = {A0, A1, A2, A3};
int outputVals[4] = {0, 45, 225, 270};

int ca = 0; //currentAngle
int na = 0; //angle
int stepPerAngle = 5 / 9; // full step = 1.8 or could have used “*1.8”
int numstep;

void setup() {
Serial.begin(9600);
for (int index = 0; index < 4; index++)
// Sets the 3 pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode(vacPin, OUTPUT); //Just added this for vac

//set values for 2 outputs
digitalWrite(enPin, LOW);
digitalWrite(dirPin, HIGH);
digitalWrite(vacPin, LOW); //just add this for vac

// set buttons as inputs, some of these will become inputs from ACS712’s
/* pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
}*/

void loop();
int n;
for (int index = 0; index < 4; index++)

/* int n;
  // Assign button degrees
  if      ( digitalRead(A0) == HIGH) {
   na = 0;
  }
  else if ( digitalRead(A1) == HIGH) {
   na = 45;
  }
  else if ( digitalRead(A2) == HIGH) {
   na = 225;
  }
  else if ( digitalRead(A3) == HIGH) {
   na = 270;
  }*/

//currentAngle=ca    new angle=na
// 1st SCENARIO if these to ARE the same nothing happens
//only if they ARE not equal then steps through to find a true statement to act ohn
//if (ca = na) {digitalWrite(vacPin, HIGH);}
if ( ca != na ) {
  //  Serial.println (ca);
  //2nd SCENARIO
  if (na - ca > 0 && na - ca <= 180)
  { digitalWrite(dirPin, HIGH);
    n = ((na - ca) * 5 / 9);
    numstep = n;
    ca = na;
  }

  //3rd SCENARIO
  else if (ca - na > 0 && ca - na > 180)
  { digitalWrite(dirPin, HIGH);
    n = ((na + 360 - ca) * 5 / 9);
    numstep = n;
    ca = na;
  }

  // 4th SCENARIO
  else if (na - ca < 0 && na - ca <= 180)
  { digitalWrite(dirPin, LOW);
    n = ((ca - na) * 5 / 9);
    numstep = n;
    ca = na; (ca);
  }

  //5th SCENARIO
  else if (na - ca > 0 && na - ca > 180)
  { digitalWrite(dirPin, LOW);
    n = ((ca + 360 - na) * 5 / 9);
    numstep = n;
    ca = na; (ca);
  }

  for (int x = 0; x < numstep; x++) {

    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);  //speed
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }  //speed
  delay(500);
}

}

[/code]`THIS ONE COMPILES WITH NO ERRORS

//same as 1.0 but names changed to na=new angle  & CA=current angle
// This one seem to work,  had change some code to get it to work in all examples.
// Using buttons to simulate analog inputs for now.
// Adding led to represent vac relay on/off PIN 4

// defines pins numbers
const int stepPin = 3;
const int dirPin  = 2;
const int enPin  = 8;
const int vacPin = 4; //Just added this for vac

// Button asignment
//const int b1 = A0;
//const int b2 = A1;
//const int b3 = A2;
//const int b4 = A3;

int ca = 0;  //currentAngle
int na = 0;  //angle
int stepPerAngle = 5 / 9; // full step = 1.8 or could have used "*1.8"
int   numstep;

void setup() {
  Serial.begin(9600);

  // Sets the 3 pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  pinMode(vacPin, OUTPUT); //Just added this for vac

  //set values for 2 outputs
  digitalWrite(enPin, LOW);
  digitalWrite(dirPin, HIGH);
  digitalWrite(vacPin, LOW); //just add this for vac


  // set buttons as inputs,  some of these will become inputs from ACS712's
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
}

void loop() {
  int n;
  // Assign button degrees
  if      ( digitalRead(A0) == HIGH) {
    na = 0;
  }
  else if ( digitalRead(A1) == HIGH) {
    na = 45;
  }
  else if ( digitalRead(A2) == HIGH) {
    na = 225;
  }
  else if ( digitalRead(A3) == HIGH) {
    na = 270;
  }

  //currentAngle=ca    new angle=na
  // 1st SCENARIO if these to ARE the same nothing happens
  //only if they ARE not equal then steps through to find a true statement to act ohn
  //if (ca = na) {digitalWrite(vacPin, HIGH);}
  if ( ca != na ) {
    //  Serial.println (ca);
    //2nd SCENARIO
    if (na - ca > 0 && na - ca <= 180)
    { digitalWrite(dirPin, HIGH);
      n = ((na - ca) * 5 / 9);
      numstep = n;
      ca = na;
    }

    //3rd SCENARIO
    else if (ca - na > 0 && ca - na > 180)
    { digitalWrite(dirPin, HIGH);
      n = ((na + 360 - ca) * 5 / 9);
      numstep = n;
      ca = na;
    }

    // 4th SCENARIO
    else if (na - ca < 0 && na - ca <= 180)
    { digitalWrite(dirPin, LOW);
      n = ((ca - na) * 5 / 9);
      numstep = n;
      ca = na; (ca);
    }

    //5th SCENARIO
    else if (na - ca > 0 && na - ca > 180)
    { digitalWrite(dirPin, LOW);
      n = ((ca + 360 - na) * 5 / 9);
      numstep = n;
      ca = na; (ca);
    }

    for (int x = 0; x < numstep; x++) {

      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);  //speed
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    }  //speed
    delay(500);
  }
}

Hi @GregM!

Could you share the complete error messages? Click the button “Copy error messages” on the right side of the orange bar and then paste the errors here.

I didn’t check the complete code but I noticed this:

if (ca = na) {digitalWrite(vacPin, HIGH);}

It should be like this

if (ca == na) {digitalWrite(vacPin, HIGH);}

“==” Compares the variable on the left with the value or variable on the right and returns true when the two operands are equal

I hope that helps!

Here is the entire error with the correction installed.
C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\g_man\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\g_man\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\g_man\OneDrive\Documents\Arduino\libraries -fqbn=arduino:avr:nano:cpu=atmega328old -vid-pid=1A86_7523 -ide-version=10813 -build-path C:\Users\g_man\AppData\Local\Temp\arduino_build_336058 -warnings=none -build-cache C:\Users\g_man\AppData\Local\Temp\arduino_cache_108587 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avrdude.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avr-gcc.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -verbose C:\Users\g_man\AppData\Local\Temp\Temp1_stepper-angle-ctrl-master.zip\stepper-angle-ctrl-master\Button_stepper_adding_vac_relay1.51\Button_stepper_adding_vac_relay1.51.ino
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\g_man\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\g_man\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\g_man\OneDrive\Documents\Arduino\libraries -fqbn=arduino:avr:nano:cpu=atmega328old -vid-pid=1A86_7523 -ide-version=10813 -build-path C:\Users\g_man\AppData\Local\Temp\arduino_build_336058 -warnings=none -build-cache C:\Users\g_man\AppData\Local\Temp\arduino_cache_108587 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avrdude.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avr-gcc.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -verbose C:\Users\g_man\AppData\Local\Temp\Temp1_stepper-angle-ctrl-master.zip\stepper-angle-ctrl-master\Button_stepper_adding_vac_relay1.51\Button_stepper_adding_vac_relay1.51.ino
Using board ‘nano’ from platform in folder: C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3
Using core ‘arduino’ from platform in folder: C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3
Detecting libraries used

“C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR “-IC:\Users\g_man\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\cores\arduino” “-IC:\Users\g_man\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\variants\eightanaloginputs” “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058\sketch\Button_stepper_adding_vac_relay1.51.ino.cpp” -o nul -DARDUINO_LIB_DISCOVERY_PHASE
Generating function prototypes

“C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR “-IC:\Users\g_man\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\cores\arduino” “-IC:\Users\g_man\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\variants\eightanaloginputs” “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058\sketch\Button_stepper_adding_vac_relay1.51.ino.cpp” -o “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058\preproc\ctags_target_for_gcc_minus_e.cpp” -DARDUINO_LIB_DISCOVERY_PHASE
“C:\Program Files (x86)\Arduino\tools-builder\ctags\5.8-arduino11/ctags” -u --language-force=c++ -f - --c+±kinds=svpf --fields=KSTtzns --line-directives “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058\preproc\ctags_target_for_gcc_minus_e.cpp”
Compiling sketch

“C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR “-IC:\Users\g_man\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\cores\arduino” “-IC:\Users\g_man\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\variants\eightanaloginputs” “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058\sketch\Button_stepper_adding_vac_relay1.51.ino.cpp” -o “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058\sketch\Button_stepper_adding_vac_relay1.51.ino.cpp.o”
Compiling libraries

Compiling core

Using precompiled core: C:\Users\g_man\AppData\Local\Temp\arduino_cache_108587\core\core_arduino_avr_nano_cpu_atmega328old_3232b59c280e8e5d4ac39d6c73f3f573.a
Linking everything together

“C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-gcc” -w -Os -g -flto -fuse-linker-plugin -Wl,–gc-sections -mmcu=atmega328p -o “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058/Button_stepper_adding_vac_relay1.51.ino.elf” “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058\sketch\Button_stepper_adding_vac_relay1.51.ino.cpp.o” “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058/
\arduino_cache_108587\core\core_arduino_avr_nano_cpu_atmega328old_3232b59c280e8e5d4ac39d6c73f3f573.a” “-LC:\Users\g_man\AppData\Local\Temp\arduino_build_336058” -lm
“C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy” -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058/Button_stepper_adding_vac_relay1.51.ino.elf” “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058/Button_stepper_adding_vac_relay1.51.ino.eep”
“C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy” -O ihex -R .eeprom “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058/Button_stepper_adding_vac_relay1.51.ino.elf” “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058/Button_stepper_adding_vac_relay1.51.ino.hex”
“C:\Users\g_man\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-size” -A “C:\Users\g_man\AppData\Local\Temp\arduino_build_336058/Button_stepper_adding_vac_relay1.51.ino.elf”
Sketch uses 2540 bytes (8%) of program storage space. Maximum is 30720 bytes.
Global variables use 190 bytes (9%) of dynamic memory, leaving 1858 bytes for local variables. Maximum is 2048 bytes.
avrdude: ser_open(): can’t open device “\.\COM3”: Access is denied.

Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.

What I’m trying to do is write a code (insert) that will turn on led(vacuum relay) while the button is still depressed “HIGH” This simulates using a shop tool and the dust collector running as long as the tool is on. I have tried several “do / while & while / do” statements that i think would fit the solution best but none have worked correctly.

Thanks for you help
Greg

Copy of current code with change.

[code]

//same as 1.0 but names changed to na=new angle & CA=current angle
// This one seem to work, had change some code to get it to work in all examples.
// Using buttons to simulate analog inputs for now.
// Adding led to represent vac relay on/off PIN 4

// defines pins numbers
const int stepPin = 3;
const int dirPin = 2;
const int enPin = 8;
const int vacPin = 4; //Just added this for vac

// Button asignment
//const int b1 = A0;
//const int b2 = A1;
//const int b3 = A2;
//const int b4 = A3;

int ca = 0; //currentAngle
int na = 0; //angle
int stepPerAngle = 5 / 9; // full step = 1.8 or could have used “*1.8”
int numstep;

void setup() {
Serial.begin(9600);

// Sets the 4 pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode(vacPin, OUTPUT); //Just added this for vac

//set values for 3 outputs
digitalWrite(enPin, LOW);
digitalWrite(dirPin, HIGH);
digitalWrite(vacPin, LOW); //just add this for vac

// set buttons as inputs, some of these will become inputs from ACS712’s
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
}

void loop() {
int n;
// Assign button degrees
if ( digitalRead(A0) == HIGH) {
na = 0;
}
else if ( digitalRead(A1) == HIGH) {
na = 45;
}
else if ( digitalRead(A2) == HIGH) {
na = 225;
}
else if ( digitalRead(A3) == HIGH) {
na = 270;
}

//currentAngle=ca new angle=na
// 1st SCENARIO if these to ARE the same nothing happens
//only if they ARE not equal then steps through to find a true statement to act ohn

if (ca == na) {
digitalWrite(vacPin, HIGH);
}
else if ( ca != na ) {
// Serial.println (ca);
//2nd SCENARIO
if (na - ca > 0 && na - ca <= 180)
{ digitalWrite(dirPin, HIGH);
n = ((na - ca) * 5 / 9);
numstep = n;
ca = na;

}

//3rd SCENARIO
else if (ca - na > 0 && ca - na > 180)
{ digitalWrite(dirPin, HIGH);
  n = ((na + 360 - ca) * 5 / 9);
  numstep = n;
  ca = na;

}

// 4th SCENARIO
else if (na - ca < 0 && na - ca <= 180)
{ digitalWrite(dirPin, LOW);
  n = ((ca - na) * 5 / 9);
  numstep = n;
  ca = na;
}

//5th SCENARIO
else if (na - ca > 0 && na - ca > 180)
{ digitalWrite(dirPin, LOW);
  n = ((ca + 360 - na) * 5 / 9);
  numstep = n;
  ca = na;
}

for (int x = 0; x < numstep; x++)

{ digitalWrite(stepPin, HIGH);
  delayMicroseconds(1000);  //speed
  digitalWrite(stepPin, LOW); //speed
  delayMicroseconds(1000);
}

}

delay(500);
}

//{
while(analogRead(sensorPin) > 100)
{
blink(); // call a function to turn an LED on and off
Serial.print(".");
}
Serial.println(analogRead(sensorPin)); // this is not executed until after
/
[/code]

avrdude: ser_open(): can’t open device “.\COM3”: Access is denied.

The problem is not the code itself, is that you were unable to upload it to your Arduino.

First check if the Arduino is presented on COM3. Unplug it. Start Device Manager. What serial ports are listed? Plug it again. Wait a few seconds. What serial ports are listed?

You could also try this:

1 - Unplug the Arduino
2 - Upload code with no Arduino connected to PC
3 - Reconnect Arduino
4 - Upload code to the Arduino

If this doesn’t work simply look for that error online, there are multiple solutions out there. If you find a solution it would be great if you can post it here :grinning: and if you get any other errors feel free to ask again.

1 Like

I was running the verify when I got the error and it shouldn’t be looking at the Arduino at that time and I could run the same code with out the new additions and it would work. I moved to to PlatFormio and didn’t get the error there? Strange! I wondered if it was looking for something from a Library? Arduino doesn’t always give me enough info to figure out what’s going on, while PlatFormio give me to much at times! Anyway onward with making changes! Thanks for your help.

Been making some progress with some help. I’m stuck on some code and was hoping you could help. The part I’m stuck on is between the ################ about half way tru the code. If you need more info please let me know.
Thanks
Greg

`[code]
/*This version what as it EXCEPT the Delay vac 4 sec has to be turned off. See line 87 for info
/*Auto Blast gate project: for dust collection in wood shop.
Arduino Nano
ACS712 current sensor
DRV8825 stepper driver
Nema 17 stepper
Each tool will be monitor by a current sensor and when that analog port exceeds a given threshold

  1. Rotate the stepper to a predetermined position to align ports for that tool.
  2. After the gate is align a digital pin will drive relay to turn on Vacuum system.
    Vacuum will stay on as long as sensor exceed the threshold while tool is running.
    When tool stops vacuum will continue for 4 seconds to clear remaining dust.
  3. If the same tool is used next, the Stepper will remain in it’s current position.
    But the vacuum stsytem should still operate the same.
  4. currently using buttons to simulate tools and also one ACS712 for testing */

// defines pins numbers
const int stepPin = 3;
const int dirPin = 2;
const int enPin = 8;
const int vacPin = 4; //simulate Vacuum relay

int ca = 0; //currentAngle
int na = 0; //angle
int stepPerAngle = 5 / 9; // full step = 1.8 or could have used “*1.8”
int numstep;
int analogValue = 0;

void setup() {
Serial.begin(9600);

// Sets the 3 pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode(vacPin, OUTPUT); //Just added this for vac

//set values for 2 outputs
digitalWrite(enPin, LOW);
digitalWrite(dirPin, HIGH);
digitalWrite(vacPin, LOW); //just add this for vac

}

void loop() {
int n;
analogValue = 0;

// Assign button degrees
if ( analogRead(A0) > 600) {
analogValue = (analogRead(A0));
Serial.print("AO = ");
Serial.println(analogValue);
na = 0;

}
else if ( analogRead(A1) > 600) {
analogValue = (analogRead(A1));
Serial.print("A1 = ");
Serial.println(analogValue);
na = 45;
}

else if ( analogRead(A2) > 600) {
analogValue = (analogRead(A2));
Serial.print("A2 = ");
Serial.println(analogValue);
na = 225;
}

else if ( analogRead(A3) > 600) { //=tool on
analogValue = (analogRead(A3));
Serial.print("A3 = ");
Serial.println(analogValue);
na = 270;

}

/*###########################################################
this is where I need help. The first if statement works as it should.
when those 2 condition are met the vacPin goes high. */

if(analogValue > 600 && ca==na) {
digitalWrite(vacPin, HIGH);
}

/* this part is not working correctly. If the delay is remove it works as the “else if” statment reads.
I need the delay so when a tool is stopped the vacuum continues for a few secounds to clear remaining saw dust */

else if (analogValue < 600) {
delay (4000); /* with this on:
the stepper does not start as quickly as before,
the analog will not read the ACS712 sensor at all, only the buttons*/
digitalWrite(vacPin, LOW);
}

//################################################################################

if ( ca != na ) {

//2nd SCENARIO
if (na - ca > 0 && na - ca <= 180)
{ digitalWrite(dirPin, HIGH);
n = ((na - ca) * 5 / 9);
numstep = n;
ca = na;

}

//3rd SCENARIO
else if (ca - na > 0 && ca - na > 180)
{ digitalWrite(dirPin, HIGH);
  n = ((na + 360 - ca) * 5 / 9);
  numstep = n;
  ca = na;
}

// 4th SCENARIO
else if (na - ca < 0 && na - ca <= 180)
{ digitalWrite(dirPin, LOW);
  n = ((ca - na) * 5 / 9);
  numstep = n;
  ca = na;
}

//5th SCENARIO
else if (na - ca > 0 && na - ca > 180)
{ digitalWrite(dirPin, LOW);
  n = ((ca + 360 - na) * 5 / 9);
  numstep = n;
  ca = na;   

}

for (int x = 0; x < numstep; x++) {

  digitalWrite(stepPin, HIGH);
  delayMicroseconds(1000);  //speed
  digitalWrite(stepPin, LOW);
  delayMicroseconds(1000);



}



delay(200);

}

}

[/code]`