Monday, May 1, 2017

Programming the MeinEnigma - Getting Started


The MeinEnigma is a great platform for learning Arduino programming. The wide variety of on-board hardware provides the opportunity to learn how to program different peripheral devices, some simple and some much more complex. Over the next few blog posts I'll present some example Arduino programs that will run on the MeinEnigma.

The programs are standalone and replace the the standard MeinEnigma software that emulates an Enigma machine. You can easily compile and upload the examples using the Ardino IDE (1) running on your desktop computer, attached to the MeinEnigma using USB.

The code was written to be straightforward and readable but not necessarily the most efficient. For example, in most cases I use variables of type int, whereas often a smaller value type could be used that would save a few bytes of memory. This can be important for larger programs, but can make the code harder to understand.

Buzzer Example

The first example is a freebie - playing the buzzer built in to the MeinEnigma. It happens to be on the same i/o port that most Arduinos use for their built-in LED, so it is identical to the example program most beginning Arduino developers start with to flash the LED. It makes a good first programming task to confirm that you have a working Arduino development environment.

The code is shown below in it's entirety. In the setup() method, which is run once on powerup, we configure the digital i/o pin connected to the buzzer to be an output.

In the loop() method, which is run repeatedly after setup() is called, we drive the output high for half a second then drive it low for the same length of time, causing the buzzer to turn on and off at a one second rate.

Note that for this type of piezoelectic buzzer hardware, a high level will produce a tone at a fixed frequency determined by the buzzer itself. We can't change the frequency or volume level (at least, not without some more complicated programming). As I mentioned in an earlier blog post, you may find the buzzer a litte loud and covering it with some electrical tape is a simple way to reduce the volume.

#define BUZZER 13

void setup() {
  pinMode(BUZZER, OUTPUT);    // Initialize digital pin 13 as an output.
}

void loop() {
  digitalWrite(BUZZER, HIGH); // Turn the buzzer on.
  delay(500);                 // Wait for half a second.
  digitalWrite(BUZZER, LOW);  // Turn the buzzer off.
  delay(500);                 // Wait for half a second.
}

Conclusions

As this blog series progresses I will introduce coding examples that are increasing more complex (and, hopefully, interesting). At the end we will tie it all together with a larger program that makes use of most of the code examples and hardware on the MeinEnigma.

I hope you will follow along and try the examples, and maybe make some changes of your own to the code.

All source code can be found at link 2 under References below.

References


  1. https://www.arduino.cc/en/Main/Software
  2. https://github.com/jefftranter/meinEnigma/tree/master/Examples


No comments: