0

This is a basic question but I don't know why it isn't working.

I want that when a condition is satisfied, a variable increases its value in one (of course I've done this before but it isn't working). This is my code:

double count0 = 0;
if (neural.getResult(emg, eda, ax, ay, az, 1000) == 1){
    count0 = count0 +1;
}

neural.getResult(emg, eda, ax, ay, az, 1000) is constantly reading real time values of sensors and it gives me 1 or 0 depending of those sensors, I'm trying to count how many ones it's giving me, but when I print count0 it only shows me a 1, like if it's only entering once in the if

user8559076
  • 55
  • 1
  • 1
  • 8
  • 2
    "like if it's only entering once in the if" `if`s only execute once. Do you mean to use `while`? Or, if this is in a loop that you've not shown, do you mean to declare `count0` outside the loop? – Andy Turner Nov 22 '17 at 21:01
  • Have you tried to step through that code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? – litelite Nov 22 '17 at 21:02
  • Also, can you post more code. At least the full method where that code is – litelite Nov 22 '17 at 21:03
  • Show your loop or timer, how often do you enter that code fragment? – eckes Nov 22 '17 at 21:06
  • You need to add a loop or probably put observer pattern into use. Basically you need to monitor neural.getResult(). What you’re doing is just calling it once (according to your code). – isank-a Nov 22 '17 at 21:12
  • I just updated the code – user8559076 Nov 22 '17 at 21:14
  • I posted all the code. I defined count0 as a global variable and it's working now, but sometimes it prints the same value several times, why? – user8559076 Nov 22 '17 at 21:37

2 Answers2

1

If you want to have it executed more than once, you have to use a loop.

The general syntax for the while loop is:

while (condition)
{
    //do sth..
}

So in your example this would get:

double count0 = 0;
boolean run = true;
while (run)
{
    if (neural.getResult(emg, eda, ax, ay, az, 1000) == 1){
        count0 = count0 + 1;
    }
    //add some sort of exit here.
    if (your_stop_condition)
    {
        run = false;
    }
}

But be careful: the above does execute infinitely, if you do not set run to false somewhere.

cramopy
  • 3,459
  • 6
  • 28
  • 42
0

Is your case in a loop? If it is, then everytime you get into the case count0 will be 0 and add 1. So 0+1=1. When it enters the case again, count0 will be 0 once again and it will add 1 to 0(count0=0). Put your count0 variable out of the loop if your case is in a loop

davigzzz
  • 134
  • 1
  • 12
  • I did that and it works, but sometimes it prints the same value several times and I don't know why. – user8559076 Nov 22 '17 at 21:46
  • Maybe because neural is not 1, so it isn't adding anything to count0 but you are still printing it. If you dont want to print the value if it doesn't changes, then put your log inside the if statement after count0 = count0 +1; – davigzzz Nov 22 '17 at 21:53