Go Down

Topic: Mothers Day code - Please Help! (Read 1 time) previous topic - next topic

skeeter_mcbee

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)!

Code: [Select]

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

-skeeter_mcbee-

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

kf2qd

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


skeeter_mcbee

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.
-skeeter_mcbee-

skeeter_mcbee

crossroads, It's wired up like this:
-skeeter_mcbee-

skeeter_mcbee

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?
-skeeter_mcbee-

winner10920

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

skeeter_mcbee

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.
-skeeter_mcbee-

CrossRoads

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
Code: [Select]

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
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

skeeter_mcbee

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.
Quote
if (digitalWrite) == LOW) {
made it so that when the pin equaled zero, it would display the message.
-skeeter_mcbee-

CrossRoads

"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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

skeeter_mcbee

when I changed it to high, it did what I wanted it to do.
-skeeter_mcbee-

Go Up