Pages: [1]   Go Down
Author Topic: Blink and LED while WiFi not Connected  (Read 571 times)
0 Members and 1 Guest are viewing this topic.
Georgia, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Jack of all, king of none.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys,

I have a nice working project and I'm looking to add a little flash to my visual feedback.  While the Arduino (WiFi Shield) is connecting to the network via the while loop,

Code:
while (status != WL_CONNECTED)...

I want to have an LED blink.  Once it's connected, I want the LED to go solid.  Right now, I just have the LED turn on once it's connected.  How can I blink an LED during this connection process?  Is it possible?  Do I have to implement an interrupt?  That's new territory for me.

Thanks in advance
Logged

--
Never argue with an idiot.  They will drag you down to their level and beat you with experience.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Take a look at the blink without delay example and add that into your while loop.
Logged

Georgia, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Jack of all, king of none.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your reply.  After reading the blink without delay page, I'm not sure that will apply here because the code waits for a connection before it does anything.

Code:
// Attempt to connect to WiFi network.
  while (status != WL_CONNECTED) {
    Serial.println("WiFi Shield Online");
    Serial.println();
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println(" Network ...");
    //status = WiFi.begin(ssid, keyIndex, key);  // WEP
    status = WiFi.begin(ssid, key);  // WPA/WPA2
   
    // Blink networkPin until connection is established.
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis > interval) {
      previousMillis = currentMillis;
      if (ledState == LOW) {
        ledState = HIGH;
      } else {
        ledState = LOW;
      }
      digitalWrite(networkPin, ledState);
    }
   
    //tenCountDot();
    //delay(10000);  // Wait 10 seconds for WiFi to connect.
  }

I tried putting the blink code both in before and after the status = WiFi.begin() statement.  No difference.  What am I missing?
Logged

--
Never argue with an idiot.  They will drag you down to their level and beat you with experience.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18800
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Post all your code? What is interval?

Logged


California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried putting the blink code both in before and after the status = WiFi.begin() statement.  No difference.  What am I missing?

If WiFi.begin() is blocking, then you either need to use a timer interrupt, or rewrite the WiFi.begin() code.
Logged

Georgia, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Jack of all, king of none.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick:  My code is over 400 lines and on several tabs.  The interval is 1000 as in the blink without delay example.

@Aarch:  I figured it would end up an interrupt situation.  Not afraid of them just not familiar with them.  As far as rewriting the WiFi.begin() function, I haven't thought about it as it's the original library released with 1.0.3.

Any ideas how to blink the LED in the background until the WiFi.begin() function completes?

Thanks in advance for all your help.



PS: Shameless plug: MUSHClient rocks!!   smiley-grin
Logged

--
Never argue with an idiot.  They will drag you down to their level and beat you with experience.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check out the TimerOne library; It's pretty simple to use.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4258
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This works for me
Code:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>

boolean status = false;
boolean ledState = true;
int ledPin = 8;
int interval = 1000;
long previousMillis = 0;
long currentMillis;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
  while (status != WL_CONNECTED)
  {
    status = WiFi.begin("foo","bar");
    currentMillis = millis();
    if (currentMillis - previousMillis > interval)
    {
      previousMillis = currentMillis;
      ledState = !ledState;
      digitalWrite(ledPin, ledState);   
    }
  }
}

void loop(){}

NOTE 1
I do not actually have a WiFi shield so all bets may be off

NOTE 2
The WiFi library appears to stop some pins acting as digital outputs, notably pin 13, but it may affect others.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18800
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

PS: Shameless plug: MUSHClient rocks!!   smiley-grin

Oh, aye.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18800
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick:  My code is over 400 lines and on several tabs.  The interval is 1000 as in the blink without delay example.

That's nothing. You can zip them up and make an attachment.

How to use this forum
Logged


Pages: [1]   Go Up
Jump to: