Skip to content

serialwrite

Write data to a serial port for arduino.

📝 Note

Up to Csound 6, this opcode was called serialWrite.

Plugin opcode in serial.

Syntax

serialwrite(iPort, iByte)
serialwrite(iPort, kByte)
serialwrite(iPort, SBytes)
serialWrite iPort, iByte
serialWrite iPort, kByte
serialWrite iPort, SBytes

Performance

iPort -- port number optained from a serialbegin opcode.

iByte -- a byte of data to write.

Examples

Here is an example of the serialwrite opcode. It uses the file serialwrite-modern.csd.

Example of the serialwrite opcode.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-n  ;;;no output
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o serialwrite.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr  = 44100
ksmps = 500 ; the default krate can be too fast for the arduino to handle
nchnls  = 2
0dbfs   = 1

instr 1
  Port:i = serialbegin("/dev/cu.usbmodemfa131", 9600) ;connect to the arduino
                                                      ;with baudrate = 9600
  Gain:k = init(16)         ;read our knob value
  Val:k = serialread(Port)
  if Val != -1 then
    Gain = Val/128
  endif

  Sig:a = in()          ;get our audio input and get its rms
  Rms:k = rms(Sig*Gain)

  Rms = Rms*Rms*255     ;scale the rms to a good value for the LED and send it out
  serialwrite(Port, (Rms < 255 ? Rms : 255))   ;must be in range: 0-255
endin
</CsInstruments>
<CsScore>
f 1 0 1024 10 1 1 1 1 1 1

i 1 0 200
e
</CsScore>
</CsoundSynthesizer>

Here is an example of the serialWrite opcode. It uses the file serialWrite.csd.

Example of the serialWrite opcode.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-n  ;;;no output
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o serialWrite.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr  = 44100
ksmps = 500 ; the default krate can be too fast for the arduino to handle
nchnls = 2
0dbfs  = 1

instr 1

iPort serialBegin "/dev/cu.usbmodemfa131", 9600                 ;connect to the arduino with baudrate = 9600

kGain init 16                                                   ;read our knob value
kVal serialRead iPort
if (kVal != -1) then
    kGain = kVal/128
endif

aSig in                                                         ;get our audio input and get its rms
kRms rms aSig*kGain

kRms = kRms*kRms*255                                            ;scale the rms to a good value for the LED and send it out
serialWrite iPort, (kRms < 255 ? kRms : 255)                    ;must be in range: 0-255

endin
</CsInstruments>
<CsScore>
f 1 0 1024 10 1 1 1 1 1 1

i 1 0 200
e
</CsScore>
</CsoundSynthesizer>

This is the matching Arduino code :

void setup() {
  // enable serial communication
  Serial.begin(9600);

  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
}

void loop() {
  // only do something if we received something (this should be at csound's k-rate)
  if (Serial.available()) {
    // set the brightness of LED (connected to pin 9) to our input value
    int brightness = Serial.read();
    analogWrite(9, brightness);

    // while we are here, get our knob value and send it to csound
    int sensorValue = analogRead(A0);
    Serial.write(sensorValue/4); // scale to 1-byte range (0-255)
  }     
}
.....

See Also

non-MIDI Devices

Credits

Author: Matt Ingalls
2011

New in version 5.14