Hello, I'm new in programming and Arduino.
I use a Step motor, an Arduino MEGA 2560 and a SMCI47-S-2 Nanotec.
I would like to know how I can choose different values, like in my program,
I can choose right 'r' or left 'l' and now I would like to choose the stepNumber.
I would like to write in serial for exemple " 1000,r" to say 1000 step and direction right.
this is my code :
int pinBouton;
int stateButton;
int pinLedG = 10; //Led Green
int pinLedR = 11; //Led Red
int pin31 = 31; //Start/Reset
int pin32 = 32; //Step Motor
int pin36 = 36; //Clock
int e=0; //RETURN TO RIGHT AFTER HIT BUTTON
int i=0; //FOR RIGHT
int j=0; //FOR LEFT
int serialData = 0;// data and serial data are just variables to hold information.
int data = 0;
int displacement = 10 ; // mm //CHOOSE YOUR DISPLACEMENT TO THE LEFT IN millimeter
int stepNumberToRight = 1000; // Step number To Right
int stepNumberToLeft = 3000; //Step number To Left
bool endswitch=false;
int incomingByte;
// the setup routine runs once when you press reset:
void setup(){
{
Serial.begin(9600); //start talking with the computer. 9600 just tells the rate.
Serial.println("*** Program start ");
Serial.print("Name of this program is :");
Serial.println(" sketch_mar06a");
Serial.println(" TAPE on 'r' going to the RIGHT or TAPE on 'l' going to the LEFT ***");
// initialize the digital pin as an output.
pinMode(pinBouton, INPUT); //mode lecture pour le bouton
pinMode(pinLedG, OUTPUT); //mode écriture pour led1
pinMode(pinLedR, OUTPUT); //mode écriture pour led2
pinMode(pin31, OUTPUT); //mode ecriture pour pin31
pinMode(pin32, OUTPUT); //mode ecriture pour pin32
pinMode(pin36, OUTPUT); //mode ecriture pour pin36
attachInterrupt(digitalPinToInterrupt(3),stopmotor,FALLING);
}
}
void loop()
{
Serial.flush();
while (Serial.available() >0) //Serial.available will tell the board to look at the information being sent to it through the serial port.
{
incomingByte = Serial.read();
//Step motor going to RIGHT (r=114 en ASCII)
{if(incomingByte == 'r')
digitalWrite(pin31,LOW); //pin31 0V START_RESET
digitalWrite(pin31,LOW); //pin31 0V START_RESET
digitalWrite(pin32, HIGH); // RIGHT
digitalWrite(pinLedG,HIGH);
while (j<stepNumberToRight){
digitalWrite(pin31,HIGH); //pin31 +5V FRONT MONTANT
digitalWrite(pin36,LOW); //pin36 0V
delay(2);
digitalWrite(pin36,HIGH); //pin36 +5V
delay(2);
j=j+1;
//Serial.println("BOUCLE 4");
}
digitalWrite(pin31,LOW); //pin31 +5V START_RESET
Serial.println("BOUCLE 1 RIGHT");}
digitalWrite(pin31,HIGH);
delay(20); // temps d'attente si supérieur a 20 ça ne fonctionne pas
digitalWrite(pin31,LOW);
//Step motor going to LEFT (l=108 en ASCII)
{if(incomingByte == 'l')
{
digitalWrite(pin31,LOW); //pin31 0V START_RESET
digitalWrite(pin31,HIGH); //pin31 +5V START_RESET
digitalWrite(pin32, LOW); // LEFT
digitalWrite(pinLedG,HIGH);
while (i<stepNumberToLeft){
digitalWrite(pin31,HIGH); //pin31 +5V FRONT MONTANT
digitalWrite(pin36,LOW); //pin36 0V
delay(2);
digitalWrite(pin36,HIGH); //pin36 +5V
delay(2);
i=i+1;
//Serial.println("BOUCLE 3");
}
digitalWrite(pin31,LOW); //pin31 0V START_RESET
Serial.println("BOUCLE 0 LEFT");
}
}
//digitalWrite(pin31,HIGH);
//delay(20); // temps d'attente si supérieur a 20 ça ne fonctionne pas
//digitalWrite(pin31,LOW);
}
}
void stopmotor(){
i=stepNumberToLeft;
digitalWrite(pin31,HIGH);
delay(20); // temps d'attente si supérieur a 20 ça ne fonctionne pas
digitalWrite(pin31,LOW);
endswitch=true;
digitalWrite(pin32,HIGH); //RIGHT
while(e<500){ /// temps de retour de charriot vers la droite
digitalWrite(pin36,LOW); //pin36 0V
delay(2);
digitalWrite(pin36,HIGH); //pin36 +5V
delay(2);
e=e+1;
Serial.println("BOUCLE 5"); //BOUCLE 5
}
Serial.println("TOUCH");
Serial.println("BOUCLE 8"); //BOUCLE 8
}
If you want, you can answer in French or English ![]()
Thanks