user88306: I am trying to create an embedded system for my university course and the concept i came up with using the components provided is a tunable distance sensor used for accurate measurement. It uses an ultrasonic sensor to determine the distance, a potentiometer to determine the distance wanted and 3 LEDs to show whether the sensor is too close, too far or at the correct distance. The code uses an interrupt system and a push button to activate the system. The system seems to be functioning properly and the serial monitor shows that both the potetentiometer and ultrasonic sensor are functioning properly, however the correct LED doesn't turn on when the relationship between the distance and distance wanted is true.
Any help is appreciated :]
*EDIT- I have moved some stuff around and now the middle LED on pin 7 is staying on when the button is pushed and it doesnt seem to fuction as intended
this is the code
int LEDPin1= 8;
int LEDPin2= 7;
int LEDPin3= 5;
int pinAnalogIn = A4; 
int interruptPin = 2;
volatile byte state = 0;
int triggerPin =9;
int echoPin = 6;
long int timeOfFlight = 0;
float speedOfSound = 343.0;
int inputCurrent;
long int myMap(long int x, long int xLow, long int xHigh, long int yLow, long int yHigh);

long int myMap(long int x, long int xLow, long int xHigh, long int yLow, long int yHigh){
  long int a = 0;
  long int y = 0;
  if (x!=xLow){
    a=(xHigh-x)/(x-xLow);
    y=(yHigh+a^yLow)/(a+1);
  }
  else {y=yLow;}
  return y;
}


void switchInt(){
  state=!state;
}

void FunctionMain(){
  int sensorValue=analogRead(A4);
  int distanceCm = 0.0;
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(5);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  timeOfFlight = pulseIn(echoPin, HIGH);
   
  distanceCm = speedOfSound*timeOfFlight/(2*10000);

  delay(10);

  int sensorValueTransformed = 0.0;
  sensorValueTransformed= myMap(sensorValue, 0, 1023, 0, 30);

  Serial.println("Sensor Value =");
  Serial.println(sensorValue);
  Serial.println("New Sensor Value =");
  Serial.println(sensorValueTransformed);
  Serial.println(distanceCm);
  Serial.println("\n");

  if (sensorValueTransformed-1<<distanceCm<<sensorValueTransformed+1){
    digitalWrite(LEDPin2, HIGH);
    digitalWrite(LEDPin1, LOW);
    digitalWrite(LEDPin3, LOW);
  }
  else{
  if (sensorValueTransformed<<distanceCm){
    digitalWrite(LEDPin3, HIGH);
    digitalWrite(LEDPin1, LOW);
    digitalWrite(LEDPin2, LOW);
    
  }
    if (sensorValueTransformed>>distanceCm){
    digitalWrite(LEDPin1, HIGH);
    digitalWrite(LEDPin3, LOW);
    digitalWrite(LEDPin2, LOW);
    
  }}
  delay(1000);

}




void setup(){
  Serial.begin(9600);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LEDPin1, OUTPUT);
  pinMode(LEDPin2, OUTPUT);
  pinMode(LEDPin3, OUTPUT);
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), switchInt, RISING);
}

void loop(){
  
  if (state == 1){
    FunctionMain();
  }

Sensor Value =
184
New Sensor Value =
6
37


Sensor Value =
186
New Sensor Value =
6
36


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
34


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
24


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
37


Sensor Value =
186
New Sensor Value =
6
25


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
187
New Sensor Value =
6
36


Sensor Value =
187
New Sensor Value =
6
43


Sensor Value =
186
New Sensor Value =
6
34


Sensor Value =
186
New Sensor Value =
6
34


Sensor Value =
186
New Sensor Value =
6
35


Sensor Value =
186
New Sensor Value =
6
37


Sensor Value =
187
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
33


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
187
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
25


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
25


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
187
New Sensor Value =
6
793


Sensor Value =
186
New Sensor Value =
6
23


Sensor Value =
186
New Sensor Value =
6
35


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
185
New Sensor Value =
6
26


Sensor Value =
187
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
24


Sensor Value =
186
New Sensor Value =
6
31


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
22


Sensor Value =
186
New Sensor Value =
6
793


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
31


Sensor Value =
186
New Sensor Value =
6
24


Sensor Value =
186
New Sensor Value =
6
25


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
36


user3917: I'll mention some minor problems before outlining a better arrangement of your if statements.
• In general, when declaring a constant, put const somewhere in its declaration.  Eg, instead of int LEDPin1= 8; say const int LEDPin1= 8;.  (gcc may well deduce on its own that constants are constant, but if not, informing it of constants can save a few bytes of memory.  Also, look up the enum statement.)
• In the statement y=(yHigh+a^yLow)/(a+1); you use ^ to compute a XOR yLow, which might be reasonable if you intended to do bit operations rather than arithmetic, but otherwise is suspect.  (In this case, with yLow being 0, a XOR yLow equals a, so the error has little effect.)
• As noted in comments, << is not a comparison operator.  In a non-IO context like this, it's a left-shift operator, and there probably should have been shift-count-out-of-range error messages.  (In IO context, it is an output operator.)  Also, regarding multiple comparisons, see suggested arrangement of if statements, below.  (Languages like Python and Julia can parse a<b<c as desired, but in C, as mentioned, it compares the boolean result of one of the comparisons to the other number.)
• I'd expect int distanceCm = 0.0; to get a compile error or warning about an improper initialization value.  Perhaps you meant int distanceCm = 0; or float distanceCm = 0.0;.
• The long int myMap(...); function declaration before the long int myMap(...){...} function definition is redundant, and not needed unless the function definition is in a different compilation unit than a function invocation.
• You say volatile byte state = 0; ... state=!state; ... if (state == 1){ FunctionMain(); }, and have no other references to state.  I suggest you change that last to if (state){ state=0; FunctionMain(); } so that you process all of the rising edges rather than every other rising edge (unless that is what you meant to do).  Also, if (state) is more C-literate than if (state == 1), plus being five characters shorter.
• Rather than Serial.println("Sensor Value ="); and Serial.println("New Sensor Value ="); say Serial.print("Sensor Value = "); and Serial.print("New Sensor Value = ");, supposing you want labels and values to output on the same line.
• To get rid of those clunky 3-line groups of digitalWrite calls, define a turnOn function as follows:
void turnOn(pin) {   
    digitalWrite(LEDPin1, LOW);
    digitalWrite(LEDPin2, LOW);
    digitalWrite(LEDPin3, LOW);
    digitalWrite(pin, HIGH);
}

and then use calls like turnOn(LEDPin1); or turnOn(LEDPin2); or turnOn(LEDPin3); to turn on selected LEDs.
Now, regarding the arrangement of if statements:  By checking the two out-of-range cases first, you can avoid the need for the four-comparisons code that you attempted.  Eg:
if (sensorValueTransformed =< distanceCm-1)
   turnOn(LEDPin3);
else
   if (sensorValueTransformed >= distanceCm+1)
     turnOn(LEDPin1);
   else turnOn(LEDPin2);

It may at first appear that the effect of this code is different than what you attempted, due to the comparisons to distanceCm-1 and distanceCm+1 instead of to distanceCm but observe that your comparisons to distanceCm were only going to happen if sensorValueTransformed - distanceCm was 1 or more.
Note, it probably doesn't make sense to have sensorValueTransformed and distanceCm being integers instead of floats.  If they are floats, we can reasonably write < and > instead of =< and >= in my code snippet above.  If they are integers, the boolean test values (sensorValueTransformed =< distanceCm-1) and (sensorValueTransformed >= distanceCm+1) have the same values as (sensorValueTransformed < distanceCm) and (sensorValueTransformed > distanceCm) which may well be not what you want.

88306: <p>I am trying to create an embedded system for my university course and the concept i came up with using the components provided is a tunable distance sensor used for accurate measurement. It uses an ultrasonic sensor to determine the distance, a potentiometer to determine the distance wanted and 3 LEDs to show whether the sensor is too close, too far or at the correct distance. The code uses an interrupt system and a push button to activate the system. The system seems to be functioning properly and the serial monitor shows that both the potetentiometer and ultrasonic sensor are functioning properly, however the correct LED doesn't turn on when the relationship between the distance and distance wanted is true.</p>
<p>Any help is appreciated :]
*EDIT- I have moved some stuff around and now the middle LED on pin 7 is staying on when the button is pushed and it doesnt seem to fuction as intended</p>
<p>this is the code</p>
<pre class="lang-cpp prettyprint-override"><code>int LEDPin1= 8;
int LEDPin2= 7;
int LEDPin3= 5;
int pinAnalogIn = A4; 
int interruptPin = 2;
volatile byte state = 0;
int triggerPin =9;
int echoPin = 6;
long int timeOfFlight = 0;
float speedOfSound = 343.0;
int inputCurrent;
long int myMap(long int x, long int xLow, long int xHigh, long int yLow, long int yHigh);

long int myMap(long int x, long int xLow, long int xHigh, long int yLow, long int yHigh){
  long int a = 0;
  long int y = 0;
  if (x!=xLow){
    a=(xHigh-x)/(x-xLow);
    y=(yHigh+a^yLow)/(a+1);
  }
  else {y=yLow;}
  return y;
}


void switchInt(){
  state=!state;
}

void FunctionMain(){
  int sensorValue=analogRead(A4);
  int distanceCm = 0.0;
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(5);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  timeOfFlight = pulseIn(echoPin, HIGH);
   
  distanceCm = speedOfSound*timeOfFlight/(2*10000);

  delay(10);

  int sensorValueTransformed = 0.0;
  sensorValueTransformed= myMap(sensorValue, 0, 1023, 0, 30);

  Serial.println(&quot;Sensor Value =&quot;);
  Serial.println(sensorValue);
  Serial.println(&quot;New Sensor Value =&quot;);
  Serial.println(sensorValueTransformed);
  Serial.println(distanceCm);
  Serial.println(&quot;\n&quot;);

  if (sensorValueTransformed-1&lt;&lt;distanceCm&lt;&lt;sensorValueTransformed+1){
    digitalWrite(LEDPin2, HIGH);
    digitalWrite(LEDPin1, LOW);
    digitalWrite(LEDPin3, LOW);
  }
  else{
  if (sensorValueTransformed&lt;&lt;distanceCm){
    digitalWrite(LEDPin3, HIGH);
    digitalWrite(LEDPin1, LOW);
    digitalWrite(LEDPin2, LOW);
    
  }
    if (sensorValueTransformed&gt;&gt;distanceCm){
    digitalWrite(LEDPin1, HIGH);
    digitalWrite(LEDPin3, LOW);
    digitalWrite(LEDPin2, LOW);
    
  }}
  delay(1000);

}




void setup(){
  Serial.begin(9600);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LEDPin1, OUTPUT);
  pinMode(LEDPin2, OUTPUT);
  pinMode(LEDPin3, OUTPUT);
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), switchInt, RISING);
}

void loop(){
  
  if (state == 1){
    FunctionMain();
  }
</code></pre>
<pre><code>Sensor Value =
184
New Sensor Value =
6
37


Sensor Value =
186
New Sensor Value =
6
36


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
34


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
24


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
37


Sensor Value =
186
New Sensor Value =
6
25


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
187
New Sensor Value =
6
36


Sensor Value =
187
New Sensor Value =
6
43


Sensor Value =
186
New Sensor Value =
6
34


Sensor Value =
186
New Sensor Value =
6
34


Sensor Value =
186
New Sensor Value =
6
35


Sensor Value =
186
New Sensor Value =
6
37


Sensor Value =
187
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
33


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
187
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
25


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
25


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
187
New Sensor Value =
6
793


Sensor Value =
186
New Sensor Value =
6
23


Sensor Value =
186
New Sensor Value =
6
35


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
185
New Sensor Value =
6
26


Sensor Value =
187
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
27


Sensor Value =
186
New Sensor Value =
6
28


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
24


Sensor Value =
186
New Sensor Value =
6
31


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
22


Sensor Value =
186
New Sensor Value =
6
793


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
29


Sensor Value =
186
New Sensor Value =
6
31


Sensor Value =
186
New Sensor Value =
6
24


Sensor Value =
186
New Sensor Value =
6
25


Sensor Value =
186
New Sensor Value =
6
26


Sensor Value =
186
New Sensor Value =
6
30


Sensor Value =
186
New Sensor Value =
6
36
</code></pre>
 3917: <p>I'll mention some minor problems before outlining a better arrangement of your <code>if</code> statements.</p>
<p>• In general, when declaring a constant, put <code>const</code> somewhere in its declaration.  Eg, instead of <code>int LEDPin1= 8;</code> say <code>const int LEDPin1= 8;</code>.  (gcc may well deduce on its own that constants are constant, but if not, informing it of constants can save a few bytes of memory.  Also, look up the <code>enum</code> statement.)</p>
<p>• In the statement <code>y=(yHigh+a^yLow)/(a+1);</code> you use <code>^</code> to compute <code>a XOR yLow</code>, which might be reasonable if you intended to do bit operations rather than arithmetic, but otherwise is suspect.  (In this case, with <code>yLow</code> being 0, <code>a XOR yLow</code> equals <code>a</code>, so the error has little effect.)</p>
<p>• As noted in comments, <code>&lt;&lt;</code> is not a comparison operator.  In a non-IO context like this, it's a left-shift operator, and there probably should have been shift-count-out-of-range error messages.  (In IO context, it is an output operator.)  Also, regarding multiple comparisons, see suggested arrangement of <code>if</code> statements, below.  (Languages like Python and Julia can parse <code>a&lt;b&lt;c</code> as desired, but in C, as mentioned, it compares the boolean result of one of the comparisons to the other number.)</p>
<p>• I'd expect <code>int distanceCm = 0.0;</code> to get a compile error or warning about an improper initialization value.  Perhaps you meant <code>int distanceCm = 0;</code> or <code>float distanceCm = 0.0;</code>.</p>
<p>• The <code>long int myMap(...);</code> function declaration before the <code>long int myMap(...){...}</code> function definition is redundant, and not needed unless the function definition is in a different compilation unit than a function invocation.</p>
<p>• You say <code>volatile byte state = 0;</code> ... <code>state=!state;</code> ... <code>if (state == 1){ FunctionMain(); }</code>, and have no other references to <code>state</code>.  I suggest you change that last to <code>if (state){ state=0; FunctionMain(); }</code> so that you process all of the rising edges rather than every other rising edge (unless that is what you <em>meant</em> to do).  Also, <code>if (state)</code> is more C-literate than <code>if (state == 1)</code>, plus being five characters shorter.</p>
<p>• Rather than <code>Serial.println(&quot;Sensor Value =&quot;);</code> and <code>Serial.println(&quot;New Sensor Value =&quot;);</code> say <code>Serial.print(&quot;Sensor Value = &quot;);</code> and <code>Serial.print(&quot;New Sensor Value = &quot;);</code>, supposing you want labels and values to output on the same line.</p>
<p>• To get rid of those clunky 3-line groups of <code>digitalWrite</code> calls, define a <code>turnOn</code> function as follows:</p>
<pre><code>void turnOn(pin) {   
    digitalWrite(LEDPin1, LOW);
    digitalWrite(LEDPin2, LOW);
    digitalWrite(LEDPin3, LOW);
    digitalWrite(pin, HIGH);
}
</code></pre>
<p>and then use calls like <code>turnOn(LEDPin1);</code> or <code>turnOn(LEDPin2);</code> or <code>turnOn(LEDPin3);</code> to turn on selected LEDs.</p>
<p>Now, regarding the arrangement of <code>if</code> statements:  By checking the two out-of-range cases first, you can avoid the need for the four-comparisons code that you attempted.  Eg:</p>
<pre><code>if (sensorValueTransformed =&lt; distanceCm-1)
   turnOn(LEDPin3);
else
   if (sensorValueTransformed &gt;= distanceCm+1)
     turnOn(LEDPin1);
   else turnOn(LEDPin2);
</code></pre>
<p>It may at first appear that the effect of this code is different than what you attempted, due to the comparisons to <code>distanceCm-1</code> and <code>distanceCm+1</code> instead of to <code>distanceCm</code> but observe that your comparisons to <code>distanceCm</code> were only going to happen if <code>sensorValueTransformed - distanceCm</code> was 1 or more.</p>
<p>Note, it probably doesn't make sense to have <code>sensorValueTransformed</code> and <code>distanceCm</code> being integers instead of floats.  If they are floats, we can reasonably write <code>&lt;</code> and <code>&gt;</code> instead of <code>=&lt;</code> and <code>&gt;=</code> in my code snippet above.  If they are integers, the boolean test values <code>(sensorValueTransformed =&lt; distanceCm-1)</code> and <code>(sensorValueTransformed &gt;= distanceCm+1)</code> have the same values as <code>(sensorValueTransformed &lt; distanceCm)</code> and <code>(sensorValueTransformed &gt; distanceCm)</code> which may well be not what you want.</p>
