Hi all,
I encountered a strange problem with a stepper motor driving an optical grid and measuring the corresponding output of a photodiode.
I have an old spectrophotometer. There is a stepper motor driving the optical grid. A photodiode mesaures the output of the whole thing.
The output should be as follows: it should rise until about 510 nm, then it should fall again. In principle this happens, but when you look nearer to the curve of the output, there are small, regular oscillations (see picture (blue symbols), which shows a small part of the curve).
This is my code, running on an Uno:
//Schrittmotorsteuerung für 28BYJ-48 5VDC mit ULN2003A
// https://www.mikrocontroller.net/topic/333820
#define NrOfMotorOutputs 4 //Anzahl der Ausgänge zur Ansteuerung des Motors
#define NrOfPatternsOneStep 8 //Anzahl der Muster zur Ansteuerung eines Schritts
//Variabeln für Drehrichtung definieren
const byte MotorOff = 0;
const byte DirMotorRight = 2;
const byte DirMotorLeft = 1;
const int lambdaEnd = 12; // Endswitch
int statusLambdaEnd = 0;
int n = 0; // counter steps
const int LampeAnPin = A5; // switch lamp
const int ADWandlerIn = A0; // A/D
int val;
int lambda;
byte ActStepNr = 0; // momentary step
//Schrittmuster definieren
byte StepPattern[NrOfPatternsOneStep][NrOfMotorOutputs] =
{
{1, 1, 0, 0}, // from: https://www.kollino.de/arduino/schrittmotor-28byj-48/
{0, 1, 0, 0}, // NrOfPatternsOneStep 8
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 0}
};
// Motor Pins for output 1-4
byte MotorOutputPin[NrOfMotorOutputs] = {8,10,9,11}; //Change of sequence: https://blog.thesen.eu/es-geht-doch-schrittmotor-28byj-48-mit-der-original-arduino-stepper-library/
void setup()
{
pinMode(lambdaEnd, INPUT_PULLUP);
pinMode(LampeAnPin, OUTPUT);
for (byte i=0; i < NrOfMotorOutputs; i++)
{
pinMode((MotorOutputPin[i]), OUTPUT);
digitalWrite(MotorOutputPin[i], LOW);
}
do
{
MotorMove(DirMotorRight,10,1);
statusLambdaEnd = digitalRead(lambdaEnd);
}
while(statusLambdaEnd == HIGH);
Serial.begin(9600);
}
void loop()
{
digitalWrite(LampeAnPin, HIGH);
for (n=0; n<943; n++)
{ //942 steps corresponds with the whole spectrum
lambda = (n*0.628)+314;
val = analogRead(ADWandlerIn);
Serial.print(lambda); Serial.print(","); Serial.print(val); Serial.print(","); Serial.println(n);
MotorMove(DirMotorLeft,50, 1);
}
digitalWrite(LampeAnPin, LOW);
while(1); // program stop
}
void MotorMove(byte MotorDir, unsigned int MotorSpeed, unsigned int StepsToGo)
{
//Motor Stop all outputs LOW
if (MotorDir == MotorOff)
{
for (byte i=0; i < NrOfMotorOutputs; i++)
{
digitalWrite(MotorOutputPin[i], LOW);
}
}
//Motor right
else if (MotorDir == DirMotorRight)
{
for (int i=0; i < StepsToGo; i++)
{
ActStepNr++;
if (ActStepNr == NrOfPatternsOneStep) ActStepNr = 0;
for (byte j=0; j < NrOfMotorOutputs; j++)
{
digitalWrite(MotorOutputPin[j], StepPattern[ActStepNr][j]);
}
delay(MotorSpeed);
}
}
//Motor left
else if (MotorDir == DirMotorLeft)
{
for (int i=0; i < StepsToGo; i++)
{
if (ActStepNr == 0) ActStepNr = NrOfPatternsOneStep-1;
else ActStepNr--;
for (byte j=0; j < NrOfMotorOutputs; j++)
{
digitalWrite(MotorOutputPin[j], StepPattern[ActStepNr][j]);
}
delay(MotorSpeed);
}
}
}
I have no idea what causes this problem. Have you?
Thanks for answering
-richard