Learn HERO Coding - No Extra Components Required

Will Sally be walking home?

The Bus Fare Dilemma

Sally Martinez stands at the corner of Cogsworth Avenue, her sneakers wet from the morning drizzle that's been falling since dawn. The old brass streetlights flicker against the gray sky, casting long shadows between the towering clockwork buildings of the industrial district. In her backpack, seven fresh apples from her family's orchard weigh heavy against her shoulders.

The Number 47 bus hisses to a stop at the corner, its steam-powered engine releasing a cloud of white vapor into the cool air. Through the fogged windows, Sally can see other students from Cogsworth Academy settling into the warm, dry seats. The bus driver, a gruff man with brass goggles pushed up on his forehead, peers down at her expectantly.

"Fare's $9.97 today, miss. Price of coal went up again."

Sally's fingers fumble in her empty pockets. She spent her last coins on lunch yesterday, and payday at the apple stand isn't until tomorrow. But those apples in her bag... Joe from the machine shop said he'd pay good money for fresh fruit. Bob from the gear works made the same offer. If she could sell them quickly, calculate the total, and figure out if it's enough...

The bus engine rumbles impatiently. Other passengers tap their pocket watches. Sally has maybe thirty seconds to run the math before the driver closes the doors and she's facing a long, cold walk home through Cogsworth's winding cobblestone streets.

Time to put your programming skills to work. Sally needs a program that can calculate her apple earnings in seconds, not minutes. Will she make it onto that warm bus, or will she be walking home in the rain?

What You'll Learn

When you finish this lesson, you'll be able to:

  • Combine multiple variables in mathematical calculations
  • Use different data types (integers and doubles) for precise calculations
  • Create conditional logic that makes decisions based on calculated results
  • Store and display different text responses using String variables
  • Build programs that solve real-world financial problems

Understanding Variables and Calculations

Think of variables as labeled boxes in your brain. When Sally counts Joe's apples, she puts that number in a mental box labeled "Joe's apples." When she counts Bob's apples, that goes in the "Bob's apples" box. Later, when she needs to know her total, she opens both boxes and adds the numbers together.

Your microcontroller works exactly the same way. Variables are storage containers with names you choose. You can put numbers, text, or other data inside them, then retrieve that data whenever you need it for calculations or decisions.

But here's where it gets interesting: not all numbers are created equal. When Sally counts apples, she deals with whole numbers (integers). But when she calculates the total price, she might get $8.40 or $12.75 (decimals). In programming, we use different variable types for different kinds of numbers. Integers handle whole numbers efficiently, while doubles handle decimal precision.

The real power comes when you combine these variables in calculations, then use the results to make decisions. Sally's microcontroller will calculate her earnings, compare that amount to the bus fare, and automatically decide which message to display. One calculation, multiple possibilities, instant results.

Setting Up the Variables

We'll start by defining our data and the messages Sally might hear:

int joesApples = 3;
int bobsApples = 4;
String busDriverAccept = "Welcome Aboard!";
String busDriverDeny = "Get Walking, bud.";

Notice how we're organizing our data logically. The apple counts are integers because you can't have half an apple. The bus driver's responses are String variables because they contain text, not numbers. By storing both possible messages up front, our program can choose the right response instantly when the calculation finishes.

Calculating Sally's Total

Inside setup(), we combine the apple counts and display the result:

void setup() {
  Serial.begin(9600);
  int sallysApples = bobsApples + joesApples;
  Serial.println(sallysApples);

The key insight here: sallysApples gets calculated once when the program starts. We're adding two existing variables together and storing the result in a new variable. This approach makes the code readable and lets us use the total in multiple places without recalculating.

Converting Apples to Money

Now we calculate Sally's earnings using decimal precision:

  double sallysMoney = sallysApples * 1.20;
  Serial.println(sallysMoney);