Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: Need logic help

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Need logic help

    I need help to write code where i want the count in redLED.setDutyCycle(count); to start at 0 and go up by.1 and when it reaches 1 go down by .1 then stay at zero
    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;   
     
            //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 (count==0) {
                  count+=0.1;
              } else {
                  count-=0.1;
              }
              redLED.setDutyCycle(count);
              Thread.sleep(1000);
     
        }
     
        }
     
     
     
        }
    Last edited by Rango; January 6th, 2021 at 01:53 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help to write code where i want the count in redLED.setDutyCycle(count); to start at 0 and go up by.1 and when it reaches 1 go down by .1 then stay at zero

    start at 0 and go up by.1 and when it reaches 1 go down by .1 then stay at zero
    Sounds like you need a boolean variable that says that count is either to be incremented or to be decremented.
    And then a test for when the value of count has hit one of the boundaries.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 6th, 2021)

  4. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: I need help to write code where i want the count in redLED.setDutyCycle(count); to start at 0 and go up by.1 and when it reaches 1 go down by .1 then stay at zero

    Quote Originally Posted by Norm View Post
    Sounds like you need a boolean variable that says that count is either to be incremented or to be decremented.
    And then a test for when the value of count has hit one of the boundaries.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    I changed the format, if I were to use a boolean variable would it affect it where once I've met the condition where count == 1 that the count down would start and count up would be false where since im counting down count is no longer 1 and therefore count up would be true

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    You would use the value in the boolean to decide if count was to be incremented or to be decremented.

    For example
    if(incrementCount) {
       count += 1;
    }else {
       count -= 1;
    }
    The code would change the value in incrementCount when the boundary was met:
    if(incrementCount && count >= boundary) {
       incrementCount = false;   // change so the value is decremented
    }

    There would be similar logic for the other conditions.
    Last edited by Norm; January 7th, 2021 at 12:10 PM. Reason: Fixed {}s
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 6th, 2021)

  7. #5
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    You would use the value in the boolean to decide if count was to be incremented or to be decremented.

    For example
    if(incrementCount)
       count += 1;
    else
       count -= 1;
    The code would change the value in incrementCount when the boundary was met:
    if(incrementCount && count >= boundary)
       incrementCount = false;   // change so the value is decremented



    There would be similar logic for the other conditions.
    Just to make sure the incrementCount boolean is true so it counts up and once it reaches the boundary in this case 1 it becomes false therfore in the the second if, increment Count is no longer true so it goes to the else and starts decrementing

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    Yes, that sounds about right. Now try writing the code for that part of it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    Yes, that sounds about right. Now try writing the code for that part of it.
     
    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;   
             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 && count <=1) 
                  count+=.1;
              else
                  count-=.1;
            incrementCount=false;
     
     
              redLED.setDutyCycle(count);
              Thread.sleep(1000);
     
        }
     
        }
     
     
        }

    So i tried implementing the logic you said, where I have the conditional where if count was less than one incrementCount would be true and that once count was => 1 it would go to the else and increment count would be false and would skip the if and go to the else and start decrementing but when i run the code i get one increment of .1 then it goes out other the boundary which is 0-1

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    The variable: incrementCount is set to false the first time through the loop. It should only be set false when the count has reached the boundary.

    The if statements need to be more complicated so that incrementCount count is only set false when the value of count has reached the boundary.
    It needs to stay true as the value is incremented until it reaches the boundary.

    Before writing any more code, write some pseudo code that describes the steps the program needs to take to solve the problem.
    i want the count to start at 0 <<< outside of loop
    and go up by.1 <<<< How can incrementCount be used to control this (if statement)
    and when it reaches 1 <<<<<< this is when incrementCount needs to change to false
    go down by .1 <<< controlled by incrementCount
    then stay at zero
    Add a few more statements to the above.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 7th, 2021)

  12. #9
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    The variable: incrementCount is set to false the first time through the loop. It should only be set false when the count has reached the boundary.

    The if statements need to be more complicated so that incrementCount count is only set false when the value of count has reached the boundary.
    It needs to stay true as the value is incremented until it reaches the boundary.

    Before writing any more code, write some pseudo code that describes the steps the program needs to take to solve the problem.

    Add a few more statements to the above.

    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) // 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>=1);  // conditional where if incrementCount is true and the count has reached the boundary increment count will be false 
                                              // when the loop iterates again and checks the first condiotional incrementCount will be false and it 
                                              //will go to the else and start decrementing 
                incrementCount=false;
     
     
     
     
     
     
     
              redLED.setDutyCycle(count);
              Thread.sleep(1000);
     
        }
     
        }
     
     
        }

    I changed the if statements and Im confused as to why it is being set as false when it iterates because to my understanding it should only be set to false when incrementCount is true and the boundary has been met where count is >=1; but since the error comes within seconds of running i can see that it becomes false too fast and I'm unsure why

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    You need to look at the compiler's warning statements when you compile the code.
    Here is the warning I get when I compile your code:
    LEDBrightness.java:35: warning: [empty] empty statement after if
             if(incrementCount && count>=1);  // conditional where if incrementCount is true and the count has reached the boundary increment count will be false 
                                           ^
    The ; ends the if statement. Any statements after that are NOT inside of the if. Remove the ;

    Also you should always use {} to enclose code that is controlled by an if statement. I was lazy and didn't. I have fixed it now.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 7th, 2021)

  15. #11
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    You need to look at the compiler's warning statements when you compile the code.
    Here is the warning I get when I compile your code:
    LEDBrightness.java:35: warning: [empty] empty statement after if
             if(incrementCount && count>=1);  // conditional where if incrementCount is true and the count has reached the boundary increment count will be false 
                                           ^
    The ; ends the if statement. Any statements after that are NOT inside of the if. Remove the ;

    Also you should always use {} to enclose code that is controlled by an if statement. I was lazy and didn't. I have fixed it now.
     
    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) { // 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>=1){
                                            // 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;
            }
     
     
     
     
     
     
              redLED.setDutyCycle(count);
              Thread.sleep(1000);
     
        }
     
        }
     
     
        }

    the warning I get is
    Exception in thread "main" PhidgetException 0x15 (Invalid Argument)
    Value must be in range: 0.000000 - 1.000000.
    at com.phidget22.DigitalOutputBase.setDutyCycle(Nativ e Method)
    I removed the ";" so that the incrementCount=false statement could be reached, but I can tell because I have an output device the shows the count going up but never going down and count is going past 1 and the conditional is not making incrementCount false the error message was after I made the changes but it runs until count is incremented past its boundary

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    What value is in count when the exception happens?

    For debugging, add some print statements that print the value of count and incrementCount as the loop iterates.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 7th, 2021)

  18. #13
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    What value is in count when the exception happens?

    For debugging, add some print statements that print the value of count and incrementCount as the loop iterates.
    0.1
    0.2
    0.30000000000000004
    0.4
    0.5
    0.6
    0.7
    0.7999999999999999
    0.8999999999999999
    0.9999999999999999
    1.0999999999999999
    Exception in thread "main" PhidgetException 0x15 (Invalid Argument)
    Value must be in range: 0.000000 - 1.000000.
    at com.phidget22.DigitalOutputBase.setDutyCycle(Nativ e Method)
    would this be a code problem or IDE problem

  19. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    So it looks like the code increments count past the value of 1 before testing if count's value is greater than 1.

    You need to notice how floating point numbers work. The value of count is never 1.0000
    It goes from 0.9999999999999999 to 1.0999999999999999
    so instead of testing count >= 1, test for >= 0.9
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 7th, 2021)

  21. #15
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    So it looks like the code increments count past the value of 1 before testing if count's value is greater than 1.

    You need to notice how floating point numbers work. The value of count is never 1.0000
    It goes from 0.9999999999999999 to 1.0999999999999999
    so instead of testing count >= 1, test for >= 0.9
    So technically the code is correct because the increment is .1 but for avoiding the error I should put .9. And if I want it to end after decrementing would how would i do that with a boolean or do i not need a boolean

  22. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    I want it to end after decrementing
    Use an if statement to prevent the value from going below the lower boundary.
    You should not need a new boolean. The value of incrementCount would be false while count is being decremented.

    So technically the code is correct
    There is a problem representing values of floating point numbers on a computer. There is not an exact fit so there is some rounding done.
    For a technical discussion: http://docs.oracle.com/cd/E19957-01/..._goldberg.html
    If you don't understand my answer, don't ignore it, ask a question.

  23. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 7th, 2021)

  24. #17
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    Use an if statement to prevent the value from going below the lower boundary.
    You should not need a new boolean. The value of incrementCount would be false while count is being decremented.


    There is a problem representing values of floating point numbers on a computer. There is not an exact fit so there is some rounding done.
    For a technical discussion: http://docs.oracle.com/cd/E19957-01/..._goldberg.html
    since
    count-=.1
    is what will be executing every time would the if statement be after the conditional where i need to make it so that its
    if (count == 0) { 
    // i want count to stop decrementing and stay at zero
    }
    the thing im a little confused with is since the increment is based on if incrementCount is true or false how does that change when count == 0 and i need it to stop

  25. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    How about something like this:
    if count not at boundary
    then decrement count
    else leave count where it is - stop changing its value
    If you don't understand my answer, don't ignore it, ask a question.

  26. #19
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    How about something like this:
    if count not at boundary
    then decrement count
    else leave count where it is - stop changing its value
    would this require any change in my current code or would i just add this to what i have
    if (count!=0) {
    incrementCount=false;
    } else {
    count=0;
     
    }

  27. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    Remember that floating point numbers are not always exactly equal to integer values.

    That code doesn't do what I suggested in my last post.

    That code is like this:
    if count != 0 then do this...
    else (count is 0) set count to 0??? but it is already 0

    What happens when you try that code from post#19 in your program?
    If you don't understand my answer, don't ignore it, ask a question.

  28. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 7th, 2021)

  29. #21
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    Remember that floating point numbers are not always exactly equal to integer values.

    That code doesn't do what I suggested in my last post.

    That code is like this:
    if count != 0 then do this...
    else (count is 0) set count to 0??? but it is already 0

    What happens when you try that code from post#19 in your program?
    ye the code I suggested gives an error where it counts below 0
     
    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) { // 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>=1){
                                            // 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;
            }
     
            System.out.println(count);
     
                if (count!=0) {
                incrementCount=false;
            } else {
                count=0;
     
        }
     
     
              redLED.setDutyCycle(count);
              Thread.sleep(1000);
     
        }
     
        }
     
     
        }

    0.1
    0.0
    -0.1
    Exception in thread "main" PhidgetException 0x15 (Invalid Argument)
    Value must be in range: 0.000000 - 1.000000.
    at com.phidget22.DigitalOutputBase.setDutyCycle(Nativ e Method)

  30. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    Where is the code that worked for 0 up to 1 to then down (but went too far)?
    The fix was to use .9 vs 1 to stop going the incrementing.
    You need to work with that code to have it stop when it gets to 0.

    I suggested this:
    while decrementing
    if count not at boundary
    then decrement count
    else leave count where it is - stop changing its value <<< THIS MEANS DO NOTHING, leave the value of count unchanged.
          if (count!=0) {   //<<<<<< This will be true as soon as its value is changed from 0 
                incrementCount=false;
    If you don't understand my answer, don't ignore it, ask a question.

  31. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 7th, 2021)

  32. #23
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    Where is the code that worked for 0 up to 1 to then down (but went too far)?
    The fix was to use .9 vs 1 to stop going the incrementing.
    You need to work with that code to have it stop when it gets to 0.

    I suggested this:
    while decrementing
    if count not at boundary
    then decrement count
    else leave count where it is - stop changing its value <<< THIS MEANS DO NOTHING, leave the value of count unchanged.
          if (count!=0) {   //<<<<<< This will be true as soon as its value is changed from 0 
                incrementCount=false;
    so the change I made in the code is I added the count!=0 in the conditional but I'm unsure if it works because of how the numbers count down I get this in the print

    0.1
    0.2
    0.30000000000000004
    0.4
    0.5
    0.6
    0.7
    0.7999999999999999
    0.8999999999999999
    0.9999999999999999
    0.8999999999999999
    0.7999999999999999
    0.7
    0.6
    0.5
    0.4
    0.30000000000000004
    0.20000000000000004
    0.10000000000000003
    2.7755575615628914E-17
    -0.09999999999999998
    So count never =0 so I assume the conditional coun!=0; is never not true and when count is zero instead of going to incrementCount it goes to else which is what I want but since in the print zero is never printed. I just want to know just the logic is it correct
     
     
    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!=0){ // 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 {
     
            }
     
            System.out.println(count);
     
     
     
     
     
     
              redLED.setDutyCycle(count);
              Thread.sleep(1000);
     
        }
     
        }
     
     
        }

  33. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need logic help

    count never =0
    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. It needs to be separate
    begin loop
    if incrementCount
    then increment count
    else if not incrementCount <<< now decrementing count
    then if count > .1 then decrement count << keep decrementing, stop when at lower boundary
    if count > .9
    then incrementCount = false << stop incrementing when at 1
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  34. The Following User Says Thank You to Norm For This Useful Post:

    Rango (January 7th, 2021)

  35. #25
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Need logic help

    Quote Originally Posted by Norm View Post
    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

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: September 8th, 2020, 06:47 AM
  2. Replies: 1
    Last Post: April 11th, 2018, 01:50 PM
  3. Replies: 10
    Last Post: July 1st, 2014, 02:41 PM
  4. Column count doesn't match value count at row 1
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2012, 01:31 AM
  5. How can i add a new count to this source code ?
    By mm2236 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 30th, 2010, 10:21 PM