Push button/counter/relay sketch feeback

Hello,

I am trying to write a simple script that will watch an input which will receive a 5v signal which will keep increasing the counter variable roughly 1 every second. I setup an if loop to activate a relay when the counter reaches a certain number then deactivate the relay after a certain amount of time and then resetting the counter to start all over. I need this to run continually while its powered up. It seems to compile but I just want to get some feedback on the code before I upload and start testing.

I also attempted to get the arduino to send back the counter variable to the IDE but for some reason it only sends the word "counter=" back twice with no variable value after the "="

#define RELAY 6
#define input 1
int counter;
int buttonState;
void setup()
{
Serial.begin(9600);
counter = 0;
pinMode (input, INPUT);
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY,LOW);
}
void loop()
{
Serial.write("Counter=");
Serial.write(counter);
Serial.print('\n');
buttonState = digitalRead(input);
while(buttonState == HIGH) {
counter++;
delay(1000); //wait 1 second
}
if(counter>=2000){
digitalWrite(RELAY,LOW); // Turns ON Relays 1
delay(30000); //time to run pump *1000
digitalWrite(RELAY,HIGH); // Turns Relay Off
counter=0;
}
}

Use Serial.print rather than Serial.write. Serial.println adds a cr/lf.

Made a few adjustments, declared the value when initializing the variable and now it outputs the variable value when it prints, still only does it twice for some reason though.

#define RELAY 6
#define input 1
int counter=0;
int buttonState;
void setup()
{
Serial.begin(9600);
pinMode (input, INPUT);
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY,LOW);
}
void loop()
{
Serial.print("Counter=");
Serial.print(counter);
Serial.print('\n');
buttonState = digitalRead(input);
while(buttonState == HIGH) {
counter++;
delay(1000); //wait 1 second
}
if(counter>=2000){
digitalWrite(RELAY,LOW); // Turns ON Relays 1
delay(30000); //time to run pump *1000
digitalWrite(RELAY,HIGH); // Turns Relay Off
counter=0;
}
}

Do you see the difference in these two pieces of code?

  while (buttonState == HIGH)
  {
    counter++;
    delay(1000); //wait 1 second
  }
  while (buttonState == HIGH)
  {
    buttonState = digitalRead(input);
    counter++;
    delay(1000); //wait 1 second
  }

#define input 1

Pins 0 and 1 are used for serial communications.

Don't use 1, try 2 instead.

larryd:
Do you see the difference in these two pieces of code?

  while (buttonState == HIGH)

{
    counter++;
    delay(1000); //wait 1 second
  }






while (buttonState == HIGH)
  {
    buttonState = digitalRead(input);
    counter++;
    delay(1000); //wait 1 second
  }

Yes, moved the button state variable to inside the while loop. I assume the first is correct because wouldn't you need to rear the variable so it'll enter the while loop.

Inputs adjusted,

#define RELAY 6
#define input 2
int counter=0;
int buttonState;
void setup()
{    
Serial.begin(9600);
pinMode (input, INPUT);
pinMode(RELAY, OUTPUT);       
digitalWrite(RELAY,LOW);
}
 void loop()
{ 
 Serial.print("Counter=");
 Serial.print(counter);
 Serial.print('\n');
 buttonState = digitalRead(input);
while(buttonState == HIGH) {
counter++;
delay(1000); //wait 1 second
  }
  if(counter>=2000){
   digitalWrite(RELAY,LOW);           // Turns ON Relays 1
   delay(30000); //time to run pump *1000
   digitalWrite(RELAY,HIGH);          // Turns Relay Off
   counter=0;
  }
}

"Yes, moved the button state variable to inside the while loop. I assume the first is correct because wouldn't you need to rear the variable so it'll enter the while loop."
Maybe you can have one outside and one inside. :wink:

buttonState = digitalRead(input);
while(buttonState == HIGH)
{
counter++;
delay(1000); //wait 1 second
}

You are not understanding.
If you never update 'buttonState' inside the 'while' loop, please explain how you get out of it? :wink:

Oh damn, i created an infinite loop. Thanks!

Any other thoughts? I uploaded the new code and now its only giving the the "counter=0" once. I havent put a push button on it yet, though.

#define RELAY 6
#define input 2
int counter=0;
int buttonState;
void setup()
{    
Serial.begin(9600);
pinMode (input, INPUT);
pinMode(RELAY, OUTPUT);       
digitalWrite(RELAY,LOW);
}
 void loop()
{ 
 Serial.print("Counter=");
 Serial.print(counter);
 Serial.print('\n');
 buttonState = digitalRead(input);
while(buttonState == HIGH) {
counter++;
delay(1000); //wait 1 second
 buttonState = digitalRead(input);
  }
  if(counter>=2000){
   digitalWrite(RELAY,LOW);           // Turns ON Relays 1
   delay(30000); //time to run pump *1000
   digitalWrite(RELAY,HIGH);          // Turns Relay Off
   counter=0;
  }
}

"Yes, moved the button state variable to inside the while loop. I assume the first is correct because wouldn't you need to rear the variable so it'll enter the while loop."

Maybe you can have one outside 'and' one inside. :wink:

I did, forgot to attach the code to the latest post(updated).

Thanks again for the help!