tecdiego:
Testa aí
#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"?