New wind chime art installation / instrument with machine learning.

Hi all,
I’m currently re-opening an art installation project first discussed on this forum here:
https://forum.arduino.cc/?topic=671727#msg4799676

I've added a simple photoresistor on the top, which can manipulate the sound as you move your hand closer to it, it sounds as if I've added a theremin to it. I've also added a DHT11 temp/humidity sensor which means in theory I could make it so the wind chime sounds different in different environments and different time of the day.

I've also discovered a piece of software called Wekinator which is an easy to use machine learning platform for artists and musicians making similar projects - which should hopefully stabalise the randomness and unpredictability that was the main pitfall of the last project.

I'm re-familiarising myself with the Arduino after a six month break so I'm slowly starting to understand things again however I'm struggling to get the new elements to work alongside the piezo system I had already created. I'll link the simple tutorials I used for the DHT11 and photoresitor:

[u]https://arduinogetstarted.com/tutorials/arduino-light-sensor[/u]

[u]https://create.arduino.cc/projecthub/techno_z/dht11-temperature-humidity-sensor-98b03b[/u]

I've managed to get the DHT11 to work with the piezo code but adding the photoresistor ruined the piezo readings somehow which is bizarre because I thought that would be the easy part. I can get each element to work individually but together something goes wrong. I've certainly messed up the code. My feeling is that it's to do with the delay times it takes for each element to process information. If anyone can point me in the right direction that would be great.

#include "DHT.h" 
 
#define DHTPIN 3  
 
#define DHTTYPE DHT11   
 
DHT dht(DHTPIN, DHTTYPE); 
 
const byte nSensors = 5; //here is the code from last time - I added another to make it 6 for resistor 
const byte nSamples = 100; 
 
const byte piezoPin[nSensors] = {A0, A1, A2, A3, A4, A5}; //A5 is the photoresistor, others are piezo 
int piezoValue[nSensors]; // No need to initialise here. 
int finalValue[nSensors]; 
 
 
void setup() { 
  analogReference(INTERNAL); // remove this line if too sensitive 
  Serial.begin(9600); 
 
  dht.begin(); 
 
} 
 
 
void loop() { 
  piezo(); 
  delay(250); //here I've pasted in the code from the dht11 
 
  // Reading temperature or humidity takes about 250 milliseconds! 
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) 
  float h = dht.readHumidity(); 
  // Read temperature as Celsius (the default) 
  float t = dht.readTemperature(); 
  // Read temperature as Fahrenheit (isFahrenheit = true) 
  float f = dht.readTemperature(true); 
 
  // Check if any reads failed and exit early (to try again). 
  if (isnan(h) || isnan(t) || isnan(f)) { 
    Serial.println(F("Failed to read from DHT sensor!")); 
    return; 
  } 
 
  // Compute heat index in Fahrenheit (the default) 
  float hif = dht.computeHeatIndex(f, h); 
  // Compute heat index in Celsius (isFahreheit = false) 
  float hic = dht.computeHeatIndex(t, h, false); 
 
  Serial.print(h); // I only wanted the humidity, temp in Celsius and heat index 
  Serial.print(F(" ")); 
  Serial.print(t); 
  Serial.print(F(" ")); 
  Serial.println(hic); 
  
} 
 
void piezo() { // and here starts the piezo code from original project 
  for (int i = 0; i < nSensors; i++) { // By convention the i is used as loop iterator. 
    for (int j = 0; j < nSamples; j++) { // Need more? Continue with the alphabet. 
      int rawValue = analogRead(piezoPin[i]); // 100 A/D readings 
      if (rawValue > piezoValue[i]) { 
        piezoValue[i] = rawValue; // store peaks 
      } 
    } 
    if (finalValue[i] < piezoValue[i]) { // fast attack 
      finalValue[i] = piezoValue[i]; 
    } 
    else { 
      finalValue[i] = (finalValue[i]) / 50; // smooth decay 
    } 
    piezoValue[i] = 0; // reset 
     
    Serial.print(F(" ")); 
    Serial.print(finalValue[i]); 
  } 
    Serial.println(F(" ")); 
 
}
const byte nSensors = 5; //here is the code from last time - I added another to make it 6 for resistor 
const byte piezoPin[nSensors] = {A0, A1, A2, A3, A4, A5};

I'm somewhat confused. What number are you actually using for nSensors in the program which fails? 5 or 6?

but adding the photoresistor ruined the piezo readings somehow which is bizarre because I thought that would be the easy part. I can get each element to work individually but together something goes wrong. I've certainly messed up the code.

Can you please explain more precisely what happens when you run the program. How are the piezo readings "ruined"? What is the "something goes wrong"?

Hi thanks for the reply, and sorry for being slow to get back myself!

Since I first posted this I've made some progress with the code and I've managed to fix the problem. I'll paste the code below. If you have any suggestions how I can make it more efficient I'd be very grateful. Thanks again

#include "DHT.h"

#define DHTPIN 3 

#define DHTTYPE DHT11  

DHT dht(DHTPIN, DHTTYPE);

const byte nSensors = 5;
const byte nSamples = 100;

const byte piezoPin[nSensors] = {A0, A1, A2, A3, A4};
int piezoValue[nSensors]; // No need to initialise here.
int finalValue[nSensors];

void setup() {
  
  Serial.begin(9600);

  dht.begin();

}

void loop() {

  int analogValue = analogRead(A5);

  piezo();
  
  // Reading temperature or humidity takes about 250 milliseconds
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  Serial.print(h);
  Serial.print(F(" "));
  Serial.print(t);
  Serial.print(F(" "));
  Serial.print(analogValue);
  
  
}

void piezo() {
  

  for (int i = 0; i < nSensors; i++) { // By convention the i is used as loop iterator.
    for (int j = 0; j < nSamples; j++) { // Need more? Continue with the alphabet.
      int rawValue = analogRead(piezoPin[i]); // 100 A/D readings
      if (rawValue > piezoValue[i]) {
        piezoValue[i] = rawValue; // store peaks
      }
    }
    if (finalValue[i] < piezoValue[i]) { // fast attack
      finalValue[i] = piezoValue[i];
    }
    else {
      finalValue[i] = (finalValue[i]) / 50; // smooth decay
    }
    piezoValue[i] = 0; // reset
    
    Serial.print(F(" "));
    Serial.print(finalValue[i]);
  }
    Serial.println(F(" "));

}