Pages: [1]   Go Down
Author Topic: Mothers Day code - Please Help!  (Read 1062 times)
0 Members and 1 Guest are viewing this topic.
Vancouver, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27352
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How is it wired up?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

South Texas
Online Online
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

crossroads, It's wired up like this:


* arduino pushbutton.JPG (268.69 KB, 960x720 - viewed 34 times.)
Logged

-skeeter_mcbee-

Vancouver, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27352
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Vancouver, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-skeeter_mcbee-

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27352
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Vancouver, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-skeeter_mcbee-

Pages: [1]   Go Up
Jump to: