Hi everyone,
I'm new to Arduino and been trying to get some code working for making a cine film scanner. I have copied parts of code from other setups but I know I must be doing something wrong!
Anyway this setup is basically for keeping tension on the film and also having a rewind option. I have two limit switches that activate when there is tension on the film and then will turn the stepper motor to give some slack. This is for both the supply and take up reels.
There is also rewind button which will rotate the stepper faster to rewind the film after scanning.
Anyway the code kind of works with issues. Currently only one stepper motor will work at a time, but they both need to be able to work at the same time. Also the rewind option will work normally but once limit switch is activated it will start moving slowly.
I might also want to add an IR breakbeam to rewind option so it cannot be activated by mistake and must be a break before it can be activated.
I don't know if my const int are correct or if I should be using #define instead.
Hope this makes sense!
Here is the code below:
/*
Control "Supply Reel", "Take-Up Reel" and "Rewind Reel" on Raspberry Pi Film Scanner.
Parts used:
1x Arduino Pro Mini
2x DVR8825 Driver
2x Limit Switch
1x ON/OFF Switch
DVR8825 pinout:
_________
Enable <- | _ | -> Voltage Motor (8,2V - 45V)
M0 <- | |_| | -> Ground
M1 <- | | -> B2
M2 <- | DVR8825 | -> B1
Reset <- | | -> A1
Sleep <- | ___ | -> A2
Step <- | |___| | -> Fault
Direction <- |_________| -> Ground
Stepper motor steps:
MODE0 | MODE1 | MODE2 -> Microstep Resolution
---------------------------------------------
Low | Low | Low -> Full step
High | Low | Low -> Half step
Low | High | Low -> 1/4 step
High | High | Low -> 1/8 step
Low | Low | High -> 1/16 step
High | Low | High -> 1/32 step
Low | High | High -> 1/32 step
High | High | High -> 1/32 step
DRV8825 Driver rotation direction (dirPin):
HIGH -> Clockwise
LOW -> Anti-clockwise
*/
//Supply Reel DVR8825 Driver Pins (A)
const int stepPin_A = 2; // Define pin 2 as the steps pin
const int dirPin_A = 3; // Define pin 3 as the direction pin
const int limitSwitchPin_A = 4; // Define pin 4 for Limit Switch 1
const int resetPin_A = 5; // Pin 5 connected to RESET pin
const int M0_A = 6; // Define pin 6 as "M0"
const int M1_A = 7; // Define pin 7 as "M1"
const int M2_A = 8; // Define pin 8 as "M2"
const int rewindSwitchPin_A = 9; // Define pin 9 for Switch for rewind
//Take-up Reel DVR8825 Driver Pins (B)
const int stepPin_B = 10; // Define pin 3 as the steps pin
const int dirPin_B = 11; // Define pin 4 as the direction pin
const int limitSwitchPin_B = 12; // Define pin 12 for limit switch 2
const int resetPin_B = 13; // Define pin 13 connected to reset pin
const int M0_B = 14; // Define pin 14 as "M0"
const int M1_B = 15; // Define pin 15 as "M1"
const int M2_B = 16; // Define pin 16 as "M2"
//Variables will change (switches):
int rewindSwitchState_A = 0;
int limitSwitchState_A = 0;
int limitSwitchState_B = 0;
void setup() {
//Supply Reel Setup
pinMode(stepPin_A, OUTPUT); // Configures "STEP" as output
pinMode(dirPin_A, OUTPUT); // Configures "DIR" as output
pinMode(limitSwitchPin_A, INPUT); // Configures "limit switch" as input
pinMode(resetPin_A, OUTPUT); // Configures reset pin as output
pinMode(M0_A, OUTPUT); // Configures "M0" as output
pinMode(M1_A, OUTPUT); // Configures "M1" as output
pinMode(M2_A, OUTPUT); // Configures "M2" as output
pinMode(rewindSwitchPin_A, INPUT); // Configures "rewind switch" as input
//Take-up Reel Setup
pinMode(stepPin_B, OUTPUT); // Configures "STEP" as output
pinMode(dirPin_B, OUTPUT); // Configures "DIR" as output
pinMode(limitSwitchPin_B, INPUT); // Configures "limit switch" as input
pinMode(resetPin_B, OUTPUT); // Configures reset pin as output
pinMode(M0_B, OUTPUT); // Configures "M0" as output
pinMode(M1_B, OUTPUT); // Configures "M1" as output
pinMode(M2_B, OUTPUT); // Configures "M2" as output
}
void loop() {
// Read the state of the limit switches
limitSwitchState_A = digitalRead(limitSwitchPin_A);
limitSwitchState_B = digitalRead(limitSwitchPin_B);
rewindSwitchState_A = digitalRead(rewindSwitchPin_A);
// If all switches are off do nothing
if (limitSwitchState_A == LOW && limitSwitchState_B == LOW && rewindSwitchState_A == LOW) {
digitalWrite(resetPin_A, LOW);
digitalWrite(resetPin_B, LOW);
}
// If rewind switch is on then rotate anticlockwise 1 full turn (NB. Need to add sensor (IR breakbeam) to detect if film is in correct position)
else if (limitSwitchState_A == LOW && limitSwitchState_B == LOW && rewindSwitchState_A == HIGH){
digitalWrite(resetPin_A, HIGH);
digitalWrite(dirPin_A, HIGH); // Rotate anitclockwise
digitalWrite(M0_A, LOW); // Configures the steps division (Full step)
digitalWrite(M1_A, LOW); // As above
digitalWrite(M1_A, LOW); // As above
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin_A, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin_A, LOW);
delayMicroseconds(500);
}
}
// If limit switch A is on then rotate anticlockwise 1/32 step
else if (limitSwitchState_A == HIGH && rewindSwitchState_A == LOW) {
digitalWrite(resetPin_A, HIGH);
digitalWrite(dirPin_A, LOW); // Rotate anticlockwise
digitalWrite(M0_A, HIGH); // Configures the steps division (1/32 step)
digitalWrite(M1_A, HIGH); // As above
digitalWrite(M2_A, HIGH); // As above
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin_A, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin_A, LOW);
delayMicroseconds(1000);
}
}
// If limit switch B is on then rotate clockwise 1/32 step
else if (limitSwitchState_B == HIGH && rewindSwitchState_A == LOW) {
digitalWrite(resetPin_B, HIGH);
digitalWrite(dirPin_B, HIGH); // Rotate clockwise
digitalWrite(M0_B, HIGH); // Configures the steps division (1/32 step)
digitalWrite(M1_B, HIGH); // As above
digitalWrite(M2_B, HIGH); // As above
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin_B, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin_B, LOW);
delayMicroseconds(1000);
}
}
}