#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)
}
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
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
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);
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
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.
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
Then measure the winding resistance and report back.