Lil problem ( i hope)

Hey,

Im trying to get some commands working with bleuthooth

there are 4 commands COM1, COM2,...

if i press the buttum ( on the app of my phone ) for COM1 it sends "COM1" so i can read it in the serial

the code checks the if the input is valid..

// Function to read available data from serial input buffer
//
void readData()
{
  while ( Serial.available() )         // Read while there are available characters
  {
    delay(3);
    char c = Serial.read();
    myString += c;                     // build the command string
  }
  Serial.print(myString);              // Output the full command string - for debugging
}
//-------------------------------------------------------------------------------------------
//
// Function to check the input data string for validity and extract the command character
//
void checkData()
{
  // Is the input data string 4 characters long, starting with sub-string "com" ?
  if ((myString.length() == 4) && (myString.startsWith("com") )) 
  {
    validCommand = true;
    command = myString[3];             // Get the command character
    Serial.print(" command is   ");
    Serial.println(command);
  }
  else  // input data string must contain at least 1 character, or we get stuck in a loop
  if ( myString.length() > 0)
  {
    validCommand = false;
    Serial.println("   command string is invalid");
  }
  myString = "";                        // Clear the input data string for next time
}
//-------------------------------------------------------------------------------------------
//
// Function to process command character and change stepper motor behaviour
//
void processCommand()
{
  justRun = false;
  stepper1.enableOutputs();              // enable the motor coils

  switch (command)
  {
  case '0':

the arduino can pick up the command, becouse it spontaniusly says :"command string is invalid"

any sugestions way (i already tryed CAPS and spacebars) plus the code compiles complete.

Thank you

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

Why are you sending complex messages like COM1, COM2 etc when A, B, C and D would be much easier to interpret on the Arduino and would convey exactly the same amount of information?

...R

    Serial.println(command);

What do you see printed ?

void readData()
{
  while ( Serial.available() )         // Read while there are available characters
  {
    delay(3);
    char c = Serial.read();
    myString += c;                     // build the command string
  }
  Serial.print(myString);              // Output the full command string - for debugging
}

You have to understand that the Arduino reads from Serial much much faster than the Serial comes in. So when the first character arrives, the if statement trips and you read it and then it goes back up to the top of this while before the second character has got there. Now you think you’ve received the whole string when you have only got one or two characters.

Please post complete code if that’s not it. It is really hard to help you from just random snippets of code without context. But I think that was your problem.

UKHeliBob:     Serial.println(command);

What do you see printed ?

 {
    validCommand = false;
    Serial.println("   command string is invalid");
  }

its says command string is invalid

Serial.println(command);

That line prints "command string is invalid"? I see the other print statement that definitely could print that. But are you saying that the line where you print out command does too?

No… im saying the adruino does not reconize the comanand com1

true its easier to keep commands with just 1 karakter. so im trying that now, a shame i don’t have that much time to test things, but keep ya posted…

btw

this is the app
https://play.google.com/store/apps/details?id=appinventor.ai_garylcyhk.Bluetooth_Stepper_Motor_Controller_21072016

the complete sketch is in the attachment

Control_stepper_motors_02082016_one_stepper_release.ino (15.6 KB)

No… im saying the adruino does not reconize the comanand com1

What you have NOT proven is that the command IS com1.

Serial.print("The crap in the memory wasting String instance is >");
Serial.print(command);
Serial.println("<");

If you get:

The crap in the memory wasting String instance is >com1<

you have one problem. If you get:

The crap in the memory wasting String instance is >com1
<

you have another problem.

We can NOT help you solve your problem until we KNOW what problem you have.

well nevermind… i always want something i just can’t have for whatever the reason is.

Rebelstudios: well nevermind... i always want something i just can't have for whatever the reason is.

So, you just wandered in here to waste our time. I'll remember that.

30 seconds to copy and paste the code I showed, run the damned program and copy and paste the results back here is NOT too much to ask of people that are looking for free help.

I tried but it did not compile anymore becouse i was cutting things out whitch i could not undo, i could upload the original version again but sinds i was fighting with this code for 2 days ...i got mad and a little frustrated. plus i dont remeber exactly where i got it.

This is whats happening when i open the sketch now, so i gave it a rest for 4 hours and started reading again....

C:\Users\Acer\Downloads\FDCSRG6INI7RRCU\FDCSRG6INI7RRCU.ino: In function 'void loop()':

FDCSRG6INI7RRCU:48: error: 'processCommand' was not declared in this scope

processCommand(); // Process command character and change stepper motor behaviour

^

C:\Users\Acer\Downloads\FDCSRG6INI7RRCU\FDCSRG6INI7RRCU.ino: At global scope:

FDCSRG6INI7RRCU:83: error: expected unqualified-id before '{' token

{

^

FDCSRG6INI7RRCU:87: error: expected unqualified-id before 'else'

else // input data string must contain at least 1 character, or we get stuck in a loop

^

FDCSRG6INI7RRCU:93: error: 'myString' does not name a type

myString = ""; // Clear the input data string for next time

^

FDCSRG6INI7RRCU:94: error: expected declaration before '}' token

}

^

exit status 1 'processCommand' was not declared in this scope

Rebelstudios: I tried but it did not compile anymore becouse i was cutting things out whitch i could not undo, i could upload the original version again but sinds i was fighting with this code for 2 days ...i got mad and a little frustrated. plus i dont remeber exactly where i got it.

Sounds like you have learned an essential lesson about programming the hard way.

Successful programming requires a careful systematic step-by-step approach. Your system sounds like how you would hack about with a billhook to cut brambles.

Make back-up copies of the code before you make more changes so you can easily revert to the older version if necessary.

The best advice I can offer at this stage is to start again from the beginning.

...R

I tried but it did not compile anymore becouse i was cutting things out whitch i could not undo, i could upload the original version again but sinds i was fighting with this code for 2 days ...i got mad and a little frustrated.

The "original" code was in the reply 4 up from the one with this rant.

You need to get a teddy bear to explain your code to. When something goes wrong, it's the teddy bear's fault, so you can whack it a good one, and get your frustrations out before coming in here an sounding like a whiny 12 year old.

Haha yeah you both got it wright, and thanks for the tip to get my original code back.. :confused:

The teddybear idee sounds good

Starting from the beginning is a plan and i will take the lessions first.

This morning, i ran into this code that works deliciously..... for 1 stepper

the app and the arduino communicate just fine and the commands are working and easy to change if i want.

This code is just a matter of adjusting it from 1 to 3 steppermotors.

Only i hope it will fit in de memory of the arduino becouse with 0nly 1 motor it says:

Sketch uses 16730 bytes (54%) of program storage space. Maximum is 30720 bytes. Global variables use 753 bytes (36%) of dynamic memory, leaving 1295 bytes for local variables. Maximum is 2048 bytes.

Rebelstudios: Only i hope it will fit in de memory of the arduino becouse with 0nly 1 motor it says:

Sketch uses 16730 bytes (54%) of program storage space. Maximum is 30720 bytes. Global variables use 753 bytes (36%) of dynamic memory, leaving 1295 bytes for local variables. Maximum is 2048 bytes.

From a very brief look, the huge size for a small task is because it is a pretty crap program. I have a program that communicates wirelessly and controls a a DC motor with a PID feedback loop which uses about 5k of program memory and 330 bytes for global variables.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

I need it for steppers ... What is a PID?

thanks for the tip of the capital S string i'll try to learn something up on that

for now it does not do any harm to the Arduino

What is a PID?

What does Mr. Google say ?

Rebelstudios: I need it for steppers ... What is a PID?

I only mentioned it to convey the notion that my program has a fair amount of work to do. PID is a feedback algorithm for controlling things (speed, in my case) without overshooting the control point.

thanks for the tip of the capital S string i'll try to learn something up on that

for now it does not do any harm to the Arduino

That's probably because your program has not be running long enough for the problem to accumulate. It's an accident waiting to happen when it is most inconvenient.

...R