button that turns off all features in embedded systems


Hey my name is Sophia,
I am new to coding and embedded systems and I wanted to make a button that turns off and on a LED and at the same time turn off all other futures in the system.
So far I had the button turn off and on but I cant seem to get it to also update the Potentiometer. For some reason the code would check if the button is pressed and if so then the LED would turn on and then check if the LED is on and if so then turn on the other LEDs but when I change the value of the Potentiometer( which should switch to other LEDs) it would not update and stay on the same LED. So my question is how can I put another if statement that would keep updating in the while loop?
the code that I wanted to keep updating while the first LED is on is the "else if code"
Hope that made sense.
:)

note:
I don't know if my approach is right as I am looking at the LED and not the button it self, as my code checks if the LED is on rather then if the button is pressed.
( btw its not a switch which would have made my life a lot easier :( "

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include "mbed.h"
 
DigitalIn userButton(PC_10);
DigitalOut led(PC_0);
 
bool buttonDown = false;
 BusOut leds_bus(PC_1, PB_0, PA_4);

AnalogIn pot1(PA_5);
void init_leds();
int cntl_val = 0;  
int main() {
    cntl_val = pot1.read_u16()/32768;
 while (true) {  // run forever
  
  if (userButton.read() == true) {  // button is pressed
     if  (!buttonDown) {  // a new button press
      led = !led;                 // toogle LED
      buttonDown = true;     // record that the button is now down so we don't count one press lots of times
      ThisThread::sleep_for(100);
      
       
}
else if (led.read()==true){
if (cntl_val < 1/3 )
        {
            leds_bus.write(4);
        }
         if (cntl_val > (1/3) && cntl_val < (2/3) )
        { 
            leds_bus.write(2);
            }
         if (cntl_val > (2/3))
        {
            leds_bus.write(1);
            
        }
   }
  }
  else { // button isn't pressed
    buttonDown = false;
   
   }
 
 }
 
 }
 
Is the knob an actual potentiometer, or is it a digital control of sorts? A potentiometer cannot be changed from software. It's a simple lever attached to a resistive strip that controls the electrical resistance across two terminals.
Yes its an potentiometer, I am using a microprocessor called Nucleo l476rg and using Mbed studio to program it
Have you heard of integer division? In c++ and most other programming languages (but not python 3) an int divided by an int must yield an int, so results in truncation toward 0.

1/3 and 2/3 will both evaluate as 0 and I suspect that line 13 will also set cntl_val to 0. So the condition on line 25 will never be true.

If you want floating-point numbers use type double for variables and put a decimal point in constants.
Last edited on
Topic archived. No new replies allowed.