0

I am using a server-client system where i send data from the server to the client to turn on a led (the onboard led). Led must be turned off using digital input (labeled 7 on the connector). I use therefore a while loop that checks the value of the input (button press). As long as value is not equal to LOW the led remains on (i use also a second led on pin 13 as doublecheck). I assume that the while loop will be left once i press the button, unfortunately this doesn't happen. The result of the condition seems not updated (remains HIGH). I measured the voltage on the input when pressing the button and it goes from 3.3V to 0.0V so hardware seems to be OK. From my undestanding the while loop is always (before each loop) checking the condition ? why doesn't this happen here ? did i do something wrong ? I must use a while because i never know in advance when the button will be pressed.

this my code :

#include <SPI.h>
#include <WiFiNINA.h>
#include <utility/wifi_drv.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)

//int keyIndex = 0;            // your network key Index number (needed only for WEP)

const int buttonPin = 7;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int buttonState;         // variable for reading the pushbutton status


int status = WL_IDLE_STATUS;
IPAddress ServerIP(192,168,0,157);
WiFiClient client;

int i;
String response;




void setup() {

  WiFiDrv::pinMode(25, OUTPUT); //GREEN
  WiFiDrv::pinMode(26, OUTPUT); //RED
  WiFiDrv::pinMode(27, OUTPUT); //BLUE

 pinMode(buttonPin, INPUT);
 pinMode(ledPin, OUTPUT);

  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
  }
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
  Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }

    // you're connected now, so print out the status:
  printWifiStatus();
}


void loop() {

if (client.connect(ServerIP, 23)) {
  client.print("90");
  String response = client.readStringUntil('\n');

  Serial.println(response);

if (response == "9") {
  buttonState = digitalRead(buttonPin);
 // Serial.println(buttonState);
  while (buttonState != LOW){
    // turn LED on:
    digitalWrite(ledPin, HIGH);
     WiFiDrv::digitalWrite(25, 255); // for full brightness
     Serial.println(buttonState);
  } 
  digitalWrite(ledPin, LOW);
  WiFiDrv::digitalWrite(25, LOW); // for full brightness           
        }
  }

  client.stop();
}



void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
 Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

thanks a lot for helping me out !

2
 while (buttonState != LOW){
    // turn LED on:
    digitalWrite(ledPin, HIGH);
     WiFiDrv::digitalWrite(25, 255); // for full brightness
     Serial.println(buttonState);
  } 

You never update the value of buttonState in this while loop. If buttonState is not LOW to get into the while loop, nothing inside the while loop will ever change it so every time it gets checked it will still not be LOW. It's not going to surmise that you meant to read a pin just because you used that variable to read a pin once before.

But do you really need this while loop? Couldn't you just write the led pin low when you first get a press and then write it high when it first gets released? See the "State Change Example". Let the loop function be the loop. It will lead to better code. You don't see this now because you are a novice, but you are about to paint yourself into a corner with this approach and you'll soon be rewriting the whole thing. It's a trap that gets most new coders. Better to get it right now.

| improve this answer | |
  • Yeah. As a rule you don't want to use while loops inside your sketch's loop function. It's almost always the wrong approach. – Duncan C Apr 24 at 17:30
  • You don't want to use while loops anywhere in your code waiting for things in the outside world to happen. A while loop is for like, while I still have more characters in this string or while I haven't cleared the whole array. You should never have while loops blocking your code waiting on things to physically happen. That traps you so that you can't have anything else happen during the wait. – Delta_G Apr 24 at 17:36
  • Good way of explaining it. – Duncan C Apr 24 at 18:22
  • thanks a lot to both of you! it's working now. I was not aware of that (no whiles when detecting outside events). – Majid Apr 26 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.