Reading from serial

Hi, I am unable to figure out why this code is unable go in the if statement(

  if (innkommende == "h") {
    vertikal.setSpeed(100);
    vertikal.moveTo(0);
    vertikal.runToPosition();
    innkommende = "0,0";
  }

) when innkommende is “h”?

#include <AccelStepper.h>

const int vertikalDirPin = 5;
const int vertikalStepPin = 6;
String vIn = "0";
String OvIn = "0";
int vSpeed = 0;
#define motorInterfaceType 1
AccelStepper vertikal(motorInterfaceType, vertikalStepPin, vertikalDirPin);

const int horisontalDirPin = 7;
const int horisontalStepPin = 8;
String hIn = "0";
String OhIn = "0";
int hSpeed = 0;
#define motorInterfaceType 1
AccelStepper horisontal(motorInterfaceType, horisontalStepPin, horisontalDirPin);
/*
  const int fokusDirPin = 3;
  const int fokusStepPin = 4;
  String fIn = "0";
  String OfIn = "0";
  int fSpeed = 0;
  #define motorInterfaceType 1
  AccelStepper fokus(motorInterfaceType, fokusStepPin, fokusDirPin);
*/
const int maxUp = 650;
const int maxDown = -350;       //hvor langt motoren kan gå før det er fare for at den treffer rammen
String innkommende;


void setup() {
  vertikal.setMaxSpeed(500);
  vertikal.setCurrentPosition(0);
  vertikal.setAcceleration(100);
  horisontal.setMaxSpeed(500);
  horisontal.setAcceleration(100);
  horisontal.setCurrentPosition(0);
  //fokus.setMaxSpeed(100);
  //fokus.setAcceleration(300);
  //fokus.setCurrentPosition(0);
  Serial.begin(115200);
  Serial.setTimeout(15);
}

void loop() {
  if (Serial.available()) {
    innkommende = Serial.readStringUntil('\n');
    Serial.println(innkommende);
    if (innkommende != "h" || innkommende != "n") {
      int komma = innkommende.indexOf(',');
      String vIn = innkommende.substring(0, komma);
      String hIn = innkommende.substring(komma + 1);
      //String fIn = innkommende.substring(komma + 2);
      if (vIn != OvIn) {
        vSpeed = vIn.toInt();
      }

      if (hIn != OhIn) {
        hSpeed = hIn.toInt();
      }

      //if (fIn != OfIn) {
      //fSpeed = fIn.toInt();
      //}
      OhIn = hIn;
      OvIn = vIn;
      //OfIn = fIn;
    }
  }


  if (innkommende == "h") {
    vertikal.setSpeed(100);
    vertikal.moveTo(0);
    vertikal.runToPosition();
    innkommende = "0,0";
  }

  else {
    //Serial.println(vertikal.currentPosition());
    if (vertikal.currentPosition() > maxUp) {
      if (vSpeed < 0) {
        vertikal.setSpeed(vSpeed);
        vertikal.run();
      }
    }
    else if (vertikal.currentPosition() < maxDown) {
      if (vSpeed > 0) {
        vertikal.setSpeed(vSpeed);
        vertikal.run();
      }
    }
    else {
      vertikal.setSpeed(vSpeed);
      vertikal.run();
    }
    horisontal.setSpeed(hSpeed);
    horisontal.run();
    //fokus.setSpeed(fSpeed);
    //fokus.run();
  }
}

I have tried to isolate the problem, removing all other non-problem-related-code, but then it works… (That’s why I posted the full code).
Using Arduino Nano, and nema 17 stepper motors driven by a4988 drivers.

Thanks in advance :slight_smile:

Tried 'h'?

    innkommende = Serial.readStringUntil('\n');
    Serial.println(innkommende);
    Serial.println(innkommende.length());  ///// Add this line to make sure the String contains exactly 1 character.

The variable innkommende is a string and its first character will be h. So, if you try innkommende[0] == h, it will work fine, check out Arduino Serial Read example code.

oliviasmithh900:
The variable innkommende is a String and its first character will be h. So, if you try innkommende[0] == ‘h’, it will work fine.

Fixed now.

Thanks for all the help :slight_smile:

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