Hello I don't know if the topic quite hit where I needed it to but here's my problem. I'm new to arduino and coding and so I'm having a hard time figuring out how to make one piece of code load when I type "RA" into Serial Monitor. While that is running I need to be able to type "VF" into the Serial Monitor and "RA" turns off and vice versa.
Please follow the posting guidelines for the forum.
Do not use delay( )s in your sketch.
Please post your actual code using code blocks. Please post your entire sketch.
If you want to be able to monitor the serial input continuously, then you need to get rid of the delay() calls and use millis() instead. The following tutorial should help:
Sorry I just saw "Using Arduino" and "Programming Questions" so I thought this would be the place to post it. If there is a better place to post the question I'll gladly post there..
The delays were in there to show whenever one of the pieces of code were running. Since they have 2 different delays I could differentiate which of the codes were running.
Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
Google “State Machine”.
String inputString = ""; // a String to hold incoming data
bool stringComplete = false;
bool light = true;
void setup() {
// put your setup code here, to run once:
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
//set LED Pins as Outputs
pinMode(13, OUTPUT);
pinMode(23, OUTPUT);
}
void parseCmd(){
if (inputString.startsWith("RA")){
Serial.print("RA ");
}
if (inputString.startsWith("VF")){
Serial.print("VF");
}
if (inputString.startsWith("GE")){
Serial.print("GE");
}
if (inputString.startsWith("GV")){
Serial.print("GV");
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '*') {
stringComplete = true;
}
}
}
void loop() {
// put your main code here, to run repeatedly:
serialEvent();
if (inputString.startsWith("RA")){
Serial.print("RA");
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
}
if (inputString.startsWith("VF")){
Serial.print("VF");
digitalWrite(23, HIGH);
digitalWrite(23, LOW);
}
}
I have a lot of stuff I need to get rid of because I was just trying a lot of different things to try and get it to work. LIke the "Bool light = true;" and the "pinMode(23, OUTPUT);"
I need to get this light to flash whenever new code starts. Preferable different speeds. Sorry for the so many replies with added stuff. I can create a new forum post about it to make it cleaner if people would like.
Your LED is only on for micro seconds.
Continue with this thread.
If that is conducive foam, suggest you not place you Arduino on it when powered.
It's not conductive foam. Alright I'll continue with this thread.
So whenever I type "RA*" into Serial Monitor it writes out "RA" but whenever I type "VF*" into Serial Monitor it doesn't stop "RA" and doesn't replace it with "VF"
It does however stop running whenever I press the button on the arduino board and then I'm allowed to type "VF*" and it will replace "RA" with "VF"
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '*') {
stringComplete = true;
}
Where is inputString reset ?
I don't know what that is.. My boss just gave me this program that he wrote and told me to write in "If "RA" is running and you type "VF" then make it to where "RA" stops and "VF" runs...
I'm very new to programming..
Do a Serial.print on inputString to see what is printed.
I'm sorry I don't quite understand what you mean..
We will take this one set at a time.
Here is your code with a Serial.print on variable inputString.
What gets printed on your serial monitor ?
String inputString = ""; // a String to hold incoming data
bool stringComplete = false;
bool light = true;
void setup()
{
// put your setup code here, to run once:
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
//set LED Pins as Outputs
pinMode(13, OUTPUT);
pinMode(23, OUTPUT);
}
void parseCmd()
{
if (inputString.startsWith("RA"))
{
Serial.print("RA ");
}
if (inputString.startsWith("VF"))
{
Serial.print("VF");
}
if (inputString.startsWith("GE"))
{
Serial.print("GE");
}
if (inputString.startsWith("GV"))
{
Serial.print("GV");
}
}
void serialEvent()
{
while (Serial.available())
{
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '*')
{
stringComplete = true;
}
}
Serial.println(inputString);
}
void loop()
{
// put your main code here, to run repeatedly:
serialEvent();
if (inputString.startsWith("RA"))
{
Serial.print("RA");
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
}
if (inputString.startsWith("VF"))
{
Serial.print("VF");
digitalWrite(23, HIGH);
digitalWrite(23, LOW);
}
}
When I type "RA*" on the Serial Monitor what comes up is "RARARARARARARARA" repeating forever.
Same thing with "VF"
See, now you have the input part working... in other words, the problem you mentioned in post #11 is solved...
Only when it starts, or do you mean continuously after it starts? There is a big difference. Have you looked at the Blink sketch that not only ships with the IDE, but is pre-loaded on almost every Arduino in existence? Usually the onboard LED pin can be referenced as LED_BUILTIN.
Like:
digitalWrite(LED_BUILTIN, HIGH);
My main problem is that while "RA" is running I need to be able to type "VF" and "RA" stops running and "VF" starts. I can't get that to happen without closing Monitor and reopening it.

