Weird glitchy stuff

So i have a RGB LED and a button (with pulldown resistor) connected to a attiny45 and i'm using this code:

//Setup the Button
int buttonInt = 2;

//Setup the LEDs
int blueLED = 4;
int redLED = 0;
int greenLED = 1;
int nullLED = 16;
int blueValue = 0;
int greenValue = 0;
int redValue = 0;
int nullValue = 0;
volatile int selectedLED = greenLED;

void setup()
{
  pinMode (redLED, OUTPUT);
  pinMode (greenLED, OUTPUT);
  pinMode (blueLED, OUTPUT);
  pinMode (nullLED, OUTPUT);
}



void loop()
{
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(selectedLED, fadeValue);    
    delay(30);  
 if (digitalRead(buttonInt) == HIGH) {
  if (selectedLED == greenLED)
    selectedLED = redLED;
  else if (selectedLED == redLED)
    selectedLED = blueLED;
  else if (selectedLED == blueLED)
    selectedLED = nullLED;
  else
    selectedLED = greenLED;    
    delay(300);

  }    
} 

for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
analogWrite(selectedLED, fadeValue);  
delay(30); 
 if (digitalRead(buttonInt) == HIGH) {
  if (selectedLED == greenLED)
    selectedLED = redLED;
  else if (selectedLED == redLED)
    selectedLED = blueLED;
  else if (selectedLED == blueLED)
    selectedLED = nullLED;
  else
    selectedLED = greenLED;    
    delay(300);

  }
} 
}

It should fade a led, then if you press the button it will keep the led at the current level and cycle to the next LED but it just glitches out when i try it.

Any help?

glitches out

This is the first hit on Google for "glitches out"...
http://soundcloud.com/thejanedoze/bring-em-glitches-out-the
Do you mean you hear that song when you press the button?

Which core are you using?

then if you press the button it will keep the led at the current level and cycle to the next LED

So if that is what you want to do that is what you must write. There is absolutely nothing in that sketch that that holds the value if it reads a low on the button.
You want to add a while loop that holds for as long as the button is held down and stops the for loop from increasing.

Grumpy_Mike:

then if you press the button it will keep the led at the current level and cycle to the next LED

So if that is what you want to do that is what you must write. There is absolutely nothing in that sketch that that holds the value if it reads a low on the button.
You want to add a while loop that holds for as long as the button is held down and stops the for loop from increasing.

and how would i do that?

With 'glitches out' i mean that when i press the button, the current color is not held, the others just increment/decrement in weird patterns

I'm using arduino tiny core with attiny45 @ 1Mhz

I would like to have this project done by tomorrow because it's my fathers birthday then and this is part of his present..

The read up on the while loop and have it hold, that is keep on looking at the input until it goes high.

okay now i'm using this code:

//Setup the Button
int buttonInt = 2;

//Setup the LEDs
int blueLED = 4;
int redLED = 0;
int greenLED = 1;
int nullLED = 16;
int blueValue = 0;
int greenValue = 0;
int redValue = 0;
int nullValue = 0;
volatile int selectedLED = greenLED;

void setup()
{
  pinMode (redLED, OUTPUT);
  pinMode (greenLED, OUTPUT);
  pinMode (blueLED, OUTPUT);
  pinMode (nullLED, OUTPUT);
}


void swap()
{
if (selectedLED == greenLED)
    selectedLED = redLED;
  else if (selectedLED == redLED)
    selectedLED = blueLED;
  else if (selectedLED == blueLED)
    selectedLED = nullLED;
  else
    selectedLED = greenLED;   
     delay(300);   
}


void loop()
{  
  while (digitalRead(buttonInt) == LOW) {
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(selectedLED, fadeValue);    
    delay(30);  
    } 

    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(selectedLED, fadeValue);  
    delay(30);
    } 
  }
  swap();
}

but i can only switch between the LED's when the LED is off..

Try this (untested)...

//Setup the Button

// This is a constant and should be marked as such...
static const int buttonInt = 2;

//Setup the LEDs

// These are all constants and should be marked as such...
static const int blueLED = 4;
static const int redLED = 0;
static const int greenLED = 1;
static const int nullLED = 16;

/* These are not used and not necessary.  Remove them.
int blueValue = 0;
int greenValue = 0;
int redValue = 0;
int nullValue = 0;
*/

/* "volatile" is not necessary.  Remove it. */
int selectedLED = greenLED;

void setup()
{
  pinMode (redLED, OUTPUT);
  pinMode (greenLED, OUTPUT);
  pinMode (blueLED, OUTPUT);
  pinMode (nullLED, OUTPUT);
}


void swap()
{
  if (selectedLED == greenLED)
    selectedLED = redLED;
  else if (selectedLED == redLED)
    selectedLED = blueLED;
  else if (selectedLED == blueLED)
    selectedLED = nullLED;
  else
    selectedLED = greenLED;   

  delay(300);   
}


void fade()
{
  while ( true ) {
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
      analogWrite(selectedLED, fadeValue);    
      delay(30);
      if ( digitalRead(buttonInt) ) return;
    } 

    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
      analogWrite(selectedLED, fadeValue);  
      delay(30);
      if ( digitalRead(buttonInt) ) return;
    }
  }
}

void loop()
{  
  fade();
  swap();
}

Thanks for the effort, but when i try that it does this:

  1. The green LED starts fading
  2. When the button is pressed the green and red both fade in and out
  3. when i press it again it just does all kinds of crazy stuff.

I uploaded a video so you can see:

Let me give you a bit of advice. You need to state clearly what you want your system to do. Then you need to write the code to do it. You need to try and understand any advice given you and understand it.
If you don't understand it ask again (and again) until you do.
What you don't want to do is what you appear to be doing and randomly trying bits of code posted to you, posting videos where you remain silent while a light goes on and off.
That video would have been actually useful if you would have said:-

Look this is what it does with nothing pressed the LED goes ......
Now when I press the button it does this .... and I would like it to do that ...
I press it again and it totally seems to do .....

Read your code like you read a book. It is what the computer is doing at that instant. But remember the code does exactly what it does and if this does not match what you think it should do then you are wrong. You are probably underestimating how fast it will run.

So now what do you want it to do?
Have you changed your mind from my last reply? Because if not you were almost there with it. You just needed to put a holding loop in. However it now looks like you have changed your mind with all that swap stuff in a function.

I would've talked in the video but my english is pretty bad.

Here's what i want it to do:
-Fade green LED on and off
-When the button is pressed, keep the green LED at the current value and move onto the next LED
-Fade the next LED on and off
-When the button is pressed, keep the next LED at the current value and move onto the next LED

and so on..

I moved the code that switches between LEDs in a void to clean up the code a bit.

OK there is so much wrong with the code last posted it is untrue. I know it is difficult to write code without the hardware.

I am not sure where to go from here. That last code had:-

static const int blueLED = 4;
static const int redLED = 0;
static const int greenLED = 1;
static const int nullLED = 16;

Defining the pins that the LED was attached to.
You should not use pin 0 as that interferes with the serial communications. All the pins used should be capable of PWM outputs an pin 0 is not one of these. The LED needs to use these pins on a Uno:-
Pin 3, 5, 6, 9, 10, 11.
Also I have no idea what the nullLED pin is supposed to be doing, that is assigned to pin 16 which is analogue pin 2.

So let's start with the swap function. I am not a fan of a long string of if eles else if sort of thing as I think it is hard to follow so try this:-

void swap()
{
  if (selectedLED == greenLED) { selectedLED = redLED; return();}
  if (selectedLED == redLED) { selectedLED = blueLED; return();}
  if (selectedLED == blueLED) { selectedLED = greenLED; return();} 
}

You can probably do it more efficency but I think that is simple enough to understand
Do you need a state where no LED is changing?

I am not sure what the ultimate application of the code is but there are variables reserved for the values of the fade numbers but they are not used. Ideally these would be stored in the swap function by passing the fade value to it. In that way it could go in the right variable. This would mean that the fade function would return the fade value.

Grumpy_Mike:
I am not sure where to go from here. That last code had:-

static const int blueLED = 4;

static const int redLED = 0;
static const int greenLED = 1;
static const int nullLED = 16;



Defining the pins that the LED was attached to. 
You should not use pin 0 as that interferes with the serial communications. All the pins used should be capable of PWM outputs an pin 0 is not one of these. The LED needs to use these pins on a Uno:-
Pin 3, 5, 6, 9, 10, 11.
Also I have no idea what the nullLED pin is supposed to be doing, that is assigned to pin 16 which is analogue pin 2.

If you read the first post, i'm using an Attiny45 not a arduino uno and it does have PWM on pin 0, 1 and 4.
The nullLed is there because i want it to stop being in 'adjust mode' so pin 16 on attiny45 is nothing.

and the goal is to do it like this:

Tutorial 10 for Arduino: Interrupts and Hardware Debouncing - YouTube but instead of a distance sensor i want the led's to fade when in 'select mode'

Ok, do you need to have a record of the values or are you just content to have them stored on the analogue output pin?

I don't know, what would be the easiest?

Well it depends on if you want to use the values after you have set them. Or is this just a coding exercise?

Grumpy_Mike:
Well it depends on if you want to use the values after you have set them. Or is this just a coding exercise?

after i press the button, i want the current value of the led to remain.

Try this:-

 int buttonInt = 2;
//Setup the LEDs

// LED outputs
 int blueLED = 4;
 int redLED = 0;
 int greenLED = 1;
 int nullLED = 16; // dummy does nothing on this processor attiny45

/* "volatile" is not necessary.  Remove it. */
int selectedLED = greenLED;

void setup()
{
  pinMode (redLED, OUTPUT);
  pinMode (greenLED, OUTPUT);
  pinMode (blueLED, OUTPUT);
}

void fade()
{
  while ( true ) {
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
      analogWrite(selectedLED, fadeValue);    
      delay(30);
      if ( digitalRead(buttonInt) ) return;
    } 

    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
      analogWrite(selectedLED, fadeValue);  
      delay(30);
      if ( digitalRead(buttonInt) ) return;
    }
  }
}

void loop()
{  
  fade();
  delay(20); // debounce delay
  while(digitalRead(buttonInt)) { } // do nothing until button is released
  delay(20); // debounce delay
  swap();
}

void swap()
{
  if (selectedLED == greenLED) { selectedLED = redLED; return;}
  if (selectedLED == redLED) { selectedLED = blueLED; return;}
  if (selectedLED == blueLED) { selectedLED = nullLED; return;} 
  if (selectedLED == nullLED) { selectedLED = greenLED; return;} 
}