Adding code for photo-resistor into existing code

Hi all,

I'm still pretty new with Arduino and a lot of stuff goes over my head, but I've come a long way with my project - An interactive wind chime instrument. Here's a video of it in action:

It's basically just mapping readings from piezo sensors to audio effect. I'm currently in the process of adding a temp/humidity sensor and a photo light resistor. I've managed to integrate the temp/humidty sensor into my code. But can't integrate the code for a photo-resistor, (which I thought would be the easy bit). However the reading for the resistor is always 1023. I know it's not a problem with the wiring because if I just upload the basic tutorial code for a photo-resistor it works fine, I just can't integrate it into my code.

Can anyone help me in adding code for a photo-resistor on the A5 input, I feel like it should be so simple and I'm missing something staring me in the face? Thanks everyone

#include "DHT.h"

#define DHTPIN 3 

#define DHTTYPE DHT11  

DHT dht(DHTPIN, DHTTYPE);

const byte nSensors = 6;
const byte nSamples = 100;

const byte piezoPin[nSensors] = {A0, A1, A2, A3, A4, A5};
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);


  // 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);
  Serial.print(F(" "));
  Serial.print(t);
  Serial.print(F(" "));
  Serial.println(hic);

  
}

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(" "));

}

Perhaps it is because there is no code in that sketch that does anything at all with a photoresistor. We can't help you with code we can't see.

Apologies that was an old version. I have actually managed to get this working just as I am typing this. Is there anything I am missing with this code that would make it more efficient?

#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() {
  analogReference(INTERNAL); // remove this line if too sensitive
  Serial.begin(9600);

  dht.begin();

}


void loop() {

  int analogValue = analogRead(A5);

  piezo();
  
  
  delay(100);


  // 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);
  Serial.print(F(" "));
  Serial.print(t);
  Serial.print(F(" "));
  Serial.println(hic);
  Serial.println(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(" "));

}

in the code

  analogReference(INTERNAL); // remove this line if too sensitive

using INTERNAL means using a built-in reference, equal to 1.1 volts on your UNO. Anything above will lead to 1023. that might be the issue you have with your photo-resistor that might be wired for 5V

what's the value added of the temperature display to your project? You compute the heat index in Fahrenheit but never use it

if you wanted to handle a number of sensors more easily, let the compiler compute how many piezo you have

const byte piezoPin[] = {A0, A1, A2, A3, A4};
const byte nSensors = sizeof piezoPin / sizeof piezoPin[0];

what's the purpose of the   delay(100);

Hi, Thanks for the advice!

I've removed analogReference(INTERNAL) and it seems to have made things run smoother so thanks for that.

The delay(100), was originally at 250 which I thought was necessary to to make the reading 'real time' as the temp sensor takes 250 milliseconds to read. I think I changed this to 100 as an experiment and never changed it back, however since seeing your comment I've deleted it and it works great.

I want the DHT11 to detect only degrees Celsius and humidity so I've amended the code to display only that (I think), the Fahrenheit heat index was code accidentally left in copy and pasted from the tutorial. This is what it's looking like now:

#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(" "));

}

I tried your suggestion regarding the piezo's but I struggled to get it to work properly. I will keep trying but this is what I have so far, thanks again.

code cleaned up a bit.
Is your photo-resistor on A5?

#include <DHT.h>
#define DHTPIN 3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const byte nSamples = 100;

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

void piezo()
{
  for (int sensorIndex = 0; sensorIndex < nSensors; sensorIndex++) {

    // prepare acquisition
    maxSample[sensorIndex] = 0;

    // perform 100 A/D readings and keep max
    for (int sample = 0; sample < nSamples; sample++) {
      int rawValue = analogRead(piezoPin[sensorIndex]);
      if (rawValue > maxSample[sensorIndex])
        maxSample[sensorIndex] = rawValue;  // store peaks
    }

    // if new peak larger than previous one
    if (maxEver[sensorIndex] < maxSample[sensorIndex]) maxEver[sensorIndex] = maxSample[sensorIndex];
    else maxEver[sensorIndex] = (maxEver[sensorIndex]) / 50; // smooth decay
    Serial.write('\t');
    Serial.print(maxEver[sensorIndex]);
  }
  Serial.println();
}

void otherData()
{
  int analogValue = analogRead(A5);

  float h = dht.readHumidity();
  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!"));
  } else {
    Serial.print(h); Serial.write('\t');
    Serial.print(t); Serial.write('\t');
    Serial.println(analogValue);
  }
}

void setup() {
  Serial.begin(115200);
  dht.begin();
}

void loop() {
  piezo();
  otherData();
}

This is great thank you. Yes it's on A5 and works a charm.

With the baud rate, I understand 115200 would transmit serial data faster than the standard 9600, but would this cause latency problems when used alongside other programmes such as wekinator and Max/Msp? The project I'm working on requires these programmes to be running simultaneously. Or would the higher baud rate be beneficial for this set up?

Thanks again!

if what's on the other side of the Serial port can handle 115200 then you spend less time sending bytes and more time in your code

note that I modified spaces you were sending to send tabs, may be that's not what wekinator is expecting

J-M-L:
if what's on the other side of the Serial port can handle 115200 then you spend less time sending bytes and more time in your code

note that I modified spaces you were sending to send tabs, may be that's not what wekinator is expecting

Thanks again,
My setup in Max reads the values in this way: '57.0 17.0 665 0 0 0 0 0' i.e. all values on the same line one space apart. I can't see how I can adjust this in the code you provided? Would you mind pointing me in the right direction?

The tabs are sent with the Serial.write('\t'); so if you want to just send a space use Serial.write(' ');