1

So I am using Arduino to control RGB Strip light, the analogueWrite(Pin,Value); Works with the prebuilt AnalogueWriteMega example - on pins 8, 9, and 10.

When I move this into my program, Pin 8 still works but pins 9 and 10 don't work.

Anywhere I write analogueWrite() it only works if the value is 255

  • Green is pin 8
  • Red is pin 9
  • Blue is pin 10
#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

#include <LiquidCrystal.h>
// encoder
int val;
int encoder0PinS = 29;
int encoder0PinA = 31;
int encoder0PinB = 33;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
int MenuPos;
int SubPos;
bool Selected = false;
int Reader = LOW;

// lcd
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//pot
int ASensePin = A0;
double ASenseValue = 0;
double AFixedValue = 0;
int AFixOut = 0;

//randoms
int ranPin;
int ranVal;

//IR

int receiver = 45; // Signal Pin of IR receiver to Arduino Digital Pin 45

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'



void translateIR() { // takes action based on IR code received

  // describing Remote IR codes
  switch (results.value)
  {

    case 16195807:
      Serial.println("Red, 0, 255, 0");
      analogWrite(8, 0);
      analogWrite(9, 255);    < ------ works
      analogWrite(10, 0);
      break;

    case 16197847:
      Serial.println("Yellow, 255, 255, 0");
      analogWrite(8, 100);      < ----don't work
      delay(50);
      analogWrite(9, 100);       <----don't work
      delay(50);
      analogWrite(10, 0);
      break;

    case 16228447:
      Serial.println("green, 255 , 0, 0");
      analogWrite(8, 255);
      analogWrite(9, 0);
      analogWrite(10, 0);
      break;

    case 16230487:
      Serial.println("turquois, 0, 255, 255");
      analogWrite(8, 0);
      delay(50);
      analogWrite(9, 100);
      delay(50);
      analogWrite(10, 100);
      delay(50);
      break;

    case 16212127:
      Serial.println("blue, 0, 0, 255");
      analogWrite(8, 0);
      analogWrite(9, 0);
      analogWrite(10, 255);
      break;

    case 16214167:
      Serial.println("Purple, 255, 0, 255");
      analogWrite(8, 100);
      delay(50);
      analogWrite(9, 0);
      delay(50);
      analogWrite(10, 100);
      delay(50);
      break;

    case 16244767:
      Serial.println("White, 255, 255, 255");
      analogWrite(8, 255);
      analogWrite(9, 255);
      analogWrite(10, 255);
      break;

    default:
      Serial.println(results.value);
  }// End Case

  delay(500); // Do not get immediate repeat
} //END translateIR



void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  pinMode (encoder0PinS, INPUT);
  pinMode (8, OUTPUT);
  pinMode (9, OUTPUT);
  pinMode (10, OUTPUT);

  lcd.begin(16, 2);
  lcd.print("      Start     ");

  Serial.begin (9600);
}

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
  }
  encoder0PinALast = n;
  if (encoder0Pos == 4) {
    encoder0Pos = 0;
  }
  if (encoder0Pos == -1) {
    encoder0Pos = 3;
  }

  MenuPos = encoder0Pos;

  Reader = digitalRead(encoder0PinS);
  // Serial.println(Reader);

  if (digitalRead(encoder0PinS) == LOW) {
    while (digitalRead(encoder0PinS) == LOW) {}
    Selected = true;
  }

  Serial.println("MenuLoop");
  switch (MenuPos) {
    case 0:
      lcd.setCursor(0, 0);
      lcd.print("     Random    ");
      while (Selected == true) {
        ranPin = random(8, 10);
        ranVal = random(0, 255);
        analogWrite(ranPin, ranVal);

        if (digitalRead(encoder0PinS) == LOW) {
          while (digitalRead(encoder0PinS) == LOW) {}
          Selected = false;
        }
      }
      break;

    case 1:
      lcd.setCursor(0, 0);
      lcd.print("       IR      ");
      Serial.println("IR Receiver Button Decode");
      irrecv.enableIRIn(); // Start the receiver

      while (Selected == true) {
        if (irrecv.decode(&results)) // have we received an IR signal?
        {
          translateIR();
          irrecv.resume(); // receive the next value
          lcd.setCursor(0, 1);
          lcd.print("               ");
          lcd.setCursor(0, 1);
          lcd.print(results.value);
        }
        if (digitalRead(encoder0PinS) == LOW) {
          while (digitalRead(encoder0PinS) == LOW) {}
          Selected = false;
        }
      }

      break;

    case 2:
      lcd.setCursor(0, 0);
      lcd.print("      Temp     ");
      break;

    case 3:
      lcd.setCursor(0, 0);
      lcd.print("      Pot      ");

      while (Selected == true) {

Not working

        for (int thisPin = 8; thisPin <= 10; thisPin++) {
          // fade the LED on thisPin from off to brightest:
          for (int brightness = 0; brightness < 255; brightness++) {
            analogWrite(thisPin, brightness);
            delay(2);
          }
          // fade the LED on thisPin from brightest to off:
          for (int brightness = 255; brightness >= 0; brightness--) {
            analogWrite(thisPin, brightness);
            delay(2);
          }
          // pause between LEDs:
          delay(100);
        }

        if (digitalRead(encoder0PinS) == LOW) {
          while (digitalRead(encoder0PinS) == LOW) {}
          Selected = false;
        }
      }
      break;
  }
}
3

The problem has nothing to do with your switch code. It's all to do with what you are trying to do, and peripherals conflicting.

Simply put: both PWM on pins 9 & 10, and the infrared library, require exclusive use of Timer 2.

Only one can use it. Either the infrared library, or PWM on those two pins, but not both.

You can modify the IR library to use a different timer instead of Timer 2 - for instance, you can set it to use Timer 1 which will remove your ability to use pins 11 and 12 as PWM.

This is the bit of code that you would need to find and modify on your system to change timers. This page shows you which timers are used for PWM on which pins.

  • Thanks this worked, I swapped pins 9 and 10 to 6 and 7 so they where not clashing with the timer from IR. – user42506 Feb 4 '18 at 20:40
  • An other option is a software pwm library. There are a few of those. They require a timer (which is not used by something else). The software pwm can be on any pin, because the pwm is done in software in the interrupt. – Jot Feb 5 '18 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.