how to increment and decrement ?

how to increment and decrement ?
for example

void loop()
{time = 4;
time += 1;}

so how to incremnet
so for example it's 4 after a loop it's 5 after a loop it's 6 after a loop it's 7....

something like this:

int time = 0;

setup()
{
    time = 4;
}

loop()
{
    time++;
}

In your case, time is set to 4 each time loop is entered.

http://arduino.cc/en/Reference/Increment

Check the above reference. If you are a newbie, I would first explore the references provided on the official website.

He knows how to increment, I think he was wondering why it started from 4 all the time!

btw, JHjh,

time += 1;

is perfectly fine, too.

Short hand

time++;

long hand

time=time+1

decrement - not +

Note time++; is NOT the same as ++time; time++; means increment AFTER use ++time means increment before use.

Can you spot the in joke that names a computer language?

Mark

Did you look here?
http://arduino.cc/en/Reference/For

Useing a for loop sounds like it would work too

KeithRB:
He knows how to increment, I think he was wondering why it started from 4 all the time!

btw, JHjh,

time += 1;

is perfectly fine, too.

I personally prefer "time += 1;" to "time++;" because then I can easily edit if I want a different increment amount. Yeah, I'm lazy! :slight_smile:

Thank you KeithRB but I want that my code take the last value incremented and continue incrementing
for example

void loop()
{time ++;}

so then if time is 4 and it increment one it will be 5
and next loop it will start 5 and be 6

thank you everyone!!!

You will have to put an extra increment at the end of the loop function, or increment by 2:

time +=2
1 Like
long startTime ;                    // start time for stop watch
long elapsedTime ;
int preincrementation = 8;
int vrai = 9;
 

float prevtension;// global variables are retained on each iteration of loop()
long time;
int valeurLue;

int analogPin = A0;
const int led =  13; 
float tension; //variable pour stocker la valeur lue après conversion


void setup() {
pinMode(analogPin, INPUT);
  pinMode( led, OUTPUT ); 
  digitalWrite(led, HIGH); 
       delay (500);
       digitalWrite(led, LOW); // 2.5cm
       Serial.begin(115200);

}



void loop() {
prevtension = tension;
  
  valeurLue = analogRead(analogPin);

	//on traduit la valeur brute en tension (produit en croix)
	tension = valeurLue * 5.0 / 1024;
  long RPMactuel = 60000000  / elapsedTime;
 long initime = 0.3 * elapsedTime;
  long RPMconsigne = 3000;
  long consigne = 60000000 / RPMconsigne;
  
 if (tension < 2.8)
  {if (tension < (prevtension - 0.005)) {  // compare them
        elapsedTime =   micros() - startTime;   
        startTime = micros(); 
      digitalWrite(led, HIGH); 
      
       if(elapsedTime > consigne)
       {time = initime;} // 4%
       
       if(elapsedTime < consigne)
       {time = 4;
 if ( (consigne - (consigne * 0.02)) < elapsedTime < (consigne + (consigne * 0.02)))
       digitalWrite (preincrementation, HIGH);
      { if ( elapsedTime < consigne )
     {
   
   if ( elapsedTime > (consigne - (consigne * 0.0001)) )
   {
   time += 0.001;}
   if ( elapsedTime < (consigne + (consigne * 0.0001)) )
   {
   time -= 0.001;}
 
  if ((consigne - (consigne * 0.0001)) < elapsedTime < (consigne + (consigne * 0.0001)))
{digitalWrite (vrai, HIGH);}}   }
       
       delayMicroseconds (time);
       digitalWrite(led, LOW); // do something, they are different
{
  } }
    }
 }
 
   
   
   Serial.print("elapsedTime = ");
	Serial.println(elapsedTime);
   Serial.print("consigne = ");
	Serial.println(consigne);
   Serial.print("rpm actuel = ");
	Serial.println(RPMactuel);
  Serial.print("rpm consigne = ");
	Serial.println(RPMconsigne);
Serial.print("initime = ");
	Serial.println(initime);
Serial.print("time = ");
	Serial.println(time);




Serial.println();
     Serial.println();
      
}

so I change it is it good now please thank you

Use the auto format in the tools section to layout your code - it will help you and if you post the code us! Good code layout is not just something you do it helps you find bugs!

Mark

{
  } }
    }
 }

Don't you just hate it when that happens?

2 Likes

No I like the artistic value of it - that four lines of code let me think of spring, when birds return in flocks.

(yeah it was a cold wind today)

1 Like

robtillaart:
No I like the artistic value of it - that four lines of code let me think of spring, when birds return in flocks.

(yeah it was a cold wind today)

But one of them is upside-down ( or, conversely, depending on which way you tilt your head, four of them are upside down)

AWOL:

robtillaart:
No I like the artistic value of it - that four lines of code let me think of spring, when birds return in flocks.

(yeah it was a cold wind today)

But one of them is upside-down ( or, conversely, depending on which way you tilt your head, four of them are upside down)

that's not upside-down, that's just the rebel of the flock, the rest are flying South, and that fella's going North...