i found out this sketch works just fine on the mega 2560 with the same exact pins i was trying to use on the uno. so that problem is solved. now my question is why it works on the mega and not the uno. any thoughts? im cool with running a mega i just would like to know the reason to help me in the future. thanks
Iv slammed my head enough and ive gotta tap and ask for help. i have a rgb shield on a uno and it uses pins 3,5,6 for some reason i cant get red to fade properly with my sketch. it works fine with other sketches that dont use a ir remote. my gut says it has to do with the timers but im fairly new at arduino and stumbling my way through it. i am using a sketch made by another artist but he used pins 10,11,12.
[code]
#include <TimerOne.h>
#include <IRremote.h>
#include <RGBMood.h>
int RECV_PIN = 2; // IR-Receiver PIN
int led = 13; // Satus-LED PIN
int modus; // Modus for Interrupt-Querry
int ledr = 3; // RGB LED red PIN
int ledg = 5; // RGB LED green PIN
int ledb = 6; // RGB LED blue PIN
int SerialBuffer = 0;
int c[3];
//RGB Pins Array
int CH[3] = {3, 5, 6};
//int val[3] = {0, 0, 0}; // led brightness 0-255
RGBMood m(ledr, ledg, ledb);
int timerwert = 20; // Timer time for Interrupt in ms
String readString;
// Color arrays
int black[3] = { 0, 0, 0 };
int white[3] = { 100, 100, 100 };
int red[3] = { 100, 0, 0 };
int green[3] = { 0, 100, 0 };
int blue[3] = { 0, 0, 100 };
//int yellow[3] = { 40, 95, 0 };
//int dimWhite[3] = { 30, 30, 30 };
int brightness = 100; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// etc.
// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];
int wait = 10; // 10ms internal crossFade delay; increase for slower fades
int hold = 0; // Optional hold when a color is complete, before the next crossFade
int Debug = 1; // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 3; // How many times should we loop before stopping? (0 for no stop)
int j = 0; // Loop counter for repeat
// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
// yellow A25AFB1E
//pink C54310C9
// lightblue C54310CA
//left bottom A2409B1A
//middle bottom 8A8F5F86
//right bottom 8A8F5F85
#define ON 0x1363ADB4
#define OFF 0x622F3ED6
#define BRIGHTNESS_UP 0x8A8F5F85
#define BRIGHTNESS_DOWN 0xA2409B1A
#define FLASH 0xC54
#define STROBE 0x8A8
#define FADE 0xA25AFB1E
#define SMOOTH 0x8A8
#define RED 0x64D30897
#define GREEN 0x31538D76
#define BLUE 0x87D57E45
#define WHITE 0x64ED689B
#define ORANGE 0xFFB04F
#define YELLOW_DARK 0xFFA857
#define YELLOW_MEDIUM 0xFF9867
#define YELLOW_LIGHT 0xFF8877
#define GREEN_LIGHT 0XFF30CF
#define GREEN_BLUE1 0XFF28D7
#define GREEN_BLUE2 0XFF18E7
#define GREEN_BLUE3 0XFF08F7
#define BLUE_RED 0XFF708F
#define PURPLE_DARK 0XFF6897
#define PURPLE_LIGHT 0XFF58A7
#define PINK 0XFF48B7
#define MAX 255
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
pinMode(ledr, OUTPUT); // Set RGB LED Pins as Output
pinMode(ledg, OUTPUT); // Set RGB LED Pins as Output
pinMode(ledb, OUTPUT); // Set RGB LED Pins as Output
// pinMode(led, OUTPUT); // set Status-LED as Output
//initiate rgb pins output
for (int i=0; i<3; i++)
{
pinMode(CH[i], OUTPUT);
}
m.setMode(RGBMood::RANDOM_HUE_MODE); // Automatic random fade.
m.setHoldingTime(4000); // Keep the same color for 4 seconds before fading again.
m.setFadingSteps(150); // Fade with 150 steps.
m.setFadingSpeed(50); // Each step last 50ms. A complete fade takes 50*150 = 7.5 seconds
m.setHSB(random(359), 255, 255);
Serial.begin(9600);
irrecv.enableIRIn(); // Start of IR-Recive
Timer1.initialize(timerwert); // Initialisation of Timer-Interrupts
Timer1.attachInterrupt(leseIR); // IR-Read from Interrupt
}
void leseIR(){
if (irrecv.decode(&results)){
irrecv.resume(); // Receive the next value
switch (results.value) {
case FADE: // Modus Fade (DIY 4)
modus = 1;
break;
case 0xFF906F: // Modus pcambi (DIY 5)
modus = 2;
break;
case ON: //Power
modus = 0;
crossFade(white); // RGB LEDs Off
break;
case OFF: //Power
modus = 0;
crossFade(black); // RGB LEDs Off
break;
case BLUE: //Blau 0,0,255
modus = 0;
crossFade(blue);
break;
case RED: //Rot
modus = 0;
crossFade(red);
break;
case GREEN://Grün
modus = 0;
crossFade(green);
break;
case WHITE: //Weiss
modus = 0;
crossFade(white);
break;
case BRIGHTNESS_UP: //DIMMING UP
modus = 0;
brightness += 5;
if (brightness > 255) brightness = 255;
c[0] = prevR; c[1] = prevG; c[2] = prevB;
analogWrite(ledr, redVal * brightness / 255); // Write current values to LED pins
analogWrite(ledg, grnVal * brightness / 255);
analogWrite(ledb, bluVal * brightness / 255);
break;
case BRIGHTNESS_DOWN: //DIMMING DOWN
modus = 0;
brightness -= 5;
if (brightness < 0) brightness = 0;
c[0] = prevR; c[1] = prevG; c[2] = prevB;
analogWrite(ledr, redVal * brightness / 255); // Write current values to LED pins
analogWrite(ledg, grnVal * brightness / 255);
analogWrite(ledb, bluVal * brightness / 255);
break;
case 0xFFAA55://Grün mitrtel
modus = 0;
break;
case 0xFF926D: //blau mittel
modus = 0;
break;
case 0xFF12ED: //rosa
modus = 0;
break;
} // Switch END
}
}
void loop() {
if(modus==1){ // Querry pb Modus:1
m.tick();
}
if(modus==2){ // Querry pb Modus:1
}
// Serial.println(prevR);
// Serial.println(prevG);
// Serial.println(prevB);
// Serial.println(results.value, HEX);
// Serial.println(DEC);
// Serial.println(DEC);
// Serial.println(DEC);
// Serial.print("channel 1,2,3 values:"); // sends brightness values to the serial monitor
// for(int i=0; i<3; i++){ // every time the remote is pressed
// Serial.print(CH[i]);
// Serial.print(" ");
// }
}
int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What's the overall gap?
if (step) { // If its non-zero,
step = 1020/step; // divide by 1020
}
return step;
}
/* The next function is calculateVal. When the loop value, i,
* reaches the step size appropriate for one of the
* colors, it increases or decreases the value of that color by 1.
* (R, G, and B are each calculated separately.)
*/
int calculateVal(int step, int val, int i) {
if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
if (step > 0) { // increment the value if step is positive...
val += 1;
}
else if (step < 0) { // ...or decrement it if step is negative
val -= 1;
}
}
// Defensive driving: make sure val stays in the range 0-255
if (val > 255) {
val = 255;
}
else if (val < 0) {
val = 0;
}
return val;
}
/* crossFade() converts the percentage colors to a
* 0-255 range, then loops 1020 times, checking to see if
* the value needs to be updated each time, then writing
* the color values to the correct pins.
*/
void crossFade(int color[3]) {
// Convert to 0-255
int R = (color[0] * 255) / 100;
int G = (color[1] * 255) / 100;
int B = (color[2] * 255) / 100;
int stepR = calculateStep(prevR, R);
int stepG = calculateStep(prevG, G);
int stepB = calculateStep(prevB, B);
for (int i = 0; i <= 1020; i++) {
redVal = calculateVal(stepR, redVal, i);
grnVal = calculateVal(stepG, grnVal, i);
bluVal = calculateVal(stepB, bluVal, i);
analogWrite(ledr, redVal); // Write current values to LED pins
analogWrite(ledg, grnVal);
analogWrite(ledb, bluVal);
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
// Update current values for next loop
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}
[/code]