Arduino programming

Thread Starter

brian25

Joined May 13, 2013
37
Rich (BB code):
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.
 
Last edited by a moderator:

skusku

Joined Aug 9, 2009
80
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.
 

ErnieM

Joined Apr 24, 2011
8,415
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.
 
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.
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
 

Potato Pudding

Joined Jun 11, 2010
688
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.
 
Top