I have a code that makes it so that when I push a button, it says a phrase on the Serial Monitor. But when I push the button, the Serial monitor puts out both of my phrases I have set. I really need this code figured out by ten o' clock PM (pacific Standard Time)!
const int analogPin = A0;
int counter = 0;
void setup() {
Serial.begin(9600);
Serial.println("Mom, push the button once, wait about a second, then push it again.");
Serial.println("");
}
void loop() {
int analogValue = analogRead(analogPin);
if (analogValue == 1023) {
counter = counter + 1;
} else {
counter = counter;
}
if (counter == 1 && analogValue == 1023) {
Serial.println("Happy Mothers Day!");
delay(500);
counter = counter + 1;
} else {
counter = counter;
}
if (counter == 2 && analogValue == 1023) {
Serial.println("I love you!");
delay(500);
counter = counter + 1;
} else {
counter = counter;
}
if (counter > 2) {
counter = 0;
}
}
if (counter == 1 && analogValue == 1023) {
Serial.println("Happy Mothers Day!");
delay(500);
counter = counter + 1; why are you now incrementing counter, because it now equals 2
} else {
counter = counter; same here
}
And because it now equals 2 the following statement is true
if (counter == 2 && analogValue == 1023) {
Serial.println("I love you!");
delay(500);
counter = counter + 1; and counter now equals 3
} else {
counter = counter; and here
}
What can I do to fix it? My dad tells me that I need more than one void function. I think I have some idea of how that works, but I don't know if it will behave as a second loop.
I've been thinking about this all day. How do I make it so that I can increase the counter without making the second phrase true? should I use the for() statement?
I would change it so its a digital read, add some switch debouncing so it doesn't think u pressed it twice
id post some code example but im on my phone so youl have to look thru the playground and the forum for some example
Thank you SO MUCH CrossRoads! Your code helped me so much! I couldn't figure out what was wrong with my code all day! although there were some problems with your code.
if (digitalWrite) == LOW) {
made it so that when the pin equaled zero, it would display the message.
"made it so that when the pin equaled zero, it would display the message. "
That's the idea - when the internal pullup holds the pin high, nothing happens.
When you connect the pin to Gnd using your switch, it starts the message sequence.