pinMode wont set output High

Hello,
This is my first post and I am a complete beginner.

My problem with the code which I will post below is that pinMode 4 works perfectly fine before I included the code that gives me real time temperature feedback over wifi.

I cant figure out why the new code would be blocking pinMode from working but I think the problem is originating in void setup()

Any help would be greatly appreciated.

const unsigned long  onTime = 500; /*latch on*/
const unsigned long  offTime = 43200000; /*latch off*/
unsigned  long previousMillis=0;

unsigned long interval = onTime;

boolean gate_solenoid=true;


#include <SPI.h>
#include <WiFiNINA.h>

#define sensorPin A0

char ssid[] = "wifi_name";
char pass[] = "wifi_password";
int keyIndex = 0;

int status = WL_IDLE_STATUS;
WiFiServer server(80);






  
void setup(){
  pinMode(4,OUTPUT); 

     
    Serial.begin(9600);
 

  
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    
  }
  server.begin();

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}
  
  


void loop(){
  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float temperatureC = (voltage - 0.5) * 100 ;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

  WiFiClient client = server.available();   
  if (client) {                             
    Serial.println("new client");           
    String currentLine = "";                
    while (client.connected()) {            
      if (client.available()) {             
        char c = client.read();             
        Serial.write(c);                    
        if (c == '\n') {                    

          if (currentLine.length() == 0) {

            client.print("Finch St Temperature: ");
            client.println(temperatureC);

            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }
      }
    }

    client.stop();
    Serial.println("client disonnected");
  }







{
  digitalWrite(4,gate_solenoid);

 unsigned long  currentMillis = millis();

  if((currentMillis - previousMillis) >= interval){
    if (gate_solenoid){
      interval = offTime;
    
    }else{
      interval = onTime;
    }
    gate_solenoid = !(gate_solenoid);
    previousMillis = currentMillis;
  }
  {
    
    
    
  }
}
}
  
  

What are all those {}s doing?

How about

pinMode(4,OUTPUT); 
digitalWrite (4, HIGH);

Ok great,
this seems to be triggering the relay again.

However it seems to leave the output 4 high for longer than I want which is 500ms but now its staying high for about 4seconds.

any idea why?

Thanks for your time on this.

Hello,
I am using the "Arduino four relays shield"

Thanks

No, none at all, I'm sorry to say, but you could alter your sketch to add some debug prints, so that you can see the times key events occur.
You can either print the value returned by the millis function, or use the serial monitor's own timestamp.

Also, strictly speaking, digitalWrite does not use booleans.

Disregard that last message, the delay only happens the first time.
I think the connection time to wifi must cause some lag on the output.

It works fine now, so thanks for that.

Can I ask, why pinMode did not work but digitalWrite did?

Hi,
Can you please post your current working code?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

What does pinMode not work mean? The pinMode command only sets whether the pin is an input or an output, and when it is an output, the digitalWrite command sets a high or low potential.

The reference library is a good resource

const unsigned long  onTime = 500; /*latch on*/
const unsigned long  offTime = 43200000; /*latch off*/
unsigned  long previousMillis=0;

unsigned long interval = onTime;

boolean gate_solenoid=true;


#include <SPI.h>
#include <WiFiNINA.h>

#define sensorPin A0

char ssid[] = "wifi_name";
char pass[] = "wifi_password";
int keyIndex = 0;

int status = WL_IDLE_STATUS;
WiFiServer server(80);






  
void setup(){
  pinMode(4,OUTPUT); 
  digitalWrite (4, HIGH);

     
    Serial.begin(9600);
 

  
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    
  }
  server.begin();

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}
  
  


void loop(){
  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float temperatureC = (voltage - 0.5) * 100 ;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

  WiFiClient client = server.available();   
  if (client) {                             
    Serial.println("new client");           
    String currentLine = "";                
    while (client.connected()) {            
      if (client.available()) {             
        char c = client.read();             
        Serial.write(c);                    
        if (c == '\n') {                    

          if (currentLine.length() == 0) {

            client.print("Finch St Temperature: ");
            client.println(temperatureC);

            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }
      }
    }

    client.stop();
    Serial.println("client disonnected");
  }







{
  digitalWrite(4,gate_solenoid);

 unsigned long  currentMillis = millis();

  if((currentMillis - previousMillis) >= interval){
    if (gate_solenoid){
      interval = offTime;
    
    }else{
      interval = onTime;
    }
    gate_solenoid = !(gate_solenoid);
    previousMillis = currentMillis;
  }
  {
    
    
    
  }
}
}

Thanks for dumbing that down for me, I understand where I was going wrong now.

What I don't understand is why when I had only got pinMode (4,OUTPUT); in the code before I added the code for temperature feedback over Wifi, output 4 was triggering perfectly fine?

Cheers.

Was this code copy/pasted from an HTML page (rather than monospace)? There is a lot of whitespace, as well as:

... there are a few stray and empty braces where working code probably existed.

Noted thanks.

There were a number of questions there! Noted thanks is not an adequate response.

You would need to show the code as it was then, to get an answer to that.

Good point, I will upload it tomorrow.