digitalWrite not working with analogWrite

Hey All,
I’m working on a sketch that will receive IR signals to turn a pin HIGH and LOW as well as PWM for an array to control LEDs. I’m using analogWrite to an array to do the PWM, here’s what it looks like to brighten the LED

        Serial.println("Brightening lights up");
        arrayLevel = arrayLevel + 10;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);

I want to toggle this array on(255) and off(0) using analogWrite as well. I’m currently doing this using digitalWrite as follows.

        if(statLED_state1 != LOW)
          statLED_state1 = LOW;
        else
          statLED_state1 = HIGH;
        digitalWrite(statLED1, statLED_state1);  //mac
        Serial.println("On/Off");

How can I write the above to use analogWrite to the array?

Thanks!
Rich

if(statLED_state1 != LOW)
{
   statLED_state1 = !statLED_state1; //toggle 0 -> 1  1 -> 0
   digitalWrite(statLED1, statLED_state1);  //mac

   //if(statLED_state1 > 0) 
   //   statLED_state1 = 255; // if statLED_state1 is greater than 0
   //else
   //   statLED_state1 = 0; // otherwise statLED_state1 is 0 or less.

   arrayWriteAnalog(statLED_state1 > 0? 255 : 0); // compact version of the above IF/ELSE statement
   Serial.println("On/Off");
}

Thanks Hazard! I got it to work with a little tweaking. Here’s my final sketch in it’s entirety.

#include <IRremote.h>

int RECV_PIN = A1;  //  IR Receiver
IRrecv irrecv(RECV_PIN);
decode_results results;

int statLED1 = 5; //LED 1
int statLED2 = 3; //LED 2
int statLED3 = 9; //LED 3

int statLED_state1 = LOW;  //mac
int statLED_state2 = LOW;  //mac
int statLED_state3 = LOW;  //mac

int pins[] = {  // array of the pins
  3, 5, 9};

int arrayLevel = 0;

void setup() {
  pinMode(RECV_PIN, INPUT);
  irrecv.enableIRIn(); // Start the receiver

  pinMode(statLED1, OUTPUT);
  pinMode(statLED2, OUTPUT);
  pinMode(statLED3, OUTPUT);

  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);

  digitalWrite(statLED1, statLED_state1);  //mac
  digitalWrite(statLED2, statLED_state2);  //mac
  digitalWrite(statLED3, statLED_state3);  //mac

  digitalWrite(3, LOW);
  digitalWrite(5, LOW);
  digitalWrite(9, LOW);

  Serial.begin(9600);
  Serial.println("Waiting: ");
}

void loop() {
  unsigned long key=0;
  if(irrecv.decode(&results))
  {
    key = results.value;         //Fetch the key
    if(key != 0) //Ignore keys that are zero
    {
      Serial.println("Key Recieved: ");
      Serial.println(key);

      switch(key)
      {
      case 0xA55A21DE:

        if(arrayLevel > 0) 
          arrayLevel = 0; // if statLED_state1 is greater than 0
        else
          arrayLevel = 255; // otherwise statLED_state1 is 0 or less.

        arrayWriteAnalog(arrayLevel); // compact of the above IF/ELSE statement
        Serial.println("On/Off");

        delay(250);
        break;

      case 0xA55A01FE:
        Serial.println("Brightening lights up");
        arrayLevel = arrayLevel + 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;

      case 0xA55A817E:
        Serial.println("Dimming lights down");
        arrayLevel = arrayLevel - 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;
      }
      irrecv.resume(); // Receive the next value
    }  
  }
}

void arrayWriteAnalog(int val) {
  val = constrain(val, 0, 255);
  for (int i = 0; i < sizeof(pins) / sizeof(int); i++) {
    Serial.print("analogWrite ");
    Serial.print(pins[i]);
    Serial.print(" ");
    Serial.println(val);
    Serial.flush();
    analogWrite(pins[i], val);
  } 
}

void arrayWrite(int pins[], int val) {
  for (int i = 0; i <= (sizeof(pins)/sizeof(int)); i++) {
    digitalWrite(pins[i], val);
  }
}

I’m taking it a step futher by adding a physical button. I’d like it to function exactly the same as the on off button from the remote but for some reason my attempt isn’t working.

void loop() {
  int reading = digitalRead(buttonPin);
  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
        if(arrayLevel > 0) 
          arrayLevel = 0; // if statLED_state1 is greater than 0
        else
          arrayLevel = 255; // otherwise statLED_state1 is 0 or less.

        arrayWriteAnalog(arrayLevel); // compact of the above IF/ELSE statement
        Serial.println("Button On/Off");
   }
  }
  
  unsigned long key=0;
  if(irrecv.decode(&results))
  {
    key = results.value;         //Fetch the key
    if(key != 0) //Ignore keys that are zero
    {
      Serial.println("Key Recieved: ");
      Serial.println(key);

      switch(key)
      {
      case 0xA55A21DE:
        if(arrayLevel > 0) 
          arrayLevel = 0; // if statLED_state1 is greater than 0
        else
          arrayLevel = 255; // otherwise statLED_state1 is 0 or less.

        arrayWriteAnalog(arrayLevel); // compact of the above IF/ELSE statement
        Serial.println("Remote On/Off");
        delay(250);
        break;

      case 0xA55A01FE:
        Serial.println("Brightening lights up");
        arrayLevel = arrayLevel + 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;

      case 0xA55A817E:
        Serial.println("Dimming lights down");
        arrayLevel = arrayLevel - 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;
      }
      irrecv.resume(); // Receive the next value
    }  
  }
}

void arrayWriteAnalog(int val) {
  val = constrain(val, 0, 255);
  for (int i = 0; i < sizeof(pins) / sizeof(int); i++) {
    Serial.print("analogWrite ");
    Serial.print(pins[i]);
    Serial.print(" ");
    Serial.println(val);
    Serial.flush();
    analogWrite(pins[i], val);
  } 
}

void arrayWrite(int pins[], int val) {
  for (int i = 0; i <= (sizeof(pins)/sizeof(int)); i++) {
    digitalWrite(pins[i], val);
  }
}

Example:

byte buttonPin = 2;
byte LED = 13;

byte buttonState;
byte latch;
byte lastState;
unsigned long time;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(LED, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(buttonPin); // read the state of the button

  if (buttonState != lastState)  //See if the button state is HIGH, and does not equal the last state
  {
    if (buttonState == HIGH) // only get the time when the buttton is pressed
    {
      time = millis();
    }
    lastState = buttonState; // update last state
  }

  if (millis() - time >= 50UL)
  {
    if (buttonState == HIGH) // if the button is still pressed, then toggle the latch
    {
      latch = !latch; // flip the state of the LED
      digitalWrite(LED, latch ? HIGH : LOW); // state of LED according to latch state
      Serial.println(latch); // visual of latch state
    }
  }
}

Thanks HM but I’d like to try and get what I have working as it includes debounce. I’ve added some serial messages to my code to help me find out where it’s not flowing. See below.

#include <IRremote.h>

int RECV_PIN = A1;  //  IR Receiver
IRrecv irrecv(RECV_PIN);
decode_results results;

int statLED1 = 5; //LED 1
int statLED2 = 3; //LED 2
int statLED3 = 9; //LED 3

int statLED_state1 = LOW;  //mac
int statLED_state2 = LOW;  //mac
int statLED_state3 = LOW;  //mac

int pins[] = {  // array of the pins
  3, 5, 9};

int arrayLevel = 0;

//Button Stuff
int buttonPin = 6;    // physical button
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(RECV_PIN, INPUT);
  irrecv.enableIRIn(); // Start the receiver

  pinMode(statLED1, OUTPUT);
  pinMode(statLED2, OUTPUT);
  pinMode(statLED3, OUTPUT);

  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);

  digitalWrite(statLED1, statLED_state1);  //mac
  digitalWrite(statLED2, statLED_state2);  //mac
  digitalWrite(statLED3, statLED_state3);  //mac

  digitalWrite(3, LOW);
  digitalWrite(5, LOW);
  digitalWrite(9, LOW);

  Serial.begin(9600);
  Serial.println("Waiting: ");
}

void loop() {
  int reading = digitalRead(buttonPin);
  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
      Serial.println("This prints to the console when button is pushed");
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {
      Serial.println("This prints to the console for infinity");
    // if the button state has changed:
    if (reading != buttonState) {
      Serial.println("This DOES NOT print to the console");
      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        if(arrayLevel > 0)
          arrayLevel = 0; // if statLED_state1 is greater than 0
        else
          arrayLevel = 255; // otherwise statLED_state1 is 0 or less.
      }
      arrayWriteAnalog(arrayLevel); // compact of the above IF/ELSE statement
      Serial.println("Button On/Off");
    }
  }

  unsigned long key=0;
  if(irrecv.decode(&results))
  {
    key = results.value;         //Fetch the key
    if(key != 0) //Ignore keys that are zero
    {
      Serial.println("Key Recieved: ");
      Serial.println(key);

      switch(key)
      {
      case 0xA55A21DE:
        if(arrayLevel > 0) 
          arrayLevel = 0; // if statLED_state1 is greater than 0
        else
          arrayLevel = 255; // otherwise statLED_state1 is 0 or less.

        arrayWriteAnalog(arrayLevel); // compact of the above IF/ELSE statement
        Serial.println("Remote On/Off");
        delay(250);
        break;

      case 0xA55A01FE:
        Serial.println("Brightening lights up");
        arrayLevel = arrayLevel + 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;

      case 0xA55A817E:
        Serial.println("Dimming lights down");
        arrayLevel = arrayLevel - 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;
      }
      irrecv.resume(); // Receive the next value
    }  
  }
}

void arrayWriteAnalog(int val) {
  val = constrain(val, 0, 255);
  for (int i = 0; i < sizeof(pins) / sizeof(int); i++) {
    Serial.print("analogWrite ");
    Serial.print(pins[i]);
    Serial.print(" ");
    Serial.println(val);
    Serial.flush();
    analogWrite(pins[i], val);
  } 
}

void arrayWrite(int pins[], int val) {
  for (int i = 0; i <= (sizeof(pins)/sizeof(int)); i++) {
    digitalWrite(pins[i], val);
  }
}

I don’t know what I did exactly but I got it working!

#include <IRremote.h>

int RECV_PIN = A1;  //  IR Receiver
IRrecv irrecv(RECV_PIN);
decode_results results;

int statLED1 = 5; //LED 1
int statLED2 = 3; //LED 2
int statLED3 = 9; //LED 3

int statLED_state1 = LOW;  //mac
int statLED_state2 = LOW;  //mac
int statLED_state3 = LOW;  //mac

int pins[] = {  // array of the pins
  3, 5, 9};

int arrayLevel = 0;

//Button Stuff
int buttonPin = 6;    // physical button
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(RECV_PIN, INPUT);
  irrecv.enableIRIn(); // Start the receiver

  pinMode(statLED1, OUTPUT);
  pinMode(statLED2, OUTPUT);
  pinMode(statLED3, OUTPUT);

  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);

  digitalWrite(statLED1, statLED_state1);  //mac
  digitalWrite(statLED2, statLED_state2);  //mac
  digitalWrite(statLED3, statLED_state3);  //mac

  digitalWrite(3, LOW);
  digitalWrite(5, LOW);
  digitalWrite(9, LOW);

  Serial.begin(9600);
  Serial.println("Waiting: ");
}

void loop() {
  int reading = digitalRead(buttonPin);
  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
      Serial.println("This prints to the console when button is pushed");
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {
      Serial.println("This prints to the console for infinity");
    // if the button state has changed:
    if (reading != buttonState) {
      Serial.println("This DOES NOT print to the console");
      // only toggle the LED if the new button state is HIGH
        if(arrayLevel > 0)
          arrayLevel = 0; // if statLED_state1 is greater than 0
        else
          arrayLevel = 255; // otherwise statLED_state1 is 0 or less.
      
      arrayWriteAnalog(arrayLevel); // compact of the above IF/ELSE statement
      Serial.println("Button On/Off");
    }
  }

  unsigned long key=0;
  if(irrecv.decode(&results))
  {
    key = results.value;         //Fetch the key
    if(key != 0) //Ignore keys that are zero
    {
      Serial.println("Key Recieved: ");
      Serial.println(key);

      switch(key)
      {
      case 0xA55A21DE:
        if(arrayLevel > 0) 
          arrayLevel = 0; // if statLED_state1 is greater than 0
        else
          arrayLevel = 255; // otherwise statLED_state1 is 0 or less.

        arrayWriteAnalog(arrayLevel); // compact of the above IF/ELSE statement
        Serial.println("Remote On/Off");
        delay(250);
        break;

      case 0xA55A01FE:
        Serial.println("Brightening lights up");
        arrayLevel = arrayLevel + 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;

      case 0xA55A817E:
        Serial.println("Dimming lights down");
        arrayLevel = arrayLevel - 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;
      }
      irrecv.resume(); // Receive the next value
    }  
  }
}

void arrayWriteAnalog(int val) {
  val = constrain(val, 0, 255);
  for (int i = 0; i < sizeof(pins) / sizeof(int); i++) {
    Serial.print("analogWrite ");
    Serial.print(pins[i]);
    Serial.print(" ");
    Serial.println(val);
    Serial.flush();
    analogWrite(pins[i], val);
  } 
}

void arrayWrite(int pins[], int val) {
  for (int i = 0; i <= (sizeof(pins)/sizeof(int)); i++) {
    digitalWrite(pins[i], val);
  }
}

Thanks HM but I’d like to try and get what I have working as it includes debounce.

? ? ? (had to space) = ??? ← how do 3 question marks turn into a face?

The code I gave you does debounce the button.