Help me program my Arduino

I have LCD connected.
Goal is to count the number of seconds when pin D9 is high and pause when D9 is low and start counting again when D9 is high.

Counting starts from 0 seconds to maximum the LCD can handle .

I used the millis function but it is not pausing.

I'm new to C ++ . Please help me with the sketch !

Someone writing the sketch for me would be appreciated. I am sure it's very simple

Goal is to count the number of seconds when pin D9 is high and pause when D9 is low and start counting again when D9 is high.

Use a loop. I'd suggest a [u]while() loop[/u]. The loop doesn't need to "do anything"... Just start the loop when D9 goes low, and sit there in a loop doing nothing but looping (just checking the state of D9) and break out of the do-nothing loop when D9 goes high.

Since you cannot pause the millis() function, you'll have to reset a variable to get a new starting-time when you come out of the loop to start counting/accumulating seconds again.

I used the millis function but it is not pausing.

The millis() function gives you the current time (the number of milliseconds since your program started). It does NOT pause your program.

delay() will pause your program, but it doesn't stop/pause the millis() function and it pauses for a certain period of time and your program can't read the state of D9 during the delay period.

I'm new to C ++

The two most important concepts in programming are conditional execution (if-statements, etc.) and loops (doing something over-and-over, usually until some condition is reached).

C++ is a very complex language, but once you understand these two concepts you can start to understand how a program is structured.

Post what you've got and we can help you, otherwise it looks like we're writing it, and that's no fun for you.

Wow so many replies. I didn't save the program. It was reading the state of D9 in the loop and the millis counting was taking place outside the loop (void count() ). The seconds on the LCD paused when D9 went low but when it went high again it jumped several hundred seconds. Looks like the millis was still counting. Write the program for me pls. If anyone can. I am sure it's easy for you guys who are familiar with C++. I'll have to learn it.

D9 is held LOW by a 22k resistor connected to gnd.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

pinMode(9, INPUT);

lcd.begin(16, 2);

lcd.print(“hello, world!”);
}

void loop() {

while (digitalRead(9) == HIGH) {
count();
}

}
void count()

{

lcd.setCursor(0, 1);

lcd.print(millis()/1000);
}

millis () counts all the time, whether or not your switch is closed.
Maybe you want to record differences, rather than absolute times.
You'd save a little hardware if you used the built-in pullup rather than an external pull down.

It's going to used with a solar panel in India. The solar panel outputs 12v but voltage drops down when it's cloudy or rainy. An op-amp is used. It's output goes high (5V) when solar cell voltage falls below 12v. The guy wants to measure the productivity of his solar panel. The Arduino will record the seconds and he can reset the Arduino each month after taking the reading. He can convert the seconds to hours and mins. We just want the Arduino to count when D9 is high and pause when its low and resume again when high

Anyone help please? Write this simple program for me.

I got it !

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int buttonPressCount;

const int buttonPin = 9;

int buttonPushCounter = 0; //counter for the number of button presses
int buttonState = 0; //current state of the button
int lastButtonState = 0; //previous state of the button
void setup() {

pinMode(buttonPin, INPUT); //initialize the button pin as a input
lcd.begin(16, 2);
lcd.print("number of seconds ");

}

void loop() {

buttonState = digitalRead(buttonPin); //read the pushbutton input pin

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) { delay(1000);
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++;

lcd.setCursor(0, 1);

lcd.println(buttonPushCounter, DEC);

}

}

}

If you want to extend your program to do other things you will find that the delay() function gets in your way. Have a look at how to manage timing using millis() in several things at a time.

...R