19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Let’s look at the computations. You calculate the <strong>to</strong>tal resistance (R <strong>to</strong>t<br />

)<br />

by adding the values of the three resis<strong>to</strong>rs <strong>to</strong>gether. Next, you calculate<br />

the current (I ) that flows in the circuit by dividing the battery voltage (V )<br />

by the <strong>to</strong>tal resistance. Then you calculate the voltage across each resis<strong>to</strong>r<br />

by multiplying the current by the value of that resis<strong>to</strong>r. (Try reading<br />

this paragraph aloud <strong>to</strong> your friends as if it’s super easy. It will blow their<br />

minds!)<br />

The following steps guide you through creating this program. So buckle<br />

your seat belt, hold on tight, and get set <strong>to</strong> rocket in<strong>to</strong> the exciting world of<br />

computer simulations.<br />

Step 1: Open the Startup File<br />

To start creating this circuit simula<strong>to</strong>r, open SeriesCircuit_Incomplete.sb from<br />

this chapter’s folder. The file contains comments that tell you where <strong>to</strong> add<br />

your code and empty placeholders for the subroutines you’ll write.<br />

The folder for this chapter also includes the two background images<br />

you’ll need: bkgndOff.bmp and bkgndOn.bmp (see Figure 12-5; we added the<br />

image names for clarity). The two images are the same except for the state<br />

of the switch: the switch is in the open position in bkgndOff.bmp but is closed<br />

in bkgndOn.bmp.<br />

bkgndOff.bmp<br />

bkgndOn.bmp<br />

Figure 12-5: The two background images of SeriesCircuit.sb<br />

When you start writing the code for this program, you’ll see many hardcoded<br />

numbers. These numbers represent the coordinate points for the<br />

text boxes and labels and for checking the switch’s boundaries. To help you<br />

understand where these numbers come from, refer <strong>to</strong> Figure 12-6. In this<br />

figure, we added coordinate axes and gridlines on <strong>to</strong>p of the background<br />

image, and we marked the coordinates of all the points you’ll use in the<br />

program.<br />

172 Chapter 12

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!