[Q] Stepping motor in a servo program

Hi people,

first to let you guys know thet my skills arent high in programming but i try!

My project:
1 - Servo motor
1 - Stepping motor
1 - webcam
1 - mouse + processing

I already did it to let my servo motor run on this program (like 180 decrees)

My program in arduino:

#include <Servo.h>
Servo One;
Servo Two;

int incomingPos = 0;
int servoX = 0;
int servoY = 0;

void setup()
{
  byte incomingPos;
  Serial.begin(9600);
  
  One.attach(9);
  Two.attach(10);
}
  

void loop()
{
if (Serial.available() >= 2)
  {
    servoX = Serial.read();    //  Put the serial input into the message
    servoY = Serial.read();    //  Put the serial input into the message
    One.write(servoX);
    Two.write(servoY);     
  }
  
  
 }

My processing code:

 import processing.serial.*;
 Serial port;
 
 void setup() {
 size(180, 180);
 
 println("Available serial ports:");
 println(Serial.list());
 
 
 port = new Serial(this, "COM3", 9600);
 }
 
 void draw() {
 // draw a gradient from black to white
 for (int i = 0; i < 180; i++) {
 stroke(i);
 line(i, 0, i, 180);
 }
 
 // write the current X-position of the mouse to the serial port as
 // a single byte
 port.write(mouseX);
 port.write(mouseY);
 }

Now, my the question is i got a step motor (i dont have another servo motor) what i have to get work with digitalWrite and a puls like 200 for a round.

How do i combined this kind of things.. i really searched alot on the internet but i cant find it.

I hope there is sombody so smart to help me :grin:

p.s.

Dutch answers are good to! :*

Thanks!

Kind Regards,

Vleud101

Nobody with the skills ? :grin:

There are plenty of people with the skills, but they will want more information from you. What type of stepper motor is it? How is it connected to your Arduino? Are you using a motor shield? How are you powering the motor? An arduino isn't capable of powering most stepper motors without an external power source.

hi, yea ok but it is difficult to say what kind of information i need to give

I use a microstep driver, so i only need to give a direction and a pulse. Like this:

int pulpin = 9;    // Puls to microstep driver
int revPin = 8;      // direction 
int i = 0;

void setup()  { 
  pinMode(pulpin, OUTPUT);      // sets the digital pin as output
  pinMode(revPin, OUTPUT);
   
}

    void loop()
{
  
do
{
  i = i + 1; 

  
 digitalWrite(pulpin,1);
delayMicroseconds(650);
 digitalWrite(pulpin,0);
delayMicroseconds(650);
 
} while (i < 200);
 digitalWrite(revPin,0);
delay(1000);
i = 0;
do
{
  i = i + 1; 
  
 digitalWrite(pulpin,1);
delayMicroseconds(2400);
 digitalWrite(pulpin,0);
delayMicroseconds(2400);
 
} while (i < 200);
 digitalWrite(revPin,1);
  
i = 0;
delay(2000);

}

Well, one obvious thing to say is don't use pin 9 for both the servo and the stepper motor.
But I don't think you've really said what your problem is.

I posted the second script as a example,

My problem is how i can control a servo motor and a stepper motor with X and Y from a Mouse.
I got this working with 2 servo motors, only i wanne use a steppermotor for one of the 2 servo's.

So like:

Servo = mouseY
Steppermotor = mouseX

But i use the servo.h and i dont know how to script my steppermotor at thet point. So i got a signal from processing trough serial port, and wanne translate it to my servo and my steppermotor.

I hope you understand my problem at this point

You keep a variable with the current stepping motor position in it. When a request comes along to move it to a new location you take the current position and work out how many steps you need to go and in what direction to get to the new position. Then you give the motor the required nu ber of steps to get to the new position. Then update the current position.

Thank you,

i go try it! :slight_smile:

#include <Stepper.h>
#include <Servo.h>

Servo One;  // Servo Motor
Servo Two;  // Stappen Motor
int revPin = 7;      // direction 
int i = 0;            // counter

int incomingPos = 0;
int servoX = 0;  // Servo motor
int servoY = 0;  // Stappen motor

void setup()
{
  byte incomingPos;
  Serial.begin(9600);
  
  One.attach(9);  // Servo motor
  Two.attach(10); // Stappen motor
  
}
  

void loop()
{
if (Serial.available() >= 2)
  {
    servoX = Serial.read();    //  Put the serial input into the message
    servoY = Serial.read();    //  Put the serial input into the message
    One.write(servoX);          // Servo motor
    digitalWrite(servoY, 1);    // Stappen motor 
    delayMicroseconds(650);    // Delay
  }
  
  
 }

Now i wanne use pinMode(Two, OUTPUT); but it says he cant recognize it because he doesn't read 1 or 0. How do i convert this?

Thank you for replying :+)

Two is a Servo object, not a pin

Yea ok,

So i make a int from it, but how do i change this to the right valua for my stepper motor?

I dont know how to read what my processing program writing to my com port :blush:

You are reading ASCII bytes from the serial port. You can't just write them to a servo, it makes no sense at all.

Yes i understand that. But what i mean, i need to know what i need to do. To change the serial input (from processing) to a variable what i can use for my stepper motor.

thanks :blush:

If you are still using the Processing code posted in the first post, that code is sending binary data, not ASCII data, to the Arduino. So, the process you have for reading the data, and passing the value directly to the servo is correct.

This, however is wrong:

    digitalWrite(servoY, 1);    // Stappen motor

The value being sent for the Y coordinate is NOT a pin number.

Exactly i already changed it, i update my code:

#include <Stepper.h>
#include <Servo.h>

Servo One;  // Servo Motor  
int pulpin = 10; // Stappen Motor
int revPin = 7;      // direction 
int i = 0;            // counter

int incomingPos = 0;
int servoX = 0;  // Servo motor
int servoY = 0;  // Stappen motor

void setup()
{
  byte incomingPos;
  Serial.begin(9600);
  
  One.attach(9);  // Servo motor
  pinMode(pulpin, OUTPUT);
  
}
  

void loop()
{
if (Serial.available() >= 2)
  {
    servoX = Serial.read();    //  Put the serial input into the message
    servoY = Serial.read();    //  Put the serial input into the message
    One.write(servoX);          // Servo motor
 [color=red]   digitalWrite(pulpin,1);
    delayMicroseconds(650);
    digitalWrite(pulpin,0);
    delayMicroseconds(650);[/color]
  }
  
  
 }

The red part, is the part what i really dont know what to do with. My skills are to low, to make a connection between (my servoY valua) and the digitalWrite() code.

I hope some of you know it (The google says nothing :|)

Thanks

The red part, is the part what i really dont know what to do with.

All that code does is to pulse the motor one time, this will move the motor only 1 step.

What you need to do here is to call a function to write as many pulses to the motor as you need to. Like I said in an earlier reply you need to work out how many pulses to give the motor to get it from where it is to where you want it to be.

The red part, is the part what i really dont know what to do with.

Only you know what you want the stepper motor to do when the mouse on the PC moves in the Y direction.

You are not creating an instance of the Stepper class. Why not?

You need not worry about an individual pin. You need to make the Stepper instance do something when servoY changes.