Category: Programming

  • The switch-case statement in the Arduino language

    The switch-case statement in the Arduino language

    When you want to branch on various conditions in Arduino, you probably write almost everything with if statements.
    But what do you do when there are 10 or 20 branches (rare 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 under case 1 runs.
    When var is 2, the code under case 2 runs.
    When var is anything other than 1 or 2, the default block 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 default block does not need a break; because default is the last branch, so it is obvious that it is the final block.
    That said, writing one is not forbidden—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.
    The syntax isn’t difficult at all, so give it a try.

  • I Don’t Really Know Much, But I Want to Program in Python! (1)

    I Don’t Really Know Much, But I Want to Program in Python! (1)

    When you start learning programming, I think the first hurdle is figuring out where to begin.
    You search online, but then what? What software are you even supposed to use to write your code?

    I struggled with exactly that myself.

    Broadly speaking, there are 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 correct terminology, though…)

    With the interactive type, you type one line and get one response back
    —you write and run the program one line at a time, over and over.

    With the script type, it’s like writing a whole essay and then getting feedback on it
    —you run the entire program at once.
    This script type is what most people picture when they think of programming: page after page of cryptic-looking code.

    The problem is, when you set out to write a script-type program, what software should you actually use?

    On top of that, beginner-level programming lessons often use the interactive style, but at the intermediate level you’re suddenly writing script code, and what to write it in is either 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-type programs in?

    Most script-type programs are written in an IDE (integrated development environment). (When in doubt, searching “Python IDE recommended” will turn up plenty of articles.)

    The tools I normally use for writing script-type programs are
    Jupyter Notebook and PyCharm.
    (Apparently people are split on whether “Jupyter” is pronounced “joo-pih-ter” or “joo-py-ter”.)

    I use Jupyter Notebook for statistical analysis and plotting graphs, and PyCharm for driving hardware or writing more complex programs.

    Let me describe 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.

    A list of the files on your computer is then displayed.
    Open the folder you want, and click “New” in the upper right.
    Then click “Python 3” and a screen like the image below will open.
    Saving this file gives you a *.ipynb file.

    You write your code in individual cells, and by clicking the RUN button at the top you can execute the program cell by cell.

    Being able to run code one cell at a time is a huge advantage when making graphs or doing statistics.
    That’s why I use it so often.

    I use PyCharm when opening *.py files.
    Note that the two tools open different file types, so be careful.

    For PyCharm, please refer to other sites.
    (I haven’t used it much yet, so I plan to cover how to use it in detail in a future post.)

    The Anaconda site is here

    The Jupyter Notebook site is here

    The PyCharm site is here
    For the download, choose the gray Community edition rather than Professional—that’s the free one.
    I’d recommend starting with that.

    I’ve been using Jupyter Notebook a lot lately, so I plan to keep posting updates, both as notes on my own learning and for anyone who wants to learn to program.

  • Reflections After a Month of Learning to Program

    The thing I feel most while learning programming is that I have no idea whether I’m actually getting any better at it.

    For a long time I assumed programming was something like English—that as I studied, I would gradually become able to write it as fluently as a language.
    In practice, though, it didn’t really feel like that was happening.

    Programming is something you study when you’re forced to by necessity; conversely, you never really pick it up unless you use it.
    In my own case, I had analysis results from DeepLabCut, but the data was so massive that I needed some way to turn it into graphs that were easy to interpret.

    You can certainly plot graphs in Excel, but with that much data, just opening the file took forever.
    So I decided to use Python to process the data and generate the graphs, and I ended up writing the programs 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 googling for the specific operation I wanted to do.
    It was a constant cycle of figuring out what processing I wanted, searching for code that would do it, and typing it in.

    Given that, going back to my point that it wasn’t like learning a language: it felt more like always speaking 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 becomes an asset. I often find myself copying and pasting my own earlier code and tweaking it slightly when I need to do something similar.

    I’ve only just started programming, and as a bit of a bandwagon-jumper I may not be in any position to hold forth on this, but I hope it’s useful to anyone thinking of starting out.