Serial.Write Help!

    Mar 12 2013 | 12:42 am
    Hey guys, I had a question regarding getting my arduino and max communicating. I have an AC Dimmer code loaded onto my arduino uno. When I open up my serial monitor and type in any number from 0 to 120 it changes the brightness of my lightbulb. I would like to hook up a slider in max because I have a kinect patch that will interact with it. I was wondering on how to get that slider to be the serial write. Doing a lot of research I think ive stumbled upon the serial.write( ) function. Im having troubles with the coding and would love some support! Is this also the best way to approach the communication? Maxuino is useless to me it seems since I have no access to inputing direct values. Feedback? THANKS!

    • Mar 12 2013 | 2:04 am
      Can you post the Arduino code, please?
      In general, keep in mind that Arduino's serial monitor sends data as ASCII code, not binary data. In Max you need to convert your binary data to ASCII before sending it to the serial object if you want it to mimic this behavior. Also, depending on your sketch, you might need to send a linefeed and/or carriage return following the data.
    • Mar 12 2013 | 2:39 am
      ok awesome, ill post the code tomorrow when i get back into my studio tomorrow! meanwhile ill do some converting research. thanks for the quick reply and ill follow up tomorrow!
    • Mar 12 2013 | 3:20 am
      Have a look at the following: [atoi] [itoa] [tosymbol] [fromsymbol] etc.
    • Mar 12 2013 | 8:57 pm
      here is my dimmer control. I just dont know where i should be putting the serial write command within the code, i am frankly very new to the actual coding side of the arduino. Any help or adjustments would be great. thanks!
      AC Light Control
      Updated by Robert Twomey
      Changed zero-crossing detection to look for RISING edge rather
      than falling. (originally it was only chopping the negative half
      of the AC wave form).
      Also changed the dim_check() to turn on the Triac, leaving it on
      until the zero_cross_detect() turns it off.
      Attach AC_PIN output to optotriac input on circuit board.
      Attach Zero Cross Detector output (on circuit board) to Pin 2 (interrupt 0)
      optional LED to pin 3
      Attach arduino GND to GND.
      Attach arduino +5 to +5V.
      Attach AC Hot (120v) to AC Hot wire on circuit board.
      Attach AC Neutral to AC Neutral wire on circuit board.
      Attach one wire from bulb to Bulb 1 on circuit board.
      Attach other wire from bulb to Bulb 2 on circuit board (also AC neutral)
      Thanks to
      int dim2 = 0;
      unsigned char serIn;
      volatile int i=0; // Variable to use as a counter
      volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero
      int AC_pin = 10; // Output to Opto Triac
      int POT_pin = 0; // Pot for testing the dimming
      int LED = 3; // LED for testing
      int dim = 0; // Dimming level (0-128) 0 = on, 128 = 0ff
      int freqStep = 65; // Set the delay for the frequency of power (65 for 60Hz, 78 for 50Hz) per step (using 128 steps)
      // freqStep may need some adjustment depending on your power the formula
      // you need to us is (500000/AC_freq)/NumSteps = freqStep
      // You could also write a seperate function to determine the freq
      void setup() { // Begin setup
      pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
      pinMode(LED, OUTPUT); // Set the LED pin as output
      attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
      Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
      Timer1.attachInterrupt(dim_check, freqStep);
      // Use the TimerOne Library to attach an interrupt
      // to the function we use to check to see if it is
      // the right time to fire the triac. This function
      // will now run every freqStep in microseconds.
      void zero_cross_detect() {
      zero_cross = true; // set the boolean to true to tell our dimming function that a zero cross has occured
      digitalWrite(AC_pin, LOW);
      // Function will fire the triac at the proper time
      void dim_check() {
      if(zero_cross == true) {
      if(i>=dim) {
      digitalWrite(AC_pin, HIGH);
      zero_cross = false;
      else {
      void loop() {
      dim = constrain(dim2, 0, 120);
      analogWrite(LED, dim);
      int serReadInt()
      int i, serAva; // i is a counter, serAva hold number of serial available
      char inputBytes [7]; // Array hold input bytes
      char * inputBytesPtr = &inputBytes[0]; // Pointer to the first element of the array
      if (Serial.available()>0) // Check to see if there are any serial input
      delay(3); // Delay for terminal to finish transmitted
      // 5mS work great for 9600 baud (increase this number for slower baud)
      serAva = Serial.available(); // Read number of input bytes
      for (i=0; i inputBytes[i] =;
      inputBytes[i] = ''; // Put NULL character at the end
      dim2 = atoi(inputBytesPtr); // Call atoi function and return result
      return -1; // Return -1 if there is no input
    • Mar 12 2013 | 9:37 pm
      so i was able to get it to work going through ascii in max and going out of serial c 9600. but the problem now is max is just freezing up a ton. Im not sure if im sending it way to many things over the serial port. Is there any cleanup in my code that would help that issue?
    • Mar 13 2013 | 1:34 am
      Now we'd have to see your Max patch to really answer this new question.
    • Mar 13 2013 | 1:48 am
      When I try to complie that Arduino code, after downloading & installing the library TimerOne, I get the following error:
      test_sketch.ino:101:17: error: empty character constant
      So, I can't compile the code and check it out.
      At this point, in re-reading your posts, I guess I'm a bit confused as to what you're actually trying to accomplish. If you want to read serial data in Arduino from Max, you don't need the 'Serial.write()' command at all - you'd be reading from the serial port, not writing to it, unless you are doing some sort of hand-shaking between Arduino & Max. Also, there's already 'Serial.println()' sending data out the serial port, so are you trying to replace that with the write command, or what?
      I think, in order to help out, I'll need you to state a little more clearly at this point what it is you're trying to do.
    • Mar 13 2013 | 9:38 am
      And there's a baud rate mismatch. Your code has Serial.begin 19200 and you say your serial object is 9600.