Problem with GPIO 5 (D1) output mode high or low

I'm trying to get a relay to function to turn on and off power to a stepper motor to open a vent. I believe I'm using the correct code however when I print the state of the pin it always returns the pin number. I have other pins that are working correctly so I'm confused on what the issue is.

I'm using

const int relayPin = 5;
digitalWrite (relayPin, HIGH);

the full code below....

#include "DHTesp.h"
#include <Stepper.h>
#define STEPS 200
Stepper stepper(STEPS, 15, 13);
#define motorInterfaceType 1

// defines pins numbers
const int LimitSwitch_Open_Pin = 2;
const int LimitSwitch_Closed_Pin = 14;
const int relayPin = 5;

DHTesp dht;
void setup() {
Serial.begin(9600);
stepper.setSpeed(500); // Sets stepper speed
dht.setup(0, DHTesp::DHT11); // Connect DHT sensor to GPIO 0
// Sets pinModes
pinMode(LimitSwitch_Open_Pin , INPUT);
pinMode(LimitSwitch_Closed_Pin , INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
}

void loop() {

delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.println("Temperature");
Serial.println(temperature);
Serial.println(humidity);
float openSw = digitalRead( LimitSwitch_Open_Pin);
float closedSw = digitalRead( LimitSwitch_Closed_Pin);
Serial.print("Closed endsop State=");
Serial.println(closedSw);
Serial.print("Open endstop State=");
Serial.println(openSw);
Serial.print("RelayPin State=");
Serial.println(relayPin);
if (temperature >= 22 && openSw == HIGH) {
digitalWrite(relayPin, LOW);
delay(200);
ventOpen();
}
else {
digitalWrite(relayPin, HIGH);
}
if (temperature <= 21 && closedSw == HIGH) {
digitalWrite(relayPin, LOW);
delay(200);
ventClose();
} else {
digitalWrite(relayPin, HIGH);
}
}

void ventOpen() {
float openSw = digitalRead(LimitSwitch_Open_Pin);
while(digitalRead(LimitSwitch_Open_Pin) == HIGH) {
stepper.step(-10);
yield();
}
Serial.println("Vent Open!");
}

void ventClose() {
float openSw = digitalRead(LimitSwitch_Closed_Pin);
while(digitalRead(LimitSwitch_Closed_Pin) == HIGH) {
stepper.step(10);
yield();
}
Serial.println("Vent Closed!");
}

const int relayPin = 5;
.
.
.

Serial.println(relayPin);

This says print the variable relayPin which you defined as 5

One more comment: is this the best variable type?
float openSw = digitalRead( LimitSwitch_Open_Pin);

Ah I see.. there's a difference between printing the "state" of the relayPin and the Variable assigned to it.

I changed the code and I'm now getting the right results. Thanks.

<< One more comment: is this the best variable type? >>
<< float openSw = digitalRead( LimitSwitch_Open_Pin); >>

Also I'm not that versed with coding so far so I'm not sure why "float" variable might not be the best variable to use. It seems to work just fine. I will look into other types.

"float" is an evil type with many flaws. It is rarely (maybe never) good to use it.

digitalRead( LimitSwitch_Open_Pin)

That can only be HIGH or LOW, 1 or 0.

float returns something like 0.00000000000, or 1.000000000

all you need is byte for reading a pin, as in:

byte LimitSwitch_Open_Pin = 2;  // 2 thru 19 on an Uno, 2 thru 69 on  Mega
byte LimitSwitchState;

pinMode (LimitSwitch_Open_Pin, INPUT_PULLUP);


LimitSwitchState = digitalRead(LimitSwitch_Open_Pin);
if (LimitSwitchState == LOW) {
// do something
}
else {
// do something else if HIGH
}

Very cool. I changed all to byte except for the temperature and humidity.

That's what I get for looking pasting together other's codes and not taking the time to understand ALL of the different types of coding that can do the same actions in similar ways.

Thanks for explaining the Byte vs Float. I'm going to go through some coding tutorials and hopefully hone my skills!

Crosspost?

Ciao, Ale.

Yeah I posted it in two different places.. I was looking to delete one but got distracted.