Hey, I am trying to make a motor respond to Serial command. I want the user to stop the motor by typing "Stop". Start the motor with "Start". Switch rotation with "Switch" and adjust the speed by giving a value from 1 to 255.
'
while trying to stop the motor when it enters the if statement a problem occurred. Problem is it won't enter the if. I had my friend try it in Java, same code, worked.
I wonder if the strings exceed the SRAM.
I am thinking about using switch with cases for each command.
I want to take the number out of a string and then applying it to the motor. (since the speed value will arrive as a string.
This is the code where the problem is
String stopCommand = "Stop";
void setup() {
Serial.begin(9600);
}
void loop() {
while(Serial.available() > 0 ) {
String command = Serial.readString();
Serial.println(command); //it prints "Stop" here telling me that command is "Stop"
if (command.equalsIgnoreCase(stopCommand)){ // yet this does not work
Serial.println("I am here"); //and "I am here" does not print
}
Serial.println(command); //still prints "Stop" here telling me that command is still "Stop"
}
}
Here is the full code my code will run with.
const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 9;
const int directionSwitchPin = 4;
const int onOfSwitchStateSwitchPin = 5;
const int potPin = A0;
int onOfSwitchState = 0;
int previousOnOfSwitchState = 0;
int directionSwitchState = 0;
int previousDirectionSwitchState = 0;
int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;
String stopCommand = "Stop";
void setup(){
pinMode(directionSwitchPin, INPUT);
pinMode(onOfSwitchStateSwitchPin, INPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
Serial.begin(9600);
}
void loop(){
takeCommand(); //here is my function where the problem is atm.
onOfSwitchState = digitalRead(onOfSwitchStateSwitchPin);
delay(1);
directionSwitchState = digitalRead(directionSwitchPin);
motorSpeed = analogRead(potPin)/4;
if(onOfSwitchState != previousOnOfSwitchState){
if(onOfSwitchState == HIGH){
motorEnabled = !motorEnabled;
}
}
if (directionSwitchState != previousDirectionSwitchState) {
if (directionSwitchState == HIGH) {
motorDirection = !motorDirection;
}
}
if (motorDirection == 1) {
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
}
else {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
}
if (motorEnabled == 1) {
analogWrite(enablePin, motorSpeed);
}
else {
analogWrite(enablePin, 0);
}
previousDirectionSwitchState = directionSwitchState;
previousOnOfSwitchState = onOfSwitchState;
}
void takeCommand ()//determins recived command
{
while(Serial.available() > 0 ) {
String command = Serial.readString();
Serial.println(command);
if (command.equalsIgnoreCase(stopCommand)){
Serial.println("I am here");
}
Serial.println(command);
}
}
The serial input basics tutorial shows how to do robust non blocking (readStrin() blocks) serial communication using null terminated character arrays (strings).
Karma for a well done first post. Most new members fail to read the guidelines. Very frustrating.
groundFungus:
In the serial monitor, set line endings to no line ending and try your first code.
"Stop" is not equal to "Stop/r/n" or "Stop/n".
Thanks that worked. Now, this is part of a assignment and the tutor will probably not change from newline to none. Is there a way to make it work with newline? I tried changing stopCommand string to "Stop/n". Yet when I typed "Stop" into Serial it did not print "I am here" but printed the "Stop" with a newline inbetween. (want to add a picture to show, but don't know how to add a jpg)
The serial input basics tutorial shows how to do robust non blocking (readStrin() blocks) serial communication using null terminated character arrays (strings).
Karma for a well done first post. Most new members fail to read the guidelines. Very frustrating.
No I am not aware. Saw something about it in the "Read before you post", but thank you for providing links
NorwegianElektroStudent:
Thanks that worked. Now, this is part of a assignment and the tutor will probably not change from newline to none. Is there a way to make it work with newline? I tried changing stopCommand string to "Stop/n". Yet when I typed "Stop" into Serial it did not print "I am here" but printed the "Stop" with a newline inbetween. (want to add a picture to show, but don't know how to add a jpg)
Found out the jpg :). Tried combining two replies here. Not sure if it will work
I do not use the String class, but a look at the reference has the trim() function. Maybe that would lop off the line endings?
That said, maybe your tutor would be impressed if you write the code so that it does not block and, perhaps, crash at random times for no apparent reason?
NorwegianElektroStudent:
Hey, I am trying to make a motor respond to Serial command. I want the user to stop the motor by typing "Stop". Start the motor with "Start". Switch rotation with "Switch" and adjust the speed by giving a value from 1 to 255.
If you send, for example, 'S' to start and 'D' (direction) to change direction it will make the Arduino code very much simpler.
groundFungus:
I do not use the String class, but a look at the reference has the trim() function. Maybe that would lop off the line endings?
That said, maybe your tutor would be impressed if you write the code so that it does not block and, perhaps, crash at random times for no apparent reason?
Yes, that would probably be smart. Thanks, I'l give it a try without String class.
Robin2:
If you send, for example, 'S' to start and 'D' (direction) to change direction it will make the Arduino code very much simpler.
...R
Yes, we have used this method of talking through Serial. It is on my own will I want it to be whole words to challange myself and learn new methods :). Yet thanks anyway
I tried changing stopCommand string to "Stop/n". Yet when I typed "Stop" into Serial it did not print "I am here" but printed the "Stop" with a newline inbetween. (want to add a picture to show, but don't know how to add a jpg)
When I run your code with
String stopCommand = "Stop\n";
and change the Serial monitor setting to "newline" I see
Stop
I am here
Stop
Download the modified code again, and verify that your monitor settings are correct.