Stuck in whileloop

Hi Everyone,
I am a newbie and have following problem:
I somehow get stuck in a while loop.
Can You help me out?

#include <TimerOne.h>
#include <SoftwareSerial.h>

//--------------------
//Board and Pin Config

//STEPPER Pins
const int EN = 16; //56;         //ENABLE PIN  Y axıs
const int dirPin = 17; //61;        // DIRECTION PIN
const int Step = 23; //60;       // STEP PIN

//Switch pins

const int buttonPinTur = 3;    // Hall Sensor The number of the pushbutton pin
const int buttonPinSag = 2;    // MicroSwitch the number of the pushbutton pin
const int buttonPinSol = 14;   //MicroSwitch the number of the pushbutton pin
const int buttonPinLoad = 18;  // Taster zmin


// Fixed Variables

float Pitch = 2.5; // vidanın hatvesi
float Ilerleme = 2.5; //filament çapı
int StepsPerRev = 200; //Motorun Step sayısı
int Microsteps = 4; // Mikrostep Ayarı
double rev = 2; //R/sec
int Delay = 1000000 / (rev * 2 * StepsPerRev);
int Speed = Delay / Microsteps; //(mikrosaniye cinsinden step hızı (küçüldükçe hızlanır)
float Mesafe = Ilerleme / Pitch * StepsPerRev / 2 * Microsteps;

//---------------------------
void setup()
{
  pinMode(EN, OUTPUT);    // ENABLE AS OUTPUT
  pinMode(Step, OUTPUT);  // STEP AS OUTPUT
  pinMode(dirPin, OUTPUT); // DIRECTION AS OUTPUT
  digitalWrite(EN, HIGH);  // SET ENABLE TO HIGH FOR EXTERNAL DRIVER, LOW FOR STEPSTICKS
  //-------------------
  pinMode(buttonPinTur, INPUT);
  pinMode(buttonPinSag, INPUT);
  pinMode(buttonPinSol, INPUT);
  pinMode(buttonPinLoad, INPUT);
  digitalWrite(dirPin, HIGH);
  Timer1.initialize(Speed);
  Timer1.attachInterrupt(makeStep);
}

void loop() {
  // put your main code here, to run repeatedly:
  byte readingSag = digitalRead(buttonPinSag);
  byte readingSol = digitalRead(buttonPinSol);
  byte readingLoad = digitalRead(buttonPinLoad);
  byte readingTur = digitalRead(buttonPinTur);

  //loadFilament(readingLoad, readingSag);
  checkEndstop(readingSag, readingSol, readingLoad);
}

void makeStep ()
{
  digitalWrite(Step, HIGH);   // STEP HIGH
  delayMicroseconds(Speed);                   // WAIT
  digitalWrite(Step, LOW);    // STEP LOW
  delayMicroseconds(Speed);
}

void checkEndstop(byte readingSag, byte readingSol, byte readingLoad)
{
  if (readingLoad == LOW && readingSag == HIGH)
  {
    digitalWrite(dirPin, LOW); // LOAd pozisyonun hangi istikamette olduğuna bağlı
    while (readingLoad == LOW && readingSag == HIGH)
    {
      readingLoad = digitalRead(buttonPinLoad);
      readingSag = digitalRead(buttonPinSag);
      makeStep();
    }
  }
  if (readingLoad == LOW && readingSag == LOW)  //at load position, load and right switch pressed
  {
    delay(10);
  }
  if (readingSag == LOW) //LOW microswitch HIGH REED
  {
    digitalWrite(dirPin, LOW);
  }
  if (readingSol == LOW) //LOW microswitch HIGH REED
  {
    digitalWrite(dirPin, HIGH);
  }
}

The while loop is checking for the condition of 2 switches, if only one of them is LOW then it "steps" if both get LOW then it should stop stepping and wait...

Also there are some more variables in the code that I haven't used yet but thats for the next steps.

Thanks in advance.

  pinMode(buttonPinTur, INPUT);
  pinMode(buttonPinSag, INPUT);
  pinMode(buttonPinSol, INPUT);
  pinMode(buttonPinLoad, INPUT);

Not INPUT_PULLUP?

The only while loop I can see is this

    while (readingLoad == LOW && readingSag == HIGH)
    {

so to exit the loop you need to have ReadingLoad == HIGH AND OR readingSag == LOW. Why is this not happening?

EDIT: Corrected logic

marco_c:
The only while loop I can see is this

    while (readingLoad == LOW && readingSag == HIGH)

{



so to exit the loop you need to have ReadingLoad == HIGH AND readingSag == LOW. Why is this not happening?

No. To exit the loop, he needs to have readingLoad == HIGH OR readingSag == LOW.

I've never used the TimerOne library, so not familiar with it, but it appears you are setting up a timer to interrupt at an interval (set by the constant speed), then delaying twice that amount inside the interrupt service routine (ISR).

  Timer1.initialize(Speed);
  Timer1.attachInterrupt(makeStep);
void makeStep ()
{
  digitalWrite(Step, HIGH);   // STEP HIGH
  delayMicroseconds(Speed);                   // WAIT
  digitalWrite(Step, LOW);    // STEP LOW
  delayMicroseconds(Speed);
}

This would result in the interrupt condition being met while you are still in the ISR, and as soon as you exit the ISR and interrupts are re-enabled the code would immediately execute the ISR again.

@RayLivingStone - you know, that's what I had in my head but that's not what came out. Thanks for the correction. I edited to correct for future generations.

david_2018:
I've never used the TimerOne library, so not familiar with it, but it appears you are setting up a timer to interrupt at an interval (set by the constant speed), then delaying twice that amount inside the interrupt service routine (ISR).

  Timer1.initialize(Speed);

Timer1.attachInterrupt(makeStep);






void makeStep ()
{
  digitalWrite(Step, HIGH);  // STEP HIGH
  delayMicroseconds(Speed);                  // WAIT
  digitalWrite(Step, LOW);    // STEP LOW
  delayMicroseconds(Speed);
}





This would result in the interrupt condition being met while you are still in the ISR, and as soon as you exit the ISR and interrupts are re-enabled the code would immediately execute the ISR again.

Good Point. Thanks




int InterruptTiming = 10;
int Delay = 1000000 / (rev * StepsPerRev)/2;
int Speed = Delay / Microsteps;

Timer1.initialize(InterruptTiming);
Timer1.attachInterrupt(makeStep);

void makeStep ()
{
  digitalWrite(Step, HIGH);  // STEP HIGH
delayMicroseconds(Speed);                  // WAIT
  digitalWrite(Step, LOW);    // STEP LOW
delayMicroseconds(Speed);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.