for loop being skipped

Hello everyone.

I am currently running this code on my ESP32

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#define dirPin 33
#define stepPin 32
#define end_max 25
#define end_zero 26

BluetoothSerial SerialBT;

const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter   = '>';
String command;
char incomingChar;
String stopString = "";
long n;
long o;
byte flag = 0;


void setup ()
{
  Serial.begin (115200);
  SerialBT.begin("Sensormessung");
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(end_max, INPUT_PULLUP);
  pinMode(end_zero, INPUT_PULLUP);

  n = 0;
}

void loop () {
  if (SerialBT.available()) {
    char incomingChar = SerialBT.read();
    if (incomingChar != '\n') {
      command += String(incomingChar);
    }
  }
  if (command != "") {
    Serial.println(command);
  }

  if (command == "2") {
    digitalWrite(dirPin, LOW);
    while (digitalRead(end_zero) == HIGH) {
      drive();
      if (SerialBT.available()) {
        char incomingChar = SerialBT.read();
        stopString = String(incomingChar);
        if (stopString == "8") {
          break;
        }
      }
    }
    flush_all();
  }

  if (command == "3") {
    digitalWrite(dirPin, LOW);
    while (digitalRead(end_max) == HIGH) {
      drive();
      if (SerialBT.available()) {
        char incomingChar = SerialBT.read();
        stopString = String(incomingChar);
        if (stopString == "8") {
          break;
        }
      }
    }
    flush_all();
  }


  if (command == "4") {
    digitalWrite(dirPin, LOW);
    while (digitalRead(end_max) == HIGH) {
      drive();
      if (SerialBT.available()) {
        char incomingChar = SerialBT.read();
        stopString = String(incomingChar);
        if (stopString == "5") {
          break;
        }
      }
    }
    flush_all();
  }


  if (command == "6") {
    digitalWrite(dirPin, HIGH);
    while (digitalRead(end_zero) == HIGH) {
      drive();
      if (SerialBT.available()) {
        char incomingChar = SerialBT.read();
        stopString = String(incomingChar);
        if (stopString == "7") {
          break;
        }
      }
    }
    flush_all();
  }



  if (command == "9") {
    while (command == "9") {
      if (SerialBT.available ())
        processInput ();
      if (flag == 1) {
        break;
      }
    }
    flush_all();
    flag = 0;
  }

  while (n != 0) {
    if (n > 0) {
      Serial.println(n);
      digitalWrite(dirPin, LOW);
      Serial.println("!");
      for (int i = 0; i > n; i++) {//skipped loop
        Serial.println("DOIT");
        drive();
      }
      Serial.println("!!");
      n = 0;
    }
    else if (n < 0) {
      digitalWrite(dirPin, HIGH);
      n = (-n);
      for (int i = 0; i > n; i++) {//skipped as well
        drive();
      }
      n = 0;
    }

  }


}


void processInput ()
{
  static long receivedNumber = 0;
  static boolean negative = false;

  byte c = SerialBT.read ();

  switch (c)
  {


    case startOfNumberDelimiter:
      receivedNumber = 0;
      negative = false;
      break;

    case '0' ... '9':
      receivedNumber *= 10;
      receivedNumber += c - '0';
      break;

    case '-':
      negative = true;
      break;

    case endOfNumberDelimiter:
      if (negative)
        n = (- receivedNumber);
      else
        n = (receivedNumber);
      flag = 1;
      break;
  }
}

void flush_all() {
  Serial.flush();
  SerialBT.flush();
  command = "";
  stopString = "";
}

void drive() {
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(1000);
}

However, the for()-loop near the end of loop() is being skipped.
Does anybody know what I am doing wrong here?

Thank you all in advance.

Did you mean " i < n"?

The loop is only run if n > 0, so “i > n” makes no sense, when i has been initialised to zero.

God, that’s embarrassing.
I took “>” instead of “<”…

Program is fixed and works fine.
Thank you very much

When I am trying to debug code that uses for loops, either mine or somebody else's, I find it helpful to mentally read the for loop as a while statement which it is what it is in fact

Doing this makes me think more carefully about what the for loop is actually doing, what the loop limits are and whether the data types used are appropriate

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