adding variables

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

adding variables

2,419 Views
DanielMorley
Contributor I
Before any one jumps on my case I have read most of ansi C programming guide.  In the guide I did notice that to add one to an integer one simply  integer= integer + 1 or ++integer.  I have tried this and it does not seem to work. In the following code
 
 void ManualControl(){
   int position;
   int stop;
 
   stop=0;
   position=0;
  if(Button1 == UP||Button2 == UP ) {
    
  while (Button1 == UP){
  Reverse();   
  position=ReadPosition(position);
           stop=position;
             stop=position-1;
     //  if (stop==-1)
     // stop=15;
  
  
  } 
  
  
  while (Button2 == UP){
   Forward();
   position=ReadPosition(position);
         stop=position;
         stop=position+1;  
    //   if (stop==16)
  //    stop=0;
           
     
    
  }        
   
  
 
       while (position != stop) {
   
   position=ReadPosition(position);
  }
 
 Brake();
 
  }
  }
   
 
This code works fine till I add a 1 to stop and then it stays in the last while statement indefinitely. 
Labels (1)
0 Kudos
3 Replies

371 Views
DanielMorley
Contributor I
Secondly I was trying to keep a motor running till position is equal to stop.  
0 Kudos

371 Views
peg
Senior Contributor IV
Hi David,
 
Not sure what you are trying to achieve here.
The line stop=position is redundant and will probably be optimised away.
If one of those two buttons is up stop will be either 1 more or less than position so it should get stuck in the != while.
 
Regards
David
 
0 Kudos

371 Views
DanielMorley
Contributor I
Your right it is redundant.  It turns out I had a bad port on a board and the pull up option was not working on that version.  So the code was doing exactly what is written here.  The board was not perfroming correctly. 
0 Kudos