When you want to branch on various conditions in Arduino, you probably write almost everything with if statements. But what should you do when you have 10 or 20 branches (unlikely as that may be)?
C/C++ has a switch statement, and since the Arduino language is based on C/C++, you can use the same syntax.
Here is an example program.
switch (var) {
case 1:
// varが1のときに実行する処理
break;
case 2:
// varが1のときに実行する処理
break;
default:
// どのcaseラベルにも一致しないときに実行する処理
// defaultは、省略可能
}
Here var is a variable: when var is 1, the code in case 1 runs. When var is 2, the code in case 2 runs. When var is anything other than 1 or 2, the default code runs.
The “break;” after the code in case 1 and case 2 indicates that the processing for that case has finished. Without it, you can run into errors.
The reason break; is not needed in the default block is that default is the last branch, so it is obvious that it is the final block of code. That said, there is no rule against writing it—the program works fine either way.
You can also omit default: itself. It feels much like omitting the else in an if-else statement.
Personally, I find switch-case statements look tidier than if statements when you read the code. It is not particularly difficult code, so give it a try.
When you start learning programming, I think the first hurdle is figuring out where to begin. You search online, but then what? And what software are you supposed to use to write the code?
I struggled with exactly that myself.
There are broadly two kinds of tools for writing programs. One is interactive, and the other is script-based. (I’m putting it this way for clarity; I’m not entirely sure it’s the technically correct terminology…)
With the interactive type, you type one line and get one response back —you write and run the program line by line, over and over.
The script-based type is more like writing a whole essay and then getting feedback on it—you run the entire program at once. What most people picture when they think of a program is the script-based type, which looks like a long list of incomprehensible code.
But when you decide to write a script-based program, the problem becomes: which software do you actually use?
On top of that, beginner-level programming lessons often use the interactive style, and then at the intermediate level you’re suddenly writing scripts—yet what to write them in is often left unstated, or differs completely from book to book.
For the interactive style, you use something already built into your computer, like Command Prompt or Terminal, and get started by typing “Python”.
But what do you write script-based programs in?
Most script-based programs are written in an IDE (integrated development environment). (When you’re stuck, searching for “Python IDE recommendations” turns up plenty of articles.)
The ones I normally use for writing script-based programs are Jupyter Notebook and PyCharm. (Apparently some people pronounce “Jupyter” as “joo-pih-ter” and others as “joo-py-ter”.)
I use Jupyter Notebook for statistical analysis and plotting graphs, and PyCharm for controlling hardware and for more complex programs.
Let me explain what each one is like.
First, Jupyter Notebook. It comes bundled when you download Anaconda. When you launch Jupyter Notebook, a browser such as Safari or Firefox opens first.
Then a listing of the files on your computer appears. Open the folder you want, and click “New” in the upper right. Then click Python3, and a screen like the image below opens. Saving this file gives you a *.ipynb file.
You write code into individual cells, and by clicking the RUN button at the top you can execute the program one cell at a time.
Being able to run code cell by cell is a huge advantage when working with graphs and statistics. That’s why I use it so often.
I use PyCharm for opening *.py files. Note that the two tools open different types of files.
For PyCharm, please refer to other sites. (I haven’t used it much yet, so I’d like to cover how to use it in detail in a future post.)
The PyCharm site is here For the download, choose the gray Community edition rather than Professional—that’s the free one. I’d recommend downloading that one to start with.
Since I’ve been using Jupyter Notebook a lot lately, I plan to keep posting updates as notes on my own learning and for anyone who wants to learn programming.
The thing I feel most strongly while learning to program is that I have no idea whether I’m actually getting any good at it.
For a long time I thought programming was something like English: that as I studied, I would gradually be able to write it as fluently as a language. In practice, though, it never really felt like that was happening.
Programming feels like something you study when you’re forced to by necessity, which is to say you never really pick it up unless you use it. In my own case, I had analysis results from DeepLabCut, but the data were so massive that I needed to find some way to turn them into easy-to-read graphs.
You can certainly make graphs in Excel, but with data this large it took forever just to open the file. So I decided to use Python to process the data and plot it, and set about writing the program while learning as I went.
Once I started writing code, I found that writing the code itself wasn’t all that hard—what was hard was searching Google for the specific operation I wanted to do. It was a constant cycle of figuring out what processing I wanted, looking up code that would do it, and typing it in.
Given all this, going back to my earlier point that programming isn’t quite like a spoken language: it’s more like always talking with a Japanese-English dictionary in hand. You can do just about anything if you spend enough time on it, but experience is what changes how fast you can look things up and how elegant your code becomes.
Also, the code you write yourself becomes an asset—I often find myself copying and pasting my own code and tweaking it slightly when I need to do something similar.
I’ve only just started programming and I’m something of a bandwagon type, so maybe I’m not really in a position to hold forth on this, but I hope it’s useful to anyone thinking of starting out.