[SOLVED] increase count by only 1

Hi I am after a way to increase a count by only 1 when conditions are met.
I am currently using Count++ but this will keep increasing to value of Count as the program goes through the loop while ever the conditions in the if statement are true while I only wish to increase Count by 1 each time the conditions are met.
Any ideas

Hi grantastley,

You should probably include your code (in code blocks - paste your code into a reply, select it, then select the # button on the toolbar above).

Anyway, what you really need to do here is include a flag variable that indicates whether you've already incremented the Count, and if so, don't do it again (in an if statement).

Pat.

Add a test - after counting one time, make sure the conditions go false before allowing counting to occur again.

here is the code so far

#include <Servo.h>

int AUX3=3;
int AUX3Val=0;

int Gear=2;
int GearVal=0;

int Count=0;
int WingFoldCount=0;

Servo WingFoldLeft;  //Create a servo object
Servo WingFoldRight; //Create a servo object
Servo WingFoldLock;  //Create a servo object

void setup(){
   Serial.begin(9600);
pinMode(AUX3, INPUT);
pinMode(Gear, INPUT);

WingFoldLeft.attach(6);  //Attach left wing fold servo
WingFoldLeft.write(10);
}

void loop(){
  GearVal=pulseIn(Gear, HIGH, 25000);   //Read RX Gear position
  AUX3Val=pulseIn(AUX3, HIGH, 25000);   //Read RX AUX3 position
  
  Serial.print("AUX3 : ");
  Serial.println(AUX3Val);
  
  Serial.print("Gear : ");
  Serial.println(GearVal);
  
  Serial.print("Count = ");
  Serial.println(Count);
  
  Serial.print("Wing fold count = ");
  Serial.println(WingFoldCount);
  
  Count++;
  delay(500);
  if(Count>10){  //sets count time to 5 seconds
    Count=0;
    WingFoldCount=0;
  }
  
  if(AUX3Val>1700&&GearVal<1500) {
  WingFoldCount++;
  Count=0;
}
  if(WingFoldCount==2){
   WingFoldLeft.write(160); 
  }
  else(WingFoldLeft.write(10));
  }

Would you please be able to tell me how to add the test condition as I am only new to the arduino environment.
Thank you

PS WingFoldCount is the one I am trying to only increase by 1

I was thinking of something like this:

At beginning...
Bool FirstTime;

In Setup...
FirstTime = true;

In loop...
if ((AUX3Val > 1700) && (GearVal < 1500)) {

  if (FirstTime) {
    WingFoldCount++;
    FirstTime = false;
  }

  Count=0;
}
else {
  FirstTime = true;  // reset flag
}

No idea - you have no comments, no way to tell what the condition is you are looking for to occur and then wait for it to pass.

Thank you patduino

Worked just as I wanted :slight_smile: