So I have a code to cycle colors on an RGB LED and then added a button to pause the RGB cycle on a color when it is pressed. The code works on the Tinkercad Simulator, but when I transfer it to my breadboard all it does is cycle the colors and stop on green every time once the button is pressed. Why is this working in the simulator but not on the breadboard? Below is the code and a short video to better show what I'm talking about.
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
unsigned int rgbColour[3];
int buttonPin = 2;
int temp = 1;
void setup() {
// Start off with the LED off.
setColourRgb(0, 0, 0);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin), Pause, FALLING);
}
void loop() {
if (temp == 1) {
// Start off with red.
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for (int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
if (temp == 0) {
return;
}
delay(5);
}
}
}
else {
// Do nothing pause
}
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void Pause()
{
if (temp == 1) {
temp = 0;
}
else {
temp = 1;
}
}
Here is the video: RGB Cycling Video (YouTube)