abecedarian 330 Posted December 24, 2013 Share Posted December 24, 2013 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. Quote Link to post Share on other sites
abecedarian 330 Posted December 24, 2013 Author Share Posted December 24, 2013 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. Quote Link to post Share on other sites
reaper7 67 Posted December 24, 2013 Share Posted December 24, 2013 but word GREEN_LED means "shortcut" to pin number not a pin state (or value), so !GREEN_LED give you logical negation of value from pins_energia.h where: static const uint8_t GREEN_LED = 14; Quote Link to post Share on other sites
abecedarian 330 Posted December 24, 2013 Author Share Posted December 24, 2013 I would think that: digitalWrite(GREEN_LED, !GREEN_LED);... would assign GREEN_LED, the first parameter of the function, the value represented by the second parameter of the function, as that's what it's supposed to do, no? Quote Link to post Share on other sites
abecedarian 330 Posted December 24, 2013 Author Share Posted December 24, 2013 Okay, I get what you mean.... GREEN_LED is a 'constant' assigned to a pin, so inverting the constant doesn't invert its state. /me goes back to my location... over here, doing over here things. Quote Link to post Share on other sites
icserny 9 Posted December 24, 2013 Share Posted December 24, 2013 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 } Quote Link to post Share on other sites
abecedarian 330 Posted December 24, 2013 Author Share Posted December 24, 2013 Yes, I'm guilty of PUI. Programming Under the Influence. Who spiked my eggnog? spirilis 1 Quote Link to post Share on other sites
spirilis 1,265 Posted December 24, 2013 Share Posted December 24, 2013 Haha in all honesty, that is the sort of "contextual expressiveness" many of us desire in a language but reality always being more mechanistic. Sent from my Galaxy Note II with Tapatalk 4 Quote Link to post Share on other sites
abecedarian 330 Posted December 24, 2013 Author Share Posted December 24, 2013 "... reality being more masochistic" is more like it. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.