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;
}
}
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;
}
}
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.
"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."