Pages: [1]   Go Down
Author Topic: [SOLVED] increase count by only 1  (Read 345 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: June 21, 2013, 09:47:06 pm by grantastley » Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26421
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is the code so far

Code:
#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
« Last Edit: June 21, 2013, 09:25:01 pm by grantastley » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was thinking of something like this:

Code:
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
}
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26421
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you patduino

Worked just as I wanted smiley
Logged

Pages: [1]   Go Up
Jump to: