Basic Arduino to Maya II

July 27th, 2009

Here’s Maya reading real-time input from an Arduino board, set up in the circuit from the Arduino Button tutorial, and assuming the environment setup from my first Arduino post:

I’ve tweaked the code for the video to make the readout easier to see.

Code follows:

Arduino code

// Serial button, modified from Tom Igoe's Button script at
// http://www.arduino.cc/en/Tutorial/Button

 const int buttonPin = 2;     // the number of the pushbutton pin
 const int ledPin =  13;      // the number of the LED pin
 int buttonState = 0;         // variable for reading the pushbutton status

 void setup() {
   // initialize the LED pin as an output:
   pinMode(ledPin, OUTPUT);
   // initialize the pushbutton pin as an input:
   pinMode(buttonPin, INPUT);
   Serial.begin(9600);        // connect to the serial port
 }

 void loop(){
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   // check if the pushbutton is pressed.
   // if it is, the buttonState is HIGH:
   if (buttonState == HIGH) {
     // turn LED on:
     digitalWrite(ledPin, HIGH);
     Serial.print("1");
   }
   else {
     // turn LED off:
     digitalWrite(ledPin, LOW);
     Serial.print("0");
   }
   delay(100); // update 10 times a second
 }
//

Maya Python code

## simple button read
import time
import sys
sys.path.append( "C:\Program Files\Common Files\Python\Python25\Lib\site-packages\win32")
import win32file
sys.path.append( "C:\Program Files\Common Files\Python\Python25\Lib\site-packages\win32\lib")
import win32con
import serial

ser = serial.Serial(
  port='COM3',
  baudrate=9600,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS
)

startTime=timerX()
totalTime = timerX(startTime=startTime)

while totalTime < 5: # 5-second timer prevents infinite loop
  print ser.read()
  totalTime = timerX(startTime=startTime)

ser.close()
##
« previously: Basic Arduino to Maya Communication | Home | next: Basic Arduino to Maya III »

One Response to “Basic Arduino to Maya II”

  1. Tang Says:

    I have some problem below when I run the code above.
    did you? Could you figure out what’s wrong with it? Thank you very much.

    # Error: ‘module’ object has no attribute ‘timerX’
    # Traceback (most recent call last):
    # File “”, line 21, in
    # AttributeError: ‘module’ object has no attribute ‘timerX’ #

Leave a Reply