Arduino programming

Discussion in 'Programmer's Corner' started by brian25, Jul 19, 2013.

  1. brian25

    Thread Starter Member

    May 13, 2013
    37
    0
    Code ( (Unknown Language)):
    1.  
    2. int pinA = 11;
    3. int pinB = 12;
    4. int val;
    5.  
    6. void setup()
    7. {
    8. pinMode(pinA, OUTPUT);
    9. pinMode(pinB, INPUT);
    10. }
    11.  
    12. void loop()
    13. {
    14. int();
    15. }
    16.  
    17.  
    18. void int()
    19. {
    20.  
    21.   val=digitalRead(pinB); // read data
    22.   if(pinB == HIGH)
    23.   {
    24.     Serial.println("PINA ON"); //write serially
    25.   }
    26.   else if(pinB == LOW)
    27.   {
    28.      Serial.println("PinA OFF"); //write serially
    29.         }
    30.  }
    31.  
    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.
     
    Last edited by a moderator: Jul 21, 2013
  2. skusku

    Active Member

    Aug 9, 2009
    63
    1
    Create a temporary software int pin B, lets say pinBtmp

    Make pinBtmp=0

    Instead of testing only the pin, test pinB and the temporary one.
    Use this: if(pinB == HIGH && pinBtmp == 0 )

    and once your arduino goes to the writeln code, follow with making pinBtmp a 1 and switch it off in the else statement.
     
  3. GabrielSalla

    New Member

    Jul 6, 2013
    6
    0
    Or you can use interrupt, but you may need to change the pinB to another pin. Check the datasheet of the microcontroller for information about it
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    You don't need an interrupt or any other variables to do this. All you need do is after the Serial.println ON statement is add another loop:

    while(pinA==HIGH);

    And similar for off. This sets a lock point so the print only works once for a press or release.

    Once you start adding more features you may want to use better techniques as this wastes lots of time.

    Sorry but using code tags on a borrowed iPad is nearly impossible for me.
     
  5. GabrielSalla

    New Member

    Jul 6, 2013
    6
    0
    But if you do this, you can't do anything while you are waiting. If for some reason he think it's better to stop until it happens, then your option is definitely better
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Yes I said so.
     
  7. Potato Pudding

    Well-Known Member

    Jun 11, 2010
    684
    92
    int is a terrible name choice for a function.

    Why are you wrapping that code in a function?

    Create a boolean done flag.

    Test for change in the pin.

    If there is a change in the Pin clear the done flag.

    Write only when the done flag is clear and then immediately set the done flag.

    It will only clear again the next time the test for change on the pin triggers.
     
Loading...