# trying to teach myself functions.

can someone walk me through where Im missing how to break out of the loop?

I wanted to write a simple function to blink the LED at either a fast rate or a slow rate but im missing how to exit out of the loop…

``````bool    sHasLooped  = false;
bool    fHasLooped  = false;

void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

// the loop function runs over and over again forever
void loop() {
Serial.println();
Serial.println();
Serial.println();
Serial.println();
digitalWrite(LED_BUILTIN, LOW);
}

if ( sHasLooped == false )
{
for ( int x = 0; x < 10; x++ )
{
digitalWrite(LED_BUILTIN, HIGH);   // Turn the LED on (Note that LOW is the voltage level
delay(500);                      // Wait for a second
digitalWrite(LED_BUILTIN, LOW);  // Turn the LED off by making the voltage HIGH
delay(500);
sHasLooped = true;
break;
}
}
}
if ( fHasLooped == false )
{
for ( int x = 0; x < 10; x++ )
{
digitalWrite(LED_BUILTIN, HIGH);   // Turn the LED on (Note that LOW is the voltage level
delay(150);                      // Wait for a second
digitalWrite(LED_BUILTIN, LOW);  // Turn the LED off by making the voltage HIGH
delay(150);
fHasLooped = true;
break;
}
}
}
``````

im missing how to exit out of the loop

Unclear what you mean.

Please describe what you really want to do.

``````   for ( int x = 0; x < 10; x++ )
{
digitalWrite(LED_BUILTIN, HIGH);   // Turn the LED on (Note that LOW is the voltage level
delay(500);                      // Wait for a second
digitalWrite(LED_BUILTIN, LOW);  // Turn the LED off by making the voltage HIGH
delay(500);
sHasLooped = true;
break;
}
``````

Let’s pretend for now that there are no other problems
It looks like you want to do something 10 times. If that is the case then why break out of the for loop unconditionally as you do ?

Hi Bob, Im just banging around trying to figure things out... programming clearly is not my strong-suite :) I included the break just to test to see what it does... i have been through several iterations moving, adding, and deleting various bits of code to see how it affects the device. Im trying to understand where I'm screwing things up.

I just got a few WeMOS boards and some PIR sensors and have got them up and running, able to connect to the WiFi and send a push message when the PIR triggers… what I am trying to do is fast blink the LED when the push message data is being sent… and in the process teach myself a little more coding.

here is what I have so far:

``````#include <ESP8266WiFi.h>
#include <WiFiClient.h>

int led = LED_BUILTIN;                // the pin that the LED is atteched to
int sensor = 4;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)

char APIKEY[] = "#######################"; // API key
char CHANNEL[] = "#########";                               // channel name

char serverName[] = "api.pushetta.com";
boolean lastConnected = false;

int status = WL_IDLE_STATUS;
const char* ssid = "#######";  //  your network SSID (name)

IPAddress ipMulti (192, 168, 1, 169);   // IP of Wemos D1 Device you want

void setup() {
WiFiClient client;   // turn on wifi client mode
pinMode(led, OUTPUT);      // initalize LED as an output
pinMode(sensor, INPUT);    // initialize sensor as an input
Serial.begin(115200);   // start serial connection
WiFi.begin(ssid, pass);  // setting up Station AP

// Wait for connect to AP
Serial.print("[Connecting]");
Serial.print(ssid);
int tries = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tries++;
if (tries > 30) {
break;
}
}
Serial.println("Connected to wifi");

}

void loop()
{

Serial.println("Connecting to Pushetta");
WiFiClient client;  // Use WiFiClient class to create TCP connections
const int httpPort = 80;
if (!client.connect("api.pushetta.com", httpPort)) {
Serial.println("Connection failed");
return;
}
if (val == HIGH) {           // check if the sensor is HIGH
digitalWrite(led, HIGH);   // turn LED ON
sendToPushetta(CHANNEL, "WeMOS Motion!");
delay(2000);
delay(10000);

if (state == LOW) {
Serial.println("No Motion Detected");
state = HIGH;       // update variable state to HIGH
}
}
}

//Function for sending the request to Pushetta
void sendToPushetta(char channel[], String text) {
WiFiClient client;
client.stop();

if (client.connect(serverName, 80))
{
client.print("POST /api/pushes/");
client.print(channel);
client.println("/ HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.print("Authorization: Token ");
client.println(APIKEY);
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(text.length() + 46);
client.println();
client.print("{ \"body\" : \"");
client.print(text);
client.println("\", \"message_type\" : \"text/plain\" }");
client.println();
}
}

digitalWrite(LED_BUILTIN, HIGH);   // Turn the LED on (Note that LOW is the voltage level
delay(150);
digitalWrite(LED_BUILTIN, LOW);  // Turn the LED off by making the voltage HIGH
delay(150);
}
``````

I suggest if you want to have a blinking LED light WHILST sending you have to look into Photothreading and blink without delay. It can get a little complicated. Remember that everything in arduino is sequential and you cannot run more than 1 thing at a time. Photothreading makes it seem like that is happening.

if you want to learn: here

I suggest that you have the LED on when sending a message and turn it off when the message has been sent.

The keyword "break;" will exit the loop when it is run.

Most people will have some condition to break. so if(something) {break;}

Otherwise you are just breaking out of the loop on it's first run

what I am trying to do is fast blink the LED when the push message data is being sent

OK, but you need to understand why this

``````    digitalWrite(led, HIGH);   // turn LED ON
sendToPushetta(CHANNEL, "WeMOS Motion!");
delay(2000);