Wednesday 12 March 2014

The Arduino Manager

One of the unsolved issues for me has been how to get my iMac to talk to the Arduino applications (or as they are called Sketches). The Arduino IDE (integrated Development Environment) has a "Serial Monitor" that allows you to give simple commands like Serial.begin(9600); followed by things like Serial.print("this and this"); to send information from the sketch to the iMac, and key presses on the iMac can be sent to the sketch. This is at least an interesting way to exchange commands and information. (I have used it is my Morse Code sender earlier in this blog).

But wouldn't it be nice to have a graphical interface with various elements available to communicate back and forward? Well there is a new one around called "Arduino Manager". And it runs on the iMac, iPad and iPhone. It is able to communicate by USB, WiFi and Ethernet. For the last two you obviously need a WiFi or Ethernet board (it uses the official Arduino boards).

So what can it do?

1. Provide a customisable interface with a variety of elements - push buttons, switches, displays, graphs...

2. Run a "Code Generator" to help you build your sketch correctly by generating the fundamentally needed bits of software code.

Here's a simple example:

Screen Shot 2014 03 12 at 10 52 27
As you can see this example has three boxes configured with actions. The first is a Push Button and a displayed red button. When the button is clicked it activates a red LED connected to the Arduino for 1 second (a programmable time of course...). The second box contains a switch and a display circle. This can switch on or off another yellow LED, when the LED is on the circle will light up. The last is very interesting, it is a box which can display data coming from the Arduino, in this example a temperature sensor.

At bottom right is the connection box where you select the IP address (for WiFi or Ethernet) or the USB serial device for USB. Connect this first and take care, if you want to load or re-upload your sketch to the Arduino you MUST Quit Arduino Manager first to break its use of the USB connection and allow the Arduino IDE to use the line.

The Arduino for this simple demo is wired as follows:

DSC 0016

On the left is a TMS36 temperature sensor IC connected to A0, and on the right are the two red and yellow LEDS connected to D6 & D8 (the red will be activated by the Arduino Manager display's REDLED push button and the yellow by the YELLOWLED switch). Here is the code needed to run this application:

// Arduino Manager 2 x LED & temp sensor
// REDLED button, YELLOWLED switch on/off, TEMP display

#include "SD.h"
#include "IOSControllerSerial.h"

// connection pins
#define RLEDPIN 6
#define YLEDPIN 8
#define TEMPPIN A0

// init global variables
int yellowLed = LOW;
int redLed = LOW;
float temp;

// create USB serial controller to execute work, sync, out and in messages
IOSControllerSerial iosController(&doWork, &doSync,
&processIncomingMessages, &processOutgoingMessages);

void setup()
{
  Serial.begin(9600);
  
  pinMode(RLEDPIN, OUTPUT);
  pinMode(YLEDPIN, OUTPUT);
  
  digitalWrite(YLEDPIN, yellowLed); // Y led off
  digitalWrite(RLEDPIN, redLed); // R led off
}

// conventional loop just calls iosController loop, every 200ms
void loop()
{
  iosController.loop(200);
}

// execute commands
void doWork()
{
  temp = getVoltage(TEMPPIN); // read temp 0-5V
  temp = (temp - 0.5) * 100; // 10mV/deg 0.5V offset, x 100
  
  digitalWrite(YLEDPIN, yellowLed); // on/off yellow led
}

// sync commands depending on name
void doSync(char *name)
{ 
  if(strcmp(name, "YLEDPIN") == 0) // if yellow led name, send current status
  {
    iosController.writeMessage(name, digitalRead(YLEDPIN));
  }
}

// read incoming messages for name with val
void processIncomingMessages(char  *name, char *val)
{
  if(strcmp(name, "REDLED") == 0) // if name button pushed, so flash red
  {
    iosController.logLn("REDLED");
    iosController.temporaryDigitalWrite(RLEDPIN, HIGH, 1000);
  }
  
  if(strcmp(name, "YELLOWLED") == 0) // get yellow name led command
  {
    yellowLed = atoi(val);
  }
}

// send outgoing messages for name with value
void processOutgoingMessages()
{
  iosController.writeMessage("YELLOWLED", yellowLed); // status of yellow led
  iosController.writeMessage("TEMP", temp); // value of temperature
}

// read and compute temperature value
float getVoltage(int pin)
{
  return (analogRead(pin) * 0.004882814); // 0-1023 = 0-5V
}


As you can see the code creates an object called "iosController". This then has various functions like "iosController.writeMessage(name, data)" where "name" is the title of the box on the iMac display, and data is the value to send to the box display. Four fundamental functions run the show, "doSync()", "doWork()", processIncomingMessages()", and "processOutgoing Messages()". These work as you can see in the example above.

Programming the iMac display is very simple, just activate the "edit" mode, top right, then click on any box. This will display the list of actions you can program into the box.

Screen Shot 2014 03 12 at 11 25 05

Each action can be customised by Name, and display.

For full information see the developers web site here.

The Arduino Manager software is available for download from either the Mac App Store or the iOS App Store.

No comments: