# Funçao dentro do loop

**URL:** <https://forum.fazedores.com/t/funcao-dentro-do-loop/8229>\
**Category:** Arduino\
**Created:** [Maio 31, 2021, 12:49am UTC](https://forum.fazedores.com/t/funcao-dentro-do-loop/8229 "2021-05-31T00:49:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robson\_Dorival\_Corde](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.fazedores.com/robson_dorival_corde/32/2949_2.png) [@Robson\_Dorival\_Corde](https://forum.fazedores.com/u/Robson_Dorival_Corde)\
**Post date:** [Maio 31, 2021, 12:49am UTC](https://forum.fazedores.com/t/funcao-dentro-do-loop/8229/1 "2021-05-31T00:49:26Z")

</div>

Boa noite pessoal, alguem pode me dar uma luz,

tenho esse programa  
#include\<SoftwareSerial.h\>

SoftwareSerial LORA(18, 19);  
SoftwareSerial nextion(22,23);

#define LEDVERMELHO 32  
//#define LEDAZUL 27  
#define pinobuzzer 26  
void chamando();  
//---------------------------------------------  
boolean pisca = false; // Definido estado da variavel pisca tipo verdadeiro ou falsa  
unsigned long delay1 = 0; // Definido variavel delay1 com início em 0  
// constants won’t change. Used here to set a pin number:  
const int ledPin = 27;// the number of the LED pin  
// Variables will change:  
int ledState = LOW; // ledState used to set the LED  
// Generally, you should use “unsigned long” for variables that hold time  
// The value will quickly become too large for an int to store  
unsigned long previousMillis = 0; // will store last time LED was updated  
// constants won’t change:  
const long interval = 2000; // interval at which to blink (milliseconds)  
//---------------------------------------------

//---------------------------------------------  
void chamando() {  
nextion.begin(9600);  
unsigned long currentMillis = millis();

if (currentMillis - previousMillis \>= interval) {  
// save the last time you blinked the LED  
previousMillis = currentMillis;

```
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
  ledState = HIGH;
} else {
  ledState = LOW;
}

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);

```

}  
if (ledState == LOW) {  
nextion.print(“vis p2,1”);  
nextion.print("\xFF\xFF\xFF");  
nextion.print(“vis p0,0”);  
nextion.print("\xFF\xFF\xFF");

```
//digitalWrite(pinobuzzer, HIGH);
} else {
  nextion.print("vis p2,0");
nextion.print("\xFF\xFF\xFF");
nextion.print("vis p0,1");
nextion.print("\xFF\xFF\xFF");
digitalWrite(pinobuzzer, LOW);
}

```

}

//---------------------------------------------

void setup() {  
Serial.begin(9600);  
nextion.begin(9600);  
LORA.begin(9600);  
pinMode(LEDVERMELHO, OUTPUT);  
//pinMode(LEDAZUL, OUTPUT);  
pinMode(pinobuzzer, OUTPUT);  
Serial.println(“Iniciando…”);

}  
void loop() {  
if(nextion.available()) {  
int c = {nextion.read()};  
Serial.println(c);  
if (c == 16){  
LORA.write(50);  
chamando();  
}  
}

}

e tenho com uma funcao dentro do loop mas ela nao roda agora quando pego ela e jogo em um sketch novo ela funciona alguem sabe o por que  
#include\<SoftwareSerial.h\>

SoftwareSerial LORA(18, 19);  
SoftwareSerial nextion(22,23);

#define LEDVERMELHO 32  
#define LEDAZUL 27  
#define pinobuzzer 26

boolean pisca = false; // Definido estado da variavel pisca tipo verdadeiro ou falsa  
unsigned long delay1 = 0; // Definido variavel delay1 com início em 0

// constants won’t change. Used here to set a pin number:  
const int ledPin = 27;// the number of the LED pin

// Variables will change:  
int ledState = LOW; // ledState used to set the LED

// Generally, you should use “unsigned long” for variables that hold time  
// The value will quickly become too large for an int to store  
unsigned long previousMillis = 0; // will store last time LED was updated

// constants won’t change:  
const long interval = 3000; // interval at which to blink (milliseconds)

void setup() {  
// set the digital pin as output:  
pinMode(LEDVERMELHO, OUTPUT);  
pinMode(LEDAZUL, OUTPUT);  
pinMode(pinobuzzer, OUTPUT);  
Serial.begin(9600);  
nextion.begin(9600);  
}

void loop() {

unsigned long currentMillis = millis();

if (currentMillis - previousMillis \>= interval) {  
// save the last time you blinked the LED  
previousMillis = currentMillis;

```
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
  ledState = HIGH;
} else {
  ledState = LOW;
}

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);

```

}  
if (ledState == LOW) {  
nextion.print(“vis p2,0”);  
nextion.print("\xFF\xFF\xFF");  
nextion.print(“vis p0,1”);  
nextion.print("\xFF\xFF\xFF");

```
//digitalWrite(pinobuzzer, HIGH);
} else {
  nextion.print("vis p2,1");
nextion.print("\xFF\xFF\xFF");
nextion.print("vis p0,0");
nextion.print("\xFF\xFF\xFF");
digitalWrite(pinobuzzer, LOW);
}

```

}

---

<div class="post-metadata">

**Author:** ![Nicolas](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.fazedores.com/nicolas/32/741_2.png) [@Nicolas](https://forum.fazedores.com/u/Nicolas)\
**Post date:** [Junho 2, 2021, 6:25pm UTC](https://forum.fazedores.com/t/funcao-dentro-do-loop/8229/2 "2021-06-02T18:25:47Z")

</div>

Quando forem colar código usem um recurso proprio para isso, porque fica horrivel ler aqui.

Demora muito mais entender o caos gerado (falta de espaço, identação etc) do que dar uma resposta, sugestão: [https://gist.github.com](https://gist.github.com)

Robson, usar prototype de função é sempre recomendado mas do jeito que esta perdeu toda vantagem, exemplo mais adequado:

> <https://gist.github.com/fdavidgithub/b5ab6cde229ab421d860ea8f465c9de0>
