Jump to content
43oh

[SOLVED] Possible issue with "digitalWrite".


Recommended Posts

MSP-EXP430F5529LP, Energia 11

 

Code:

void setup()
{
  pinMode(GREEN_LED, OUTPUT);
  digitalWrite(GREEN_LED, HIGH);
}

void loop()
{
  delay(500);
  digitalWrite(GREEN_LED, !GREEN_LED);     // toggle green LED
}
... is used, the green LED will light after the sketch is uploaded then turn off, but does not light again.

 

 

But-

void setup()
{
  pinMode(GREEN_LED, OUTPUT);
  digitalWrite(GREEN_LED, HIGH);
}

void loop()
{
  delay(500);
  digitalWrite(GREEN_LED, !digitalRead(GREEN_LED));     // toggle green LED
}
... does cause the LED to toggle.
Link to post
Share on other sites

Similarly,

void setup()
{
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  digitalWrite(GREEN_LED, HIGH);
  digitalWrite(RED_LED, !(GREEN_LED));
}

void loop()
{
  delay(500);
  digitalWrite(GREEN_LED, !digitalRead(GREEN_LED));
  digitalWrite(RED_LED, !GREEN_LED);
}
... should cause the red LED to be in the opposite state of the green LED, but it never changes state.

 

But...

void setup()
{
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  digitalWrite(GREEN_LED, HIGH);
  digitalWrite(RED_LED, LOW);
}

void loop()
{
  delay(500);
  digitalWrite(GREEN_LED, !digitalRead(GREEN_LED));
  digitalWrite(RED_LED, !digitalRead(GREEN_LED));
}
... works as expected.
Link to post
Share on other sites

digitalWrite(GREEN_LED, !digitalRead(GREEN_LED)); // toggle green LED

 

digitalWrite(GREEN_LED, !digitalRead(GREEN_LED));     // toggle green LED

... does cause the LED to toggle.

 

Yes of course. You must first read the digital status of the GREEN_LED pin so that you could negate it.

 

An alternative would be: you can store the status of the GEEN_LEED pin in a RAM variable:

int ledstate;

void setup()
{
  pinMode(GREEN_LED, OUTPUT);
  digitalWrite(GREEN_LED, HIGH);
  ledstate = HIGH;
}

void loop()
{
  delay(500);
  ledstate = !ledstate;                          //toggle status
  digitalWrite(GREEN_LED, ledstate); // update green LED status
}
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...