Pages: [1]   Go Down
Author Topic: Why isn't this case structure working....?  (Read 939 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

/*
 * 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();
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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]:

Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1556
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   while (Serial.available() == 0);
    select = Serial.read() - '0';

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

Pete
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   while (Serial.available() == 0);
    select = Serial.read() - '0';

use this:
Code:
   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]:
 

=\
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6136
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Edison Member
*
Karma: 43
Posts: 1556
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Works for me. I get this output:
Code:
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:
/*
 * 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
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. :\
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6136
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it now!
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6136
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!"
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Pages: [1]   Go Up
Jump to: