0

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 <Messenger.h>

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) && (switchinput = LOW))
  {
      void start();
  }
}

// Stay same -- already on
void staysameon()
{
  if ((currentstate = switchinput) && (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) && (switchinput = HIGH))
  {
      void offtoon();
  }

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

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

  if ((currentstate = switchinput) && (switchinput = HIGH))
  {
      void samestayon();
  }
}
  • if ((currentstate = switchinput) && (switchinput = LOW)) - this (and all similar lines) are wrong. You use == for comparing, not =. – Nick Gammon Jun 15 '16 at 5:55
  • { void start(); } - that does not call the function start. Don't use the word void here. – Nick Gammon Jun 15 '16 at 5:56
  • String pin53; - not a String. Try: const byte pin53 = 53; - although what that achieves I am not sure. How about: const byte valvePin = 53; – Nick Gammon Jun 15 '16 at 5:58
  • As for reading serial input, see How to process incoming serial data without blocking – Nick Gammon Jun 15 '16 at 5:59
  • Fixed the first three comments-- those are all things I really should've caught. So this works pretty similarly to the state machine example on your link, right? Except I've only got two possible inputs, whereas that has three options. Correct? – CEL Jun 15 '16 at 7:30
2

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.

  • Thank you! I've only got one switch with two states happening for now, but I'm going to be working with multiple states later on as more components are added. Single digits might still work for that but for other users who aren't me later on it might be worth switch over to the single command line interpreter. – CEL Jun 16 '16 at 1:14

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.