int PULC = 7; //define Pulse pin
int DIRC = 6; //define Direction pin
int ENAC = 5; //define Enable
int nC = 0;
int PULP = 2; //define Pulse pin
int DIRP = 3; //define Direction pin
int ENAP = 4; //define Enable
int nP = 5;
String inString = ""; // string to hold input
int pos;
void setup()
{
Serial.begin(9600);
// portOne.begin
pinMode (PULC, OUTPUT);
pinMode (DIRC, OUTPUT);
pinMode (ENAC, OUTPUT);
pinMode (PULP, OUTPUT);
pinMode (DIRP, OUTPUT);
pinMode (ENAP, OUTPUT);
}
void loop()
{
if (Serial.available()>0)
{
Serial.print("Which direction are you trying to move ?");
Serial.println(" Left/Right(P) OR Front/Back(C)? ");
char ch = Serial.read();
delay(50); // some delay
Serial.print("Your have entered ");
Serial.println(ch); // for debugging
if (ch == 'p'|| ch == 'P')
{
moveinpower();
}
if (ch == 'c'|| ch == 'C')
{
moveincenter();
}
else
{
Serial.print("INCORRECT INPUT!, YOU CAN ONLY CHOOSE BETWEEN 'c' OR 'P'");
delay(50);
}
}
}
void moveincenter()
{
if (Serial.available()>0)
{
Serial.print("What is your center value ?");
pos=Serial.parseInt();
Serial.println(pos); // for debugging
}
// n=800;
nP = map(pos, -50, 50, -2000, 2000);
nP = abs(nP);
Serial.print("nP: ");
Serial.println(nP); // for debugging
if(pos<0)
{
for (int i=0; i<nP; i++) //Forward 5000 steps
{
digitalWrite(DIRC,HIGH);
digitalWrite(ENAC,HIGH);
digitalWrite(PULC,HIGH);
delayMicroseconds(50);
digitalWrite(PULC,LOW);
digitalWrite(ENAC,LOW);
delayMicroseconds(50);
}
}
if(pos>0)
{
for (int i=0; i<nP; i++) //Forward 5000 steps
{
digitalWrite(DIRC,LOW);
digitalWrite(ENAC,HIGH);
digitalWrite(PULC,HIGH);
delayMicroseconds(50);
digitalWrite(PULC,LOW);
digitalWrite(ENAC,LOW);
delayMicroseconds(50);
}
}
if (pos=0)
{
digitalWrite(ENAC,LOW);
digitalWrite(PULC, LOW);
}
delay(500);
}
void moveinpower()
{
if (Serial.available()>0)
{
Serial.print("What is your power value ?");
pos=Serial.parseInt();
Serial.println(pos); // for debugging
}
// n=800;
nP = map(pos, -50, 50, -2000, 2000);
nP = abs(nP);
Serial.print("nC: ");
Serial.println(nP); // for debugging
if(pos<0)
{
for (int i=0; i<nP; i++) //Forward 5000 steps
{
digitalWrite(DIRP,HIGH);
digitalWrite(ENAP,HIGH);
digitalWrite(PULP,HIGH);
delayMicroseconds(50);
digitalWrite(PULP,LOW);
digitalWrite(ENAP,LOW);
delayMicroseconds(50);
}
}
if(pos>0)
{
for (int i=0; i<nP; i++) //Forward 5000 steps
{
digitalWrite(DIRP,LOW);
digitalWrite(ENAP,HIGH);
digitalWrite(PULP,HIGH);
delayMicroseconds(50);
digitalWrite(PULP,LOW);
digitalWrite(ENAP,LOW);
delayMicroseconds(50);
}
}
if (pos=0)
{
digitalWrite(ENAP,LOW);
digitalWrite(PULP, LOW);
}
delay(500);
}
So now everything is uploading, however, the logic isn’t quite working the way I want it too. after it goes into either one of the void functions, it still does not wait for the user to enter in a value. it immediately jumps out of the function and back to the void loop(), and executes the else statement.
Hope I am describing my issue properly…
Thank you
Gijo