# limitswitch , arduino and easy drive

I wanna make a project but I didn't create arduino code please help me ! ...scenario is here ;

materials ;
1 x arduino , 2 limitswitch ( refer to limitx and limity) , 1 stepper motor

at the start , stepper motor rotate any direction , if limitx is HIGH stepper motor change direction of rotation and similarly if limity is HIGH again stepper motor change direction of rotation.hopefully , you got it

Is this homework ?

actually ,for a long time I have made a project and this one is only part of project

What do you want us to do?

If you have a program that is not working properly you need to post it and explain what it is actually doing and what it ought to do.

Break the problem down. Do you know how to do each of these ?

Rotate the stepper motor in either direction
Change the direction of rotation of the stepper motor
Read a limit switch and determine its state

third one, belt connect to stepper motor and I have an object so this object move y axis due to stepper motor . firstly , stepper motor move downstream and if limity is high , stepper motor change direction of rotation and will move upstream then if limitx is HIGH , again stepper motor change direction of rotation...sorry about that my english level is not enough , I hope I made myself clear

I think that I understand the requirement, but the language barrier is getting in the way.
This forum has sections in other languages. What is your native language ?

my native language is türkish

commando:
my native language is türkish

Unfortunately there is no Turkish section of this forum so we will have to do our best here.

Have you read the links that Robin included in post #3 ?
Does your project already use an Arduino or are the stepper motor and limit switches new parts of it ?

yeap I read post #3 but I didn't get it .I write somethings , but this code make only one statement , if I energized this system ,imotor move upstream and limitx is HIGH so stepper motor stop

int dirpin = 8;
int steppin = 9;
int pinx = A0 ;
int piny = A1;
int i;

void setup()
{

pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(pinx,INPUT);
pinMode(piny,INPUT);
}

void loop()
{

digitalWrite(dirpin, HIGH);
delay(100);

for (i = 0; i<50000; i++) // Iterate for 4000 microsteps.
{
digitalWrite(steppin, HIGH); // This LOW to HIGH change is what creates the
delayMicroseconds(1000);
digitalWrite(steppin, LOW); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(1000); // This delay time is close to top speed for this

if ( t == HIGH ) {

digitalWrite(dirpin, LOW); // Set the direction.
delay(100);
break;

}
}

}

Each time through loop() you set dirpin HIGH regardless of its current state. Is that what you want to do ?

the problem has been resolved by me, if anybody need similar code , I wanna share it . that's really simple

// created by commando
//09.05.2016

int dirpin = 3;
int steppin = 4;
int pinx = A0 ;
int piny = A1;
int i;
int laser =11 ;
int lamp =2 ;
void setup()
{

pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(pinx,INPUT);
pinMode(piny,INPUT);
pinMode (laser, OUTPUT);
pinMode (lamp, OUTPUT);
}

void loop()
{
digitalWrite(lamp, HIGH);
digitalWrite(laser, HIGH);
digitalWrite(dirpin, HIGH);
delay(100);

for (i = 0; i<50000; i++)
{
digitalWrite(steppin, HIGH);
delayMicroseconds(1000);
digitalWrite(steppin, LOW);
delayMicroseconds(1000);

if ( t == LOW ) {

digitalWrite(dirpin, LOW);
delay(10);

}
if ( k == LOW ) {

digitalWrite(dirpin, HIGH);
delay(10);
}
}
}

Good for you for getting it working. Now, can you tidy it up and make it use less memory ? Not important in this program but you should always aim to declare variables of a type appropriate for the values that they are expected to hold. For instance, each of these variables takes 2 bytes

``````int dirpin = 3;
int steppin = 4;
int pinx = A0 ;
int piny = A1;
int i;
int laser = 11 ;
int lamp = 2 ;
``````

Could any of them be declared as byte ?

Taking it further, the value of some of those variables will never change in the program so could be declared as const. Pin numbers are always good candidates to be declared as const because by definition they will not change.

Another observation. You have declared i as a global int but the only place that it is used is in a for loop in the loop() function. There is nothing inherently wrong with this but for transient variable like this it is more normal to declare them as part of the for loop like this

``````  for (int i = 0; i < 50000; i++)
``````

Now it is more obvious that i is an int and it is expected to hold values between 0 and 49999. Is an int able to hold a number as big as 49999 ?

I see that you have declared local variables named t and k as ints but the values they are required to hold are either 0 or 1. Could they be a smaller data type and could they have a more descriptive name ? Could you even remove the need for intermediate variables such as t and k by doing the digitalRead()s in the if clause ?