Connect with us

Help Arduino Program

Discussion in 'Microcontrollers, Programming and IoT' started by brian22, Jul 19, 2013.

Scroll to continue with content
  1. brian22

    brian22

    40
    0
    Jul 19, 2013
    int pinA = 11;
    int pinB = 12;
    int val;

    void setup()
    {
    pinMode(pinA, OUTPUT);
    pinMode(pinB, INPUT);
    }

    void loop()
    {
    int();
    }


    void int()
    {

    val=digitalRead(pinB); // read data
    if(pinB == HIGH)
    {
    Serial.println("PINA ON"); //write serially
    }
    else if(pinB == LOW)
    {
    Serial.println("PinA OFF"); //write serially
    }
    }

    the message is keep repeating. how to stop the message in serial monitor even the pin is still in continuous active high or low status. it's just a one time message.
     
  2. GreenGiant

    GreenGiant

    842
    6
    Feb 9, 2012
    do you want it to write once every time the pin changes?

    or just when you power it up and connect serially?
     
  3. brian22

    brian22

    40
    0
    Jul 19, 2013
    yes. i want to write it once every time the pin changes. tnx
     
  4. GreenGiant

    GreenGiant

    842
    6
    Feb 9, 2012
    alright in that case you have many many options for this.

    you can use your if statements with some small additions

    Code:
    int pinA = 11;
    int pinB = 12;
    int val;
    int printed;
    
    void setup()
    {
    pinMode(pinA, OUTPUT);
    pinMode(pinB, INPUT);
    printed = 0;
    }
    
    void loop()
    {
    int();
    }
    
    void int()
    {
    val=digitalRead(pinB); // read data
    if([COLOR="Red"]val[/COLOR] == HIGH [COLOR="Blue"]&& printed == 0[/COLOR])
    {
    Serial.println("PINA ON"); //write serially
    [COLOR="blue"]printed = 1;     //change state case to 1[/COLOR]
    }
    else if([COLOR="red"]val[/COLOR] == LOW [COLOR="blue"]&& printed == 1[/COLOR])
    {
    Serial.println("PinA OFF"); //write serially
    [COLOR="blue"]printed = 0;     //change state case to 0[/COLOR]
    }
    }
    
    first off you dont need to create a function for it, but I can understand your desire to do so.
    second you weren't using "val" you were reading into it but not using it in the if statement so I fixed that. (red text)
    Third... the big one, I added a state case (the variable "printed") to the if statement, so while it is high and the state is 0 it will print serially then change the state to 1 so it cannot meet the requirements of either the if or else, therefor not printing again. (blue text)

    alternative ways you can do this are with case statements with a state variable, interrupts (though depending on the switch you may need to add some buffering in there), and any other way people can think of
     
  5. brian22

    brian22

    40
    0
    Jul 19, 2013
    thank you so much this is a big help.. :))

    last question sir. is it still the same ouput if i use a lcd?
     
  6. sirch

    sirch

    109
    1
    Dec 6, 2012
  7. brian22

    brian22

    40
    0
    Jul 19, 2013
    hi sir/ma'am how to use eeprom i'm littlebit confused.

    void setup()
    {
    for (int i = 0; i < 512; i++)
    EEPROM.write(i, i);
    }

    that is the use of (i, i) can i use different characters like b,a?




    #include "eeprom.h"


    int pinA = 11;
    int pinB = 12;
    int val;
    int printed;

    void setup()
    {
    pinMode(pinA, OUTPUT);
    pinMode(pinB, INPUT);
    printed = 0;

    }

    void loop()
    {
    CheckPassword();

    }

    void CheckPassword()
    {
    checkNum=true;
    lcd.print("Correct Password");
    ChangePass();
    int();
    }
    else
    {
    lcd.print("Incorrect Password");
    }



    void int()
    {
    val=digitalRead(pinB); // read data
    if(val == HIGH && printed == 0)
    {
    lcd.printn("PINA ON");
    printed = 1; //change state case to 1
    }
    else if(val == LOW && printed == 1)
    {
    lcd.print("PinA OFF");
    printed = 0; //change state case to 0
    }
    }

    void changePass();
    {

    (here to put eeprom codes)

    }
     
    Last edited: Aug 10, 2013
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-