Computer to Arduino to hardware, controlling on / off state of hardware via user input on computer First time coding for Arduino. I'm working on code to operate an electronic valve switch. We have a laptop connected to an Arduino Mega, and that Arduino is hooked up to the valve (hardware) on Pin 53. 

I was trying to work off of the "blink" example originally, but that gives no basis for user prompted control. Basically I want to have the computer ask me if I should turn it on or off, type in my response (I was using "HIGH" for on and "LOW" for off, as in voltage control), and have that activate or deactivate the hardware. 

My main problem right now stems from how best to ask for that input. I've been trying to work with the Messenger.h library thus far. I'm definitely getting hung up on how to define the pin, or what the simplest method for user input would be. (HIGH or LOW? 0 or 1? Something else?)

Code is below. Thanks!

#include &lt;Messenger.h&gt;

Messenger message = Messenger(); 

// Variable definitions. Switchinput is what the user commands, current state is whether the air is flowing or not. 
boolean currentstate;
boolean switchinput;
String pin53; // Something is up on this line?? How to define this pin??

// FUNCTION DEFINITIONS
// Startup fxn
void start() 
{
  Serial.print("LOW = off, HIGH = on. Switch is ");
  Serial.println(currentstate);
  Serial.print("Enter LOW to turn on or HIGH to turn off: ");
  switchinput = Serial.read();
}

// Go off to on
void offtoon()
{
    digitalWrite(pin53,HIGH);
    currentstate = switchinput;     
}

// Go on to off
void ontooff()
{
    digitalWrite(pin53,LOW);
    currentstate = switchinput;     
}

// Stay same -- already off
void staysameoff()
{
  if ((currentstate = switchinput) &amp;&amp; (switchinput = LOW))
  {
      void start();
  }
}

// Stay same -- already on
void staysameon()
{
  if ((currentstate = switchinput) &amp;&amp; (switchinput = HIGH))
  {
    void start(); 
  }
}


// Running actual code
void setup() 
{
  // initialize digital pin 53 as an output.
  pinMode(pin53, OUTPUT);

  // Sets default state to switch being off (Off = LOW, On = HIGH)
  String currentstate = LOW;
}


void loop() 
{
   if ((currentstate != switchinput) &amp;&amp; (switchinput = HIGH))
  {
      void offtoon();
  }

  if ((currentstate != switchinput) &amp;&amp; (switchinput = LOW))
  {
      void ontooff();
  }

  if ((currentstate = switchinput) &amp;&amp; (switchinput = LOW))
  {
      void staysameoff();
  }

  if ((currentstate = switchinput) &amp;&amp; (switchinput = HIGH))
  {
      void samestayon();
  }
}
This seems way too complex for your purpose:


you really do not need to do anything to stay in the same state
if you want to switch the valve ON, no need to check whether it's OFF:
there is no harm in switching ON a valve that is already ON.


Here is a simplified version of the program:

void setup()
{
    Serial.begin(9600);
    Serial.println("Type '0' to turn OFF, '1' to turn ON");
}

void loop()
{
    int command = Serial.read();
    if (command == '0') digitalWrite(53, LOW);
    if (command == '1') digitalWrite(53, HIGH);
}


Notice that Serial.read() returns -1 if there is nothing in the input
buffer, and since -1 is neither '0' nor '1', this will not change the
output.

If you want to use more elaborate commands (not single characters), you
may want to take a look at this simple command-line
interpreter. It is intended to be easy to adapt to
different purposes.