HELP! How to choose steps i by one by ? !!!!!!!!!

Hello everyone

I have 5 steps and each 5 steps has its own 5 steps. For example;

Step1
x
x^2
x^3
x^4
x^5
.
.
.
.
.
Step5
x
x^2
x^3
x^4
x^5

And for every each Steps (1,2..,5) i have input from serial like;

1 = 65 0 1 0 ffff ffff ffff
2 = 65 0 2 0 ffff ffff ffff
.
.
5 = 65 0 5 0 ffff ffff ffff

And for every inner Steps (x,x^2,...,x^5) i have input frome serial like;

for Step1 ( x ) = 65 1 1 0 ffff ffff ffff
for Step1 ( x^2 ) = 65 1 2 0 ffff ffff ffff
.
.

for Step2 ( x ) = 65 2 1 0 ffff ffff ffff
for Step2 ( x^2) = 65 2 2 0 ffff ffff ffff
.
.
.
.
for Step5(x^5) = 65 5 5 0 ffff ffff ffff

So i have no problem about reading serial and processing. What i need to do is choosing these steps by one by and at the end they should printed all together.

For example, first i'll choose Step1, in Step1, x^3. second i'll choose Step2, in Step2, x^2 and x^4. It goes like that till the have serial input ( 65 0 6 0 ffff ffff ffff ). After i got this serial input, i need to see on the Serial monitor what i choose.

Should i save the results in Array ?, do i need to use interrupts ? or etc... I need an idea. How can i solve this problem ?

Thank You

/*Code i wrote.*/
/**********************************************************************/

/* Program gets data from Nextion Touch Screen and processing */
/**************************************************************/

#include <SoftwareSerial.h>
#include <Nextion.h>

SoftwareSerial nextion(2, 3);           // Nextion Tx to 3, Rx to 2
Nextion myNextion(nextion, 9600);       // Create a Nextion object named myNextion using the nextion serial port @ 9600bps

int x = 5;                              // Number that powered
int stp1,stp2,stp3,stp4,stp5;           // Steps Results

void setup() {
Serial.begin(9600);                   // Start serial communication 
myNextion.init();                     // Initialize nextion

} 

void loop() {


 String message = myNextion.listen();               // Check for message
 if(message != ""){                                // If a message is received
  Serial.println(message);  
   
}
}



/* Functions*/
/**********************************************************/



/*IF STEP 1 CHOOSEN */

int step1(){      
  String message = myNextion.listen();            // Check for message
  if(message != ""){                              // If a message is received
   Serial.println(message);                       // Print it out

  if(message == "65 1 1 0 ffff ffff ffff"){       //Pow1
  return  (pow(x,1));
  }
 
  else if(message == "65 1 2 0 ffff ffff ffff"){  //Pow2 
  return  (pow(x,2));
  }

  else if(message == "65 1 3 0 ffff ffff ffff"){   //Pow3
  return  (pow(x,3));
  }
 
  else if(message == "65 1 4 0 ffff ffff ffff"){   //Pow4
  return  (pow(x,4)); 
  }

  else if(message == "65 1 5 0 ffff ffff ffff"){  //Pow5
  return  (pow(x,5));    
  }
 }
}


/*IF STEP 2 CHOOSEN */

int step2(){
  String message = myNextion.listen();            // Check for message
  if(message != ""){                              // If a message is received
   Serial.println(message);                       // Print it out

  if(message == "65 2 1 0 ffff ffff ffff"){       //Pow1
  return  (pow(x,1));
  }
 
  else if(message == "65 2 2 0 ffff ffff ffff"){  //Pow2 
  return  (pow(x,2));
  }

  else if(message == "65 2 3 0 ffff ffff ffff"){   //Pow3
  return  (pow(x,3));
  }
 
  else if(message == "65 2 4 0 ffff ffff ffff"){   //Pow4
  return  (pow(x,4)); 
  }

  else if(message == "65 2 5 0 ffff ffff ffff"){  //Pow5
  return  (pow(x,5));    
  }
 }
}


/*IF STEP 3 CHOOSEN */

int step3(){
  String message = myNextion.listen();            // Check for message
  if(message != ""){                              // If a message is received
   Serial.println(message);                       // Print it out

  if(message == "65 3 1 0 ffff ffff ffff"){       //Pow1
  return  (pow(x,1));
  }
 
  else if(message == "65 3 2 0 ffff ffff ffff"){  //Pow2 
  return  (pow(x,2));
  }

  else if(message == "65 3 3 0 ffff ffff ffff"){   //Pow3
  return  (pow(x,3));
  }
 
  else if(message == "65 3 4 0 ffff ffff ffff"){   //Pow4
  return  (pow(x,4)); 
  }

  else if(message == "65 3 5 0 ffff ffff ffff"){  //Pow5
  return  (pow(x,5));    
  }
 }
}


/*IF STEP 4 CHOOSEN */

int step4(){
  String message = myNextion.listen();            // Check for message
  if(message != ""){                              // If a message is received
   Serial.println(message);                       // Print it out

  if(message == "65 4 1 0 ffff ffff ffff"){       //Pow1
  return  (pow(x,1));
  }
 
  else if(message == "65 4 2 0 ffff ffff ffff"){  //Pow2 
  return  (pow(x,2));
  }

  else if(message == "65 4 3 0 ffff ffff ffff"){   //Pow3
  return  (pow(x,3));
  }
 
  else if(message == "65 4 4 0 ffff ffff ffff"){   //Pow4
  return  (pow(x,4)); 
  }

  else if(message == "65 4 5 0 ffff ffff ffff"){  //Pow5
  return  (pow(x,5));    
  }
 }
}

/*IF STEP 5 CHOOSEN */

int step5(){
  String message = myNextion.listen();            // Check for message
  if(message != ""){                              // If a message is received
   Serial.println(message);                       // Print it out

  if(message == "65 5 1 0 ffff ffff ffff"){       //Pow1
  return  (pow(x,1));
  }
  
  else if(message == "65 5 2 0 ffff ffff ffff"){  //Pow2 
  return  (pow(x,2));
  }

  else if(message == "65 5 3 0 ffff ffff ffff"){   //Pow3
  return  (pow(x,3));
  }
  
  else if(message == "65 5 4 0 ffff ffff ffff"){   //Pow4
  return  (pow(x,4)); 
  }
  
  else if(message == "65 5 5 0 ffff ffff ffff"){  //Pow5
  return  (pow(x,5));    
  }
 }
 
}

/*void callFunctions()
{
  String message = myNextion.listen();            // Check for message
  if(message != ""){                              // If a message is received
   Serial.println(message);                       // Print it out

  if(message == "65 0 1 0 ffff ffff ffff"){       //Pow1
  stp1 = step1();
  Serial.print(stp1);
  Serial.println(" ");
  }
  
  else if(message == "65 0 2 0 ffff ffff ffff"){  //Pow2 
  stp2 = step2();
   Serial.print(stp2);
   Serial.println(" ");
  }

  else if(message == "65 0 3 0 ffff ffff ffff"){   //Pow3
  stp3 = step3();
   Serial.print(stp3);
   Serial.println(" ");
  }
  
  else if(message == "65 0 4 0 ffff ffff ffff"){   //Pow4
  stp4 = step4();
   Serial.print(stp4);
   Serial.println(" ");
  }
  
  else if(message == "65 0 5 0 ffff ffff ffff"){  //Pow5
  stp5 = step5(); 
   Serial.print(stp5);
   Serial.println(" ");  
  }
 }
 
}*/

_5step.ino (5.31 KB)

Please read this first: http://forum.arduino.cc/index.php/topic,148850.0.html

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long to study quickly without copying to a text editor.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

I can't help feeling that your overall approach to the problem needs to be reconsidered.

If you need to send 5 pieces of data for each option why not send 6 pieces of data all at once like
<2,5,67,83,0,121>
and parse it so that the first item represents the group and the other 5 items are the data for the group.

...R

Some problems with your code, in addition to reply #2:

Every non-void function must return a value. Add a default value if none of the conditions holds.

You use x without assigning a value to it.

You process different messages in loop() and in your step functions.

Sorry about the rules.

Robin2, i am using Nextion touch screen, each time i can send only 1 data which is in form of String (like: 65 5 5 0 ffff ffff ffff) and i will send 11 datas like that. I have to save them in to Array but the only part that is changing is 3. and 5. index.

As an example, i'll touch the step1 and then x^2. Then i'll touch the step2 and then X^3 for example. It'll end after finishing step5. After that, i'll touch the Start button and will see all the results.Example Output:

Step1 X = 5
Step2 X^2 = 25
.
.
.
.

Sorry, but I still don't understand exactly what you are trying to send. I can't relate your references to "step1" and "X^2" to the string example "65 5 5 0 ffff ffff ffff"

Are you saying that there are a series of buttons on your touch screen with captions like "step1", "step2", "X^2" and "X^3". Maybe if you post a photo of the touch screen it will be more obvious.

If you need to make 11 different screen-presses to send 11 items of data how can you ensure that you don't make a mistake. Or, if you do, how will the Arduino recognize it as a mistake and know to ignore it?

Please explain the overall purpose of the project.

...R

This is Touch Screen Main Page. Step1,Step2,Step3,Step4,Step5 are 5 different DC Motors.

** (Screenshot by Lightshot)

When you touch the Step1, options are opened and these buttons for the speed control.

** (Screenshot by Lightshot)

So, first i choose Step1 (motor1). After, i choose its speed. Then return back to main page.
Second, i choose Step2 (motor2). After, i choose its speed. Then return bakc to main page.

I'll continue up to Step5 (motor5).

Basically,i'll choose first Step(Motor) and then its' speed (X,X^2,.....X^5). Between these steps, motors should'nt move. After i choose the Motor5 and its' speed, i'll touch the START button and all of them should start at the same time.

Now, the each button on the screen has its own adress. For exampe, when i touch step1 arduino reads
(65 0 1 0 ffff ffff ffff ) this. After, when i touch X, it reads ( 65 1 1 0 ffff ffff ffff ). Then for step2 (65 0 2 0 ffff ffff ffff ) and
x^3 (65 3 2 0 ffff ffff ffff ). So i'll get these information in the order of, for example, Step1 - X, Step2 - X^4, ............Step5 - X^2. Basically choose motor first then its' speed. Do it for the 5 different motors.

I can get all buttons' information from serial monitor. What should i do next ? In my oppinion, I need to save these datas in to Array. Like in Array, index 0 for Step1,1 for Step2,....4 for Step5. Contents of Array may be Speeds. (X,X^2 .... X^5).

What do you think ? How can i handle this ?

Thank you.

Thanks for the pictures - that's a big help.

May I ask some follow-up questions ...
Wouldn't it be easier to label the buttons "motor1" rather than "step1"?
Or does "step" have some significance that you have not told us?

Am I correct to think that, for each step (motor) you MUST press twice - once to select the motor and once to select the speed.

Is it essential to do this for all 5 steps on every occasion?

The style looks like it may be a program you wrote yourself? Is that correct?
If so I suggest you modify it so that it only sends data to the Arduino when you have finished all your button presses. Sending data to the Arduino in small pieces makes it very difficult to ensure that the parts that need to be taken together remain together.

You still have not explained what the "65 0 1 0 ff ff ff ff ff ff" means. ( I have added spaces)
Are they all hex values or is it a mix of decimal and hex
Also, are these the values of 10 bytes, or are they ascii characters that are sent. or what are they?

...R

Now it makes sense to me. Your code structure is okay for such an application, except the details mentioned before.

You can simplify coding by writing one function, that retrieves and decodes a message, like this

String message; //the last message
char msg3, msg5; //the values parsed from a message
bool getMsg() {
 message = myNextion.listen();            // Check for message
 if(message == "") return false;             // If no message is received
 msg3 = message[3];
 msg5 = message[5];
 return true; //message received and decoded
}

I do not know why i wrote there Step1 instead of Motor1. I'll change it.

Yes, for each motor, we have to touch twice to screen, choosing motor and its' speed.

I have 5 DC motor. They both work together in the project.

Hello Robin2

I did not write any progrom for the Interface. There is a GUI editor for the NEXTION Touch Screen. You can only click what you want to create on your screen. Like below picture:

( Screenshot by Lightshot )

Like in the picture, for each button you press, you send the ID/Adress of it to Arduino. For example;

"65 0 1 0 ffff ffff ffff". 65 stands for : The Button is touched.
0 stands for : The Button touched in Page 0
1 stands for : The Button ID is 1

They are HEX values and coming as String which means that i can easily use. The rest is i do not know. But they always constant. Never change. Because i did not write the program, i cannot send the information all together at the end of my job. I will select motor1, it will send the Adress, i will select its' speed and it will send the Adress. Just like that.

Motor1 << Press << Speed << Press
Motor2 << Press << Speed << Press
.
.
.
Motor5 << Press << Speed << Press

START << Press

First i'll have all information, keeping all of them in Memory, then process it. I think it might be like that.

What do you think ?

Thank You.

Hello DrDiettrich thank you,i'll try this.

Help us by making the pictures visible in your own Post. See this Image Guide

...R

That's a very inconvenient system. It's author should be sent back to school.

Have you a link to the documentation for the Nextion Arduino library.

Try writing a very simple Arduino program that just receives button data and displays the values on the Serial Monitor.

...R

Robin2:
That's a very inconvenient system. It's author should be sent back to school.

It's a configurable event-driven GUI program. Nothing wrong so far.

DrDiettrich:
It's a configurable event-driven GUI program. Nothing wrong so far.

It seems to be unable to gather several connected pieces of data and send them all at once.

...R

I would opt for the approach by DrDiettrich in reply #8. One listen() to determine the motor (store in a variable) and one listen() to determine the speed.

Robin2:
It seems to be unable to gather several connected pieces of data and send them all at once.

...R

It just reports which button was pressed; makes sense to me.

Robin2:
It seems to be unable to gather several connected pieces of data and send them all at once.

That's the typical GUI approach: every user action triggers a message.
It doesn't make a big difference whether the message handler is implemented in the GUI program, or in another process.

Most probably the items can be configured, to also transmit a unique handle of the pressed button or menu item.

Of course Processing offers more functionality in its GUI, provided that it runs on the controlling device. But look at the amount of code, required for the implementation of the same GUI and connectivity in Processing.