loop while

Oct 18, 2020 at 3:46pm
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);
     } 
Last edited on Oct 19, 2020 at 12:39pm
Oct 18, 2020 at 4:23pm
your code, properly indented
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
Last edited on Oct 18, 2020 at 4:26pm
Oct 18, 2020 at 5:48pm
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
     }

Last edited on Oct 19, 2020 at 12:32pm
Oct 19, 2020 at 11:17am
You have the same mistake in your if statements that ne555 has already pointed out.

Also, please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Oct 19, 2020 at 12:37pm
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.
Oct 19, 2020 at 2:51pm
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);
      }


Do you see now?
Last edited on Oct 19, 2020 at 3:08pm
Oct 19, 2020 at 3:35pm
Is this what you're after?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void loop() {
	for (int i = 0; i < 2000; ++i) {
		const int val = analogRead(audio);

		if (val > 19) {
			digitalWrite(power, HIGH);
			break;
		}

		if (i == 1999)
			digitalWrite(power, LOW);
		else
			delay(60);
	}
}


int main()
{
	loop();
}

Oct 23, 2020 at 7:01pm
Thank you all. I have learn't a lot. Amazing how you turned this"problem"into something i can understand.
Topic archived. No new replies allowed.