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();
}
}
1 Answer 1
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– CEL2016年06月16日 01:14:26 +00:00Commented Jun 16, 2016 at 1:14
if ((currentstate = switchinput) && (switchinput = LOW))
- this (and all similar lines) are wrong. You use==
for comparing, not=
.{ void start(); }
- that does not call the functionstart
. Don't use the wordvoid
here.String pin53;
- not a String. Try:const byte pin53 = 53;
- although what that achieves I am not sure. How about:const byte valvePin = 53;