Page 1 of 1

input/output programming

Posted: Fri Mar 02, 2012 1:18 am
by magice
until now everything I have done in programming has been in data management. now I want to venture into input/output programming. l have been looking at ardino boards. their programming is done in a special Ide in c++. I would like to write a user interface to send commands to these boards using livecode. has anyone done anything like this, and where would I look to get started in learning how?

Re: input/output programming

Posted: Fri Mar 02, 2012 2:09 am
by sturgis
I haven't done a whole lot with this stuff but here goes.

First, I think there are a few threads here that you can search for that deal with the arduino. If I recall correctly, these mostly deal with getting around problems talking to the arduino uno on a mac. If you're on windows it should be a bit simpler as you can just open the com port and it'll work. There are also some threads on the use list that you might search for.

Having said all that, I have a simple (really simple) stack that i've been playing with that will do a few things based on what program I have loaded into my arduino.

For example here is an arduino program that controls an led plugged in to led pin 13, and that sends and receives data from the usb (mapped to com port) on a windows machine.

Code: Select all

int ledPin = 13; // sets var ledPin to 13
char state='o';
int blinking=0; // blinking, 0 is no, 1 is yes
int blinkrate=100; // a var that tracks blinkrate that can be changed via serial
int blinkState; 

void setup() {
    pinMode(ledPin, OUTPUT); // pin will be used to for output. We're only writing to the pin, not reading a state
    Serial.begin(115200); // same as in your c++ script. I changed this to 115200 and of course matched it in my lc stack
}

void loop() { // main loop
  if (Serial.available() > 0) // check for data from the serial port
  {
    state = Serial.read(); // used to read incoming data. If theres data, read it

    switch(state)// see what was sent to the board. My stack sends single chars. Use switch to determine what to do
    {
      case 'O': // if the the 'O' was sent, turn the pin on, turn off blink 
        digitalWrite(ledPin,HIGH);
        Serial.println("LED On"); // write to serial 
        blinking = 0;  
        blinkState = 0;
      break;
      case 'o': // if 'o' was sent, turn it off, disable blink 
        digitalWrite(ledPin,LOW);
        Serial.println("LED Off"); 
        blinkState = 0;
        blinking = 0;
      break;
      case 'b': // if 'b' sent, then blink
      blinking = 1;
      break;
      case '1': // these statements change the blinkrate and state
      if (blinkrate != 100)
      {
      blinkrate = 100;
      blinkState = 0;
      }
      break;
      case '2':
      blinkrate = 200;
      blinkState = 0;
      break;
      case '3':
      blinkrate = 300;
      blinkState = 0;
      break;
      case '4':
      blinkrate = 400;
      blinkState = 0;
      break;
      case '5':
      blinkrate = 500;
      blinkState = 0;
      break;
      case '6':
      blinkrate = 600;
      blinkState = 0;
      break;
      case '7':
      blinkrate = 700;
      blinkState = 0;
      break;
      case '8':
      blinkrate = 800;
      blinkState = 0;
      break;
      case '9':
      blinkrate = 900;
      blinkState = 0;
      break;
      case '0':
      if (blinkrate != 1000)
      {
      blinkrate = 1000;
      blinkState = 0;
      }
      break;
      default:
      Serial.print(state);
      break;
    }
  }
if (blinking == 1)
{
  blink();
}
}

void blink() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(blinkrate);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(blinkrate);              // wait for a second
  if (blinkState == 0)
  {
  Serial.print("Blinking");
  Serial.println(blinkrate);
  blinkState = 1;
  }
}
Then there is my simple stack. The card script follows.

Code: Select all

local tReading,tOut
on openCard
   set the serialcontrolstring to "baud=115200 parity=N data=8 stop=1" -- set params for the connection
   doconnect -- call the connection handler
end openCard

on closecard
   if tReading then dodisconnect -- disconnect on close
end closecard

command toggleConnect -- toggles the connect on and off. To upload from the arduino software, disconnect, do the upload, reconnect. 
   if tReading then
      dodisconnect
   set the label of button "tConB" to "Disconnected"
   else
      doconnect
      set the label of button "tConB" to "Connected"
      end if
end toggleConnect

command turnon
   if tReading then  write "O" to file "COM3:" -- send "O" to the serial port (com 3 for me) if there is a connection
end turnon

command turnoff
   if tReading then write "o" to file "COM3:" -- same but with "o"
end turnoff

command makeBlink
   if tReading then write "b" to file "COM3:" -- again, same. Could consolidate all these into a function or handler of course.
end makeBlink

command doconnect -- does the connection, update mode for read and write to com 3. 
   put true into tReading
   open file "COM3:" for update
   set the label of button "tConB" to "Connected"
   readLoop -- starts the readloop
end doconnect

command dodisconnect -- disconnects
   put false into tReading
   close file "COM3:"
   set the label of button "tConB" to "Disconnected"
end dodisconnect

command readLoop
   if tReading then -- if we're connected then read from com 3
      read from file "COM3:" until empty
      if it is not empty then put it after tOut
      send readLoop to me in 50 milliseconds
   else
      put empty into tOut -- "outField" -- clear the field
   end if
   if the number of lines in tOut > 20 then
      delete line 1 to -21 of tOut -- limit the data in outField to 20 lines
   end if
   lock screen
   put tOut into field "outField"
   unlock screen
end readLoop

command setSpeed -- sends a number to set a blinkrate. 
   if tReading then
      switch field "blinkRate"
         case 1
         case 2
         case 3
         case 4
         case 5
         case 6
         case 7
         case 8
         case 9
            write field "blinkRate" to file "COM3:"
            break
         case 10
            write 0 to file "COM3:" -- To keep things simple I am only sending 1 char. So 0 = 10 with the arduino.  bigger means slower blink
            break
      end switch
   end if
end setSpeed
And of course buttons just call the appropriate handler. (blink speed is little arrows and a field)

If you're on a mac, I think I remember something about having to use applescript as an intermediary connector. Will post the links if I find them.

Re: input/output programming

Posted: Fri Mar 02, 2012 3:29 am
by magice
Mostly what I needed to know, is IF it can be done before I buy the boards/servos/relays and everything else I need to build my project. From your post, I can see that it can. In fact, what you have given me there answers a lot of "How does it work" questions that I had. I am sure I will have more questions later, but for now, That was a huge help.
Thank You.