Ciao, Vi mando il codice del programma Light Theremin modificato per gestire due sensori (photoresistors).
Il secondo sensore regola il ritardo mentre il primo la frequenza.
Provatelo, basta fare copia e incolla!
/*
Arduino Starter Kit example
Project 6 - Light Theremin
This sketch is written to accompany Project 6 in the
Arduino Starter Kit
Parts required:
2 x photoresistor
2 x 10 kilohm resistor
1 x piezo
Created 13 September 2012
by Scott Fitzgerald
modified by m.i. march 2016
http://www.arduino.cc/starterKit This example code is part of the public domain
*/
// variable to hold sensors value
int sensor0Value;
int sensor1Value;
// variable to calibrate low value
int sensor0Low = 1023;
int sensor1Low = 1023;
// variable to calibrate high value
int sensor0High = 0;
int sensor1High = 0;
// LED pin
const int ledPin = 13;
void setup() {
// initialize serial communication at 9600 bits per second:
// need to open Strumenti, Monitor seriale
Serial.begin(9600);
// Make the LED pin an output and turn it on
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
// calibrate for the first seven seconds after program runs
while (millis() < 7000) {
sensor0Value = analogRead(A0);
sensor1Value = analogRead(A1);
// record the maximum sensor A0 value
if (sensor0Value > sensor0High) {
sensor0High = sensor0Value;
}
// record the maximum sensor A1 value
if (sensor1Value > sensor1High) {
sensor1High = sensor1Value;
}
// record the minimum sensor A0 value
if (sensor0Value < sensor0Low) {
sensor0Low = sensor0Value;
}
// record the minimum sensor A1 value
if (sensor1Value < sensor1Low) {
sensor1Low = sensor1Value;
}
}
// turn the LED off, signaling the end of the calibration period
digitalWrite(ledPin, LOW);
}
void loop() {
// read the input on analog pin 0:
int sensorValue0 = analogRead(A0);
// read the input on analog pin 1:
int sensorValue1 = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage0 = sensorValue0 * (10.0 / 1023.0);
// print out the value you read:
Serial.print(" A0=");
Serial.print(voltage0);
float voltage1 = sensorValue1 * (10.0 / 1023.0);
// print out the value you read:
Serial.print(" A1=");
Serial.print(voltage1);
Serial.print(" -- ");
Serial.print(sensorValue1);
//read the input from A0 and store it in a variable
sensor0Value = analogRead(A0);
//read the input from A1 and store it in a variable
sensor1Value = analogRead(A1);
// map the sensor A0 values to a wide range of pitches
int pitch0 = map(sensor0Value, sensor0Low, sensor0High, 20, 5000);
// map the sensor A1 values to a wide range of pitches
int pitch1 = abs(map(sensor1Value, sensor1Low, sensor1High, 1, 60));
Serial.print(" -- ");
Serial.println (abs(pitch1));
// play the tone for 20 ms on pin 8
// play the tone for a variable time depending from Voltage1 value
tone(8, pitch0, 20);
//tone(8, pitch1, 10);
// wait for a moment (10) or depending on pitch1 value (the second Photoresistor)
delay(pitch1);
}
---M.
---
---
Nessun commento:
Posta un commento