9 years, 10 months ago.

Toggling LED1/LED2 when voltage changes ??

Hello there,

using mbed application board, am trying to light up LED1 and LED2 when changing the 2 potentiometers using the following code:

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
AnalogIn pot1(p19);
AnalogIn pot2(p20);

int main() {
float  p1,p2;
    while(1) {
        p1=pot1.read()*3.3;
        p2=pot2.read()*3.3;

      float  sum=(p1+p2);
float p1c=p1;
float p2c=p2;
    if (p1 !=  p1c){ led1 = 1;}
    else {led1=0;}
    if (p2 != p2c) {led2 = 1;}
    else {led2=0;}
}

} 

I tried first using IterruptIn but it turns out p19 and 20 doesn't work. Now am struggling to know how to utilize if-statement for changing variable and I didn't find a solution until now ... Any ideas ?

1 Answer

9 years, 10 months ago.

Please use <<code>> and <</code>> tags around your posted code to keep it readable. Like this:

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
AnalogIn pot1(p19);
AnalogIn pot2(p20);

int main() {
  float p1,p2;
  while(1) { 
    p1=pot1.read()*3.3;
    p2=pot2.read()*3.3;

    float sum=(p1+p2);
    float p1c=p1;
    float p2c=p2; 
  
    if (p1 != p1c) {led1 = 1;}
    else {led1=0;}

    if (p2 != p2c) {led2 = 1;}
    else {led2=0;} 
  }
}

There are some issues with your code: first it makes p1c equal to p1, and then right afterwards the software tests to see if p1c is unequal to p1. Obviously, that will never be the case, so the LED is always off. Second issue is that the while loop will execute very fast. So even when the pot changes and the values are unequal at some time, the LED will flash only very briefly (microseconds..) and then be off again. Finally, the analog value will always fluctuate a bit (noise etc) and the LED might light up briefly even when you dont touch the pot.

Try something like this:

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
AnalogIn pot1(p19);
AnalogIn pot2(p20);

int main() {
  float p1,p2;
  float p1c=0; // declare outside the loop and initialise
  float p2c=0; 
  float sum=0;
  
  while(1) { 
    p1=pot1.read()*3.3;
    p2=pot2.read()*3.3;

    sum=(p1+p2);

    if (p1 != p1c) {  // you may want to use a threshold to respond only to real changes and ignore noise
      led1 = 1;
      p1c = p1;  // save the changed value for the next test
    }
    else {led1=0;}

    if (p2 != p2c) {
      led2 = 1;
      p2c = p2;
    }
    else {led2=0;} 

    wait(0.5); // delay a bit to show the effect
  }
}

Accepted Answer

yep, it did work . Thanks very much, I did update the post and put the "<<code>>" .. now am trying to learn what do you mean by "Threshold"

posted by Mohammed Almoosawi 29 Jun 2014

Great. Threshold means that you dont just check that (p1 != p1c) but instead you test to see if the difference between the new value and the old value is more than some minimum. The difference should be larger than what could be caused by typical analog noise on the pot. Obviously this difference can be positive or negative. So you test to see is the absolute value of the difference is larger than some minimum threshold.

posted by Wim Huiskamp 29 Jun 2014

Can I limit the reading of analogue in to two decimal places ?? I mean limiting pot1.read() & pot1.read() to x.xx only ?

posted by Mohammed Almoosawi 29 Jun 2014