Skip to main content
more suitable function signature
Source Link

Your code becomes more readable, if you use functions with return values.

For example, something like this structure

unsigned int limit; // 0 = off, else limit in cm  ...
void loop() {
  if (irrecv.decode(&results)) {
      byte key = getIRKey(&resultsresults.value); // 0 , 1, 2, etc.,  0xFF for unknown 
      irrecv.resume();
      switch (key) {
         case 0: limit = 0; break;
         case 1: limit = 5; break;
         ... 
         default: break; // wrong key does not change limit
      }   
  } 
  if (limit) {
     unsigned int dist = sonic(); // returns the measured distance or 0 
     if (dist && dist < limit) NewTone(buzPin, 2000);
     else noNewTone(buzPin); // off: too far
  } else noNewTone(buzPin); // off: no check
}

This code should not block. Have fun modifying sonic() and creating the other helper function.

Your code becomes more readable, if you use functions with return values.

For example, something like this structure

unsigned int limit; // 0 = off, else limit in cm  ...
void loop() {
  if (irrecv.decode(&results)) {
      byte key = getIRKey(&results); // 0 , 1, 2, etc.,  0xFF for unknown 
      irrecv.resume();
      switch (key) {
         case 0: limit = 0; break;
         case 1: limit = 5; break;
         ... 
         default: break; // wrong key does not change limit
      }   
  } 
  if (limit) {
     unsigned int dist = sonic(); // returns the measured distance or 0 
     if (dist && dist < limit) NewTone(buzPin, 2000);
     else noNewTone(buzPin); // off: too far
  } else noNewTone(buzPin); // off: no check
}

This code should not block. Have fun modifying sonic() and creating the other helper function.

Your code becomes more readable, if you use functions with return values.

For example, something like this structure

unsigned int limit; // 0 = off, else limit in cm  ...
void loop() {
  if (irrecv.decode(&results)) {
      byte key = getIRKey(results.value); // 0 , 1, 2, etc.,  0xFF for unknown 
      irrecv.resume();
      switch (key) {
         case 0: limit = 0; break;
         case 1: limit = 5; break;
         ... 
         default: break; // wrong key does not change limit
      }   
  } 
  if (limit) {
     unsigned int dist = sonic(); // returns the measured distance or 0 
     if (dist && dist < limit) NewTone(buzPin, 2000);
     else noNewTone(buzPin); // off: too far
  } else noNewTone(buzPin); // off: no check
}

This code should not block. Have fun modifying sonic() and creating the other helper function.

Source Link

Your code becomes more readable, if you use functions with return values.

For example, something like this structure

unsigned int limit; // 0 = off, else limit in cm  ...
void loop() {
  if (irrecv.decode(&results)) {
      byte key = getIRKey(&results); // 0 , 1, 2, etc.,  0xFF for unknown 
      irrecv.resume();
      switch (key) {
         case 0: limit = 0; break;
         case 1: limit = 5; break;
         ... 
         default: break; // wrong key does not change limit
      }   
  } 
  if (limit) {
     unsigned int dist = sonic(); // returns the measured distance or 0 
     if (dist && dist < limit) NewTone(buzPin, 2000);
     else noNewTone(buzPin); // off: too far
  } else noNewTone(buzPin); // off: no check
}

This code should not block. Have fun modifying sonic() and creating the other helper function.