program behaving wierdly, help me please!

hi all, sorry about this, i am sure this must be a simple problem but my brain just cant solve it.

i have a flashing light, i want to get the flash per minute of.

i am using a spark fun light sensor, it seems to respond pretty well, i did a very simple program consisting of:
Serial.println(analogRead(pinBPMInput));

and the serial data showed a clear difference between when the light was off and when it was on. when it was on i got a reading of up to 24 and when it was off the reading was as low as 3.

the code i have posted below is supposed to serial print 1, the first time the light flashes, and continue counting up until the light has flashed 4 times and then reset to one.

so the serial data should be:
1
2
3
4
1
2
3
4

if the light has flashed 8 times for example.

BUT!!! this is my problem, the serial data is putting out the above example for just one flash of the light, it puts out a few numbers every time it flashes rather than just 1 number at a time.

what am i doing wrong? this is really bugging me, because i cant see how it is wrong!

int pinBPMInput = 0;
int count;
int BPM;
int tick = false;
int oldTick = false;

 void setup() {
  Serial.begin(9600);
  Serial.println("Online");
  delay(2000);
  count = 0;
  pinMode(pinBPMInput,INPUT);
 }

void loop() {
  oldTick = tick;
  BPM = analogRead(pinBPMInput);
  if (BPM >10) tick = true;
    else tick = false;  
  if (tick&&(oldTick != tick)) {
    count = count++;
    if (count > 4){
      count =1;
    }
   Serial.println(count);
  }
}

thanks everyone!

You probably need to "debounce" your readings just as with buttons. Try adding a short delay after the code has bumped up 'count'.

Oh, and

count++;

is enough code to increment a variable.

thanks for the tip on the count++ thats going to help alot actually!

i can put in a delay thats no problem, i was actually just looking at the debounce code just now.

just out of interest, why does it need a debounce? is the sensor so fast that it could be seeing flickering in the light that i cant see?

when i do the simplest program where i output the sensor reading straight to serial i get these results:

4
3
3
3
3
3
2
3
3
3
2
2
3
3
3
3
3
3
3
2
3
4
3
2
2
2
2
3
2
4
3
2
2
3
3
3
3
3
5
10
14
16
17
18
19
18
19
19
17
15
13
11
9
8
6
5
5
4
4
4
3
4
4
3
3
3
3
3
3
2
3
3
3
3
3
3
3
3
3
2
3
3
3
3
3
3
3
3
3
3
3
3
2
3
3
3
3
3
2
3
3
3
2
3
3
3
3
3
3
3
3
2
3
3
3
3
3
3
3
3
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3
3
3
3
3
3
2
3
3
3
3
3
2
3
2
3
2
3
3
3
3
2
3
3
2
3
3
2
2
3
2
3
3
3
3
3
3
3
2
3
3
2
3
2
2
2
3
3
3
3
3
3
3
2
3
3
3
3
3
3
3
5
3
3
3
3
2
3
3
3
3
3
3
3
3
3
3
3
3
2
3
3
3
3
3
3
3
3
3
2
3
2
2
2
3
2
3
3
3
3
3
3
3
3
2
2
3
3
2
3
2
3
3
3
3
3
3
3
3
3
3
7
11
14
17
17
18
19
19
19
19
17
14
12
11
9
6
5
5
4
4
4
4
4
3
3
3
4
3
3
3
3
1
3
3
3
3
4
3
3
3
3
3
2
3
3
3
3
3
3
3
3
3
3
3
2
3
3
3
3
3
3
3
3
3
3
4
3
3
3
3
2
3
3
3
3
2
3
3
3
3
3
3
3
3
3
2
3
3
2
3
3
3
3
2
3
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
4
3
3
3
3
3
2
3
3
3
4
3
3
3
3
3
3
3
3
3
2
3
3
3
3
3
3
3
3
2
3
3
3
3
3
3
3
3
3
3
3
2
3
3
2
2
3
3
7
11
14
16
17
17
19
19
19
20
16
14
13
11
9
7
5
5
4
4
4
3
3
4
3
3
3
3
3
3
3
3
2
2
3
3
3
3
4
3
3

they seem ok, there is no random jumps, which is why i assumed it didn't need debouncing,

Those serial writes take about 3 ms each though, so they're already "debounced".
Try reading 1000 values quickly, store them in an array and print them all afterwards.

ah ok thanks for that, so the serial is slow then, again useful to know.

i put in some debounce code and that part is working fine now, so i guess that must have been the problem. i debounced it by 10ms so maybe i can drop that to about 3 if thats how long the serial takes.

thanks again for your help, this is a great forum i always get useful replies :slight_smile: