Pages: [1]   Go Down
Author Topic: Simple counting problems  (Read 340 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm constructing a device which measures an analogue input.

when the analogue input is below a threshold, an output goes high once, until the analogue input goes above a threshold.

Code:
int sensorPin = A0; // select the input pin for the detector
unsigned int count = 0;
byte latch = 0;  //initial variable
unsigned int sensorValue = 0;  // variable to store the value coming from the detector


void setup()
{
  
  pinMode(13, OUTPUT);
  //Start Serial port
  Serial.begin(9600);
count=0;  
}
void loop()
{
  // read the value from the soil detector:
  sensorValue = analogRead(sensorPin);    
  if(count==0&&sensorValue<500)
  digitalWrite(13, HIGH);
  delay(1000); // 1 second delay
  ++count;// this increments the count by 1
  Serial.println(sensorValue);
  Serial.println(count);
  digitalWrite(13, LOW);

if(sensorValue>600)
count=0;
}

The problems I have..

the count variable displays a "1" when held high.... and when running, why isn't it 0 as per initialisation.

When taking A0 low the count keeps incrementing, ignoring the "count==0", why not just +1,

When taking A0 HIGH the count displays a "1" not 0 ?



thanks for any help

David
« Last Edit: February 07, 2013, 06:50:16 am by Hackdub » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24470
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(count==0&&sensorValue<500)
  digitalWrite(13, HIGH);
That's the extent of your conditional code, everything else is executed unconditionally.
I'm not sure if that's what you intended.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(count==0&&sensorValue<500)
  digitalWrite(13, HIGH);
That's the extent of your conditional code, everything else is executed unconditionally.
I'm not sure if that's what you intended.

No thats not what I want..

If the condition is met.

I want the pin to go high , ++count,then go low after a time..

Is this a bracket issue?

or lack of else?

is it only the line directly below the condition that is linked, and how do i add more...

Sorry for the dumb questions, but this is driving me mad, that i'm missing the obvious.

David
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


If the condition is met.

I want the pin to go high , ++count,then go low after a time..

Is this a bracket issue?
Yes
Quote
is it only the line directly below the condition that is linked, and how do i add more...

if (condition)
   statement;

where statement is either one line like
   digitalWrite( 13, HIGH);

or multiple like
   {
       digitalWrite(13, HIGH);
       count++;
       ... etc
   }

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

Cheers,
John
« Last Edit: February 07, 2013, 07:12:17 am by johncc » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John.. Ive tried that.

now its not incrementing... :-(

Code:
int sensorPin = A0; // select the input pin for the detector
int count = 0;
//byte latch = 0;  //initial variable
unsigned int sensorValue = 0;  // variable to store the value coming from the detector


void setup()
{
 
  pinMode(13, OUTPUT);
  //Start Serial port
  Serial.begin(9600);
}
void loop()
{
  Serial.println(sensorValue);
  Serial.println(count);
  // read the value from the soil detector:
  sensorValue = analogRead(sensorPin);
  delay(500); 
  if(count=0&&sensorValue<500)
    {digitalWrite(13, HIGH);
      delay(5000); // 5 second delay
      count++;// this increments the count by 1
      delay(500);
    digitalWrite(13, LOW);}
 

if(sensorValue>600)
{count=0;
delay(2000);}
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24470
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
now its not incrementing...
Maybe it is, but you're just not waiting long enough to see the result.

On second thoughts, you're right, it will never increment
Code:
if(count=0
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nearly done..

its counting correctly, but no LED output .!
Code:
int sensorPin = A0; // select the input pin for the detector
int count = 0;
byte latch = 0;  //initial variable
unsigned int sensorValue = 0;  // variable to store the value coming from the detector
const int LED = 4;

void setup()
{
 
  pinMode(LED, OUTPUT);
  //Start Serial port
  Serial.begin(9600);
}
void loop()
{
  Serial.println(sensorValue);
  Serial.println(count);
  // read the value from thedetector:
  sensorValue = analogRead(sensorPin);
  delay(500); 
  if(count=0&&sensorValue<500)
    {digitalWrite(LED, HIGH);
      delay(5000); // 5 second delay
     
      delay(500);
    digitalWrite(LED, LOW);}
  count++;
 
  // this increments the count by 1
 

if(sensorValue>600)
{count=0;
delay(2000);
//digitalWrite(LED, LOW);
}
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24470
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(count=0


Quote
its counting correctly
unconditionally
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

BINGO! tx..

for some--- we need to learn the hard way ;-)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46267
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Istheresomethingwrongwithyourspacekey?

Code:
  if(count=0&&sensorValue<500)
would be heck of a lot easier to parse as
Code:
  if(count = 0 && sensorValue < 500)
Therefore, it would be a lot simpler to spot the mistake.
Logged

Pages: [1]   Go Up
Jump to: