Originally Posted by
Norm
Yes, that is one of the problems with floating point numbers. Look at the printed values.
Instead of using == 0 use >= with a value very close to 0.
So when the value goes below 0.10000000000000003 then stop decrementing
The code has merged two parts of the logic into one.
begin loop
if incrementCount
then increment count
else decrement count
if count > .9
then incrementCount = false << stop incrementing when at 1
if not incrementCount <<< now decrementing count
then if count > .1 then decrement count << keep decrementing, stop when at lower boundary
end loop
package gettingstarted;
//Add Phidgets Library | You added a file called phidget22 when configuring your project. Import gives you access to the Phidgets library code inside that file.
import com.phidget22.*;
public class LEDBrightness {
//Handle Exceptions
public static void main(String[] args) throws Exception{
double count=0; // count starts at zero
boolean incrementCount =true;
//Create
DigitalOutput redLED = new DigitalOutput();
//Address
redLED.setHubPort(1);
redLED.setIsHubPortDevice(true);
//Open
redLED.open(1000);
//Use your Phidgets with Duty Cycle
while (true) {
if (incrementCount==true && count <=.99) { // if incrementCount is true which it starts as true count will incremetn by .1
count+=.1;
}else{
count-=.1; // if incrementCount is not true is will decrement the count by.1
}
if(incrementCount && count>=.99 && count>=.10000000000000003){ // when count is zero it does nothing
// conditional where if incrementCount is true and the count has reached the boundary increment count will be false
//will go to the else and start decrementing
incrementCount=false;
} else {
count=0;
}
System.out.println(count);
redLED.setDutyCycle(count);
Thread.sleep(1000);
}
}
}
the change I made is instead of count!=0; I put in the decrement conditional that
if(incrementCount && count>=.99 && count>=.10000000000000003)
so that if it is greater than .99 and greaterthan or equal to .100..3 that it will decrement, my else is if it isn't greater than .1000..3 that count=0; therefore staying at 0 and since incrementCount is false it cant go up but what I'm getting is that it prints
0.0
0.0
0.0
0.0
i assume that this is because its checking the statement for decrementing and after it increments its sets it self to zero but I'm unsure how i coul dmake it do nothing or stay at zero