Hi there, I'm trying to write program in wich i measure a signal to switch on/of a relais. the relais should inmediate turn on if there is a signal, if there is no signal for 2 minutes the relais should turn off. I tried several ways but i can't get it right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
{
void loop(){
int i=0;
val = analogRead(audio);
Serial.println(val);
while (val < 20);{
for (i = 0; i < 2000; i++)
delay(50);
Serial.println(i);
if (i=1999){
digitalWrite(power, LOW);
}
if (val > 19);{
digitalWrite(power, HIGH);
Serial.println(val);
val = analogRead(audio);
}
int i = 0;
val = analogRead(audio);
Serial.println(val);
//for(initialization; condition; increment)
//¿i<2000 as initialization?
for (i < 2000; val < 20;) {
analogRead(audio); //the value is discarded
while (val < 20) //infinite loop
; //this is the statement of the lop, so you do nothing a lot of times
i++;
delay(60);
if (i = 1999) { //= is assignment, == is comparison, ¿why 1999?
digitalWrite(power, LOW);
}
if (val > 19)
; //this is the statement
{ digitalWrite(power, HIGH); } //this is outside the conditional
write pseudocode first, decide the meaning of your variables and if you work by polling or interruptions
thanks for youre reply. In spite off several ways to write the code , i can't get it right.
I'm a beginner in c++. I want a signal measured for 2 minutes, if the signal is below 20 and then switch an output to LOW. but it should jump out of the loop if the signal is above 20 and then switch the output to high. i tried this:
int main() {
void loop(){
int i;
val = analogRead(audio);
Serial.println(val);
for (val < 20; i < 2000;)
Serial.println(i);// just to check the value of i
i++;
delay(60);
if (i=1999); //max of i = 1999 (i < 2000)
digitalWrite(power, LOW);
}
if (val > 19);{
digitalWrite(power, HIGH);
Serial.println(val);
val = analogRead(audio);
digitalWrite(power, HIGH);
}
Thanks, but due to my bad understanding of the englisch language i can't figure out how to wright the correct code. Please advice. Any help will be most appriciated.
The problem is that you're putting a semi-colon in your "if" statements that is legal C++ code, but makes it mean something different from what you intended:
12 13
if (i=1999); //max of i = 1999 (i < 2000)
digitalWrite(power, LOW);
The semicolon in line 12 is an empty statement, but because you put it there, that empty statement is the thing that only executes if the condition is true. It's the equivalent of writing:
12 13 14 15 16
if (i=1999) //max of i = 1999 (i < 2000)
{
;
}
digitalWrite(power, LOW);
when what you really wanted to do was probably:
12 13 14 15
if (i=1999) //max of i = 1999 (i < 2000)
{
digitalWrite(power, LOW);
}