Help Arduino Program

brian22

Jul 19, 2013
40
Joined
Jul 19, 2013
Messages
40
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.
 

GreenGiant

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

or just when you power it up and connect serially?
 

brian22

Jul 19, 2013
40
Joined
Jul 19, 2013
Messages
40
yes. i want to write it once every time the pin changes. tnx
 

GreenGiant

Feb 9, 2012
842
Joined
Feb 9, 2012
Messages
842
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
 

brian22

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

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

brian22

Jul 19, 2013
40
Joined
Jul 19, 2013
Messages
40
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:
Top