Program is doing both for loop and if statement at the same time

Hello, I'm trying to code a program which will cause my nucleo board to output different symbols each time the blue user button is pressed. However, The program performs both the for loop and the if statement at the same time whereas the for loop should be finished first and then the if statement should be done. Any help in fixing this would be appreciated.
This is my code:

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
#include "mbed.h"
AnalogIn ADC(A0);
AnalogIn BED(A5);
DigitalIn myswitch(USER_BUTTON); //switch is called 'myswitch'

int main(void)
{
	double Vin, Vin2, Voltage, Voltage2;
	while (1)
	{
		if (!myswitch.read()) //if button is on
		{
			Vin = ADC.read(); //reads input from pin
			Voltage = Vin*3.3;
			for (int i = 0; i <= Voltage; i++)
			{
				printf("^");
				i++;
			}
			
			
			
			if (!myswitch.read())
			{
				Vin2 = BED.read();
				Voltage2 = Vin2*3.3;
				for (int x = 0; x <= Voltage2; x++)
				{
					printf("*");
					x++;
				}
			}
		}
	}
}

Last edited on
L11 and L23 are the same condition.

Also having i++ and x++ (L18 and 30) in the body of the loop is also wrong.
It's already incremented in the loop control.

Are your switches debounced?
https://www.circuitbasics.com/switch-debouncing/

If not, your while(1) is going to go round that loop many times before the switch settles on the "on" or "off" position.
Hello HumanGoose,

A couple of things to start with:

Without the "mbed.h" file the program does not compile. It is always best to post the whole program or at least enough that can be compiled and testes. This includes any input file(s) that go with the program.

Using the tab key in your IDE may look fine, but when posting code here the tab key tends to be exaggerated here. Not knowing what IDE you are using it is hard to say exactly what steps to take to make changes. It may start with a tab called tools -> options or preferences after that and possibly under "Text Editor" look for the part to do with tabs. In MSVS it is "tools -> Options -> Text Editor -> C/C++ -> Tabs". For the size of the tab 4 is a good number then choose to insert spaces.

Unless it is your intention the while loop is an endless loop with no way out.

I have a feeling that you misplaced the closing } for the first if statement making the 2nd if accessible only if the 1st if statement is true. Unless that is what you want.

Should the 1st if statement be true the 1for loop would finish before the 2nd if statement is reached. It is possible that the 1st for loop could be bypassed if the value of "Voltage" is less than (0)zero.

Another part I noticed:
15
16
17
18
19
20
for (int i = 0; i <= Voltage; i++)
{
    printf("^");

    i++;
}

Line 19 adds 1 to "i" before the last part of the for loop add 1. This means that "i" is incremented twice. The same for "x" in the next for loop.

Tip: Since you define the loop iterator< "i", in the for loop it is a local variable to that for loop and when finished the variable is destroyed. Meaning that you can use "i" in the next for loop without any problem.

Andy
Thanks for the response salem c, I didn't realise I had incremented twice so I've fixed that. The task requires that the blue user button on the nucleo board switches between these outputs therefore I'm unsure of what to do instead for the condition on L23. Also, I'm not entirely sure about switch debouncing, I've heard of it but I've never done or seen it before. The task does require that the code loops back to the start after the second button press which is why I did the super loop "while(1)", is this wrong in this case?
Hey Andy. I'm using Keil uVision 5 for this, I used to use visual studio so I'm not sure how to check the tabbing for you on Keil, but all I did was copy and paste the code into here, does that help make it easier to read?

My intention with the while loop is to have it so that after the blue user button is pressed a second time, it will loop back and wait for the first button press so that the button presses go like 1 ->2 -> 1 -> 2.

The } is indeed an error on my part and I've fixed it, thank you.

The value of voltage should never be less than zero as I'm using a potentiometer so I don't think that's the issue.

Thank you for pointing out the double incrementation, I didn't realise that I did this.


The main issue I think I'm having as salem c pointed out is that both if statements have the same condition, therefore, it is doing both which is causing both for loops to occur at once. However, I'm not sure how I could change the 2nd condition as I need the condition for both to be that the blue user button is pressed.
The task requires that the blue user button on the nucleo board switches between these outputs
In that case, the sense of the second if is wrong. Shouldn't that be looking for the button to be off instead of on?

You're trying to do something when the state of the button changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (1) {
     currentState = myswitch.read();

     // wait for the state to change, checking every 100ms
     do {
          sleep(100ms);
     } while (currentState == myswitch.read())

     if (myswitch.read()) {
         print stars
    } else {
         print carets
    }
}

Topic archived. No new replies allowed.