Good evening everyone.
I am not sure if what I am trying to do is even possible.
I have only been messing around with my arduino for just over a week.
I am trying to change the value of a variable (String) via the serial monitor to see how the program works.
I am aware of the recommendation of using the "char" functions, but since I didnt completely understand how that works, I was sticking to using the string since it serves my purpose at the moment.
So I have a string variable in my code (below) called "doorfunction", and I wanted to change its value via the serial monitor.
I was expecting to be able to write in the serial monitor something like: doorfunction = "open"
Maybe also be able to read the value of the variable with another command?
#include <Servo.h>
Servo doorlatch; // create servo object to control the latch
int pos = doorlatch.read (); // variable to store the servo position.
String doorpos = "none"; // variable to declare the position of the door - starts as none
String doorfunction = "none"; // variable to call the function open/close door - starts as none
void setup() {
doorlatch.attach(2); // attaches the servo on pin 2 to the servo object
Serial.begin(9600); // refresh rate
Serial.println(doorfunction);
Serial.println(doorpos);
}
void opendoor() {
if ((doorpos == "DoorClosed") & (pos == 20) & (doorfunction == "Open"));
doorlatch.write(-20);
delay(200);
doorfunction = "none";
doorpos = "DoorOpen";
}
void closedoor() {
if ((doorpos == "DoorOpen") & (pos == -20) & (doorfunction == "Close"));
doorlatch.write(20);
delay(200);
doorfunction = "none";
doorpos = "DoorClosed";
}
void loop() {
if ((pos != 20) & (pos != -20)) {
Serial.println("Unknown latch position");
}
if (doorfunction == "Open"){
opendoor();
}
if (doorfunction == "Close"){
closedoor();
}
}
Ps: Please dont kill me for my poor coding skills, I am just trying to learn