If Statements not responding when receiving an input

Hello, I'm currently running into an issue where even though I've verified that my Arduino is receiving a propper 5v signal from the propper input pin, the code is still skipping right over the if statement which the said signal should activate. theirs no issue with the wiring as I'm doing a direct jumper w a 5v signal to simulate for the program.

Blockquote
#include <Stepper.h>
int StartMotor = 3;
int reverseMotor = 4;
const int stopmotor = 22;
int RMotorStep = 50;
int RMotorDir = 52;
int LMotorStep = 51;
int LMotorDir = 53;
int DOctrl = 30;

void setup() {
Serial.begin(9600);
pinMode(stopmotor, INPUT);
pinMode(reverseMotor, INPUT);
pinMode(StartMotor, INPUT);
pinMode(DOctrl, INPUT);
pinMode(RMotorStep, OUTPUT);
pinMode(RMotorDir, OUTPUT);
pinMode(LMotorStep, OUTPUT);
pinMode(LMotorDir, OUTPUT);
}

void loop()
{
START:
//reviewing inputs and outputs
Serial.print("time to work"); //looping text before sub actions start
delay(2000);
digitalRead(StartMotor);
digitalRead(reverseMotor);
digitalRead(stopmotor);
if (StartMotor == HIGH)
{
digitalWrite(RMotorStep, HIGH);
digitalWrite(RMotorDir, HIGH);
digitalWrite(LMotorStep, HIGH);
digitalWrite(LMotorDir, HIGH);
Serial.print(" Motors goin Forward ");
delayMicroseconds (100);
goto START;
}
else if (reverseMotor == HIGH)
{
digitalWrite(RMotorStep, HIGH);
digitalWrite(RMotorDir, LOW);
digitalWrite(LMotorStep, HIGH);
digitalWrite(LMotorDir, LOW);
Serial.print(" Motors reverse ");
delayMicroseconds (100);
goto START;
}
else if (stopmotor == HIGH)
{
Serial.print(" Motors stopopopop ");
delayMicroseconds (100);
goto START;
} else
{
Serial.print("gotta reset");
delay(1500);
goto START;
}
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

if (StartMotor == HIGH)

StartMotor is a pin number, not the state of that pin. Did you mean

if (digitalRead(StartMotor) == HIGH)
1 Like

Thank you, yup that's what I meant.

It helps considerably if you give move descriptive names to variables such as

const byte startMotorPin = 3;

In passing, note the use of a byte rather than an int to save memory and the fact that, as the value will not change the variable has been declared as a constant value

1 Like

And please, get rid of the gotos.

1 Like

Hello, Sorry for the hiatus just getting back. down below is the new improved program. although I'm having an issue when running it. occasionally while running it will cycle through the start motor section, without having anything plugged in. is this a hardware issue, or does it happen on others Arduino as well? on arduino mega

#include <Stepper.h>
const byte StartMotor = 4;
const byte reverseMotor = 16;
const byte stopmotor = 39;
int RMotorStep = 50;
int RMotorDir = 52;
int LMotorStep = 51;
int LMotorDir = 53;
int DOctrl = 30;
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 50, 51, 52, 53);
void setup() {
  Serial.begin(9600);
  pinMode(stopmotor, INPUT);
  pinMode(reverseMotor, INPUT);
  pinMode(StartMotor, INPUT);
  pinMode(DOctrl, INPUT);
  pinMode(RMotorStep, OUTPUT);
  pinMode(RMotorDir, OUTPUT);
  pinMode(LMotorStep, OUTPUT);
  pinMode(LMotorDir, OUTPUT);
  
  digitalWrite(RMotorStep, LOW);
  digitalWrite(RMotorDir, LOW);
  digitalWrite(LMotorStep, LOW);
  digitalWrite(LMotorDir, LOW);
  myStepper.setSpeed(200);
}


void loop()
{
  //reviewing inputs and outputs
  delay(250);
  digitalWrite(StartMotor, LOW);
  digitalWrite(reverseMotor, LOW);
  digitalWrite(stopmotor, LOW);
  delay(100);
  digitalRead(StartMotor);
  digitalRead(reverseMotor);
  digitalRead(stopmotor);
  delay(500); 
  Serial.print(" Motor Home ");
  myStepper.step(0);
  
  if (digitalRead(StartMotor) >= HIGH)
  {
    Serial.print(" Motors goin Forward ");
    delayMicroseconds (50);
    myStepper.step(-200);
  }
  else if (digitalRead(reverseMotor) >= HIGH)
  {
    delay(200);
    Serial.print(" Motors reverse ");
    delayMicroseconds (50);
    myStepper.step(200);
  }
  else if (digitalRead(stopmotor) >= HIGH)
  {
    Serial.print(" Motors stopopopop ");
    delayMicroseconds (100);
    myStepper.step(0);
    delay(1000);
  }
  else
  {
    delay(100);
    Serial.print("motor home");
    delayMicroseconds (200);
    

  }
}

That's very . . . defensive.

Do you have a pull-down resistor on the StartMotor pin?

no, that was my answer when == was doing the same thing

"no" you don't have a pull-down?

no, I don't have a pull-down resistor. to add one, would that just be attaching it in series to the incoming 5v signal?

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