Hi,
I'm hacking an old film projector and I've a problem with the code.
When I use the full code with "Motor" (foto_01) and "sensor"(foto_02) option, the optocoupler doesn't wont correctly. The idea is that the Stepper motor move the axis that generate a 5v pulse each time pass trough the optocoupler.
If I borrow the "motor" part of the code and try to move the axis manually... the optocoupler give me the perfect signal...
So, I don't know if the problem is in loop part or what happen??
Here the full code:
// ---------- PIN FOR MOTOR ----------------
#define stepPin 3
#define dirPin 2
// ---------- PIN FOR OPTOCOUPLER ----------------
int ledPin = 7; // output 5v signal pin
int inPin = 4; // pin for read optocoupler
void setup() {
// ---------- MOTOR ----------------
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
// ----------OPTOCOUPLER ----------------
// --------- Setup pins -------------
pinMode(ledPin, OUTPUT); // sets the digital pin 10 as output
pinMode(inPin, INPUT); // sets the digital pin 4 as input
}
void loop(){
// ---------- MOTOR ----------------
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 800; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(1500); // by changing this time delay between the steps we can change the rotation speed
digitalWrite(stepPin,LOW);
delayMicroseconds(1500);
}
// ----------OPTOCOUPLER ----------------
int sensorVal = digitalRead(inPin);
if (sensorVal == HIGH) {
digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin, HIGH);
}
}
It looks to me like you run the stepper, and read the opto, at different times. Did you notice? It's obvious that won't work.
Yes. It looks like you run the stepper for 800 steps:
// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 800; x++)
{
And then look t the optocoupler:
// ----------OPTOCOUPLER ----------------
int sensorVal = digitalRead(inPin);
if (sensorVal == HIGH)
If you remove the loop that repeats the step 800 times, then you will get a blink on 'ledPin' each time the optocoupler activates.
// ---------- PIN FOR MOTOR ----------------
#define stepPin 3
#define dirPin 2
// ---------- PIN FOR OPTOCOUPLER ----------------
int ledPin = 7; // output 5v signal pin
int inPin = 4; // pin for read optocoupler
void setup()
{
// ---------- MOTOR ----------------
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// ----------OPTOCOUPLER ----------------
// --------- Setup pins -------------
pinMode(ledPin, OUTPUT); // sets the digital pin 10 as output
pinMode(inPin, INPUT); // sets the digital pin 4 as input
}
void loop()
{
// ---------- MOTOR ----------------
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
// Take one step
digitalWrite(stepPin, HIGH);
delayMicroseconds(1500); // by changing this time delay between the steps we can change the rotation speed
digitalWrite(stepPin, LOW);
delayMicroseconds(1500);
// ----------OPTOCOUPLER ----------------
digitalWrite(ledPin, digitalRead(inPin));
}
If I understand right each time the stepper-motor has completed one revolution
a short pulse shall be created?
Well as a stepper-motor is a very well controlled thing where your code knows at every single step at which position the steppermotor is you could do the switching of an IO-pin based on the number of steps and eliminate the optocoupler completely.
Without knowing what you want to do in the end, it is impossible to make further suggestions
So please describe in detail what you want to do all in all at the end
best regards Stefan
Thanks to all answer...
The idea at the end is: each time the motor make a full rotation, the optocupler send a 5v to a camera trigger that take 1 photo.
It's really simple...
I'll try the suggestions.
You probably don't need an Arduino for that. A simple opto circuit and maybe a transistor... but since maybe you need the stepper, and considering post #4, all you really need is to make your sketch non-blocking, in other words, run the stepper and run the opto at the same time. There are multiple links about that in the forum support threads.
Using a non-blocking, existing, stepper control library would make that 1000% easier.