Go Down

Topic: Why isn't this case structure working....? (Read 1 time) previous topic - next topic

/*
* Serial control for stepper motors.
*/

#include <Stepper.h>

const int stepsPerRevolution = 2000;

Stepper stepper1(stepsPerRevolution, 8, 9);
Stepper stepper2(stepsPerRevolution, 10, 11);

void setup()
{
  Serial.begin(9600); //Set up Serial Library, 9600 bps

  stepper1.setSpeed(30);
  stepper2.setSpeed(30);
}

void loop()
{
  Serial.print("Which stepper motor would you like to select? [1 or 2]: \n \n");

  while(Serial.available() == 0);

  int motor = Serial.read()- '0';
 
  Serial.println(motor);

  int select;

  switch (motor)
  {

  case 1:
    Serial.flush();
   
    Serial.print("How many steps would you like to take? \n 1. 500 \n 2. 1000 \n 3. 1500 \n 4. 2000 \n 5. 3000 \n \n");
         
    while (Serial.available() == 0);

    select = Serial.read() - '0';
   
    Serial.println(select); //this is just so I can see if it is storing a number in select.

    switch(select)
    {
      case 1:
        stepper1.step(500);
        Serial.print("You have selected 1");
        break;
      case 2:
        stepper1.step(1000);
        break;
      case 3:
        stepper1.step(1500);
        break;
      case 4:
        stepper1.step(2000);
        break;
      case 5:
        stepper1.step(3000);
        break;
    }
    break;

  case 2:
    Serial.print("How many steps would you like to take? \n 1. 500 \n 2. 1000 \n 3. 1500 \n 4. 2000 \n 5. 3000 \n \n");
    while (Serial.available() > 0);
    select = Serial.read() - '0'+'36';

    switch(select)
    {
      case 1:
        stepper2.step(500);
        break;
      case 2:
        stepper2.step(1000);
        break;
      case 3:
        stepper2.step(1500);
        break;
      case 4:
        stepper2.step(2000);
        break;
      case 5:
        stepper2.step(3000);
        break;
    }
    break;
  }
 
  Serial.flush();
}

PaulS

Code: [Select]
Serial.flush();
Why? There are incredibly few situations where it makes sense to flush serial data.

There are two switch statements. Which one "isn't working"? What do you expect it to do? What does it do, instead?



Code: [Select]
Serial.flush();
Why? There are incredibly few situations where it makes sense to flush serial data.

There are two switch statements. Which one "isn't working"? What do you expect it to do? What does it do, instead?




I was just testing that out....

Well the code is supposed to start with a prompt for the user to insert a number 1 or 2 to symbolize which motor they wanted to use.

After that, it is supposed to prompt the user with another choice to choose how many steps they desire the selected motor to rotate.

The code breaks after the first case structure....

In the serial monitor it will show this:

Which stepper motor would you like to select? [1 or 2]:

1 "<== This is the printed input for the first case structure"
How many steps would you like to take?
1. 500
2. 1000
3. 1500
4. 2000
5. 3000

-35 "This is the printed input for the second case structure to determine the number of steps"
Which stepper motor would you like to select? [1 or 2]:


el_supremo

When you type an answer, you also have to type the Enter key but you aren't looking for, and ignoring, it. So after your first answer, the second question reads the newline character as your answer, subtracts '0' from it and then enters the switch with that value.
Instead of just this:
Code: [Select]
   while (Serial.available() == 0);
    select = Serial.read() - '0';


use this:
Code: [Select]
   while (Serial.available() == 0);
    select = Serial.read() - '0';
   // Read and ignore the enter key.
   while (Serial.available() == 0);
   Serial.read();


Pete


When you type an answer, you also have to type the Enter key but you aren't looking for, and ignoring, it. So after your first answer, the second question reads the newline character as your answer, subtracts '0' from it and then enters the switch with that value.
Instead of just this:
Code: [Select]
   while (Serial.available() == 0);
    select = Serial.read() - '0';


use this:
Code: [Select]
   while (Serial.available() == 0);
    select = Serial.read() - '0';
   // Read and ignore the enter key.
   while (Serial.available() == 0);
   Serial.read();


Pete

I tried it...it didn't work...all I got as the output was:

Which stepper motor would you like to select? [1 or 2]:

1
How many steps would you like to take?
1. 500
2. 1000
3. 1500
4. 2000
5. 3000

-35
Which stepper motor would you like to select? [1 or 2]:

-35
Which stepper motor would you like to select? [1 or 2]:


=\

James C4S

Make your life simpler, turn you case statements into:

case '1', case '2', etc.

That way any extra characters (such as those associated with new line) get ignored. 
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

el_supremo

Works for me. I get this output:
Code: [Select]

Which stepper motor would you like to select? [1 or 2]:

1
How many steps would you like to take?
1. 500
2. 1000
3. 1500
4. 2000
5. 3000

1
You have selected 1Which stepper motor would you like to select? [1 or 2]:

2
How many steps would you like to take?
1. 500
2. 1000
3. 1500
4. 2000
5. 3000

Which stepper motor would you like to select? [1 or 2]:


From this code:
Code: [Select]

/*
* Serial control for stepper motors.
*/

#include <Stepper.h>

const int stepsPerRevolution = 2000;

Stepper stepper1(stepsPerRevolution, 8, 9);
Stepper stepper2(stepsPerRevolution, 10, 11);

void setup()
{
  Serial.begin(9600); //Set up Serial Library, 9600 bps

  stepper1.setSpeed(30);
  stepper2.setSpeed(30);
}

void loop()
{
  Serial.print("Which stepper motor would you like to select? [1 or 2]: \n \n");

  while(Serial.available() == 0);

  int motor = Serial.read()- '0';
   while (Serial.available() == 0);
   Serial.read();

  Serial.println(motor);

  int select;

  switch (motor)
  {

  case 1:
    Serial.flush();
   
    Serial.print("How many steps would you like to take? \n 1. 500 \n 2. 1000 \n 3. 1500 \n 4. 2000 \n 5. 3000 \n \n");
         
    while (Serial.available() == 0);

    select = Serial.read() - '0';
   while (Serial.available() == 0);
   Serial.read(); 
    Serial.println(select); //this is just so I can see if it is storing a number in select.

    switch(select)
    {
      case 1:
        stepper1.step(500);
        Serial.print("You have selected 1");
        break;
      case 2:
        stepper1.step(1000);
        break;
      case 3:
        stepper1.step(1500);
        break;
      case 4:
        stepper1.step(2000);
        break;
      case 5:
        stepper1.step(3000);
        break;
    }
    break;

  case 2:
    Serial.print("How many steps would you like to take? \n 1. 500 \n 2. 1000 \n 3. 1500 \n 4. 2000 \n 5. 3000 \n \n");
    while (Serial.available() > 0);
    select = Serial.read() - '0'+'36';
   while (Serial.available() == 0);
   Serial.read();
    switch(select)
    {
      case 1:
        stepper2.step(500);
        break;
      case 2:
        stepper2.step(1000);
        break;
      case 3:
        stepper2.step(1500);
        break;
      case 4:
        stepper2.step(2000);
        break;
      case 5:
        stepper2.step(3000);
        break;
    }
    break;
  }

  Serial.flush();
}


Pete


Make your life simpler, turn you case statements into:

case '1', case '2', etc.

That way any extra characters (such as those associated with new line) get ignored. 


Change all of them into that format? Because I just tried, and it wouldn't work. :\

PaulS

Quote
Because I just tried, and it wouldn't work.

What did you try? What happened? "it wouldn't work" is pretty lame. It gives us NO information to go on.

James C4S


Change all of them into that format? Because I just tried, and it wouldn't work. :\

How much of your other code did you change? 

As Paul said, "it wouldn't work" doesn't tell us anything.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com


Quote
Because I just tried, and it wouldn't work.

What did you try? What happened? "it wouldn't work" is pretty lame. It gives us NO information to go on.


You're absolutely right, sorry. Just...never used the serial comm before for the arduino and I'm slightly confused about some of the syntax...but I did change all of the case structures where it would say for example:

case 1:
     stepper2.step(500);
     break;
   case 2:
     stepper2.step(1000);
     break;
   case 3:
     stepper2.step(1500);

Say:

case '1':
     stepper2.step(500);
     break;
   case '2':
     stepper2.step(1000);
     break;
   case '3':
     stepper2.step(1500);


James C4S

When asking for help (or replying to suggestions):

1.  Don't post sections of code.  Post everything.
2.  PLEASE use the code tags ("#" button in the editor) when posting the code.
3.  It is helpful to others to post how you solved a problem and not just "Got it!"
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up