Vancouver, WA
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« on: May 12, 2012, 09:08:23 pm » |
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; } }
|
|
|
|
|
Logged
|
-skeeter_mcbee-
|
|
|
|
Global Moderator
Boston area, metrowest
Online
Brattain Member
Karma: 247
Posts: 16535
Available for Design & Build services
|
 |
« Reply #1 on: May 12, 2012, 09:17:45 pm » |
How is it wired up?
|
|
|
|
|
Logged
|
|
|
|
|
South Texas
Offline
God Member
Karma: 8
Posts: 976
|
 |
« Reply #2 on: May 12, 2012, 09:22:42 pm » |
here's some suggestions.
if (analogValue == 1023) { counter = counter + 1; } else { counter = counter; Un-needed - counter already equals counter } 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 } if (counter > 2) { counter = 0; } }
|
|
|
|
|
Logged
|
|
|
|
|
Vancouver, WA
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #3 on: May 12, 2012, 10:49:02 pm » |
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.
|
|
|
|
|
Logged
|
-skeeter_mcbee-
|
|
|
|
Vancouver, WA
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #4 on: May 12, 2012, 10:50:17 pm » |
crossroads, It's wired up like this:
|
|
|
|
|
Logged
|
-skeeter_mcbee-
|
|
|
|
Vancouver, WA
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #5 on: May 12, 2012, 11:23:06 pm » |
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?
|
|
|
|
|
Logged
|
-skeeter_mcbee-
|
|
|
|
Offline
Edison Member
Karma: 3
Posts: 1712
|
 |
« Reply #6 on: May 12, 2012, 11:28:24 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Vancouver, WA
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #7 on: May 12, 2012, 11:32:32 pm » |
Ok. I think I get it now. I use digitalRead(), and use int val = digitalRead(digitalPin); int counter = counter + val;
that way I think it will make it so that second phrase isn't going to be automatically true.
|
|
|
|
|
Logged
|
-skeeter_mcbee-
|
|
|
|
Global Moderator
Boston area, metrowest
Online
Brattain Member
Karma: 247
Posts: 16535
Available for Design & Build services
|
 |
« Reply #8 on: May 12, 2012, 11:34:24 pm » |
What do you have in there to make the analog pin go high? I don't see any pullup or pulldown resistors that would make the pin sit high or low. I would simplify things a little inputPin = 14; // A0 pin byte counter; void setup(){ pinMode (inputPin, INPUT); digitalWrite (inputPin, HIGH); // enable internal pullup Serial.begin(9600); Serial.println("Mom, push the button once, wait about a second, then push it again."); Serial.println(""); } void loop(){ if (digitalRead (inputPin) == LOW){ // pin is pulled high, close switch to connect to ground counter = counter +1; if (counter == 1){ Serial.println ("message 1"); delay (500); // debounce switch } // end counter 1 if (counter == 2){ Serial.println ("message 2"); delay (500); counter = 0; } // end counter 2 } // end digitalRead check } // end void loop
|
|
|
|
|
Logged
|
|
|
|
|
Vancouver, WA
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #9 on: May 12, 2012, 11:49:23 pm » |
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.
|
|
|
|
|
Logged
|
-skeeter_mcbee-
|
|
|
|
Global Moderator
Boston area, metrowest
Online
Brattain Member
Karma: 247
Posts: 16535
Available for Design & Build services
|
 |
« Reply #10 on: May 13, 2012, 12:08:34 am » |
"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.
|
|
|
|
|
Logged
|
|
|
|
|
Vancouver, WA
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #11 on: May 13, 2012, 12:11:31 am » |
when I changed it to high, it did what I wanted it to do.
|
|
|
|
|
Logged
|
-skeeter_mcbee-
|
|
|
|
|