blink LED to button count

I’ve only been messing with this for about a week, and my background does not extend beyond basic knowledge of writing Crystal Reports and what i learned in my HTML college classes years ago… Thanks to the internet and adaFruit, and youtube instructional videos, plus the example sketches, i was able to get up and ready pretty quick. Its just when it comes to combining things that im starting to get kind of stuck…

For no particular reason other than learning more about hte logic, i wanted to write a sketch that would count my button pushes, display that count in the serial monitor , then/and blink the LED the corresponding number of blinks. My code is a little sloppy in an attempt to combine multiple examples and lessons… i kept it sloppy to see how (or not, evidently) linking variables together works. I have had both sketches (count and blinks) running independently of each other.

the error is: the count never increments in the serial monitor… when the program first loads there is no message, when i click once, it will increment to one, but not beyond. The RedLED never blinks, even after the increment… Additionally, there should be, i thought, a 7second window with which i could push the button before the program advanced to the next block. Or, have i misunderstood the WHILE function?

so, here’s the important part of the body:

buttonState=digitalRead(buttonPin);
unsigned long startTime = millis(); //to capture start time
int buttonCount = 0;
while (millis() - startTime>7000); //7 second count
{
  if (buttonState != lastButtonState)
{
  if (buttonState==LOW) //setup uses INPUT_PULLUP
  {
    buttonCount++;
    Serial.print ("Button Count:  ");
    Serial.println(buttonCount);
  }
}
}

entire code:

const int ledR = 9;
const int ledG = 10;
const int redTime = 250;
const int greenTime = 250;
const int redOfftime = 250;
const int greenOfftime=250;
const int numRedBlink=0;
const int buttonPin=11;

int buttonCount = 0;
int buttonState=0;
int lastButtonState=0;
int startTime=0;

void setup() {
  // put your setup code here, to run once:
pinMode (ledR,OUTPUT);
pinMode (ledG,OUTPUT);
pinMode (buttonPin,INPUT_PULLUP);
Serial.begin (9600);
}

void loop() {
buttonState=digitalRead(buttonPin);
unsigned long startTime = millis(); //to capture start time
int buttonCount = 0;
while (millis() - startTime>7000); //7 second count
{
  if (buttonState != lastButtonState)
{
  if (buttonState==LOW) //setup uses INPUT_PULLUP
  {
    buttonCount++;
    Serial.print ("Button Count:  ");
    Serial.println(buttonCount);
  }
}
}
lastButtonState = buttonState;
buttonCount = numRedBlink;  
for (int j=1; j<=numRedBlink; j=j+1)
{
  if (j<numRedBlink){Serial.print(j);}
    else { Serial.println (j);Serial.println ("OVER"); } 
  digitalWrite (ledR, HIGH);
  delay (redTime);
  digitalWrite(ledR,LOW);
  delay (redOfftime);
  }
digitalWrite (ledG,HIGH);
delay (greenTime);
digitalWrite (ledG,LOW);
delay(greenOfftime);
}

thanks for helping me get a handle on this…

thanks
dan

while (millis() - startTime>7000); //7 second count

The semicolon on the end of the while terminates the while immediately. Remove it and see what happens.

Now i have no readings at all in the serial monitor.

I see what your saying though, i need to treat the "while" like an "if", in that the semicolin comes at the end of the whole statement, not somewhere in the middle.

while im at it, im having trouble with a while statement in another project...

in the following example, there is no delay for the second prompt. It will properly hold for "red blinks", the monitor will prompt for green, but then it executes the routine before i have a chance to enter green... thanks

Serial.println("how many times to blink RED?");
while(Serial.available()==0){}
numRedBlink = Serial.parseInt ();
Serial.println (numRedBlink);

delay (500);

Serial.println("how many times to blink green?");
while(Serial.available()==0){}
numGreenBlink = Serial.parseInt ();
Serial.println (numGreenBlink);

For your last example, Within serial monitor, look down the bottom right hand corner. Chances are you have it set on "newline", "Carriage return" or "both" Change this to "No line ending" see if that resolves your problem.

Just a breakdown of what I think is happening.
You prompt "How many times to blink Red"
You then wait for some serial to arrive
User enters a number and a newline is appended

Serial turns up and Serial.parseInt takes the number part (but leaves the new line in the buffer)
Number gets Serial.printed

You then prompt "how many times to blink Green"
You then attempt to wait for some serial to arrive BUT
IMMEDIATELY that newline is still sitting in the buffer so your wait is over
ParseInt then attempts to get a number from the serial buffer but finds nothing there
So your green LED is left dejected.

Another way out of this problem is, Just before you prompt "How many times to blink green"
run the line while(Serial.available()) Serial.read();
This will make sure there's nothing still lurking in the serial buffer.

the first suggestion cleared it up! the second seemed to stall everything out (removed it, and changing to "no line ending" fixed it)

thanks!