Counting inside function for PWM output

#include <IRremote.h>
int RECEIVE_PIN = 10;
int ledRed = 5;
int ledBlue = 6;
int ledOrange = 3;
int ledGreen = 9;
int state = 0;
IRrecv irrecv(RECEIVE_PIN);
decode_results results;
static float angle = 0.0;

void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
pinMode (ledRed, OUTPUT);
pinMode (ledBlue, OUTPUT);
pinMode (ledGreen, OUTPUT);
pinMode (ledOrange, OUTPUT);
pinMode (RECEIVE_PIN, INPUT);
}

void loop() {

angle = angle + 0.01;
if (angle > 3.142)
{
angle = 0;
}
if (irrecv.decode(&results)) {
switch (results.value){

case 0xFFA25D:
if (state ==0) {
state = 1;
analogWrite(ledRed, 255-(int)255 * sin(angle));
} else {
state = 0;
analogWrite(ledRed, 0);
}
break;

case 0xFF629D:
if (state ==0) {
state = 1;
analogWrite(ledBlue, 255-(int)255 * sin(angle));
} else {
state = 0;
analogWrite(ledBlue, 0);
}
break;

case 0xFFE21D:
if (state ==0) {
state = 1;
analogWrite(ledOrange, 255-(int)255 * sin(angle));
} else {
state = 0;
analogWrite(ledOrange, 0);
}
break;

case 0xFF02FD:
if (state ==0) {
state = 1;
analogWrite(ledGreen, 255-(int)255 * sin(angle));
} else {
state = 0;
analogWrite(ledGreen, 0);
}
break;
}
delay(30);
irrecv.resume();
}
}

The goal of the code above is when a specific IR button is pushed, a colored LED (denoted by the pin) will pulse in synchronization with any other led that has been enabled.

The math function again, I was hoping, would count numbers between 0-255 with sin for a better lighting effect and that value would be written to each pin that is activated.

I've gotten to work using a pin as a "count" pin that outputs to ALL of the constant current drivers and also has a TIP122 to connect the circuit to ground by making the "color"Pin write high but I'd like to eliminate some hardware and clean the entire thing up a bit.

Right now, when I press a button on the remote, it writes the value as it should, but it doesn't cycle through the scale, and each button press yields a different value, somewhat fun, but not what I want lol.

I'm not sure whether I should write another loop to do the "counting" for the value to write, but I'm thinking that it's only going to write the value that it reads when the button is pressed to change the case (please correct me if I'm wrong).

^is as far as I've come with understanding the programming so far, I'm still new at this :roll_eyes: (and yes, I know the code is really dirty but I'm just trying to get functionality first)

You are incrementing angle, and resetting when it exceeds approximately 2 * PI, on every pass through loop, regardless of whether or not you intend to use the value of angle on that pass. Why?

You have some { on lines after the statement (where they belong, in my opinion) and some on the same line as the statement. Pick ONE style and use it consistently.

Why is you code in quote tags? The sticky at the top of the forum that you were supposed to read first, said to use code tags.

The goal of the code above is when a specific IR button is pushed, a colored LED (denoted by the pin) will pulse in synchronization with any other led that has been enabled.

There is nothing in that code that does anything like that, except for determining that the specific IR button was pushed.

This would be one way of doing it:

#include <IRremote.h>

const int RECEIVE_PIN = 10;
enum ledColors {RED, BLUE, ORANGE, GREEN};
const int ledPins[]={5, 6, 3, 9};
boolean state[4];
int Value;


IRrecv irrecv(RECEIVE_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  for(int i = RED; i <= GREEN; i++) pinMode(ledPins[i], OUTPUT);
}//()

void loop(){
  //get remote pushed and switch state
  if(irrecv.decode(&results)) switch (results.value){
      case 0xFFA25D: state[RED] = !state[RED]; break;
      case 0xFF629D: state[BLUE] = !state[BLUE]; break;
      case 0xFFE21D: state[ORANGE] = !state[ORANGE]; break;   
      case 0xFF02FD: state[GREEN] = !state[GREEN]; break;
  }//switch(results.value)
  Value = 255*(sin(71.0*millis()/26600)+0.5);//calculate intensity
  //Display led with current intesity or off
  for(int i = RED; i <= GREEN; i++) analogWrite(ledPins[i], Value*state[i]);
}//loop()

PaulS:
You are incrementing angle, and resetting when it exceeds approximately 2 * PI, on every pass through loop, regardless of whether or not you intend to use the value of angle on that pass. Why?

You have some { on lines after the statement (where they belong, in my opinion) and some on the same line as the statement. Pick ONE style and use it consistently.

Why is you code in quote tags? The sticky at the top of the forum that you were supposed to read first, said to use code tags.

The goal of the code above is when a specific IR button is pushed, a colored LED (denoted by the pin) will pulse in synchronization with any other led that has been enabled.

There is nothing in that code that does anything like that, except for determining that the specific IR button was pushed.

  1. As you can see, my math needed some work. I'm coming from a hardware side so the programming is still new to me. The math was just my basic reasoning to getting a smoother transition from the light as I thought the fade example using delay and fixed steps was not very smooth.

  2. The formatting is dirty, and I know that it is. There also aren't any comments (which I realize that I should add for my own sanity). In the future I will make sure to keep with ONE formatting style to make it easier to debug.

  3. I copied the code from my IDE using the "for Forum", I didn't realize that it was going to do quotes instead of code. SO once again, my apologies, I'll make sure it doesn't happen again :wink: This is a learning experience after all so I appreciate all your input!

nilton61:
This would be one way of doing it:

#include <IRremote.h>

const int RECEIVE_PIN = 10;
enum ledColors {RED, BLUE, ORANGE, GREEN};
const int ledPins[]={5, 6, 3, 9};
boolean state[4];
int Value;

IRrecv irrecv(RECEIVE_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  for(int i = RED; i <= GREEN; i++) pinMode(ledPins[i], OUTPUT);
}//()

void loop(){
  //get remote pushed and switch state
  if(irrecv.decode(&results)) switch (results.value){
      case 0xFFA25D: state[RED] = !state[RED]; break;
      case 0xFF629D: state[BLUE] = !state[BLUE]; break;
      case 0xFFE21D: state[ORANGE] = !state[ORANGE]; break; 
      case 0xFF02FD: state[GREEN] = !state[GREEN]; break;
  }//switch(results.value)
  Value = 255*(sin(71.0millis()/26600)+0.5);//calculate intensity
  //Display led with current intesity or off
  for(int i = RED; i <= GREEN; i++) analogWrite(ledPins[i], Value
state[i]);
}//loop()

Nilton, thank you for that code, looking through it it makes good sense. I can piece together what everything does in it but I wouldn't trust myself to write something QUITE that complex yet! So thank you again!

I'll throw ^ on when I get home from work and troubleshoot from there!

Thanks for all the help!

nilton61:
This would be one way of doing it:

#include <IRremote.h>

const int RECEIVE_PIN = 10;
enum ledColors {RED, BLUE, ORANGE, GREEN};
const int ledPins[]={5, 6, 3, 9};
boolean state[4];
int Value;

IRrecv irrecv(RECEIVE_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  for(int i = RED; i <= GREEN; i++) pinMode(ledPins[i], OUTPUT);
}//()

void loop(){
  //get remote pushed and switch state
  if(irrecv.decode(&results)) switch (results.value){
      case 0xFFA25D: state[RED] = !state[RED]; break;
      case 0xFF629D: state[BLUE] = !state[BLUE]; break;
      case 0xFFE21D: state[ORANGE] = !state[ORANGE]; break;   
      case 0xFF02FD: state[GREEN] = !state[GREEN]; break;
  }//switch(results.value)
  Value = 255*(sin(71.0millis()/26600)+0.5);//calculate intensity
  //Display led with current intesity or off
  for(int i = RED; i <= GREEN; i++) analogWrite(ledPins[i], Value
state[i]);
}//loop()

The code that you posted works for turning on, but it won't turn off so I'm working on adding that.

As well the pulse is a little weird, I guess I didn't describe it very well, I needed something like a breathing led effect that takes ~3 sec from up to down so just a little something to modify =)

In any case, just wanted to say thank you again!

I dont have a IR receiver so i wasn't able to test the code.

Sorry, i made a mistake in the value function:
sin(71.0millis()/13300) gives you a 1sec period (since pi~355/113, one of the best estimates)
0.5
sin(213.0*millis()/13300)+0.5 gives you a 3 sec period varying between 0 and 1