Programming a stepper motor

Hello everyone,

i am a student and i'm working on a project at school. i am building an automatic drying rack. basically when it detects rain it covers the rack and when it stops it opens it. am using a stepper motor with A4899 driver and a rain drop sensor. I am having problems concerning the program. When it detects rain it closes it but do not stop. it countinues to rotate. i have included the code.

please, help me,
t.y

automateddryingrack.ino (1.4 KB)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Do you have limit switches to detect fully open and fully closed cover?

Thanks.. Tom.. :slight_smile:

Note: The statement "n == n;" does nothing useful. It compares a value to itself and throws away the result. What did you intend it to do? Similarly for 'count' and 'value'.

// Connections to A4988
const int dirPin = 2;  // Direction
const int stepPin = 3; // Step


// Motor steps per rotation
const int STEPS_PER_REV = 200;
const int rsensor = A3;
boolean n;
int count;
int value;


void setup() {
  // Setup the pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(rsensor, INPUT);
  Serial.begin(9600);
  n = false ;
  count = 0;
}


void loop () {
  value = analogRead(rsensor);
  Serial.println(value);
  delay(1000);




  if (value > 25 && count == 0) {
    n = true ;
    count == 1;
    // Set motor direction clockwise
    digitalWrite(dirPin, LOW);


    // Spin motor one rotation slowly
    for (int x = 0; x < (STEPS_PER_REV * 2); x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(2000);
    }
    n == n;
    count == count;
    value == value ;


  } else {
    n == n;
    count == count;
    value == value;
  }


  if ((count == 1)  && (n = true) && ( value < 25) )  {
    count = count  - 1 ;


    // Set motor direction counterclockwise
    digitalWrite(dirPin, HIGH);


    // Spin motor two rotations quickly
    for (int x = 0; x < (STEPS_PER_REV * 4); x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(2000);
    }
    n == n;
    count == count;
    value == value ;


  } else {
    n == n;
    count == count;
    value == value;
  }
}

Warnings when you turn up the warning level to ALL (recommended):

/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino: In function 'void loop()':
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:31:11: warning: statement has no effect [-Wunused-value]
     count == 1;
           ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:42:11: warning: statement has no effect [-Wunused-value]
     n == n;
           ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:43:19: warning: statement has no effect [-Wunused-value]
     count == count;
                   ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:44:20: warning: statement has no effect [-Wunused-value]
     value == value ;
                    ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:47:11: warning: statement has no effect [-Wunused-value]
     n == n;
           ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:48:19: warning: statement has no effect [-Wunused-value]
     count == count;
                   ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:49:19: warning: statement has no effect [-Wunused-value]
     value == value;
                   ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:65:11: warning: statement has no effect [-Wunused-value]
     n == n;
           ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:66:19: warning: statement has no effect [-Wunused-value]
     count == count;
                   ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:67:20: warning: statement has no effect [-Wunused-value]
     value == value ;
                    ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:70:11: warning: statement has no effect [-Wunused-value]
     n == n;
           ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:71:19: warning: statement has no effect [-Wunused-value]
     count == count;
                   ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:72:19: warning: statement has no effect [-Wunused-value]
     value == value;
                   ^
Sketch uses 2508 bytes (7%) of program storage space. Maximum is 32256 bytes.
Global variables use 193 bytes (9%) of dynamic memory, leaving 1855 bytes for local variables. Maximum is 2048 bytes.

Even if you change == to = these statements cannot do anything.

    n== n;
    count == count;
    value ==value ;

They are the equivalent of saying "elephant = elephant" - which everyone knows already.

If you want to set the variables back to an initial value then you need separate variables to hold the initial value and which are not changed by your code.

...R

hey john ,
i tried to use the count to know if the stepper motor has already close and open it (one cycle). i used n as a flag to know if it has already closed the rack. can you give me suggestions on the programming itself on what i can do because i'm not that good in c++?
thanks

Hey tom ,
i have not used any limit switches. i tried to close the rack by counting how many revolutions needed to close the rack and stop the motor. but that's not working. can i code it to do this or i have to use limit switches?

You should be able to count the steps while opening and reverse the count to close if the motor is powerful enough to never miss any steps. But you need some way to "home" the motor at the beginning so it always starts from a known position. That is usually done with a limit switch.

Ashish010101:
i have not used any limit switches. i tried to close the rack by counting how many revolutions needed to close the rack and stop the motor. but that's not working. can i code it to do this or i have to use limit switches?

If it will not cause any damage to make the motor continue trying to make steps after it has reached the end then it is perfectly practical to home the motor by getting it to to move more steps than could every be required to get to the end. When it gets to the end it will just start missing steps until all the designated steps have been completed.

At that point you can be confident that the motor is at the end and you can count all subsequent moves from that point PROVIDED you are certain that it will not miss any subsequent steps.

...R