Go Down

Topic: executar açao apenas uma vez dentro do loop (SOU INICIANTE) (Read 622 times) previous topic - next topic

Vitor_Brzozkowski

#15
May 25, 2019, 02:20 am Last Edit: May 25, 2019, 02:24 am by Vitor_Brzozkowski
Code: [Select]





boolean acionaRele = true;/******************************************************************************/

int i;

int sign;

long value;

float result;

int clockpin = 4;

int datapin = 5;

int releX = 13;

int rele11 = 11;

int rele2 = 10;

int rele22 = 9;

int rele3 = 8;

int rele33 = 7;




unsigned long tempmicros;


///////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {




  Serial.begin(9600);

  pinMode(clockpin, INPUT);

  pinMode(datapin, INPUT);

  pinMode(releX, OUTPUT);

  pinMode(rele2, OUTPUT);

  pinMode(rele3, OUTPUT);

  pinMode(rele11, OUTPUT);

  pinMode(rele22, OUTPUT);

  pinMode(rele33, OUTPUT);



}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop () {





  while (digitalRead(clockpin) == HIGH) {} //if clock is LOW wait until it turns to HIGH

  tempmicros = micros();

  while (digitalRead(clockpin) == LOW) {} //wait for the end of the HIGH pulse

  if ((micros() - tempmicros) > 500) { //if the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence

    decode(); //decode the bit sequence

  }



}


  ///////////////////////////////////////////////////////////////////////////////////////////////////////////

void decode() {

  sign = 1;

  value = 0;

  for (i = 0; i < 23; i++) {

    while (digitalRead(clockpin) == HIGH) { } //wait until clock returns to HIGH- the first bit is not needed

    while (digitalRead(clockpin) == LOW) {} //wait until clock returns to LOW

    if (digitalRead(datapin) == LOW) {

      if (i < 20) {

        value |= 1 << i;

      }

      if (i == 20) {

        sign = -1;

      }

    }

  }




  result = (value * sign) / 3856.8376068376;

  Serial.println(result, 2); //print result with 2 decimals

  delay(5);


  ///////////////////////////////////////////////////////////////////////////////////////////////////////////

  if ( result > 2.12 && result < 2.45 ) {
    /******************************************************************************/
    if (acionaRele == true){
      digitalWrite(releX, HIGH);
delay(1000);
      digitalWrite(releX, LOW);
delay(10);


 acionaRele = false; 
         
    }
   




  }




 
  else {

acionaRele = true; 

 
  }
 




}

 






esta funcionando assim, mas so com 1 das tolerancias, se eu adicionar mais uma.

 exemplo:

Code: [Select]




  if ( result > 2.12 && result < 2.45 ) {
    /******************************************************************************/
    if (acionaRele == true){
      digitalWrite(releX, HIGH);
delay(1000);
      digitalWrite(releX, LOW);
delay(10);


 acionaRele = false; 
         
    }
   




  }




 
  else {

acionaRele = true; 

 
  }
 




  if ( result > 2.45 && result < 3.19 ) {
    /******************************************************************************/
    if (acionaRele == true){
      digitalWrite(releX, HIGH);
delay(1000);
      digitalWrite(releX, LOW);
delay(10);


 acionaRele = false; 
         
    }
   




  }




 
  else {

acionaRele = true; 

 
  }
 




}

 


 







o codigo entra em loop, e pisca ligando e desligando o rele (eu entendo o pq do loop mas nao sei escrever algo que pare isso)

tecdiego

Testa aí
Code: [Select]






#define acoplador 13

boolean acionaRele = true;/******************************************************************************/
int intervaloRele;/******************************************************************************/
int i;

int sign;

long value;

float result;

int clockpin = 4;

int datapin = 5;

int tempo_de_acopl = 5000;

int rele1 = 12;

int rele11 = 11;

int rele2 = 10;

int rele22 = 9;

int rele3 = 8;

int rele33 = 7;




unsigned long tempmicros;


///////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {




  Serial.begin(9600);

  pinMode(clockpin, INPUT);

  pinMode(datapin, INPUT);

  pinMode(rele1, OUTPUT);

  pinMode(rele2, OUTPUT);

  pinMode(rele3, OUTPUT);

  pinMode(rele11, OUTPUT);

  pinMode(rele22, OUTPUT);

  pinMode(rele33, OUTPUT);

  pinMode(acoplador,OUTPUT);   

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop () {





  while (digitalRead(clockpin) == HIGH) {} //if clock is LOW wait until it turns to HIGH

  tempmicros = micros();

  while (digitalRead(clockpin) == LOW) {} //wait for the end of the HIGH pulse

  if ((micros() - tempmicros) > 500) { //if the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence

    decode(); //decode the bit sequence

  }



}



void decode() {

  sign = 1;

  value = 0;

  for (i = 0; i < 23; i++) {

    while (digitalRead(clockpin) == HIGH) { } //wait until clock returns to HIGH- the first bit is not needed

    while (digitalRead(clockpin) == LOW) {} //wait until clock returns to LOW

    if (digitalRead(datapin) == LOW) {

      if (i < 20) {

        value |= 1 << i;

      }

      if (i == 20) {

        sign = -1;

      }

    }

  }



  ///////////////////////////////////////////////////////////////////////////////////////////////////////////

  result = (value * sign) / 3856.8376068376;

  Serial.println(result, 2); //print result with 2 decimals

  delay(5);
  if ((intervaloRele + 3000) <= millis()){
   
    digitalWrite(rele1, LOW);

    digitalWrite(rele11, HIGH);

    acionaRele = true;     
  }

  if ( result > 2.12 && result < 2.45 ) {
    /******************************************************************************/
    if (acionaRele == true){
      digitalWrite(rele1, HIGH);

      digitalWrite(rele11, LOW);

      acionaRele = false;     
      intervaloRele = millis();
    }
    /******************************************************************************/

  }
  else {

    digitalWrite(rele1, LOW);

    digitalWrite(rele11, HIGH);

 
  }

  if ( result > 2.45 && result < 3.19 ) {


    digitalWrite(rele3, HIGH);

    digitalWrite(rele33, HIGH);

 

  }
  else {


    digitalWrite(rele3, LOW);

    digitalWrite(rele33, HIGH);


  }













 if ( result > 2.12 && result < 3.19 ) {


    digitalWrite(rele2, LOW);

     digitalWrite(rele22, HIGH);

  }
  else {


    digitalWrite(rele2, HIGH);

     digitalWrite(rele22, LOW);


 
  }















//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


if ( result > 2.12 && result < 2.45 ) {


  acopl ();
  }



if ( result > 2.45 && result < 3.19 ) {

acopl ();
 
}
 
 if ( result > 0.00 && result < 2.12) {

  acopl ();

 
 }


 if ( result > 3.19 && result < 10.00) {

  acopl ();

 }



}

void acopl() {

 


digitalWrite(acoplador, HIGH);
 
delay(tempo_de_acopl);

digitalWrite(acoplador, LOW);


 
}
 
 




Vitor_Brzozkowski

#17
May 25, 2019, 08:31 pm Last Edit: May 25, 2019, 08:39 pm by Vitor_Brzozkowski
Testa aí
Code: [Select]






#define acoplador 13

boolean acionaRele = true;/******************************************************************************/
int intervaloRele;/******************************************************************************/
int i;

int sign;

long value;

float result;

int clockpin = 4;

int datapin = 5;

int tempo_de_acopl = 5000;

int rele1 = 12;

int rele11 = 11;

int rele2 = 10;

int rele22 = 9;

int rele3 = 8;

int rele33 = 7;




unsigned long tempmicros;


///////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {




  Serial.begin(9600);

  pinMode(clockpin, INPUT);

  pinMode(datapin, INPUT);

  pinMode(rele1, OUTPUT);

  pinMode(rele2, OUTPUT);

  pinMode(rele3, OUTPUT);

  pinMode(rele11, OUTPUT);

  pinMode(rele22, OUTPUT);

  pinMode(rele33, OUTPUT);

  pinMode(acoplador,OUTPUT);   

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop () {





  while (digitalRead(clockpin) == HIGH) {} //if clock is LOW wait until it turns to HIGH

  tempmicros = micros();

  while (digitalRead(clockpin) == LOW) {} //wait for the end of the HIGH pulse

  if ((micros() - tempmicros) > 500) { //if the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence

    decode(); //decode the bit sequence

  }



}



void decode() {

  sign = 1;

  value = 0;

  for (i = 0; i < 23; i++) {

    while (digitalRead(clockpin) == HIGH) { } //wait until clock returns to HIGH- the first bit is not needed

    while (digitalRead(clockpin) == LOW) {} //wait until clock returns to LOW

    if (digitalRead(datapin) == LOW) {

      if (i < 20) {

        value |= 1 << i;

      }

      if (i == 20) {

        sign = -1;

      }

    }

  }



  ///////////////////////////////////////////////////////////////////////////////////////////////////////////

  result = (value * sign) / 3856.8376068376;

  Serial.println(result, 2); //print result with 2 decimals

  delay(5);
  if ((intervaloRele + 3000) <= millis()){
   
    digitalWrite(rele1, LOW);

    digitalWrite(rele11, HIGH);

    acionaRele = true;     
  }

  if ( result > 2.12 && result < 2.45 ) {
    /******************************************************************************/
    if (acionaRele == true){
      digitalWrite(rele1, HIGH);

      digitalWrite(rele11, LOW);

      acionaRele = false;     
      intervaloRele = millis();
    }
    /******************************************************************************/

  }
  else {

    digitalWrite(rele1, LOW);

    digitalWrite(rele11, HIGH);

 
  }

  if ( result > 2.45 && result < 3.19 ) {


    digitalWrite(rele3, HIGH);

    digitalWrite(rele33, HIGH);

 

  }
  else {


    digitalWrite(rele3, LOW);

    digitalWrite(rele33, HIGH);


  }













 if ( result > 2.12 && result < 3.19 ) {


    digitalWrite(rele2, LOW);

     digitalWrite(rele22, HIGH);

  }
  else {


    digitalWrite(rele2, HIGH);

     digitalWrite(rele22, LOW);


 
  }















//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


if ( result > 2.12 && result < 2.45 ) {


  acopl ();
  }



if ( result > 2.45 && result < 3.19 ) {

acopl ();
 
}
 
 if ( result > 0.00 && result < 2.12) {

  acopl ();

 
 }


 if ( result > 3.19 && result < 10.00) {

  acopl ();

 }



}

void acopl() {

 


digitalWrite(acoplador, HIGH);
 
delay(tempo_de_acopl);

digitalWrite(acoplador, LOW);


 
}
 
 




Bah não funcionou, acho q vou desistir do projeto, esse q você me mandou deixa ligado continuamente igual o anterior, mas obrigado pela ajuda, o problema agora é quando coloco mais 1 tolerância ele trava o sistema, pensei em usar goto mas aí ele ia só ficar reiniciando e ligaria novamente o relé , tem como usar 1 "else" para vários "If"?

tecdiego

Diz oq você quer fazer! Assim podemos fazer o código do zero!

Go Up