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 functionstart
. Don't use the wordvoid
here. – Nick Gammon♦ Jun 15 '16 at 5:56String 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