Cardona Designs | Setting Standards

CAT | Arduino

Apr/11

29

Multiplication table in C for Arduino

Recently I got an Arduino Uno which I’ve been using to get a better understanding of electronics and hardware hacking. The Uno has given me a great respect for scarcity of resources and has caused me to look at programming in an entirely new light.

Having come to programming in my late 20’s I missed the era of slow computers with little memory. Of course I had a computer growing up but I used it mainly to surf the web and do homework and never experienced the wonder of Moore’s Law up close.

My first computer after becoming a coder was a Macbook and because the machine was so powerful and I was programming at such a high level I never even thought about what was happening on the hardware. In a way I feel fortunate because every facet of computing has come as a great surprise and discovery to me as I used each island of new found knowledge as a base to further my quest for discovery.

The Uno has been treasure trove with regards to lessons related to programming. Suddenly I’m in a world of 32k flash memory and 2k ram and I am forced to actually think about what’s going on in the memory.

To program the Uno you code in a watered down C. To better learn the language I have been reading The C Programming Manual. C is way more powerful then any language I’ve yet seen and I’m humbled by how much I have to learn.

Each morning my 2 year old son and I do multiplication exercises. We do 1–12 * 1–12. This morning after finishing our exercises I thought it would be good practice to create a C program that wrote the multiplication table to the console. It was fun and below is what I came up with. The bottom code works on the Arduino and will write to the Serial port.

/#include <stdio.h>

main() { /#define MAX 12 /#define MIN 1 int subbase; int i; int j;

for (i = MIN; i <= MAX; ++i) { for (j = MIN; j <= MAX; ++j) { printf("%3d ", subbase = i * j); }
printf("\n"); } }

Arduino Code

/#define MAX 12
/#define MIN 1
void setup() {
  Serial.begin(9600);
  for (int i = MIN; i <= MAX; ++i) {
    for (int j = MIN; j <= MAX; ++j) {
      Serial.print(i * j);
    }
    Serial.print("\n");
  }
}

void loop() { }

All code is licensed Creative Commons Share Alike.

No tags

Theme Design by devolux.nh2.me