Debian Codegraph @

src/Examples/FLOSS Manual Examples/08 Other Communication/08B04_Serial_Read_multiple.csd

raw
1 <CsoundSynthesizer>
2 <CsOptions>
3 -d -odac
4 </CsOptions>
5 <CsInstruments>
6
7 sr = 44100
8 ksmps = 500 ; the default krate can be too fast for the arduino to handle
9 nchnls = 2
10 0dbfs = 1
11
12 giSaw ftgen 0, 0, 4096, 10, 1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8
13
14 instr 1
15
16 ; Initialize the three variables to read
17 kPot init 0
18 kLight init 0
19 kButton init 0
20
21 iPort serialBegin "/COM5", 9600 ;connect to the arduino with baudrate = 9600
22 serialWrite iPort, 1 ;Triggering the Arduino (k-rate)
23
24 kValue = 0
25 kType serialRead iPort ; Read type of data (pot, light, button)
26
27 if (kType >= 128) then
28
29 kIndex = 0
30 kSize serialRead iPort
31
32 loopStart:
33 kValue = kValue << 7
34 kByte serialRead iPort
35 kValue = kValue + kByte
36 loop_lt kIndex, 1, kSize, loopStart
37 endif
38
39 if (kValue < 0) kgoto continue
40
41 if (kType == 128) then ; This is the potmeter
42 kPot = kValue
43 elseif (kType == 129) then ; This is the light
44 kLight = kValue
45 elseif (kType == 130) then ; This is the button (on/off)
46 kButton = kValue
47 endif
48
49 continue:
50
51 ; Here you can do something with the variables kPot, kLight and kButton
52 ; printks "Pot %f\n", 1, kPot
53 ; printks "Light %f\n", 1, kLight
54 ; printks "Button %d\n", 1, kButton
55
56 ; Example: A simple oscillater controlled by the three parameters
57 kAmp port kPot/1024, 0.1
58 kFreq port (kLight > 100 ? kLight : 100), 0.1
59 aOut oscil kAmp, kFreq, giSaw
60
61 if (kButton == 0) then
62 out aOut
63 endif
64
65 endin
66
67 </CsInstruments>
68 <CsScore>
69 i 1 0 60 ; Duration one minute
70 e
71 </CsScore>
72 </CsoundSynthesizer>
73 ;example written by Sigurd Saue