# CD-rom stepper motor + magnetic contact ( NO )

Hi to all !

Am using this code to drive cd-rom stepper motor, and it’s working great.

``````#define DIR_PIN 2
#define STEP_PIN 3

void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
}

void loop(){

//rotate a specific number of degrees
rotateDeg(180, .5);
delay(1000);

rotateDeg(-180, .5);  //reverse
delay(5000);

/*
//rotate a specific number of microsteps (8 microsteps per step)
//a 200 step stepper would take 1600 micro steps for one full revolution
rotate(1600, .5);
delay(1000);

rotate(-1600, .25); //reverse
delay(1000);
*/
}

/*
void rotate(int steps, float speed){
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);

digitalWrite(DIR_PIN,dir);

float usDelay = (1/speed) * 70;

for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);

digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
}
}
*/

void rotateDeg(float deg, float speed){
//rotate a specific number of degrees (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (deg > 0)? HIGH:LOW;
digitalWrite(DIR_PIN,dir);

int steps = abs(deg)*(1/0.225);
float usDelay = (1/speed) * 70;

for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);

digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
}
}
``````

How can i add that the program in loop starts after the magnetic contact is triggered ?

So, program should wait until magnetic contact is triggered and when this is done, code in the loop part should be executed. After that, program should wait for another “impulse”.

Basically, instead of looping all the time, program should execute only on NC magnetic contact.

Btw. this is the magnatic contact that i use :
http://www.belcosecurity.com/products/AMC001---Miniature-Magnetic-Sensor/79

Best regards !
Arnix

Connect one wire from the switch to GND, the other to pin 4 and try this mod:

``````#define DIR_PIN 2
#define STEP_PIN 3
#define triggerPin 4

bool triggered;

void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(triggerPin, INPUT_PULLUP);
}

void loop()
{
if (triggered == false && triggerPin == LOW)
{
triggered = true;

//rotate a specific number of degrees
rotateDeg(180, .5);
delay(1000);

rotateDeg(-180, .5);  //reverse
delay(5000);

triggered = false;
}
}

void rotateDeg(float deg, float speed)
{
//rotate a specific number of degrees (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (deg > 0) ? HIGH : LOW;
digitalWrite(DIR_PIN, dir);

int steps = abs(deg) * (1 / 0.225);
float usDelay = (1 / speed) * 70;

for (int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);

digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
}
}
``````

Hi !
I tested your suggestion today, but is looks something is missing.
When i upload the code and give contact ( just shortcut with wire between pin 4 and GND ), nothing happens...

It looks like, that the code isnt passing the IF condition...

Best regards !

For some unknown reason i can not login with my old account (Arnix). I try to reset password, 3 times and after resetting it, i get error message, "ups something went wrong" , so i created new account.

Does someone knows why the code suggested by 756E6C , isnt working ?

Arnix

Usernames have become case sensitive. Pay attention to where you had capital letters before and try again.