0

I'm working on an experience design project for one of my classes using a rotary phone and arduino kit to create a game based on automated phone menus. Serial input from the rotary dial is running through arduino and now I am using processing to write the menu.

I have an outline of actions and have started to code some if then statements to get going but now I have stumbled upon case and switch.

I am completely new to this but have learned a lot in class.

My question is how do I make a continuous set of nested if/then statments OR use case and switch to move through a series of prompts and inputs?

Here is my sketch so far:

import processing.serial.*;

Serial port;  // Create object from Serial class
float val;    // Data received from the serial port

boolean task1prompted;
boolean task1;
boolean task2;
boolean dialed;

PFont font;

void setup() {
    size(800, 400);
    background(0, 0, 0);
    smooth();
    // IMPORTANT NOTE:
    // The first serial port retrieved by Serial.list()
    // should be your Arduino. If not, uncomment the next
    // line by deleting the // before it. Run the sketch
    // again to see a list of serial ports. Then, change
    // the 0 in between [ and ] to the number of the port
    // that your Arduino is connected to.
    //println(Serial.list());
    String arduinoPort = Serial.list()[0];
    port = new Serial(this, arduinoPort, 9600);

    task1 = false;
    task2 = false;
    task1prompted = false;

    font = createFont("Arial", 32);
    textFont(font, 32);
    textAlign(CENTER);
}

void draw() {
    if (port.available() > 0) { // If data is available,
        val = port.read();      // read it and store it in val
        if (val >= 48 && val <= 57) {
            val = map(val, 48, 57, 0, 9);  // Convert the value
        }
        println(val);
    }

    if (val == 97) {
        println("dialing");
    }

    if (val == 98){
        println("dialed");
        dialed = true;
    }

    /// switch will activate the task1 variable.
    // Play sound file for the prompt.
    if (task1prompted == false){
      delay(1000);
       println("for spanish, press one. for french, press 2...");
      task1prompted = true;
    }

    task1 = true;

    if (task1 == true && dialed == true) {
        ///play sound file

        if (val == 5) {
            println("Thank you for playing... Blah blah next prompt.");
            dialed = true;
            task1=false;
            task2=true;
        } else
            if (val != 5) {
            println("We're sorry, all of our international operators are busy");
            task1 = true;
            task2 = false;
            dialed = false;
        }

    }
    else
        if (task2 == true){
            delay(1000);
            println("task2 start");
        }
}

My instructor helped me to get this far and I have been scouring for answers on how to keep going on to the next task/prompt. Would it be easier to use case and switch? And am I even doing nested if statements the right way?

Well I just tried this out with sketch and case commands as follows:

    /// Switch will activate the task1 variable.
    //  Play sound file for the prompt.
    if (task1prompted == false){
        delay(1000);
        println("for spanish, press one. for french, press 2...");
        task1prompted = true;
    }

    task1 = true;

    if (task1 == true && dialed == true) {
        ///Play sound file

        int lang = (int)(val+0);

        switch(lang) {
            case 1:
            case 2:
            case 3:
            case 4:
                println("sorry no international operators");  // If 1-4 go back to choices
                task1 = true;
                task2 = false;
                dialed = false;
                break;
            case 5:
                println("thank you, move to next prompt");  // If 5 go to next prompt
                task1=false;
                task2=true;
                dialed = true;
                break;
            case 6:
            case 7:
            case 8:
            case 9:
            case 0:
                println("not a valid option, you lose");  // If not 1-5 go back to beginning
                task1=false;
                task2=false;
                dialed = true;
                break;
        }

        if (task2prompted == false){
            delay(1000);
            println("please listen while we test the line");
            task2prompted = true;
        }

        task2 = true;

        if (task2 == true && dialed == true) {
        } ///Play sound file

        int tone = (int)(val+0);

        switch(tone) {
          case 1:
          case 2:
          case 3:
          case 5:
          case 6:
          case 7:
          case 8:
          case 9:
          case 0:
            println("not a valid connection, you lose");  // If not 4 go back to beginning
            task2 = false;
            task3 = false;
            dialed = false;
            break;
          case 4:
            println("thank you, move to next prompt");  // If 4 move to next prompt
            task2=false;
            task3=true;
            dialed = true;
            break;
        }
    }
}

I'm still confused on how to make this have levels and not all happen simultaneously.

2 Answers 2

1

You might want to look up finite state machines. It's a pretty common approach to dealing with event driven user interfaces.

Sign up to request clarification or add additional context in comments.

1 Comment

If you google the terms Arduino and Finite State Machines, you'll find a lot of relevant answers. See playground.arduino.cc/Code/FiniteStateMachine for a library that may make your work easier.
0

I'm not entirely sure what your question is but maybe something here will answer it, if not just clarify what you're looking for

With case statements you don't need to make a case for every single output that can happen. the way you can avoid doing that is by using a default statement

Example:

    switch(tone) {
      case 4:
        println("thank you, move to next prompt");  // If 4 move to next prompt
        task2=false;
        task3=true;
        dialed = true;
        break;
      default:
        println("not a valid connection, you lose");  // If not 4 go back to beginning
        task2 = false;
        task3 = false;
        dialed = false;
    }

The default case doesn't need a break because it is at the end. But essentially it is the catch all case if nothing else is hit.

Also in some of your code above

if (val == 97) {
    println("dialing");
}

if (val == 98){
    println("dialed");
    dialed = true;
}

it is better to use an "else if" to make it not have to check through both if one is correct

if (val == 97) {
    println("dialing");
}

else if (val == 98){
    println("dialed");
    dialed = true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.