Schedule‎ > ‎

Lab 06: Strings, Switch Statement and Ohm's Law

Objectives

  • Use functions in a program.
  • Use String variables in a program.
  • Use for-statements
  • Continue working with Ohm's law.

^ top

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You may NOT give a copy of your code to anyone else.
  • You must breadboard the circuits yourself.
  • You may NOT look at another student's code until you complete and submit this assignment.
  • You may get help from people if you get stuck, but only if they do not show or tell you the code to type.

^ top

Part 1: Month Names

In the project you work with Strings and substrings.

Specifications

  1. Start with a new sketch

    File > New

  2. Save the sketch using the name months.
  3. Write a sketch that transforms the numbers 1, 2, 3, ..., 12 into the corresponding month names January, February, March, ..., December.
  4. Use a Switch statement in your code.
  5. In your Canvas comment show the operation of your program for the following examples and at least two other original examples, with the program name as a heading.
    9
    5
    
  6. If the user enters an incorrect month number,  print an error message:  "invalid month number,  try again"
  7. Submit your months.ino file as part of this assignment.

Example Output

Enter the month number 9
Month 9 is September

Notes and Hints

  • The underlined font shows what is typed by the user in the text field at the top of the window.

^ top

Part 2: Ohm's Law Calculator

In this project you write a program that calculates voltage, amperes or resistance using Ohm's law.

Specifications

  1. Start with a new sketch

    File > New

  2. Save the sketch using the name ohms_law.
  3. Write a program that calculates voltage, amperes or resistance using Ohm's law.
  4. The program offers three menu choices, as shown in the example run below.
  5. Depending on the choice, the program asks the user for the two dependent variables and then calculates the value.
  6. Use double for your voltage, current and resistance variables.
  7. You can use if-else,  switch statement, or other method of your choice.
  8. Print an error message if the user enters an invalid menu choice:  "Invalid choice, try again"
  9. In your header show the operation of your program with an example for all three menu choices, with the program name as a heading.

    See the Example Output below for an example, though you must use different values.

  10. Submit your ohms_law.ino file as part of this assignment.

Example Output

1. Calculate voltage (V)
2. Calculate amperes (I)
3. Calculate resistance (R)
Enter the number of your choice 1

Calculating voltage using Ohm's law (V = IR)
Enter current in Amperes (I) 2
Enter resistance in Ohms (R) 10
For 2.00 Amps and 10.00 Ohms the voltage is 20.00 Volts.

1. Calculate voltage (V)
2. Calculate amperes (I)
3. Calculate resistance (R)
Enter the number of your choice 2

Calculating amperes using Ohm's law (I = V / R)
Enter the voltage (V) 10
Enter resistance in Ohms (R) 5
For 10.00 Volts and 5.00 Ohms the current is 2.00 Amps.

1. Calculate voltage (V)
2. Calculate amperes (I)
3. Calculate resistance (R)
Enter the number of your choice 3

Calculating resistance using Ohm's law (R = V / I)
Enter the voltage (V) 10
Enter current in Amperes (I) 2
For 10.00 Volts and 2.00 Amps the resistance is 5.00 Ohms.

Notes and Explanations

  • The underlined font shows what is typed by the user in the text field at the top of the window.

^ top

Style Requirements

Remember to follow all the style rules from previous assignments, as well as the new rules we recently covered, including:

  1. File comment block (see: File Comment Block)
  2. Descriptive and meaningful variable names (see: Use Meaningful Names)
  3. Using the variable naming conventions (see: Variable Names)
  4. Indentation inside curly braces (see: Indentation)
  5. Proper use of spaces around operators (see: Spacing Around Operators

^ top

Extra Credit

The following are worth extra credit points:

  1. Complete month names with no test conditions or if-statements (3 points)
    Hint: Make a very long string "January  February March ... ", in which you add spaces such that each month name has the same length. Then use substring() to extract the month you want.  Call this file month_substring.ino   You must also submit the original month file from Project A above.
  2. Extend the ohms_law project by adding a fourth menu option to calculate the resistor needed for an LED resistor. (5 points)
    A. Save the extended project as ohms_lawXC and turn in the ohms_lawXC.ino file.
    B. Read the tutorial to discover the equation: LED Current Limiting Resistors
    C. The user inputs are:
    1. Power supply voltage
    2. Diode forward voltage drop
    3. Diode forward current in Amps


^ top

Grading Criteria (20)

Grading:  Part 1 10 points  (Header 1pt, Compiles 2 pts, Proper Formatting 1 pt,  Works as Specified 6 pts)
                Part 2 10 points  (Header 1pt, Compiles 2 pts, Proper Formatting 1 pt, Works as Specified 6 pts)
                Extra Credit:  3 and/or 5 points

Submit your files to Canvas

Complete Code for Month Names:
#include <ArduinoSTL.h>
using namespace std;

void setup() {
  Serial.begin(9600);
  cout << "Enter the month number: " << endl;

}

void loop() {
  while (Serial.available()) {
    int month = 0;
    String monthString = "";
    bool badEntry = false;
    cin >> month;
    while (Serial.available())  {  //put this after your cin statement
      Serial.read();
    }
    switch (month) {
      case 1:
        monthString = "January";
        break;
      case 2:
        monthString = "February";
        break;
      case 3:
        monthString = "March";
        break;
      case 4:
        monthString = "April";
        break;
      case 5:
        monthString = "May";
        break;
      case 6:
        monthString = "June";
        break;
      case 7:
        monthString = "July";
        break;
      case 8:
        monthString = "August";
        break;
      case 9:
        monthString = "September";
        break;
      case 10:
        monthString = "October";
        break;
      case 11:
        monthString = "November";
        break;
      case 12:
        monthString = "December";
        break;
      default:
        cout << "invalid month number entered,  try again" << endl;
        badEntry = true;
        break;
    }
    if (!badEntry)  {
      cout << "Month " << month << " is ";
      Serial.println(monthString);
    }
  }

Complete Code for Month Names Extra Credit: 
#include <ArduinoSTL.h>
using namespace std;

void setup() {
  Serial.begin(9600);
  cout << "Enter the month number: " << endl;

}

void loop() {
  while (Serial.available()) {
    int month = 0;
    String monthString = "";
    bool badEntry = false;
    cin >> month;
    String monthNames = "January  February March    April    May      June     July     August   SeptemberOctober  November December ";
    while (Serial.available())  {  //put this after your cin statement
      Serial.read();
    }
    cout << "Month " << month << " is ";
    Serial.println(monthNames.substring((month-1)*9,month*9));
  }
}

Complete Code for Ohm's Law:
#include <ArduinoSTL.h>
using namespace std;

void setup() {
  Serial.begin(9600);
  cout << "1. Calculate voltage (V)" << endl;
  cout << "2. Calculate amperes (I)" << endl;
  cout << "3. Calculate resistance (R)" << endl;
  cout << "Enter the number of your choice:" << endl;
}

void loop() {
  while (Serial.available()) {
    int choice = 0;
    cin >> choice;
    double current = 0;
    double voltage = 0;
    double resistance = 0;
    while (Serial.available())  {  //put this after your cin statement
      Serial.read();
    }
    if (choice == 1) {
      cout << "Calculating voltage using Ohm's law (V = IR)" << endl;
      cout << "Enter current in Amperes (I):" << endl;
      cin >> current;
      cout << "Enter resistance in Ohms(R):" << endl;
      cin >> resistance;
      cout << "For " << current << " Amperes and " << resistance << " Ohm the voltage is " << current * resistance <<  endl;
    }  else if (choice == 2) {
      cout << "Calculating amperes using Ohm's law (I = V / R)" << endl;
      cout << "Enter voltage (V) in volts:" << endl;
      cin >> voltage;
      cout << "Enter resistance in Ohms(R):" << endl;
      cin >> resistance;
      cout << "For " << voltage << " volts and " << resistance << " Ohm the current is " << voltage / resistance << " Amps" << endl;
    }  else if (choice == 3) {
      cout << "Calculating resistance using Ohm's law (R = V / I)" << endl;
      cout << "Enter voltage (V) in volts:" << endl;
      cin >> voltage;
      cout << "Enter current in Amperes (I):" << endl;
      cin >> current;
      cout << "For " << voltage << " volts and " << current << " Amps the resistance is " << voltage / current << " Ohms" << endl;
    }  else  {
      cout << "invalid choice, try again" << endl;
    }
    cout << endl;
  }
}

COMPLETED CODE FOR OHM'S EXTRA CREDIT:
#include <ArduinoSTL.h>
using namespace std;

void setup() {
  Serial.begin(9600);
  cout << "1. Calculate voltage (V)" << endl;
  cout << "2. Calculate amperes (I)" << endl;
  cout << "3. Calculate resistance (R)" << endl;
  cout << "4. Calculate resistor needed for an LED resistor" << endl;
  cout << "Enter the number of your choice:" << endl;
}

void loop() {
  while (Serial.available()) {
    int choice = 0;
    cin >> choice;
    double current = 0;
    double voltage = 0;
    double resistance = 0;
    double diodeForwardVoltageDrop = 0;
    double diodeForwardCurrent = 0;
    double resistorNeeded = 0;
    while (Serial.available())  {  //put this after your cin statement
      Serial.read();
    }
    if (choice == 1) {
      cout << "Calculating voltage using Ohm's law (V = IR)" << endl;
      cout << "Enter current in Amperes (I):" << endl;
      cin >> current;
      cout << "Enter resistance in Ohms(R):" << endl;
      cin >> resistance;
      cout << "For " << current << " Amperes and " << resistance << " Ohm the voltage is " << current * resistance <<  endl;
    }  else if (choice == 2) {
      cout << "Calculating amperes using Ohm's law (I = V / R)" << endl;
      cout << "Enter voltage (V) in volts:" << endl;
      cin >> voltage;
      cout << "Enter resistance in Ohms(R):" << endl;
      cin >> resistance;
      cout << "For " << voltage << " volts and " << resistance << " Ohm the current is " << voltage / resistance << " Amps" << endl;
    }  else if (choice == 3) {
      cout << "Calculating resistance using Ohm's law (R = V / I)" << endl;
      cout << "Enter voltage (V) in volts:" << endl;
      cin >> voltage;
      cout << "Enter current in Amperes (I):" << endl;
      cin >> current;
      cout << "For " << voltage << " volts and " << current << " Amps the resistance is " << voltage / current << " Ohms" << endl;
    }  else  if (choice == 4) {
      cout << "Calculating resistor needed for an LED resistor." << endl;
      cout << "Enter voltage (V) in volts:" << endl;
      cin >> voltage;
      cout << "Enter diode forward voltage drop in volts:" << endl;
      cin >> diodeForwardVoltageDrop;
      cout << "Enter diode forward current in Amps:" << endl;
      cin >> diodeForwardCurrent;
      cout << "For " << voltage << " volts and " << diodeForwardVoltageDrop << " Diode Forward Voltage Drop and ";
      cout << diodeForwardCurrent << " amps Diode Forward Current: " << endl;  
      resistorNeeded = (voltage - diodeForwardVoltageDrop)/diodeForwardCurrent;
      cout << "the resistor needed for an LED resistor is " << resistorNeeded << " Ohms" << endl;
    }  else  {
      cout << "invalid choice, try again" << endl;
    }
    cout << endl;
  }
}



Comments