SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By brian25
#161680
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.
By jremington
#161685
There are many problems with this program. Of course it does not matter what the status of "PinB" is. Take a deep breath and think through what each line does.
Last edited by jremington on Fri Jul 19, 2013 8:35 am, edited 1 time in total.
By Mee_n_Mac
#161686
First off ... which message ? I deduce that since there's a problem you're getting the PinA ON message but it's always helpful to state exactly what's happening and not happening. We can't read your mind or see what's happening on your PC. Only the NSA can do that !

Second you're getting a message all the time because that's what your code instructs the Arduino to do. It reads pinB and if it's HIGH is sends the PINA ON message. If pinB was LOW it then reads pinB again and if it's LOW (which it must be), it sends the PINA OFF message. PinB is either HIGH or LOW, it can't be anything else ... so you get a message all the time. (ps - you're missing the Serial.begin(XXXX) statement)

What do you want it to do ? Send the messages only on the transitions from HIGH to LOW and from LOW to HIGH ? I might guess this is what you want but ????

BTW what do you have attached to pinB ? Does it always put pinB into a defined state ? Is pinB ever left floating, not connected to anything ? When floating the pins can switch back and forth between HIGH and LOW and so you should enable the input pull-up resistor.
http://arduino.cc/en/Reference/Constants (scroll down to "inputs")
http://arduino.cc/en/Tutorial/InputPullupSerial

Lastly it helps when you use the code button. It keeps the formatting and puts the code in a nice scrolling box.
(click on to open and enlarge)
CodeButtonTags.jpg
You do not have the required permissions to view the files attached to this post.
By Mee_n_Mac
#161687
jremington wrote:There are many problems with this program. Of course it does not matter what the status of "PinB" is. Take a deep breath and think through what each line does.
I'll guess what we see is a snippet of a larger program. It doesn't compile for me.

The use of the name int() makes me wonder if the OP wasn't trying to use external interrupts.
By bsagan
#161689
I'd guess you are only getting pinA high messages. Check the condition on your if statements for the answer.
jremington wrote:There are many problems with this program. Of course it does not matter what the status of "PinB" is. Take a deep breath and think through what each line does.
I think he just means that the variable 'val' is never used... hence the status of PinB does not matter...
By Mee_n_Mac
#161694
bsagan wrote:I'd guess you are only getting pinA high messages. Check the condition on your if statements for the answer.
jremington wrote:There are many problems with this program. Of course it does not matter what the status of "PinB" is. Take a deep breath and think through what each line does.

I think he just means that the variable 'val' is never used... hence the status of PinB does not matter...
Ha ! I saw the high level goof and missed the low lever goof(s). :oops:

I saw
Code: Select all
if(pinB == HIGH)
and read it as
Code: Select all
if(digitalRead(pinB) == HIGH)
let alone the
Code: Select all
if(val == HIGH)
that OP probably intended.