1

So for my project, I am making a button that turns on and off an LED, but also display the state of the led on an LCD and by Serial. But my issue is, I don't want it to just show a 0 or a 1, I want it to say On or Off.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int inPin = 22;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers


void setup(){
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    // Print a message to the LCD.
    lcd.print("System Activated");
    //
    lcd.setCursor(0, 1);
    lcd.print("Ready...");

    pinMode(inPin, INPUT);
    pinMode(outPin, OUTPUT);

    Serial.begin(9600);
    Serial.println("System Activated");
    Serial.println("Made by Mateo Holguin");
    Serial.println("0 = Light is Off | 1 = Light is On");
    Serial.println("=======================================");
}

void loop(){
    reading = digitalRead(inPin);

    if (reading == HIGH && previous == LOW && millis() - time > debounce) {
        Serial.println("Button Switch State Change:");
        if (state == HIGH)
            state = LOW;
        else
            state = HIGH;

        Serial.println(state);
        Serial.println("");
        lcd.clear();
        lcd.print("State: ");
        lcd.print(state);

        time = millis();    
    }

    digitalWrite(outPin, state);

    previous = reading;
}
  • Both of these 2 answers works well, I just used the second one as I am a bit of a noob so I used one that I can understand more. – Mateo Holguin Nov 14 '15 at 20:43
2

Another option: use the "inline" if construct:

lcd.print(state ? F("On") : F("Off"));

The format is:

<if comparison> ? <true result> : <false result>

Note the use of F(...) to force the strings to stay in flash memory.

  • I am going to try this option as well, as it makes a lot of sense to me. Thank you very much! – Mateo Holguin Nov 11 '15 at 16:19
  • When I use that line I get " '_F' was not declared in this scope" – Mateo Holguin Nov 14 '15 at 20:40
  • @MateoHolguin Sorry, wasn't thinking straight that day - it's F() not _F() – Majenko Nov 14 '15 at 20:41
  • I uploaded it to the Arduino, and it works perfectly the way I needed it to! Thanks again – Mateo Holguin Nov 14 '15 at 20:42
5

Put the strings to display in an array, and output them instead.

char const * const stateDisplay[] = {"Off", "On"};

 ...

lcd.print(stateDisplay[state]);
  • Could you please explain why "const" is used twice there? I've never seen that before and don't know how to look it up. – Jerry Nov 10 '15 at 10:04
  • 1
    @Jerry: One is for stateDisplay (i.e. the pointer is constant), and the other is for *stateDisplay (i.e. the contents are constant as well). – Ignacio Vazquez-Abrams Nov 10 '15 at 10:06
  • Thanks! I can actually use that in a sketch I'm working on now. – Jerry Nov 10 '15 at 10:14
  • Thank you very much! I can finally get that one issue fixed! – Mateo Holguin Nov 11 '15 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.