Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Stepper sync from encoder? on: December 28, 2012, 06:40:30 pm
PaulS:

Thanks for your reply.

The code(sketch) I need:
"use Rotary Encoder(OUTPUT A, B) to control Stepper motor(DIR, STEP), when encoder turns 1 rev. , Stepper motor turns 3 rev.)

(Using Interrupt in code is OK)
2  Using Arduino / Programming Questions / Stepper sync from encoder? on: December 28, 2012, 05:26:21 pm
Hi Everyone:

I am very new to Arduino.
Could somebody write code for me? I want to use encoder to control Stepper(sync 1:1 or any changeable Ratio, like 1:3).
BTW, I prefer NOT use "Interrupt" to achieve, because I will use this code as one subroutine in my loop, "Interrupt" may cause mess in loop.

Thanks in advance.

Richard
3  Using Arduino / Programming Questions / How to use Timer to exit LoopA,then run LoopB? on: November 27, 2012, 10:54:02 pm
Hi, I am very new to Arduino, my question is:
How to use Timer to do this:  Run LoopA 10sec. then Run LoopB 2sec., then LoopA 10sec. LoopB 2sec., repeatly. Could somebody write some code for me? Thanks in advance.

????
void LoopA()
????
void LoopB()
4  Using Arduino / Programming Questions / Re: Need some help to complete my code! on: November 21, 2012, 07:12:56 am
Quote
I try to use additional Subroutines(Toggle between "Matching" and "Run 300 Rev.") to do that, but failed. (because Interupt??)
I don't see any evidence of that in your code. Nor do I see any proximity sensor on pin 7.

Nor, do I understand what the encoders are connected to or why you are counting motor steps, or why motor_position is volatile when it is only referenced in loop.

This is original code, I did test, It works, when encoder turn, stepper motor will turn, My question is: I need add sensor on PIN7, once get sensor signal, motor will turn 300 rev, ....loop..


Thanks
5  Using Arduino / Programming Questions / Need some help to complete my code! on: November 20, 2012, 11:06:03 pm
Somebody gave me the following code,  the function: stepper motor to match encoder position.

I'd like modify it, add one "Proximity sensor" on PIN7, when get sensor signal, stepper motor will run 300 Rev., after that, motor start to match encoder motion again, then run 300 Rev., ...over and over again.

I try to use additional Subroutines(Toggle between "Matching" and "Run 300 Rev.") to do that, but failed. (because Interupt??)

Could somebody add additional code for me?

Thanks in advance!!

Code:
#define encoder_a 2
#define encoder_b 3
#define motor_step 5
#define motor_direction 6

volatile long motor_position, encoder;

void setup () {
Serial.begin(11500);
pinMode(motor_step, OUTPUT);
pinMode(motor_direction, OUTPUT);
pinMode(encoder_a, INPUT);
pinMode(encoder_b, INPUT);
digitalWrite(encoder_a, HIGH);
digitalWrite(encoder_b, HIGH);

attachInterrupt(0, encoderPinChangeA, CHANGE);
attachInterrupt(1, encoderPinChangeB, CHANGE);
encoder = 0;
}

void loop() {
if (encoder > 0) {
digitalWrite(motor_direction, HIGH);
digitalWrite(motor_step, HIGH);
digitalWrite(motor_step, LOW);
delay(200);
motor_position++;
encoder = 0;
}
else if (encoder < 0) {
digitalWrite (motor_direction, LOW);
digitalWrite (motor_step, HIGH);
digitalWrite (motor_step, LOW);
delay(200);
motor_position--;
encoder = 0;
}
}

void encoderPinChangeA() {
if (digitalRead(encoder_a)==digitalRead(encoder_b)) {
encoder--;
}
else{
encoder++;
}

}

void encoderPinChangeB() {
if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
encoder--;
}
else {
encoder++;

}

}

Moderator edit:  CODE TAGS
6  Using Arduino / Programming Questions / Re: how to toggle between two subrountines? on: November 11, 2012, 09:37:30 am
Could you give me some example codes? Thanks!
7  Using Arduino / Programming Questions / how to toggle between two subrountines? on: November 11, 2012, 09:25:41 am
Hi, I am very new to Arduino.
My questions: How to use "Timer" or "Sensor" to toggle between "Subroutine-A" and "Subroutine-B" easily?

1. Enable "Subroutine-A"; Disable "Subroutine-B"
2. After 30 seconds or read Sensor signal, Disable "Subroutine-A"; Enable "Subroutine-B"
3. Do above again and again for 45 min.

Could somebody give me example codes? (user "Timer" or use "sensor" solution, or both)

Thanks very much in advance.


8  Using Arduino / Programming Questions / How to use sensor signal trigger/stop code execution? on: November 10, 2012, 04:05:27 pm
Hi I am very new to Arduino, so please excuse the simplicity of my question. 
I try to do this:
Read 1st. Proximity sensor signal, then trigger/start my code execution, once get 2nd. Proximity sensor signal, then stop my code execution, and do something else, such as rotate stepper motor 500 rev.

Could somebody give me similar example to achieve this?

Thanks very much in advance.




9  Using Arduino / Motors, Mechanics, and Power / Re: Arduino Encoder to Stepper Motor on: May 24, 2012, 07:53:24 pm
kf2qd:

Thank you very much for your post.

In my mind, Motor Shield works as stepper driver.

Could I add "AFmotor.h" in the code?

Hope someone can help me modify above code.

Thanks
10  Using Arduino / Motors, Mechanics, and Power / Arduino Encoder to Stepper Motor on: May 23, 2012, 07:59:40 pm
Hi, Everyone:

I am new to arduino, I found "Arduino Encoder to Stepper Motor " on youtube, it's very similar as what I want to do, but the code is not complete!

1) arduino duemilanove
2) Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0
3) Stepper motor (Connect to M3 & M4)
4) Encoder from old printer(connect to PIN2 & PIN3)

My questions:
 how to declare "motor_step" & "motor_direction"?
   INT motor_step=????
   INT motor_direction=????
   INT encoder_a=2;
   INT encoder_b=3;
---------------------------------------------------




volatile long motor_position, encoder;

void setup () {

//set up the various outputs
pinMode (motor_step, OUTPUT);
pinMode (motor_direction, OUTPUT);
// then the encoder inputs
pinMode (encoder_a, INPUT);
pinMode (encoder_b, INPUT);
digitalWrite (encoder_a, HIGH);
digitalWrite (encoder_b, HIGH);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, encoderPinChangeA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, encoderPinChangeB, CHANGE);
encoder = 0;
}

void loop() {
//do stuff dependent on encoder position here
//such as move a stepper motor to match encoder position
//if you want to make it 1:1 ensure the encoder res matches the motor res by dividing/multiplying
if (encoder > 0) { digitalWrite (motor_direction, HIGH);
digitalWrite (motor_step, HIGH);
digitalWrite (motor_step, LOW); _delay_us(200); motor_position++; encoder = 0; }
else if (encoder < 0)  {
digitalWrite (motor_direction, LOW);
digitalWrite (motor_step, HIGH);
digitalWrite (motor_step, LOW); _delay_us(200);
motor_position--;
encoder = 0;
}}

void encoderPinChangeA() {
 if (digitalRead(encoder_a)==digitalRead(encoder_b)) {
encoder--;
 } else{
encoder++;
 }
}
void encoderPinChangeB() {
 if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
 encoder--; 
 } else { encoder++; }
}
Pages: [1]