Unwanted stepper motor action

Hi all!

First let me describe you my hardware. I have arduino, stepper motor driver and motor. With arduino I am controlling the speed of the motor through CLK on driver, the speed is selected with a rotary switch (works as voltage divider and I am reading value on analog pin), for start and stop I have two pushbuttons. Everything works fine to this point. But when I want to integrate another signal, this one is from pump through optocoupler, I don't receive just pulse but I have constant voltage for the time that pump runs. At this point stepper motor reaches max speed and starts accelarating again, and so on, same way as I would hold my start button. How would I be able to read only change of state and take that as a pulse?

int selectorPin;
int numOfSteps;
float divider;
int enable = 13;
int has1 =1;
int has2 =2;
int has3 =3;
int has4 =4;
int x;
int start = 8;
int start1;
int avtstop = 9;
int stop1;
int stopled = 6;
int startled = 7;
int memo = 0;
int opto = A3;
int sv;

void setup()
{
 
//pin za enable
pinMode(enable, OUTPUT);
pinMode(start, INPUT);
pinMode(avtstop, INPUT);
pinMode(stopled, OUTPUT);
pinMode(startled, OUTPUT);
pinMode(opto, INPUT);

digitalWrite(enable, LOW);
digitalWrite(stopled, HIGH);



//analogni pin za branje vrednosti stikalnega preklopnika
  selectorPin = 0;

  //stevilo preklopov stikalnega preklopnika
  numOfSteps = 5;
 
  //divider for decoding analog input
  divider = 1023.0 / numOfSteps;

}


void loop()
{
  //divide and round the input as float
  float selectorValueFloat = round(analogRead(selectorPin) / divider);
  
  //cast to integer for e.g. indexing arrays
  sv = selectorValueFloat;
 
  stop1 = digitalRead(avtstop);
  start1= digitalRead(start);
  
    
  
  
            
  if(sv ==0 || stop1 || memo !=sv) 
             {
        noTone(12);
      digitalWrite(enable, LOW); 
      digitalWrite(stopled, HIGH);
      digitalWrite(startled, LOW);
      has1 = 1;
      memo = sv;
             }
  
  if(sv == 1){
    memo=sv;
    if(start1){
      digitalWrite(enable, HIGH);
      digitalWrite(startled, HIGH);
      digitalWrite(stopled, LOW);
      if(has1 == 1)
            {
        for(x = 200; x<=1850; x++)
        {
          tone(12,x);
          delay(3);
          has1 = 2;
          has2 = 2;
        }
      }
        else
        {
          tone(12, 1850);
          has1 = 1;
        }
            }}
      
    if(sv == 2){
      memo=sv;
      if(start1){
    
      digitalWrite(enable, HIGH);
      digitalWrite(startled, HIGH);
      digitalWrite(stopled, LOW);
      if(has2 == 2)
      {
        for(x = 200; x<=1100; x++)
        {
          tone(12,x);
          delay(3);
          has2 = 1;
          has3 = 3;
          has1 = 1;
        }
      }
        else
        {
          tone(12, 1100);
          has2 = 2;
        }
      
              }}
    if(sv == 3){
      memo=sv;
     if(start1){
      digitalWrite(enable, HIGH);
      digitalWrite(startled, HIGH);
      digitalWrite(stopled, LOW);
      if(has3 == 3)
      {
        for(x = 100; x<=555; x++)
        {
          tone(12,x);
          delay(3);
          has2 = 2;
          has3 = 2;
          has4 = 4;
        }
      }
        else
        {
          tone(12, 555);
          has3 = 3;
        }
      
              }}
    if(sv == 4){
      memo=sv;
      if(start1){
      digitalWrite(enable, HIGH);
      digitalWrite(startled, HIGH);
      digitalWrite(stopled, LOW);
      if(has4 == 4)
      {
        for(x = 200; x<=1850; x++)
        {
          tone(12,x);
          delay(3);
          has3 = 3;
          has4 = 3;
        }
      }
        else
        {
          tone(12, 1850);
          has4 = 4;
        }
      
           }}
}

Thank you for your help and best regardd,
Alex

  float selectorValueFloat = round(analogRead(selectorPin) / divider);

WTF? You divide an integer by a float, resulting in a float. You round that off, resulting in an int, which you store in a float. Why?

  //cast to integer for e.g. indexing arrays
  sv = selectorValueFloat;

You wouldn't have to do this if you hadn't stored the int in a float in the first place.

How would I be able to read only change of state

The change of state of what?

  if(sv ==0 || stop1 || memo !=sv) 
             {
        noTone(12);
      digitalWrite(enable, LOW); 
      digitalWrite(stopled, HIGH);
      digitalWrite(startled, LOW);
      has1 = 1;
      memo = sv;
             }

WTF? You haven't learned to indent properly? Use Tools + Auto Format!

            }}

NEVER!

Hi, can you post a copy of your circuit, either a CAD in jpg or png, or a picture of a hand drawn circuit.

This way we can see how you are inputting your buttons and outputting to the stepper controller.

How are you powering the stepper, I hope not from the arduino, the stepper should have a supply that is separated from the arduino, but connected at gnd connections.

Tom....... :slight_smile:

Code:

float selectorValueFloat = round(analogRead(selectorPin) / divider);

WTF? You divide an integer by a float, resulting in a float. You round that off, resulting in an int, which you store in a float. Why?

I did it like this in the first place and I haven't changed.

The change of state of what?

To read only the change of state of the button or the signal on my optocoupler. It is not in the code now as I don't know how to integrate it.

WTF? You haven't learned to indent properly? Use Tools + Auto Format!

No I didn't as this is my first program and no one told me how to. Would you be so kind and tell me why is this important.

NEVER!

I don't quite understand what do you want to tell me.

Best Regards,
Ales

TomGeorge:
Hi, can you post a copy of your circuit, either a CAD in jpg or png, or a picture of a hand drawn circuit.

This way we can see how you are inputting your buttons and outputting to the stepper controller.

How are you powering the stepper, I hope not from the arduino, the stepper should have a supply that is separated from the arduino, but connected at gnd connections.

Tom....... :slight_smile:

No problem...

I did it like this in the first place and I haven't changed.

It's time you did then. What you are doing makes no sense.

To read only the change of state of the button or the signal on my optocoupler. It is not in the code now as I don't know how to integrate it.

Look at the state change detection example.

Would you be so kind and tell me why is this important.

Do it. Watch your code become much better organized. Decide for yourself. Or, accept that you need to before posting here. I think that you'll find that the former makes your code more understandable TO YOU.

I don't quite understand what do you want to tell me.

Every curly brace belongs on its own line. EVERY ONE!

Hi, thanks mate, nice and simple.

Tom.... :slight_smile:

I tried that button state change but without any luck. Any more ideas?

I tried that button state change but without any luck. Any more ideas?

There's no luck involved. You wire the switch correctly. You write the code correctly. It works. Since you haven't posted a complete wiring diagram or any recent code, you're on your own.

PaulS:

  float selectorValueFloat = round(analogRead(selectorPin) / divider);

WTF? You divide an integer by a float, resulting in a float. You round that off, resulting in an int, which you store in a float. Why?

  //cast to integer for e.g. indexing arrays

sv = selectorValueFloat;



You wouldn't have to do this if you hadn't stored the int in a float in the first place.



> How would I be able to read only change of state


The change of state of what?



if(sv ==0 || stop1 || memo !=sv)
            {
       noTone(12);
     digitalWrite(enable, LOW);
     digitalWrite(stopled, HIGH);
     digitalWrite(startled, LOW);
     has1 = 1;
     memo = sv;
            }



WTF? You haven't learned to indent properly? Use Tools + Auto Format!



}}



NEVER!

WOW Way to overreact right there. Yes the int/float rounding arrangement he has is very convoluted
but there is no reason to be so rude about it.

Indentation is something that they will get the hang of. It isn't a big deal

Every curly brace belongs on its own line. EVERY ONE!

Not true. Putting opening braces on their own line is usually nothing other than a waste of screen space.
Closing braces should be on their own line though.

PaulS:

I tried that button state change but without any luck. Any more ideas?

There's no luck involved. You wire the switch correctly. You write the code correctly. It works. Since you haven't posted a complete wiring diagram or any recent code, you're on your own.

Did you by any chance miss my post? I did post wiring diagram, actually looks more like sketch, but if you have any knowledge in electronics you would know what do I want.
I guess you're one of those guys with a lot of talking and no action. As Elvis would say a little less conversation a little more action please.

Best Regards,
Ales

What powers the pump? It isn't the same supply as the Arduino is it?

smeezekitty:
What powers the pump? It isn't the same supply as the Arduino is it?

No of course not, then optocoupler would be useless.

mravlcax:
Hi all!

First let me describe you my hardware. I have arduino, stepper motor driver and motor. With arduino I am controlling the speed of the motor through CLK on driver, the speed is selected with a rotary switch (works as voltage divider and I am reading value on analog pin), for start and stop I have two pushbuttons. Everything works fine to this point. But when I want to integrate another signal, this one is from pump through optocoupler, I don't receive just pulse but I have constant voltage for the time that pump runs. At this point stepper motor reaches max speed and starts accelarating again, and so on, same way as I would hold my start button. How would I be able to read only change of state and take that as a pulse?

Maybe if we go back to the start and try to understand the problem we will make more progress.

@mravlcax - you should realize that formatting your code in a reasonably standard manner makes it easier for people here to make sense of it, and will probably be easier for you also.

I don't understand what you mean by

But when I want to integrate another signal, this one is from pump through optocoupler, I don't receive just pulse but I have constant voltage for the time that pump runs

. Also, I can't figure out what should happen in response to the optocoupler signal?

If the optocoupler produces a high (or low) signal all the time the pump is running it is logically equivalent to a toggle switch rather than a momentary switch and your code will need to take that into account.

...R