/* タッチスクリーン入力、SX-150 への CV 出力 (その1) 抵抗膜式のタッチスクリーンを読み取って、断続的に制御信号を SX-150 に送ります。 X軸は音程、Y軸は断続の速さに割り当てます。 回路: * タッチスクリーンはアナログピン 0 から 3 に接続します。 0 -- X+ 1 -- Y+ 2 -- X- 3 -- Y- 各ピンは無接続の時 0V に落ちるよう、100kΩの抵抗を介して GND に接続します。 * SX-150 への CV 出力は デジタルピン 9 からとります。 出力にはカットオフ周波数約 1.5kHz のローパスフィルタをかけます。 GND 同士も接続します。 created 9 May 2010 by Gan */ // タッチスクリーンのピン割り当て // 機種によりピンの割り当てが異なるときにはここを変更する // #define xPositive_A 0 #define yPositive_A 1 #define xNegative_A 2 #define yNegative_A 3 // Map analog ports to digital ones. // #define xPositive_D (xPositive_A + 14) #define yPositive_D (yPositive_A + 14) #define xNegative_D (xNegative_A + 14) #define yNegative_D (yNegative_A + 14) const uint8_t cvOutPin = 9; #define BASE 240 /** * Set the touch screen to non-measuring state */ void ts_Reset() { // change all pins to output mode pinMode( xPositive_D, OUTPUT ); pinMode( xNegative_D, OUTPUT ); pinMode( yPositive_D, OUTPUT ); pinMode( yNegative_D, OUTPUT ); // set low to all pins digitalWrite( xPositive_D, LOW ); digitalWrite( xNegative_D, LOW ); digitalWrite( yPositive_D, LOW ); digitalWrite( yNegative_D, LOW ); } /** * Shift the touch screen reading state, followed by measurement. * This routine sets the state back to non-measuring at the final step. */ void ts_ReadPosition(uint8_t resist_Positive_D, uint8_t resist_Negative_D, uint8_t probe_Positive_D, uint8_t probe_Negative_D, uint8_t probe1_A, uint8_t probe2_A, int* value1, int* value2) { digitalWrite( resist_Positive_D, HIGH ); // Set the positive side of resistor terminals. delayMicroseconds(100); // Wait a while for onset of the terminal pinMode(probe_Positive_D, INPUT); // Change probe terminals to input mode pinMode(probe_Negative_D, INPUT); delayMicroseconds(100); // Wait a while for the probe terminal onsets at the measuring voltage *value1 = analogRead( probe1_A ); // OK, here we go // this is optional read for Z-axis // We may sometimes want to read at opposite side of the probes as well. if (value2 != NULL) { *value2 = analogRead( probe2_A ); } ts_Reset(); // The measurement finished. Getting back to normal state. } /** * Read X Position */ int ts_ReadXPosition() { int value; ts_ReadPosition( xPositive_D, xNegative_D, yPositive_D, yNegative_D, yPositive_A, 0, &value, NULL ); return value; } /** * Read Y Position */ int ts_ReadYPosition() { int value; ts_ReadPosition( yPositive_D, yNegative_D, xPositive_D, xNegative_D, xPositive_A, 0, &value, NULL ); return value; } // The Arduino program starts from here void setup() { TCCR1A = TCCR1A & 0b11111100 | _BV(WGM10) | _BV(WGM11); TCCR1B = TCCR1B & 0b11100000 | 0x1 | _BV(WGM12); pinMode(cvOutPin, OUTPUT); ts_Reset(); } void loop() { int xSensorValue, ySensorValue; delay(1); // sleep xSensorValue = ts_ReadXPosition(); // eat delay(1); // sleep ySensorValue = ts_ReadYPosition(); // eat // X軸で決めた高さで CV 出力 xSensorValue = xSensorValue > 10 ? xSensorValue * 2 / 3 + BASE : 0; if (xSensorValue >= 1024) { xSensorValue = 1023; } analogWrite(cvOutPin, xSensorValue); // Y軸で決めた速さで CV を断続する int delayMS = 128 - ySensorValue / 6; delayMS = delayMS > 0 ? delayMS : 0; if (delayMS < 90) { delay(70); analogWrite(cvOutPin, 0); delay(delayMS); } else { delay(6); } }