Toggle dirPin in a stepper motor using easydriver

Hi!
I am trying to toggle dirPin (HIGH/LOW)

#include <Stepper.h>
int microSecDelay = 500;          // delay between steps and speed of the motor
int counter = 0;                                // (about as fast as the system can react,
                                // higher number = slower)
int dirPin = 8;                  // output pin for stepper motor direction
int stepPin = 9;                 // output pin for the pin used to step the motor

void setup()
{
 pinMode(dirPin, OUTPUT);       // Assign output mode to pin for direction
 pinMode(stepPin, OUTPUT);      // Assign output mode to pin for setp
 digitalWrite(dirPin, LOW);     // Initialize dir pin
 digitalWrite(stepPin, LOW);    // Initialize step pin
}
void loop()
{
 for(counter=0; counter<1600; counter++)
 {
 digitalWrite(dirPin, HIGH);      // Keep direction pin HIGH
 digitalWrite(stepPin, HIGH);       // Step motor
 delayMicroseconds(microSecDelay);  // Wait microseconds
 digitalWrite(stepPin, LOW);        // Step motor
 delayMicroseconds(microSecDelay);  // Wait microseconds
 Serial.println(counter);
 }
 delay(2000);
 for(counter=0; counter<1600; counter++)
  {
 [b]digitalWrite(8, !digitalRead(8));  // Toggle (motor clogs) stupid smiley is 8[/b]
 digitalWrite(stepPin, HIGH);       // Step motor
 delayMicroseconds(microSecDelay);  // Wait microseconds
 digitalWrite(stepPin, LOW);        // Step motor
 delayMicroseconds(microSecDelay);  // Wait microseconds
 Serial.println(counter);
  }
  delay(2000);
}

This way to toggle did work for me with LEDs like in the example here:

void setup() {
 pinMode(13, OUTPUT);  // use on-board LED
}
void loop() {
 digitalWrite(13, !digitalRead(13));  // toggle state
 delay(1000);  // wait around for 1 sec (1000 ms)
}

Why doesn´t work with stepper?
Thanx!

The reason for the stupid smilye is because you did not use the code button </> so your code looks like this and is easy to copy to a text editor. Please modify your post and show the code correctly. See How to use the Forum

This Simple Stepper Code should work with the Easydriver.

Post a link to the datasheet for your stepper motor.

And give details of the power supply for the motor (volts and amps).

…R
Stepper Motor Basics

Next time, use code tags, as I've added for you.
If you just want to change a pin from HIGH to LOW, or from LOW to HIGH, simple way is with direct port manipulation.
For an Uno, 8 in PORTB bit 0, so you would do this:

PINB = PINB | 0b00000001;  // toggle bit 0 of PORT B by writing to the Input register

If you're expecting it to do more than just change levels, then check your logic flow, might be an error.

Hi!
I am trying to develop a program that makes a stepper changing direction depending on the difference in light between one step and the next one. For that I need to toggle dirPin
I found somewhere in internet that I could do it with

digitalWrite(dirPin, !analogRead(8));

But it is not working. I tryed also with

digitalWrite(dirPin, analogRead(!8));    
digitalWrite(dirPin, analogRead(-8));    
digitalWrite(dirPin, -analogRead(8));

This is the code I am trying

int a,b,c;
int LDR=0;
int dirPin=8;
int stepPin=9;
int microSecDelay=2000;
int counter=0;
int pause=500;
int steps=50;

void setup() {
  pinMode(LDR, INPUT);                  // Assign input for photoresistor
  pinMode(dirPin, OUTPUT);             // Assign output mode to pin for direction
  pinMode(stepPin, OUTPUT);          // Assign output mode to pin for setp
  digitalWrite(dirPin, HIGH);             // Initialize dir pin 
  digitalWrite(stepPin, LOW);           // Initialize step pin
  Serial.begin(9600);
}

void loop() {
a = analogRead(LDR);
digitalWrite(dirPin, HIGH);                             // Direction clockwise
   for(counter=0; counter<steps; counter++)   // Decides lenght of the step
    {
    digitalWrite(stepPin, HIGH);                       // Steps motor
    delayMicroseconds(microSecDelay);           // Wait microseconds 
    digitalWrite(stepPin, LOW);                       // 
    delayMicroseconds(microSecDelay);           // 
    }
Serial.print("a= ");
Serial.println(a);
b = analogRead(LDR);
if(b>=a)
{
digitalWrite(dirPin, analogRead(dirPin));
}
else
digitalWrite(dirPin, analogRead(-dirPin));
    for(counter=0; counter<steps; counter++)     // Decides lenght of the step
    {
    digitalWrite(stepPin, HIGH);                         // Steps motor
    delayMicroseconds(microSecDelay);              // Wait microseconds 
    digitalWrite(stepPin, LOW);                          // 
    delayMicroseconds(microSecDelay);              // 
    }
Serial.print("b= ");
Serial.println(b);  
delay(10000);

Any suggestion about how can I toggle dirPin??
Thanx!

analogRead() returns a number, not a truth value. You have to convert that number to
some boolean representing that the value is too big, or too small, or outside some range or
whatever:

#define THRESHOLD 512  // for instance, halfway point
...

digitalWrite (dirPin, analogRead(8) > THRESHOLD);
... or
digitalWrite (dirPin, analogRead(8) < THRESHOLD);

This is the same question as in your other Thread that you never bothered replying to. Don’t double post.

…R

Hi Robin and thanx! sorry about not replying, As a beginner I was lost with the port manipulation from CrossRoads and didn´t actually realize! thanx for the links!

The stepper has no number whatsoever. I don´t have a clue about it. I just found out has 1600 steps or very close number by making it tu turn once. I just got it from someone who had it. Thanx CrossRoads and Mark :slight_smile:

If you are still looking for assistance please remind us what you need help with. This Thread is rather confused now. If you have newer code that is not working please post it.

...R

olbapnairda:
Hi Robin and thanx! sorry about not replying, As a beginner I was lost with the port manipulation from CrossRoads and didn´t actually realize! thanx for the links!

The stepper has no number whatsoever. I don´t have a clue about it. I just found out has 1600 steps or very close number by making it tu turn once. I just got it from someone who had it. Thanx CrossRoads and Mark :slight_smile:

Then measure the winding resistance and report back.