Arduino sends high to every LED rather than controlling them

Hi
I wrote this sketch where depending on the input from the sensor, an RGB LED is controlled. However the code sends high to every pin that I assigned a value to instead of controlling the LED colour. IE instead of sending high to only the pin which has the red leg of the LED, it sends high to all 3 legs of the LED.
I believe that I must be making a mistake in the way that I assign a value to the LED however I have added the whole code just in case.

My code is:

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 22     // what pin we're connected to
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;// the analog reading from the analog resistor divider
int sensorValue;
int redPin = 13;
int greenPin = 12;
int bluePin = 11;
int redPin2 = 10;
int greenPin2 = 9;
int bluePin2 = 8;
int redPin3 = 7;
int greenPin3 = 6;
int bluePin3 = 5;
int redPin4 = 4;
int greenPin4 = 3;
int bluePin4 = 2;




// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
// might need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold.  It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value.  The default for a 16mhz AVR is a value of 6.  For an
// Arduino Due that runs at 84mhz a value of 30 works.
// Example to initialize DHT sensor for Arduino Due:
//DHT dht(DHTPIN, DHTTYPE, 30);

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
  pinMode(bluePin, OUTPUT);   
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT); 
  pinMode(redPin2, OUTPUT);
  pinMode(greenPin2, OUTPUT);
  pinMode(bluePin2, OUTPUT); 
  pinMode(redPin3, OUTPUT);
  pinMode(greenPin3, OUTPUT);
  pinMode(bluePin3, OUTPUT); 
  pinMode(redPin4, OUTPUT);
  pinMode(greenPin4, OUTPUT);
  pinMode(bluePin4, OUTPUT);   
  
  dht.begin();
}

void loop() {
  
  
  sensorValue = analogRead(1);  
  Serial.print("air pollution ");
  // read analog input pin 0
  Serial.println(sensorValue, DEC);  // prints the value read 
  
  
    photocellReading = analogRead(photocellPin);  
  // Wait a few seconds between measurements.
  delay(2000);
  Serial.print("LDR  ");
  Serial.print(photocellReading);

  // 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
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  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("Failed to read from DHT sensor!");
    return;
  }
  


  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");
  
   if (sensorValue < 55) {
     digitalWrite(redPin4, 255);
     digitalWrite(greenPin4, 0);                                        
     digitalWrite(bluePin4, 255);
  } else if (sensorValue <65) {
      digitalWrite(redPin4, 0);
     digitalWrite(greenPin4, 0); 
     digitalWrite(bluePin4, 255);
  } else {
     digitalWrite(redPin4, 0);
     digitalWrite(greenPin4, 255); 
     digitalWrite(bluePin4, 255);
  }

  
    if (t < 12) {
     digitalWrite(redPin, 0);
     digitalWrite(greenPin, 255); 
     digitalWrite(bluePin, 255);
  } else if (t <15) {
     digitalWrite(redPin, 0);
     digitalWrite(greenPin, 0); 
     digitalWrite(bluePin, 255);
  } else if (t <20) {
     digitalWrite(redPin, 255);
     digitalWrite(greenPin, 0); 
     digitalWrite(bluePin, 255);
  } else if (t <22) {
     digitalWrite(redPin, 0);
     digitalWrite(greenPin, 0); 
     digitalWrite(bluePin, 255);
  } else {
     digitalWrite(redPin, 0);
     digitalWrite(greenPin, 255); 
     digitalWrite(bluePin, 255);
  }
  
  if (h <45) {
     digitalWrite(redPin2, 0);
     digitalWrite(greenPin2, 255); 
     digitalWrite(bluePin2, 255);
  } else if (h <50) {
     digitalWrite(redPin2, 0);
     digitalWrite(greenPin2, 0); 
     digitalWrite(bluePin2, 255);
  } else if (h <65) {
     digitalWrite(redPin2, 255);
     digitalWrite(greenPin2, 0); 
     digitalWrite(bluePin2, 255);
  } else if (h <70) {
     digitalWrite(redPin2, 0);
     digitalWrite(greenPin2, 0); 
     digitalWrite(bluePin2, 255);  
  } else {
     digitalWrite(redPin2, 0);
     digitalWrite(greenPin2, 255); 
     digitalWrite(bluePin2, 255);    
  }
  
  if (photocellReading < 10) {
     digitalWrite(redPin3, 0);
     digitalWrite(greenPin3, 255); 
     digitalWrite(bluePin3, 255);   
  } else if (photocellReading <500) {
     digitalWrite(redPin3, 0);
     digitalWrite(greenPin3, 255); 
     digitalWrite(bluePin3, 255);   
  } else if (photocellReading < 600) { 
     digitalWrite(redPin3, 0);
     digitalWrite(greenPin3, 0); 
     digitalWrite(bluePin3, 255);   
  } else if (photocellReading < 700) {
     digitalWrite(redPin3, 255);
     digitalWrite(greenPin3, 0); 
     digitalWrite(bluePin3, 255);   
  } else {
     digitalWrite(redPin3, 255);
     digitalWrite(greenPin3, 0); 
     digitalWrite(bluePin3, 255);   
  }
  
  
  
  
  
  
  
 
}

void setColor(int red, int green, int blue, int red2, int green2, int blue2, int red3, int green3, int blue3, int red4, int green4, int blue4)
{

  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
  analogWrite(redPin2, red2);
  analogWrite(greenPin2, green2);
  analogWrite(bluePin2, blue2); 
  analogWrite(redPin3, red3);
  analogWrite(greenPin3, green3);
  analogWrite(bluePin3, blue3); 
  analogWrite(redPin4, red4);
  analogWrite(greenPin4, green4);
  analogWrite(bluePin4, blue4);   
}

Thanks for your time :slight_smile:

Let's focus on the section using the variable t . What value does t have and what LED is lit?

Temporarily give t a fixed value rather than taking its input from the sensor. Try all the different ranges to prove that each works properly.

I notice that some of the tested values result in the same output - that may be confusing the issue.

...R

Not all the pins work with analogWrite, only the pins with a little ~ next to the number on the board are analogue (PWM).

T is the temperature of the DHT sensor. I assigned it the value of 15 and the pins it controls (redpin, bluepin, greenpin which are 13, 12, 11) and they were all still high.

The following code for my LDR works as intended:

int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the analog resistor divider
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
  
void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);  
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);   
}
 
void loop(void) {
  photocellReading = analogRead(photocellPin);  
 
  Serial.print("Analog reading = ");
  Serial.print(photocellReading);     // the raw analog reading
 
  // We'll have a few threshholds, qualitatively determined
  if (photocellReading < 10) {
    Serial.println(" - Very bright");
     setColor(0, 255, 255);  // red
  } else if (photocellReading <500) {
    Serial.println(" -  Bright");
     setColor(0, 255, 255);  // red
  } else if (photocellReading < 600) { 
    Serial.println(" - Light");
     setColor(0, 0, 255);  // yellow
  } else if (photocellReading < 700) {
    Serial.println(" -Dim");
     setColor(255, 0, 255);  // green
  } else {
    Serial.println(" - Dark");
     setColor(255, 0, 255);  // green
  }
  delay(1000);
}

void setColor(int red, int green, int blue)
{
  #ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  #endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

You are passing an integer (255) to digitalWrite You should be passing HIGH or LOW.

analogWrite is used with integer values where you set the brightness.

I changed the photocell section of the code to test your theory and I still had the same error.

  if (photocellReading < 10) {
     digitalWrite(redPin3, HIGH);
     digitalWrite(greenPin3, LOW); 
     digitalWrite(bluePin3, LOW);   
  } else if (photocellReading <500) {
     digitalWrite(redPin3, HIGH);
     digitalWrite(greenPin3, LOW); 
     digitalWrite(bluePin3, LOW);   
  } else if (photocellReading < 600) { 
     digitalWrite(redPin3, HIGH);
     digitalWrite(greenPin3, HIGH); 
     digitalWrite(bluePin3, LOW);   
  } else if (photocellReading < 700) {
     digitalWrite(redPin3, LOW);
     digitalWrite(greenPin3, HIGH); 
     digitalWrite(bluePin3, LOW);     
  }

Perhaps you should reverse the logic in your code. If the reading is above 700, do something. Else, if the reading is above 600, do something.

Arrays are definitely in your future, as are functions. There is no reason to be diddling with the temperature LEDs in the humidity function. Create a separate function to diddle with the humidity LEDs. Create a separate function to deal with the temperature LEDs.

bexx123: I changed the photocell section of the code to test your theory and I still had the same error.

Did you try my suggestion to use a fixed value - in this example for photocellReading ?

If so, what happened?

...R

I tried assigning a set value to the variables and I still had the same problem as well as reversing the logic and I still had the problem, I can't help but think that the problem is down to the way I assign the values of the LED or the way I write them.

bexx123: I tried assigning a set value to the variables and I still had the same problem

Write a very short program with just one section of your IF statements and get that to work with a fixed input value - no sensors or anything complicated or unpredictable. For test purposes change the value by uploading a new program.

If you can't get it to work post that short code.

I presume you are able to get the 3-colour LED to work properly with some really simple code?

...R