Question about increment/assignment

Id like to be able to use my code to increment a variable if a certain condition is true, and then when that condition is false and another condition is true, set the variable back to its original value before it was incremented. That is possible in C++ right? Thanks for the help.

Here’s some code:

 if(angles[2] > 23) //(pitch) leaning forward
  { 
    servoval2++;
    myservo2.write(servoval2)
  }

 if((angles[2] > 23) && (angles[2] < -37))//pitch some-what stable
  {
  ( this is where i want it to set set servoval2 back to its original value )
}

if, else if http://arduino.cc/en/Reference/Else

if(A == TRUE)
{
    C++;
}

else if (A == FALSE && C == TRUE)
{
   C = 0;
}

I thought of that but my main problem is is that the servoval2 variable will be changing due to conditions I have in my loop, so I want to always be able to get it back to the original value not just ‘0’.

assign that value to a variable before modifying it :)

I would do that, but that value will be changing as well. So, I will have servoval2 at 53 and then i may want to increment it a few and put it back to 53 if that condition that i specified is true. Then due to other conditions in my loop the value may be assigned to 62 and I may want to increment it and then put it back to 62 if that condition that I specified is true. I am not sure how clear i am sounding but that is the best way I know how to describe it.

Not very ..., perhaps posting a better example.

the basic setup is you going to have to store a value, and use local variables to do your work in until everything is finalized, but yea need a bit more to work on if you could

Well, is it possible to assign one variable to another?

Yes

Its basically a try at Quadcopter stabilization without the use of a PID Loop…still not sure if it’s achievable…
Ok Here’s a bit more code:

#include <FreeSixIMU.h>
#include <FIMU_ADXL345.h>
#include <FIMU_ITG3200.h>

#include <Wire.h>

float angles[3];

#include <Servo.h>

Servo myservo1;  // first motor to be controlled by potentiometer (back)
Servo myservo2;  // second motor to be controlled by potentiometer (front)
Servo myservo3;  // third motor to be controlled by potentiometer  (right)
Servo myservo4;  // fourth motor to be controlled by potentiometer (left)

int incomingByte; // data received over serial from computer
int servovalprimey;
int servovalprimex;
int servovalprime1;
int servovalprime2;
int servovalprime3;
int servovalprime4;

// Set the FreeSixIMU object
FreeSixIMU sixDOF = FreeSixIMU();

void setup()
{
   
 //Start Serial
 Serial.begin(9600); 
  
  myservo1.attach(6);      // back motor   yellow
  myservo2.attach(9);      // front motor  orange
  myservo3.attach(10);     // right motor  green
  myservo4.attach(11);     // left motor   blue

Wire.begin();
  
  delay(5);
  sixDOF.init(); //begin the IMU
  delay(5);

}

void loop()
{
  { 
  sixDOF.getEuler(angles);
      
 
  Serial.print(angles[0]); 
  Serial.print(" | ");  
  Serial.print(angles[1]); 
  Serial.print(" | ");
  Serial.println(angles[2]); 
  
  if(angles[2] > 23) 
  { 
    servovalprime2++;
    myservo2.write(servovalprime2);
  
  }
  
  if(angles[2] < -37)
  {
    servovalprime1++;
    myservo1.write(servovalprime1);
 
  }
  
  if((angles[2] > 23) && (angles[2] < -37))
  {

  <<<<<<<<<<**********(this is where I need to set servovalprime2 back to the value it had before the increment)**********>>>>>>>>>>
   
  }
   {

   Serial.println(servovalprime1);
   Serial.println(servovalprime2); 
   delay(100);
    
    
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
  incomingByte = Serial.read();

  if (incomingByte == 'g') { // value to arm motors is sent to the arduino
  
   servovalprime1 = 28;
   servovalprime2 = 28;
   servovalprime3 = 28;
   servovalprime4 = 28;
   servovalprimex = 28;
   servovalprimey = 28;
   
  myservo1.write(servovalprime1); 
  myservo2.write(servovalprime2);
  myservo3.write(servovalprime3);
  myservo4.write(servovalprime4); 
    
  servovalprime1 = constrain(servovalprime1, 0, 180);
   servovalprime2 = constrain(servovalprime2, 0, 180);
    servovalprime3 = constrain(servovalprime3, 0, 180);
      servovalprime4 = constrain(servovalprime4, 0, 180);
      servovalprimex = constrain(servovalprimex, 0, 180);
      servovalprimey = constrain(servovalprimey, 0, 180);
  
  Serial.flush(); 
  }
  
 if (incomingByte == 'h') { 
 
   servovalprime1 = 40;
   servovalprime2 = 40;
   servovalprime3 = 40;
   servovalprime4 = 40;
   servovalprimey = 40;
   servovalprimex = 40;
   
   
  myservo1.write(servovalprime1); 
  myservo2.write(servovalprime2);
  myservo3.write(servovalprime3);
  myservo4.write(servovalprime4); 
    
    servovalprime1 = constrain(servovalprime1, 0, 180);
   servovalprime2 = constrain(servovalprime2, 0, 180);
    servovalprime3 = constrain(servovalprime3, 0, 180);
      servovalprime4 = constrain(servovalprime4, 0, 180);
      servovalprimex = constrain(servovalprimex, 0, 180);
      servovalprimey = constrain(servovalprimey, 0, 180);
      
  Serial.flush();  
 }
  if(incomingByte == '+') {     // if a 45 byte of data is received 
  
      servovalprime1++;
      servovalprime2++;
      servovalprime3++;
      servovalprime4++;
      servovalprimex++;
      servovalprimey++;
      
  myservo1.write(servovalprime1); 
  myservo2.write(servovalprime2);
  myservo3.write(servovalprime3);
  myservo4.write(servovalprime4); 
    
    servovalprime1 = constrain(servovalprime1, 0, 180);
   servovalprime2 = constrain(servovalprime2, 0, 180);
    servovalprime3 = constrain(servovalprime3, 0, 180);
      servovalprime4 = constrain(servovalprime4, 0, 180);
      servovalprimex = constrain(servovalprimex, 0, 180);
      servovalprimey = constrain(servovalprimey, 0, 180);
      
  Serial.flush(); 
  }
   if(incomingByte == '-') { // if a 48 byte of data is received
   
      servovalprime1--;
      servovalprime2--;
      servovalprime3--;
      servovalprime4--;      // then the speed is decreased
      servovalprimex--;
      servovalprimey--;
      
  myservo1.write(servovalprime1); 
  myservo2.write(servovalprime2);
  myservo3.write(servovalprime3);
  myservo4.write(servovalprime4); 
    
    servovalprime1 = constrain(servovalprime1, 0, 180);
   servovalprime2 = constrain(servovalprime2, 0, 180);
    servovalprime3 = constrain(servovalprime3, 0, 180);
      servovalprime4 = constrain(servovalprime4, 0, 180);
      servovalprimex = constrain(servovalprimex, 0, 180);
      servovalprimey = constrain(servovalprimey, 0, 180);
      
  Serial.flush(); 
  }

Also, how would I go about assigning one variable to the value of another, that may solve my problem.

ok lets say you have a global A and a local B

int A;

setup()

loop()
{
   int B = A;
   if(TRUE)
   {
      B ++;
   }
  if(! TRUE && TRUE)
  {
     B = A;
  }
}
etc

GamecubePerson111: Well, is it possible to assign one variable to another?

What sort of question is that? What if we said "no"?

if(TRUE)

Interesting.

sketch_dec29a.cpp: In function 'void setup()':
sketch_dec29a:2: error: 'TRUE' was not declared in this scope

I am about 160 lines deep in a apple II basic program, use your imagination

:P

Ach, Apple Basic. I used to use that. :)

You had hi-res mode, all sorts of nice stuff.

is it possible to assign one variable to another?

Its basically a try at Quadcopter stabilization without the use of a PID Loop

These two statements seem diametrically opposed, if you are asking that question you are well out of your depth with this project IMO.


Rob

GamecubePerson111: Its basically a try at Quadcopter stabilization without the use of a PID Loop...still not sure if it's achievable... Ok Here's a bit more code:

...

Also, how would I go about assigning one variable to the value of another, that may solve my problem.

int x = 7; int y = 3; int j = x + y; // j now has 10 int z= x; // z now has 7 x++; // x now has 8 x=z; // x is back to 7

You should probably browse http://arduino.cc/en/Reference/HomePage, especially the sections on Variables.

Cheers, John

Graynomad: These two statements seem diametrically opposed, if you are asking that question you are well out of your depth with this project IMO.

My thoughts exactly.

Alriggt, thanks so much for all the clarification.

suggestion from an old-timer: stop coding for a few minutes, and re-analyze the problem.

Reading the thread, it sounds like there are a finite number of cases of input-range/result-range, and it would be possible to calculate a fresh absolute or near-absolute solution to the system, and then apply a small offset if necessary, rather than trying to "remember" many "odds and ends" and trying to piece the solution together from "saved scraps."

Draw out your system in a diagram so you can see a "bigger picture" and create a fresh solution to your system based on the new understanding you have learned during the development process so far. Then reassess your object and create a new, more accurate object with the same exposures, or change the definition of the object exposures if necessary.

Over the years, I have found that as the internal code became more convoluted, I would save much more time with a "do-over" than by creating fix-on-top-of-fix ad nauseum. Instead of raging ahead on the current path, step back and see if it's the right path, or if it would be better to redefine the path a little to make it easier to follow. If your current model is correct, wonderful!

There are many simple code solutions to your question. Maybe one is right. But while you're thinking about the system, think about the bigger picture, too.

My experience has been that "10% pondering time" usually saves about 50% of the coding time. Code for a while, then step back and think about it for a while. Take a break, then look at it again before you keep coding.

Good luck.