Go Down

Topic: [SOLVED] increase count by only 1 (Read 410 times) previous topic - next topic

grantastley

Jun 22, 2013, 04:08 am Last Edit: Jun 22, 2013, 04:47 am by grantastley Reason: 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

patduino

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.
There are 10 types of people in the world, those that understand binary, and those that don't.

CrossRoads

Add a test - after counting one time, make sure the conditions go false before allowing counting to occur again.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

grantastley

#3
Jun 22, 2013, 04:21 am Last Edit: Jun 22, 2013, 04:25 am by grantastley Reason: 1
here is the code so far

Code: [Select]

#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

grantastley

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

patduino

I was thinking of something like this:

Code: [Select]
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
}
There are 10 types of people in the world, those that understand binary, and those that don't.

CrossRoads

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

grantastley

Thank you patduino

Worked just as I wanted :)

Go Up