change delay after x steps_step mottor

Hallo,
I tried to find answer for this before, but I saw only another question wich was not fit to mine.

I am work on one small project with arduino and need to use two modes for stepp motor moving.

this is the code I have got…

int Pin4 = 6; 
int Pin5 = 7; 
int Pin6 = 8; 
int Pin7 = 9;
int _step = 0; 
boolean dir = true;
const int buttonPin = 5 ;
int buttonState = 0; 
int i=0;
void setup() 
{ 
 pinMode(buttonPin,INPUT); 
  
 pinMode(Pin4, OUTPUT);  
 pinMode(Pin5, OUTPUT);  
 pinMode(Pin6, OUTPUT);  
 pinMode(Pin7, OUTPUT);
} 
 void loop() 
{ 
  buttonState = digitalRead(buttonPin); 
  
  if(buttonState == HIGH){
  for(i=0;i<48;i++){
  {//vykonanie ak je zopnuty spinac I
 switch(_step){ 
   case 0: 
 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, HIGH); 
   break;  
   case 1: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, HIGH); 
     digitalWrite(Pin7, HIGH); 
   break;  
   case 2: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, HIGH); 
     digitalWrite(Pin7, LOW); 
   break;  
   case 3: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, HIGH); 
     digitalWrite(Pin6, HIGH); 
     digitalWrite(Pin7, LOW); 
   break;  
   case 4: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, HIGH); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW);  
   break;  
   case 5: 
     
     digitalWrite(Pin4, HIGH);  
     digitalWrite(Pin5, HIGH); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW); 
   break;  
     case 6: 
     digitalWrite(Pin4, HIGH);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW); 
   break;  
   case 7: 
     digitalWrite(Pin4, HIGH);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, HIGH); 
   break;  
   default: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW); 
   break;  
 } ///  
  }
 if(dir){ 
   _step++; 
 }else{ 
   _step--; 
 } 
 if(_step>7){ 
   _step=0; 
 } 
 if(_step<0){ 
   _step=7; 
 } 
 delay(1); 
}
  };
  
 
[color=red]delay(5000);[/color]
}

My problem in this case is: I want to rewrite this code in order to step motor rotate 360 degrees and then stop. . Motor has 48 steps. I tried to use for cycle for that and delay 5 sec.
If button is pressed it should start, than make one full turn /48 steps/ and wait for 5 sec.

Why it isnt working ? what is wrong ? … I know it is loop , but it should do all cases 48 times - and wait.

There are “if” part of code for changing directions too, I want to use it for changig direction with press button. / I tried it in another code it works well - that is not my problem , my problem is to make it works for one turn and wait for some time /

Thank you for advices …

What exactly does “not work”?

Are you sure that you want to wait 5 seconds in every invocation of loop()?
I’d think that such a delay is sufficient directly after the for loop.

May I suggest that you reorganize your code to put all the motor stuff in a function so that you can more easily see what is happening in loop(). I haven’t tested this.

int Pin4 = 6; 
int Pin5 = 7; 
int Pin6 = 8; 
int Pin7 = 9;

boolean dir = true;
const int buttonPin = 5 ;
int buttonState = 0; 
int i=0;
void setup() 
{ 
 pinMode(buttonPin,INPUT); 
    
 pinMode(Pin4, OUTPUT);  
 pinMode(Pin5, OUTPUT);  
 pinMode(Pin6, OUTPUT);  
 pinMode(Pin7, OUTPUT);
} 
 void loop() 
{ 
    buttonState = digitalRead(buttonPin); 
    
    if(buttonState == HIGH){
    for(i=0; i<48; i++){
    {//vykonanie ak je zopnuty spinac I
        if (dir){ 
            motorStep('F'); 
        }
        else { 
            motorStep('R'); 
        } 
    }
    delay(5000);
    dir = !dir;    // toggle the direction flag
}

void motorStep(byte dirn) {
	static char _step = 0;
	if (dirn == 'F') {
		_step ++;
	}
	else {
		_step --;
	}
	if(_step > 7){ 
		_step = 0; 
	}
	if(_step < 0){ 
		_step = 7; 
	} 
    switch(_step){
    case 0: 
 
        digitalWrite(Pin4, LOW);  
        digitalWrite(Pin5, LOW); 
        digitalWrite(Pin6, LOW); 
        digitalWrite(Pin7, HIGH); 
    break;  
    case 1: 
        digitalWrite(Pin4, LOW);  
        digitalWrite(Pin5, LOW); 
        digitalWrite(Pin6, HIGH); 
        digitalWrite(Pin7, HIGH); 
    break;  
    case 2: 
        digitalWrite(Pin4, LOW);  
        digitalWrite(Pin5, LOW); 
        digitalWrite(Pin6, HIGH); 
        digitalWrite(Pin7, LOW); 
    break;  
    case 3: 
        digitalWrite(Pin4, LOW);  
        digitalWrite(Pin5, HIGH); 
        digitalWrite(Pin6, HIGH); 
        digitalWrite(Pin7, LOW); 
    break;  
    case 4: 
        digitalWrite(Pin4, LOW);  
        digitalWrite(Pin5, HIGH); 
        digitalWrite(Pin6, LOW); 
        digitalWrite(Pin7, LOW);  
    break;  
    case 5: 
        
        digitalWrite(Pin4, HIGH);  
        digitalWrite(Pin5, HIGH); 
        digitalWrite(Pin6, LOW); 
        digitalWrite(Pin7, LOW); 
    break;  
        case 6: 
        digitalWrite(Pin4, HIGH);  
        digitalWrite(Pin5, LOW); 
        digitalWrite(Pin6, LOW); 
        digitalWrite(Pin7, LOW); 
    break;  
    case 7: 
        digitalWrite(Pin4, HIGH);  
        digitalWrite(Pin5, LOW); 
        digitalWrite(Pin6, LOW); 
        digitalWrite(Pin7, HIGH); 
    break;  
    default: 
        digitalWrite(Pin4, LOW);  
        digitalWrite(Pin5, LOW); 
        digitalWrite(Pin6, LOW); 
        digitalWrite(Pin7, LOW); 
    break;  
    } // end of SWITCH
}

…R

Robin2:
May I suggest that you reorganize your code to put all the motor stuff in a function so that you can more easily see what is happening in loop(). I haven’t tested this.

int Pin4 = 6; 

int Pin5 = 7;
int Pin6 = 8;
int Pin7 = 9;

boolean dir = true;
const int buttonPin = 5 ;
int buttonState = 0;
int i=0;
void setup()
{
pinMode(buttonPin,INPUT);
   
pinMode(Pin4, OUTPUT); 
pinMode(Pin5, OUTPUT); 
pinMode(Pin6, OUTPUT); 
pinMode(Pin7, OUTPUT);
}
void loop()
{
    buttonState = digitalRead(buttonPin);
   
    if(buttonState == HIGH){
    for(i=0; i<48; i++){
    {//vykonanie ak je zopnuty spinac I
        if (dir){
            motorStep(‘F’);
        }
        else {
            motorStep(‘R’);
        }
    }
    delay(5000);
    dir = !dir;    // toggle the direction flag
}

void motorStep(byte dirn) {
static char _step = 0;
if (dirn == ‘F’) {
_step ++;
}
else {
_step --;
}
if(_step > 7){
_step = 0;
}
if(_step < 0){
_step = 7;
}
    switch(_step){
    case 0:

digitalWrite(Pin4, LOW); 
        digitalWrite(Pin5, LOW);
        digitalWrite(Pin6, LOW);
        digitalWrite(Pin7, HIGH);
    break; 
    case 1:
        digitalWrite(Pin4, LOW); 
        digitalWrite(Pin5, LOW);
        digitalWrite(Pin6, HIGH);
        digitalWrite(Pin7, HIGH);
    break; 
    case 2:
        digitalWrite(Pin4, LOW); 
        digitalWrite(Pin5, LOW);
        digitalWrite(Pin6, HIGH);
        digitalWrite(Pin7, LOW);
    break; 
    case 3:
        digitalWrite(Pin4, LOW); 
        digitalWrite(Pin5, HIGH);
        digitalWrite(Pin6, HIGH);
        digitalWrite(Pin7, LOW);
    break; 
    case 4:
        digitalWrite(Pin4, LOW); 
        digitalWrite(Pin5, HIGH);
        digitalWrite(Pin6, LOW);
        digitalWrite(Pin7, LOW); 
    break; 
    case 5:
       
        digitalWrite(Pin4, HIGH); 
        digitalWrite(Pin5, HIGH);
        digitalWrite(Pin6, LOW);
        digitalWrite(Pin7, LOW);
    break; 
        case 6:
        digitalWrite(Pin4, HIGH); 
        digitalWrite(Pin5, LOW);
        digitalWrite(Pin6, LOW);
        digitalWrite(Pin7, LOW);
    break; 
    case 7:
        digitalWrite(Pin4, HIGH); 
        digitalWrite(Pin5, LOW);
        digitalWrite(Pin6, LOW);
        digitalWrite(Pin7, HIGH);
    break; 
    default:
        digitalWrite(Pin4, LOW); 
        digitalWrite(Pin5, LOW);
        digitalWrite(Pin6, LOW);
        digitalWrite(Pin7, LOW);
    break; 
    } // end of SWITCH
}




...R

Yes, that could be helpfull for me I add another one button to call the function, and will test it, and than I can post final code if it will work if not I will post some question or note, thanks a lot

Edit at all :
I did this code, but somewhere there is a mistake, one or more … please , could someone help me please, I got lost somewhere but I tried to comment it properly to describe what I want …
Please , can someone have a look at this code and try to correct it please …

I can draw wiring schematics if it be needed , I will be thankfull for helping, I am newbie and I am not sure using libaries like stepmotor or something will be easy to understand for me, because that I used this way …

here is the code:

int Pin4 = 6; 
int Pin5 = 7; 
int Pin6 = 8; 
int Pin7 = 9;
//Output pins 

int _step = 0; 
// step state 

const int manual = 5 ;
int switchState = 0; 
// switch state , I want to do manual ON OFF it could be connected at the relay at same time to swtich 
// power on/off when signal comes / does not come 

const int up = 4; //pushbutton there 
int up_state = 0;
// that is used to manual motor control to UP way if switch buttonState is 0 - OFF and you can control motor manually

const int down = 3; //pushbutton there
int down_state = 0;
// that is used to manual motor control down - way , when switch buttonState is LOW - OFF and 
// another push button is pressed down 


int i=0; // this is for "for" cycle ... my motor has 48 steps, I want to stop it for 5 sec after one torque.

const int psd = 2; //photo diode signal - only high, and low comes ,
              //do not care, state will come in digital form other device where range are set as I want )
int psd_state = 0; //default PSD state 


void setup() 
{ 
 pinMode(manual,INPUT);  // manual switch //it will be conected to +5V and  port5 , grounded with 10kOhm, and to relay, 
 //to swtich it automaticaly OFF and ON to unplug the mottor from voltage 
  
 pinMode(Pin4, OUTPUT);  //motor outs 
 pinMode(Pin5, OUTPUT);  
 pinMode(Pin6, OUTPUT);  
 pinMode(Pin7, OUTPUT);
 
 
 pinMode(down, INPUT ); //pushbutton one
 pinMode(up, INPUT); //pushbutton two 
 
 pinMode(psd, INPUT); // HIGH LOW SIGNAL from another device 
 
 
 }

 void loop() 
{ 
  psd_state = digitalRead(psd);
  switchState = digitalRead(manual); 
  up_state = digitalRead(up);
  down_state = digitalRead (down);
  

  if((switchState == ) &&( psd_state == HIGH){
      for(i=0;i<48;i++){
        _step++; 
  motorStep;};} 
     
}   // I am not sure about that code there, but I want to make 48steps ++ = one torque if button is HIGH and signal is comming from psd at the same time , if no , do not do that 

 
 
 
  if((switchState == HIGH)&& (psd_state == LOW)){ //if this condition is right do this code below :
      for(i=0;i<48;i++){
        _step--; {
  motorStep;};}  /// I want there if button is switched ON and signal is not there on PSD , to rotate it once - 48 steps to opposite way  , - want to skip code below 

}  
  
  if (switchState == LOW) /// if buttonState low , do not care about diode , it can be high or low , it does not matter 
    if((up_state == HIGH) && (down_state == LOW)                    // I wrote it this way because it can happen sombody push buttons at the same time 
  {  
   _step++; {
   motorStep;}
 }                                                        //WHAT I HAVE TO write there  THERE ???? I want to put there step++  ,
                                                              //but  not for 48 steps only so that for time the push button is pressed motor is still moving
 

  if((up_state == LOW) && down_state == HIGH)  // I want motor rotate to oposite side  step-- , I dont know how to connect information from there to void motorStep 
 // there is IF statment  for example: if(up_state == LOW && down state HIGH ) _step--  but I dont know how to connect it together 
     _step--; {
     motorStep;}



/// FUNCTION motorStep starts here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void motorStep()
  { 
  
 if(_step>7){ 
   _step=0; 
 } 
 if(_step<0){ 
   _step=7; 
 } 
  
  
 switch(_step){ 
   case 0: 
 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, HIGH); 
   break;  
   case 1: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, HIGH); 
     digitalWrite(Pin7, HIGH); 
   break;  
   case 2: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, HIGH); 
     digitalWrite(Pin7, LOW); 
   break;  
   case 3: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, HIGH); 
     digitalWrite(Pin6, HIGH); 
     digitalWrite(Pin7, LOW); 
   break;  
   case 4: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, HIGH); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW);  
   break;  
   case 5: 
     
     digitalWrite(Pin4, HIGH);  
     digitalWrite(Pin5, HIGH); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW); 
   break;  
     case 6: 
     digitalWrite(Pin4, HIGH);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW); 
   break;  
   case 7: 
     digitalWrite(Pin4, HIGH);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, HIGH); 
   break;  
   default: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW); 
   break;  
 }; ///  
  };

;

thanks , this time, every word will be helpfull for me

When you call a function you must include () after it. so it should be motorStep()

I would tidy things a little (but it won’t change the functionaliy) by calling motorStep() with 1 or -1
motorStep(1) or motorStep(-1)

and change the beginning of the code in the function to

void motorStep(int dirn)
  { 

_step = _step + dirn; 

 if(_step>7){ 
   _step=0; 
 }

That way you don’t need to have step++ or step – in the main body of your code.

…R

at first , Thank you Robin for your interest,

first thing I want is make it functional, What about ways … hm
I would like to use two buttons , if I press button one ( I call it UP for example ) it save logical 1 to valiable “motorStep();” …
in motor step there are cases and they shows how motor rotates … If I want to change direction I have to do it backwards … read the code backwards or something like that … How I can do it without IF ??

I am not sure I understand meaning motorStep(-1) and motorStep(1) … what the 1 or -1 means ? Could you explain me … ?

my explanation - is it correct ?

void motorStep(int dirn)   -//there I declare some valiable with name dirn , yes ?
  { 

_step = _step + dirn;      ///   and here I add or subtract this number or logicaly do (something is   ////  here ) + ( dirn is here ) ... than It will be works ? 

 if(_step>7){ 
   _step=0; 
 }

OK but that WITH switch /cases/ will be in void motor Step(intdirn) function … yes ?

Than , I would like to ask I have to initialize dirn in setup again , I dont have ?

And can I write somewhere in loop :

if (up == 1) dirn=1

if (down == 1) dirn = -1

So, last question is how can I make it works - in another case, only for 48 steps ( motor has 48 steps ) ???

for example :

if (sensor == 0 ) {
dirn=1
for (i=0;i<48;-++)
motorStep(int dirn)}

if (sensor == 1 ) {
dirn=-1
for (i=0;i<48;-++)
motorStep(int dirn)}

Can I call function this way , is it written correctly ? Thank you a lot for your time and interest. I wanted to ask before doing it for some help to clear my information, Thanks

ghost0007:
my explanation - is it correct ?

I think you have it correct, but just ignore that suggestion if you prefer

if (sensor == 1 ) {
dirn=-1
for (i=0;i<48;-++)
motorStep(int dirn)}

Can I call function this way , is it written correctly ? Thank you a lot for your time and interest. I wanted to ask before doing it for some help to clear my information, Thanks

This is wrong

motorStep(int dirn)

It should be

motorStep(dirn);

Note also the semi-colon
When you use “int” the compiler creates a new variable rather than using the existing one.

You have other comments and questions that I can’t understand. It is easier to help if you just ask one question at a time.

…R

for this time thank you a lot for your time and interest, I have to wait for new arduino mega and test all functionality with this function either in same code , thanks

hallo , again, I tried to do it but I have problem I and I dont know how to solve it … where can be problem , please could you help me and explain what is wrong ? thanks a lot …

int Pin4 = 10; 
int Pin5 = 11; 
int Pin6 = 12; 
int Pin7 = 13;
//Output pins 

int _step = 0; 
const int manual = 5;
int up = 9;
int down = 8;
int relay = 7;

int dirn = 0; // for changing direction 

void setup() { 
 pinMode(manual,INPUT);  // manual switch //it will be conected to +5V and  port5 , grounded with 10kOhm, and to relay, 
 //to swtich it automaticaly OFF and ON to unplug the mottor from voltage 
  
 pinMode(Pin4, OUTPUT); 
 pinMode(Pin5, OUTPUT);  
 pinMode(Pin6, OUTPUT);  
 pinMode(Pin7, OUTPUT);
 
 pinMode(relay,OUTPUT); //relay
 
 
 pinMode(down, INPUT ); //pushbutton one
 pinMode(up, INPUT); //pushbutton two
} 
 
 void loop(){
  
int  switchState = digitalRead(manual); 
int  up_state = digitalRead(up);
int  down_state = digitalRead (down);
int  manual_state = digitalRead (manual);

 
 
    if(manual_state == HIGH){
               if(down_state == HIGH){
                 dirn = "1";
                digitalWrite(relay, HIGH);
                 motorStep(dirn);
                     }
       
         
                 else if(up_state == HIGH){
                  dirn = "-1";
                 digitalWrite(relay, HIGH);
                 motorStep(dirn);
                  }
         
         
                 else{
                digitalWrite(relay, LOW);
                 };}
         
    else{
    digitalWrite(relay, LOW);} 
    
    
    delay(2);
 
 
 
    
 }

/// FUNCTION motorStep starts here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void motorStep(){ 

  _step = _step + dirn;
  
  
 if(_step>7){ 
   _step=0; 
 };
 
 if(_step<0){ 
   _step=7; 
 }; 
  
  
 switch(_step){ 
   case 0: 
 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, HIGH); 
   break;  
   case 1: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, HIGH); 
     digitalWrite(Pin7, HIGH); 
   break;  
   case 2: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, HIGH); 
     digitalWrite(Pin7, LOW); 
   break;  
   case 3: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, HIGH); 
     digitalWrite(Pin6, HIGH); 
     digitalWrite(Pin7, LOW); 
   break;  
   case 4: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, HIGH); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW);  
   break;  
   case 5: 
     
     digitalWrite(Pin4, HIGH);  
     digitalWrite(Pin5, HIGH); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW); 
   break;  
     case 6: 
     digitalWrite(Pin4, HIGH);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW); 
   break;  
   case 7: 
     digitalWrite(Pin4, HIGH);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, HIGH); 
   break;  
   default: 
     digitalWrite(Pin4, LOW);  
     digitalWrite(Pin5, LOW); 
     digitalWrite(Pin6, LOW); 
     digitalWrite(Pin7, LOW); 
   break;  
 }; ///  
  };

ghost0007: I tried to do it but I have problem I and I dont know how to solve it

You need to give me a little assistance. What problem have you got? Provide as much detail as possible.

...R

I cant verify and compile it ... because there is some problem it tels me problem with variables ... in my previous message there are code, seriously , I tried to rewrite some lines but it cant help ...

it tells I cant write this: void motorStep (dirn) is wrong .. I try to type motorStep( dirn ) but this didnt help ...

I cant find mistake in code ...

ghost0007: I cant verify and compile it.

Then give us a half-chance to help you by posting the error messages.

...R