Hello everybody,
I am using the Arduino to control some electronics in a dollhouse as part of a school project. I have coding experience in Java and COBOL but I am pretty new to the Arduino language. I need to make an app that will control the Arduino and the electronic components. My goal is to be able to print out a statement in a terminal window that would say something like "LIGHTS ON OR OFF: " and if the user wrote "OFF" the lights connected to the board would turn off. Basically my question is, can I write code that executes certain functions on the board based on user text input, and how can I read text input.
Thank you.
You could collect serial input as it comes in and act on it using a state machine.
char chr;
if ( Serial.available() )
{
chr = Serial.read();
// what you do here can vary
}
If serial monitor is set to add a newline char (ascii '\n', value = 10) it will add an end-of-input marker.
Then you want to watch for letters to arrive in sequence to form a 'legal' message.
Do you know state machines? They can be very simple.
Read Robin2's Serial Input Basics - updated thread to give you some ideas.
Here's a small program to experiment with. It doesn't answer your question completely, but play around with it, then try to think how you might implement GOForSmoke's state machine.
#define LEDPIN 13
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Enter ON or OFF");
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW); // Make sure onboard LED is off to start
}
void loop() {
char buffer[10];
int charsRead;
int i;
if (Serial.available() > 0) {
charsRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
buffer[charsRead] = NULL; // It now a C string
i = 0;
while (buffer[i]) { // Spin through letters until we read the NULL
buffer[i] = toupper(buffer[i]); // All letters now uppercase
i++;
}
if (strcmp(buffer, "OFF") == 0) {
digitalWrite(LEDPIN, LOW);
} else {
if (strcmp(buffer, "ON") == 0) {
digitalWrite(LEDPIN, HIGH);
}
}
}
}
While it may be a little less elegant, if you get the user to send a single character command it will make the Arduino code much simpler. For example they could send 'N' for ON and 'F' for OFF. Or maybe '+' for ON and '-' for OFF.
Just my laziness ...
...R