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 (CW or CCW) to the next selection regardless of order of selection. If you select the same button twice it remain in current position as it is already there! As this is a work in progress, I am making changes one at a time such as in adding coding to write a digital pin high for a relay and changing over to an array for portions of the code. 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.
enter code here
//THIS ONE GIVE THE ABOVE ERROR//
//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);
}
}
//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);
} }