So, my objective is to control SCARA arm, but issue is both arms need to move simultaneously (There is variation in rotation θ1 and θ2, but for now simultaneous rotation is okey)
after searching in forums. I get to know we can directly manipulate port to reduce delay due to digitalRead and digitalWrite also having advantage to doing simultaneous operation, also came across timer (of crystal oscillator) to replace it with delays.
but I don't understand how. Is there tutorial for that?
This is my program that I done from what I understand.
/*
Stepper Motor Tester v3 @Kunal Panchal (MicroStepping)
Board - Arduino Uno R3
*/
//Config
int Pulse_Delay = 100;
int cwStep = 10000;
int ccwStep = 10000;
int Microstep_Resolution = 16;
void setup() {
DDRB |= 0x1f; //Equivalent to setting pin 12,11,10,9,8 as output !
DDRD |= 0xfc; //Equivalent to setting pin 7,6,5,4,3,2 as output !
/*
//how to put input_pullup ?
DDRB |= 1<<DDB2;
PORTB |= 1<<DDB2; //send pin high for pullup.... ?
DDRB |= 1<<DDB2;
//well it's not working though
*/
pinMode(2, INPUT); //above code as example for this as input_pullup ?
pinMode(A0, INPUT_PULLUP); //if correct same logic apply to analog pin ?
pinMode(A1, INPUT_PULLUP);
//MicroStepping logic
if (Microstep_Resolution == 32){
PORTB |= 0x07;
PORTD |= 0x38;
/*
Example for PORTB |= 0x07;
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
*/
}
if (Microstep_Resolution == 16){
PORTB |= 0x04;
PORTD |= 0x20;
/*
Example for PORTB |= 0x04;
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
*/
}
if (Microstep_Resolution == 8){
PORTB |= 0x03;
PORTD |= 0x18;
/*
Example for PORTB |= 0x03;
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
*/
}
if (Microstep_Resolution == 4){
PORTB |= 0x02;
PORTD |= 0x10;
/*
Example for PORTB |= 0x02;
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
*/
}
if (Microstep_Resolution == 2){
PORTB |= 0x01;
PORTD |= 0x08;
/*
Example for PORTB |= 0x01;
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
*/
}
if (Microstep_Resolution == 1){
PORTB &= 0;
PORTD &= 0;
/*
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
*/
}
}
void RUN_all (){
PORTB |= 0x08; //pulse 1
PORTD |= 0x40; //pulse 2
delayMicroseconds(Pulse_Delay);
PORTB &= 0xf7; //pulse 1
PORTD &= 0xbf; //pulse 2
delayMicroseconds(Pulse_Delay);
}
void RUN_1 (){
PORTB |= 0x08; //pulse 1
delayMicroseconds(Pulse_Delay);
PORTB &= 0xf7; //pulse 1
delayMicroseconds(Pulse_Delay);
}
void RUN_2 (){
PORTD |= 0x40; //pulse 2
delayMicroseconds(Pulse_Delay);
PORTD &= 0xbf; //pulse 2
delayMicroseconds(Pulse_Delay);
}
void loop(){
if (digitalRead(2)==HIGH){
/*
Is there way to digital read using port like
if (PORTD == (1<<PD2)){"command"} //well ig its completly wrong ?
*/
if (digitalRead(A0)==HIGH && digitalRead(A1)==LOW){
PORTB |= 0X10; //Direction Signal 1
for (int i=0; i<=cwStep; i++)
RUN_1();
PORTB &= 0xEF; //Direction Signal 1
delay(1000);
for (int i=0; i<=ccwStep; i++)
RUN_1();
delay(1000);
}
if (digitalRead(A0)==LOW && digitalRead(A1)==HIGH){
PORTD |= 0x80; //Direction Signal 2
for (int i=0; i<=cwStep; i++)
RUN_2();
PORTD &= 0x7f; //Direction Signal 2
delay(1000);
for (int i=0; i<=ccwStep; i++)
RUN_2();
delay(1000);
}
if (digitalRead(A0)==HIGH && digitalRead(A1)==HIGH){
PORTB |= 0X10; //Direction Signal 1
PORTD |= 0x80; //Direction Signal 2
for (int i=0; i<=cwStep; i++) //simultaneous operation same θ
RUN_all();
PORTB &= 0xEF; //Direction Signal 1
PORTD &= 0x7f; //Direction Signal 2
delay(1000);
for (int i=0; i<=ccwStep; i++) //simultaneous operation same θ
RUN_all();
delay(1000);
}
if (digitalRead(A0)==LOW && digitalRead(A1)==LOW){
PORTB |= 0X10; //Direction Signal 1
PORTD |= 0x80; //Direction Signal 2
for (int i=0; i<=cwStep; i++) //simultaneous operation same θ
RUN_all();
PORTB &= 0xEF; //Direction Signal 1
PORTD &= 0x7f; //Direction Signal 2
delay(1000);
for (int i=0; i<=ccwStep; i++) //simultaneous operation same θ
RUN_all();
delay(1000);
}
}
}
So, what I want help with
- Direct port manipulation to Set pin as INPUT, OUTPUT, INPUT_PULLUP. I know DDRx but don't know how it works i seen different variation in code.
- Direct port manipulation to Set pin as HIGH, LOW, So I suppose "|=" for High and "&=" for low? And why it don't interrupt my old "|=" (HIGH)
- Direct port manipulation to Read pin, Well I don't understand anything about read with PORTx.
- Is it different for analog pin? And what about analogRead and analogWrite as direct PORT manipulation (Just curious not that important for my project)
- And how i can replace delays with timer.
I attached image for the circuit.
