have an action be done, both from serial, and from "hardware" input.

okey, i know that the tittle sucks. but i have the following problem.
in my sketc, i want an action to be done, both , when a specific serial input arrives, or/and a hardware input is given (button press).

my code is as follows ::

int LU=0;
int LD=0;
int RU=0;
int RD=0;
int defrost=0;
int Rfog=0;

void setup() {
Serial.begin(9600);
Serial.setTimeout(50);

pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(13,HIGH) ;
digitalWrite(12,HIGH) ;
digitalWrite(11,HIGH) ;
}

void loop() {

if (Serial.available()>0 )
{
int LU = Serial.parseInt();
int LD = Serial.parseInt();
int RU = Serial.parseInt();
int RD = Serial.parseInt();
int defrost = Serial.parseInt();
int Rfog= Serial.parseInt();

if ( (digitalRead(A1)==LOW)||(LU==1) ) {digitalWrite(4,HIGH);}
else{digitalWrite(4,LOW);}

if ( (digitalRead(A2)==LOW)||(LD==1) ){digitalWrite(5,HIGH);}
else{digitalWrite(5,LOW);}

if ( (digitalRead(A3)==LOW)||(RU==1) ){digitalWrite(2,HIGH);}
else{digitalWrite(2,LOW);}

if ( (digitalRead(A4)==LOW)||(RD==1) ) {digitalWrite(3,HIGH);}
else{digitalWrite(3,LOW);}

Serial.print(LU);
Serial.print(LD);
Serial.print(RU);
Serial.print(RD);
Serial.print(defrost);
Serial.println(Rfog);

}

/* THIS HERE IS MY TRY, TO MAKE IT WORK EVEN WHEN NO SERIAL ARRIVES.
if ( (digitalRead(A1)==LOW)||(LU==1) ) {digitalWrite(4,HIGH);}
else{digitalWrite(4,LOW);}

if ( (digitalRead(A2)==LOW)||(LD==1) ){digitalWrite(5,HIGH);}
else{digitalWrite(5,LOW);}

if ( (digitalRead(A3)==LOW)||(RU==1) ){digitalWrite(2,HIGH);}
else{digitalWrite(2,LOW);}

if ( (digitalRead(A4)==LOW)||(RD==1) ) {digitalWrite(3,HIGH);}
else{digitalWrite(3,LOW);}
*/

}

i dont know if there is something that i dont understand, BUT: if the arduino is connected to pc, and serial comes all the time, then it works both with buttton, and with serial. but if its not connected to pc, and no serial arrives, then nothing happens even when i press the button.

the second paragraph (commented out), is my try to make it work withg no serial input, but if i add that paragraph, then nothing works at all... anyone can tell me what i am missing??? thanks!!

i dont know if there is something that i dont understand, BUT: if the arduino is connected to pc, and serial comes all the time, then it works both with buttton, and with serial. but if its not connected to pc, and no serial arrives, then nothing happens even when i press the button.

Of course not. If your indenting was anywhere near proper, you'd see why. Use Tools + Auto Format, if you can't indent properly as you go.

If you were to put all the code that happens when serial data arrives into a function, your code would look like this:

void loop()
{
   if(Serial.available() > 0)
  {
     handleSerialData();
  }
}

Where is the code to read the switch states? When does it get called?

i understand what you mean. but why do you think that? dosent the :

if ( (digitalRead(A1)==LOW)||(LU==1) ) {digitalWrite(4,HIGH);}
else{digitalWrite(4,LOW);}

read the state of A1, instantly??

if you mean that it dosent ender the IF to begin with, if there is no serial, that is why i have the commented code on the end. can you explain a bit more?

Id get rid of the 'or's (the ||) in and out of the 'serial' routine.
If serial available - ignore the button
if no serial - just deal with the button - this way it will closer resemble the button demos.

ok. maybe i explained it wrong. i have tried with the last paragraph, not commended. but still it didint worked

settra:
ok. maybe i explained it wrong. i have tried with the last paragraph, not commended. but still it didint worked

no, my bad, I didnt see it correctly. so I edited my post. Sorry.

For clarity, I think whats happening is, the button is switching so fast you cant tell - its like 'auto-repeating' - very fast.
Thus you need to see how to make the buttons deal with that, which may vary depending on other things, I presume there's more to your code.

Which is easier to read and understand:

if ( (digitalRead(A4)==LOW)||(RD==1) ) {digitalWrite(3,HIGH);}

or

if (digitalRead(A4) == LOW || RD==1))
{
   digitalWrite(3, HIGH);
}

There really is no excuse for jamming code together the way you are.

if (Serial.available()>0 )
{
  int LU = Serial.parseInt();
  int LD = Serial.parseInt();
  int RU = Serial.parseInt();
  int RD = Serial.parseInt();
  int defrost = Serial.parseInt();
  int Rfog=  Serial.parseInt();

If there is at least one byte available to read, read all 6 ints. No!

ALL of the action in your code happens ONLY if there is serial data to read. That is still NOT what you seem to want, as suggested in reply #1.

"but if i add that paragraph, then nothing works at all.. "
so stop repeating the same bullshit.

as for the code, i have written it like that on propose, and it is none of your business. if you are too stupid to be able to read it, then just dont. you anyway seem to be unable to help at all. quit your bitching and get lost from the post

Moderator edit: Gratuitous insults deleted. (Nick Gammon)

it may be as simple as putting a cap across the inputs with buttons to act as a filter, but you can do a debounce in software too, which, IIAC, those pullups on the inputs is what makes software debounce practical.

I point to bounce because you indicate it working if there IS 'serial' data available. But not otherwise - the extra overhead in the serial read may be slowing it down enough to be seen/practical/useful etc.

i dont think its a problem with the buttons.
what i want is to have 2 portions of the code.
one portion will be done when there is serial connection with a PC.
the other portion must be done , only when there is no serial at all.

i found out that i should use the if(Serial) , but it dosnet seem to work ...

Now now guys...

"as for the code, i have written it like that on propose, and it is none of your business. "

Paul does have a point, when seeking help from others, help them help you, like formatting for easier reading, clipping it down to accentuate the error/problem etc.

Your format is harder to read. Hence my first mistake/post

settra:
i dont think its a problem with the buttons.

Yet you seek help. lol :wink:

settra:
what i want is to have 2 portions of the code.
one portion will be done when there is serial connection with a PC.
the other portion must be done , only when there is no serial at all.

But clearly, butts are considered 2 times - in serial routine and commented out currently at bottom.
Same with serial - its considered (or'd) at the bottom as well (now commented out)

settra:
i found out that i should use the if(Serial) , but it dosnet seem to work ...

Shouldn't THAT be your topic of post? Talk about burying lead scoop.

Seriously, that is an issue of board type, which brings me to another point - not all hardware environments are equal - switch bounce experience one time does not make all switch bounce experience.

i can tottaly get that. but when sed polite. if sed with then atittude of Paul, then i just dont . i found my problem.

in this code :

int LU=0;
int LD=0;
int RU=0;
int RD=0;
int defrost=0;
int Rfog=0;

void setup() {
Serial.begin(9600);
Serial.setTimeout(50);

pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(13,HIGH) ;
digitalWrite(12,HIGH) ;
digitalWrite(11,HIGH) ;
}

void loop() {

if (Serial.available()>0 )
{
LU = Serial.parseInt();
LD = Serial.parseInt();
RU = Serial.parseInt();
RD = Serial.parseInt();
defrost = Serial.parseInt();
Rfog= Serial.parseInt();

if ( (digitalRead(A1)==LOW)||(LU==1) ) {
digitalWrite(4,HIGH);
}
else{
digitalWrite(4,LOW);
}

if ( (digitalRead(A2)==LOW)||(LD==1) ){
digitalWrite(5,HIGH);
}
else{
digitalWrite(5,LOW);
}

if ( (digitalRead(A3)==LOW)||(RU==1) ){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
}

if ( (digitalRead(A4)==LOW)||(RD==1) ) {
digitalWrite(3,HIGH);
}
else{
digitalWrite(3,LOW);
}

}

Serial.print(LU);
Serial.print(LD);
Serial.print(RU);
Serial.print(RD);
Serial.print(defrost);
Serial.println(Rfog);

if ( (digitalRead(A1)==LOW)||(LU==1) ) {
digitalWrite(4,HIGH);
}
else{
digitalWrite(4,LOW);
}

if ( (digitalRead(A2)==LOW)||(LD==1) ){
digitalWrite(5,HIGH);
}
else{
digitalWrite(5,LOW);
}

if ( (digitalRead(A3)==LOW)||(RU==1) ){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
}

if ( (digitalRead(A4)==LOW)||(RD==1) ) {
digitalWrite(3,HIGH);
}
else{
digitalWrite(3,LOW);
}

}

(the difference is that the serial.print() 's are outside from the first if statement, ) the serial.print()'s print :
000000
no matter if i give other serial input, meaning that the LU,LD.. variables, loose their value's as soon as the IF is done. , so at the second portion of IF's, if there is no button press, then everything is returned to low...
why does this happen?

Moderator edit: Edited for bad language. No more warnings. Please use CODE TAGS when posting code. AWOL

They are set in the context of the serial routine, never reset outside that routine - even tho its visible to other parts of the loop() func.
Buttons OTH, and, many demos dont accent this fact, rely on an LED as a sort of invisible buffer as part of their debounce routines. Which makes understanding them a little misleading, imho.

"why does this happen"
default:
digitalWrite(3,LOW);

not why it gets low. why the values are being reset

As a general rule of thumb, and would be true in this case only assign those vars once per loop

your assigning several 2 times. serial and in the else. when you dont need half of them - no need for serial data in the button testing, and no need to test buttons in the serial routine.

settra:
not why it gets low. why the values are being reset

Vars set in {} and outside of {} are difficult to trace.

I would recommend making user functions for parsing serial data and another for debouncing your button inputs and then call those funcs based on an 'if else' that is based on if serial data available. Keeping those vars separate

edited: read up on var 'scope'

i am suspecting there is something else.
imagine this:
if i have an arduino in external power source. running this code:

void setup() {
Serial.begin(9600);
}

void loop() {

if(Serial) {

digitalWrite(13,HIGH) ;
}
else
{
digitalWrite(13,LOW) ;
}

}

IF i have the USB connected, then the led should be open. but if i dont have the USB connected at all, the led should be turned off?? (cause that is not the case with me. led is always on)