Displaying the information

Hey,

I’m trying to write the a code that counts consecutive odd / even numbers. I’m getting the counts but is there any way that you could help me eliminate the zero that occurs when I run the code? This is my code so Far

#define head 11 // pin 11 is odd
#define tail 10 // pin 10 is even

#define PAUSE 500 // delay for 5 ms
int headsCounter;
int tailsCounter;

long loopCounter;
long randomNumber = 0;

int evenArray [100];
int oddArray [100];

void setup() {
 Serial.begin(9600);
 headsCounter = 0;
 tailsCounter = 0;
 loopCounter = 0;
 pinMode(head,OUTPUT);
 pinMode(tail,OUTPUT);
 randomSeed(analogRead(A0));
}

void loop() {
 randomNumber = generateRandomNumber(); // generate a random number this is a funcion
 digitalWrite(head,LOW); // Turns off both LED
 digitalWrite(tail,LOW);

 if (loopCounter % 10 ==  loopCounter++) { // This runs for 1000 loop
 
if (randomNumber % 2 == 1) { // Odds is head , pin 11
   digitalWrite(head,HIGH); // turns off the leds.
   headsCounter++;
   Serial.print(tailsCounter);
   Serial.println(" Tails");
   tailsCounter = 0;
   }
if (randomNumber % 2 == 0) { 
   digitalWrite(tail,HIGH); 
   tailsCounter++;
   Serial.print(headsCounter);
   Serial.println(" Heads");
   headsCounter = 0;
 }


delay(1000);
 }

 
}

long generateRandomNumber() // this is a function
{ return random(0, 50);
}

Screen Shot 2015-11-21 at 11.45.59 PM.png

It's interesting that when you get a heads, you add one to heads counter and then display the tails. And when you get a tails you add one to the tails counter and display the heads. Is that what you meant to do?

Sort of, I only want to print the consecutive results after a change, so in this case when the number goes from even to odd or odd to even

But you don't only print when you get a change. Say you got 3 tails in a row. Then you get a heads, so it prints 3 Tails and resets tailsCounter to 0. Then you get another heads, it adds one to headsCounter and again prints the info for tails. But this time it's zero because there haven't been any tails since you set it back to 0.

If you only want to print when it changes, then you need to keep track of the last one. This could be as easy as testing that your counter isn't zero before you do your printing.

Delta_G:
But you don't only print when you get a change. Say you got 3 tails in a row. Then you get a heads, so it prints 3 Tails and resets tailsCounter to 0. Then you get another heads, it adds one to headsCounter and again prints the info for tails. But this time it's zero because there haven't been any tails since you set it back to 0.

If you only want to print when it changes, then you need to keep track of the last one. This could be as easy as testing that your counter isn't zero before you do your printing.

Can you explain what you mean by keep track of the last one? I am fairly new to Aruduino

Well, you're already doing that. If you're in the heads section and tailsCounter is 0, then you know the last one you got was a heads. If you're in the heads section and tailsCounter IS NOT 0, then you know that you just switched from getting tails to getting heads as the tailsCounter hasn't been reset yet. So this must be the first heads of the run. So your fix could be as easy as:

if (randomNumber % 2 == 1) { // Odds is head , pin 11
    digitalWrite(head,HIGH); // turns off the leds.
    headsCounter++;
    if(tailsCounter != 0) {  
        Serial.print(tailsCounter);
        Serial.println(" Tails");
        tailsCounter = 0;
    }
 }

And similar in the tails section.