Arduino basic

I am new in arduino.My question is ..... I want to execute a function for a condition for the 1st time,but for the second time I want to execute another function with respect to same condition.How can I do this? Please anyone help me, its very urgent.....

You use a “flag”, which starts off initialised clear. Then when you check the condition you also check that flag. So first time the condition is met, the flag is clear and you do option “A”, and then you set the flag. So second time through, the condition is met but this time the flag is set, so you do option “B”.

Here’s pseudo code:

// this is NOT REAL CODE
bool myFlag = false

if (condition is met)
  {
     if(myFlag is false)
     {
     do A
     myFlag = true
     }

     else //which means the flag is set
     {
     do B
     }
}

You’ll need to decide what to do with the flag after the second time through, ie what happens the third time? A or B? So you might clear the flag after B

I have written this according to your instruction...can you chect it?

here's the code....

int switchPin=9; int led=10; int led1=11; bool myFlag=false; void setup() { pinMode(switchPin,INPUT); pinMode(led,OUTPUT); pinMode(led1,OUTPUT); }

void loop() { if (digitalRead(switchPin)==HIGH) { if (myFlag=false) { digitalWrite(led,HIGH); myFlag=true; } else { digitalWrite(led1,LOW); } } else { digitalWrite(led,LOW); digitalWrite(led1,LOW); } }

if (myFlag=false)

Bzzzt.

Please use code tags.

if (myFlag=false)

is it wrong?

It isn't wrong (it will compile, for example), but it's an assignment, not a comparison.

AWOL: It isn't wrong (it will compile, for example), but it's an assignment, not a comparison.

What he is saying is that in a comparison If (a == b) Use double == signs. Otherwise the result is a takes the value of b.

Weedpharma

Otherwise the result is a takes the value of b.

. . . and the expression always evaluates to false.

. . . and the expression always evaluates to false.

(because a != b)

weedpharma:
What he is saying is that in a comparison
If (a == b)
Use double == signs.

Funny thing is, the OP got it right first time and then immediately wrong:

void loop()
{
  if (digitalRead(switchPin)==HIGH) // <<<<<<<<<<<< right
  {
    if (myFlag=false)        // <<<<<<<<<<<< wrong
    {

Although by his or her admission, didn’t realise that.

Piyal:
if (myFlag=false)

is it wrong?

couple of points:

code tags are the things you have to select in this forum by going to preview, then selecing the code tag button

your code then shows up in a box in the post. it is frowned upon to put more than a few lines in a general text format.

int switchPin=9;
int led=10;
int led1=11;
bool myFlag=false;
void setup()
{
  pinMode(switchPin,INPUT);
  pinMode(led,OUTPUT);
  pinMode(led1,OUTPUT);
}

void loop()
{
  if (digitalRead(switchPin)==HIGH)
  {
    if (myFlag=false)
    {
      digitalWrite(led,HIGH);
      myFlag=true;
    }
    else
    {
      digitalWrite(led1,LOW);
    }
  }
  else
  {
    digitalWrite(led,LOW);
    digitalWrite(led1,LOW);
  }
}

second, where does myFlag get set to false ?

AWOL: . . . and the expression always evaluates to false.

raschemmel: (because a != b)

Because a != 0

Unless b = 0

Weedpharma

Because a != 0

Unless b = 0

Ok , you lost me. Isn't that the same as a != b ? What am I missing ?