Arduino Sketch

Save the code below as a new sketch in the Arduino IDE, and compile and upload it to your Arduino. Note that pin assignments may need to be changed to work with your circuit.

  Minimal Arduino-EMC Interface
  (C) 2011 Colin Kingsbury
  This code is offered for educational purposes only and
  without any warranty of suitability or safety for any
  particular application. It is the responsibility of the
  end user to adapt this properly for their own purposes.

  This code will control 2 LEDs and take inputs from a single
  normally-open switch. It was written to work with an NKK
  illuminated pushbutton switch but will function with any
  devices having the same basic characteristics, polarity
  etc. The basic design shown here can be extended to
  support much more complex controls.

//Pin numbers for the LEDs and the switch
//I am leaving pins 2 and 3 open for future incorporation of
//an encoder, which will require the interrupts on those pins.
int redLed = 9;
int greenLed = 8;
int sw = 4;

//This variable holds a state indicating whether the switch
//is currently on (1) or off (0). We do this because
//we only send a message to the PC when the switch state
//changes (ON->OFF or vice versa)
int swState = 0;

void setup() {
  // initialize the pins as appropriate
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(sw, INPUT);

  //Open the serial connection
  //I use the lowest speed for maximum fault-tolerance.
  //9600 is more than fast enough for almost anything
  //the Arduino could handle in this application.

  //As a test, let's loop through the two LEDs for a second.
  //My LEDs are wired to be ON when the pins are driven low.
  //If your LEDs don't work, make sure the polarity is correct.
  //First, I set both to high so they are off.
  //If you want, you can comment this out once you're sure the
  //circuit is working properly.
  //Turn the green one on for a half second
  //Then the red one


void loop() {
  //First. check the state of the switch
  if(digitalRead(sw) == HIGH) {
    if(swState == 0) {           //the state has changed
      Serial.print("+S");       //Send a message to the PC
      swState = 1;              //Update the switch state
    //If the switch state was 1, then we wouldn't care
    //since it means it hasn't changed.
  else { //the switch is low, see if it's gone high
    if(swState == 1) {
      swState = 0;

  //Now check to see if the LEDs need to be updated
    This is one of the tricky parts. The easiest way I've found to
    do serial comms is by using fixed length messages.
    This way, we can be assured of getting a complete message
    in the order we expect. However, the outbound and inbound
    messages do not need to be the same size.
  if(Serial.available() == 2) {
      Tricky part #2 and #3: Arduino receives serial messages as ACSCII codes
      and reads them one at a time. So you need to take the message packets
      apart properly. For simple applications, you can probably do everything
      with single-character messages. I'm showing how to do multi-char ones
      to help anyone who wants to do something more intricate.
    int cmd =; //This will be a + or - sign (ASCII 43 or 45)
    int led =; //This will be a G or R (ASCII 71 or 82)

    if(cmd == 43) {          //Command to turn an LED on
      if(led == 71) {        //turn on the green
      if(led == 82) {        //it's the red one
    if(cmd == 45) {          //Command to turn an LED off
      if(led == 71) {        //turn off the green
      if(led == 82) {        //it's the red one

One response to “Arduino Sketch

  1. dewy721 February 14, 2012 at 9:33 pm

    I just wanted to say thank you:
    Based on your example code I have been able to setup a (beta) Arduino/EMC Servo Plotter. EMC2.4/AXIS parses the G-Code while the Arduino entirely handles the mechanics. No printer cable required!

    You button/Led code + X/Y/Z co-ordinates as well as virtual limit switches sent bi-directionally via usb serial link to my servo-driven closed-loop plotter. Now to move beyond the conceptual piece onto something more shop worthy. 🙂

    Thanks again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: