| 1 |
Arduino-Joystick2Csound1g-fm1-port (version with smoothing) |
| 2 |
|
| 3 |
- Push Joystick button to turn on note |
| 4 |
- Use JoystickX to offset and control the modulation index of |
| 5 |
the foscil opcode from 0-40 via the scale opcode |
| 6 |
- Use JoystickY to offset and control the Frequency of the foscil |
| 7 |
opcode up/down two octaves via the scale opcode |
| 8 |
|
| 9 |
<CsoundSynthesizer> |
| 10 |
|
| 11 |
<CsInstruments> |
| 12 |
|
| 13 |
sr = 44100 |
| 14 |
ksmps = 441 |
| 15 |
nchnls = 2 |
| 16 |
0dbfs = 1 |
| 17 |
|
| 18 |
giport init 0 |
| 19 |
|
| 20 |
// NOTE: change USB port "/dev/cu.usbmodem1414301" to correspond |
| 21 |
// with USB port used by Arduino on your system |
| 22 |
giport arduinoStart "//dev/ttyACM0", 9600 // for GNULinux |
| 23 |
|
| 24 |
instr 1 |
| 25 |
|
| 26 |
kY arduinoRead giport, 1 ; Joystick Y |
| 27 |
kX arduinoRead giport, 2 ; Joystick X |
| 28 |
kSW arduinoRead giport, 3 ; Joystick Button/Switch |
| 29 |
|
| 30 |
kAmp init 0 |
| 31 |
kFreq init 0 |
| 32 |
kIndx init 1 |
| 33 |
|
| 34 |
kXraw = kX |
| 35 |
kX port kXraw, .02 // smoothed kY stream |
| 36 |
kYraw = kY |
| 37 |
kY port kYraw, .02 // smoothed kY stream |
| 38 |
|
| 39 |
kYscaled scale kY, 400, 100, 1023, 0 ; scaling the raw sensor |
| 40 |
; data to a user-defined |
| 41 |
; range of (100-400) |
| 42 |
kXscaled scale kX, 40, 0, 1023, 0 ; scaling the raw sensor |
| 43 |
; data to a user-defined |
| 44 |
; range of (0-40) |
| 45 |
|
| 46 |
if(kSW == 1) then |
| 47 |
kAmp = .333 |
| 48 |
elseif(kSW == 0) then |
| 49 |
kAmp = 0 |
| 50 |
endif |
| 51 |
|
| 52 |
aOut foscil 1, kFreq + kYscaled, 1, 1, kIndx + kXscaled, 1 |
| 53 |
|
| 54 |
outs aOut * kAmp, aOut * kAmp |
| 55 |
|
| 56 |
printks "Button=%d, RawX=%d, ScaleX=%d, RawY=%d, ScaleY=%d \\n", \ |
| 57 |
.5, kSW, kX, kXscaled, kY, kYscaled |
| 58 |
|
| 59 |
endin |
| 60 |
|
| 61 |
</CsInstruments> |
| 62 |
|
| 63 |
<CsScore> |
| 64 |
|
| 65 |
f 1 0 16384 10 1 |
| 66 |
|
| 67 |
i 1 0 z |
| 68 |
|
| 69 |
e |
| 70 |
|
| 71 |
</CsScore> |
| 72 |
|
| 73 |
</CsoundSynthesizer> |
| 74 |
|
| 75 |
<arduinoSketch> |
| 76 |
// Joystick Example |
| 77 |
|
| 78 |
// John ffitch & Richard Boulanger |
| 79 |
// June 11, 2020 |
| 80 |
|
| 81 |
// BreadBoard & Arduino Setup |
| 82 |
|
| 83 |
// Insert the Joystick Controller into the breadboard |
| 84 |
// NOTE: Joystick Pins (in order from left to right) are: |
| 85 |
GND, 5V, vrX, vrY, SW |
| 86 |
// Connect the power rails on breadboard to 5V and GND on Arduino |
| 87 |
// Connect GND and 5V from Joystick to + and - power rails on |
| 88 |
// the breadboard |
| 89 |
// Connect Joystick vrX on breadboard to Analog In A0 on the Arduino |
| 90 |
// Connect Joystick vrY on breadboard to Analog In A1 on the Arduino |
| 91 |
// Connect Joystick SW on breadboard to Digital Pin 2 on Arduino |
| 92 |
|
| 93 |
// Arduino pin numbers |
| 94 |
// Note: Because of the way that the Joystick stands up in a |
| 95 |
// breadboard, in this example the vrX and vrY have been swapped. |
| 96 |
|
| 97 |
const int SW_pin = 2; // Joystick switch output connected to |
| 98 |
// Arduino digital pin 2 |
| 99 |
const int Y_pin = 0; // Joystick Y output connected to Arduino |
| 100 |
// analog pin A0 |
| 101 |
const int X_pin = 1; // Joystick X output connected to Arduino |
| 102 |
// analog pin A1 |
| 103 |
|
| 104 |
int lastState_SW_pin = 1; |
| 105 |
int currentState_SW_pin; |
| 106 |
|
| 107 |
void setup() { |
| 108 |
// NOTE: Digital pins can be either inputs or outputs. |
| 109 |
pinMode(SW_pin, INPUT_PULLUP); // Setting digital pin to input mode |
| 110 |
// and using onboard pullup resistor |
| 111 |
// to reduce noise. |
| 112 |
|
| 113 |
Serial.begin(9600); |
| 114 |
} |
| 115 |
|
| 116 |
// put_val( ) - a function to send data values to the Csound |
| 117 |
// "arduinoRead" opcode |
| 118 |
// The first argument of the put_val function "int senChan" sets |
| 119 |
// the software channel number that Csound reads |
| 120 |
// NOTE: "senChan" does "not" define the input pin that is used on |
| 121 |
// the Arduino for a specific sensor |
| 122 |
// The specific Arduino input pin used by any sensor is assigned |
| 123 |
// and set elsewhere in the Arduino sketch and mapped to a |
| 124 |
// user-defined put_val "senChan" channel |
| 125 |
|
| 126 |
void put_val(int senChan, int senVal) |
| 127 |
// Set the Csound receive channel "senChan", and read from |
| 128 |
// the sensor data stream "senVal" |
| 129 |
{ // The packing of the data is sssssvvv 0vvvvvvv where s is a |
| 130 |
// senChan bit, v a senVal bit and 0 is zero` bit |
| 131 |
int low = senVal&0x7f; |
| 132 |
int hi = ((senVal>>7)&0x07) | ((senChan&0x1f)<<3); |
| 133 |
Serial.write(low); Serial.write(hi); |
| 134 |
} |
| 135 |
|
| 136 |
void loop() { |
| 137 |
|
| 138 |
Serial.write(0xf8); |
| 139 |
|
| 140 |
int currentState_SW_pin = digitalRead(SW_pin); // reading digital |
| 141 |
// input 2 and |
| 142 |
// assigning it to |
| 143 |
// "currentState..." |
| 144 |
|
| 145 |
if (currentState_SW_pin != lastState_SW_pin) |
| 146 |
{ // checking if the value has changed |
| 147 |
if (currentState_SW_pin == 1) |
| 148 |
{ |
| 149 |
put_val(3,0); // In this sketch, the Joystick button, |
| 150 |
//in Arduino digital pin 2, is sending a 0 or 1 |
| 151 |
// to Csound arduinoRead channel 3 |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
put_val(3,1); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
lastState_SW_pin = currentState_SW_pin; |
| 160 |
|
| 161 |
int X = analogRead(X_pin); // reading the Joystick vrX data |
| 162 |
// (0-1023) and asigning to X |
| 163 |
put_val(2,X); // In this sketch, the Joystick vrX, |
| 164 |
// in Arduino analog pin A1, is sending |
| 165 |
// a 0-1023 to Csound arduinoRead channel 2 |
| 166 |
|
| 167 |
int Y = analogRead(Y_pin); // reading the Joystick vrY data |
| 168 |
// (0-1023) and asigning to Y |
| 169 |
put_val(1,Y); // In this sketch, the Joystick vrY, |
| 170 |
// in Arduino analog pin A0, is sending |
| 171 |
// a 0-1023 to Csound arduinoRead channel 1 |
| 172 |
|
| 173 |
delay(10); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
</arduinoSketch> |