xChip Programming Basics - VCSFA-MARS/ThinSat-Program GitHub Wiki

Programming Basics with xChips

Now that we've installed the necessary drivers and libraries for the xChips, we will move into the basics of programming the chips.

Take a Tour of Arduino IDE

The main features we will be using have toolbar buttons on the IDE.

  • Verify - This button will check our program for errors and prepare the code for uploading.
  • Upload - This will upload your program. Once the upload is completed, the xChips will reset and automatically start running your code!
  • Serial Monitor - Allows you to interact with your xChips from your computer over a USB cable.
  • Program Editor Area - Where you write your code.
  • Message Area - Error messages and success messages from program verification and uploading are displayed here.


In the Arduino IDE programs are referred to as "sketches" or codes and are written in a variant of the C language. Sketches are essentially a list of specific instructions for our xSystem to follow--we can use sketches to display information on the LED, measure weather, detect light and much more!

To write these sketches, we must follow some rules in the code language, just like you need to follow rules in the English language. The Arduino IDE can read, understand, and upload these instructions to the xSystem.

The Basic Rules of C Language:

  1. Read from Top to Bottom

    The computer will go line by line, from top to bottom, and follow each instruction.

    There are exceptions to this that we will learn about later, but in general, your program instructions will be executed sequentially.

  2. Spelling Counts - Always!

    In order for the IDE to understand you, you have to spell things the same way every time, including capitalization and spaces. It means that print and Print would be two different instructions, and apple_pie is different than apple pie.

  3. Format Matters

    Every language has a syntax. This just means a set of rules for how to write the instructions, and how the computer will understand them. It's kind of like punctuation and grammar for programming. There aren't a lot of rules, but you have to follow them or the computer will get very confused.

    We will cover the specifics as we go, but some here are some key points:

    • Every instruction ends with a ; just like how sentences end in a period.

    • Every parentheses (), curly bracket {}, square bracket [], and quote "" must be closed. For example, every { needs eventually needs to be followed by a }. These symbols work to group lines of code together.

    • Any text following // is ignored by the program, and is called a "comment". Comments are important for clarity, you can leave notes for yourself and others to better understand the code. You can also use the comment to temporarily hide parts of code without erasing them completely, which is great for testing. You will see more examples of this.

The Arduino IDE has some tools built in that help you catch most mistakes. And once you've made the mistake once or twice, you'll know to prevent the issue and how to fix it.

Basic Program Structure:

Let's take a look at what happens when you open a new sketch in the IDE.

Arduino IDE with a new program in the editor window

You should see a window like the one above, where we have two empty functions called setup() and loop(). Functions are essentially mini programs within the entire sketch that we can call on. For now, you just need to know that every Arduino sketch must have the two functions void setup() and void loop().

The void setup() will contain everything you want to happen immediately when your Arduino first turns on, and this function will only run once.

void setup() {

	// put your setup code here, to run once:

}

void loop() will contain the instructions that execute the intent of the program, and then repeat over and over.

void loop() {

	// put your main code here, to run repeatedly:

}

Interacting with the xChip Over USB

After writing a sketch, we must upload the finished code to our xSystem in order to use it. When we are ready to upload the sketch to the xSystem, we connect through the IP01 USB interface chip, which is plugged into our computer's USB port. This chip also provides power to the xSystem.

USB Plug

The CW01 xChip is the brains of the xSystem. This is where programs are uploaded to and ran from. You will always need to include the CW01 xChip in your assembly.

CW01 Chip

⚠️ **GitHub.com Fallback** ⚠️