Go Down

Topic: Weird glitchy stuff (Read 3 times) previous topic - next topic

Grumpy_Mike

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?

niekwoo10

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

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?

niekwoo10


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.

Grumpy_Mike

Try this:-

Code: [Select]
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;}
}

niekwoo10

Now it does (almost) exactly the same as before:

-The green LED fades
-When i press the button, the green and red start fading alternately
-When i press the button again, red stays on but green and blue just blink (what appears to be) randomly.

Grumpy_Mike

Quote
When i press the button, the green and red start fading alternately

Sounds like your button hardware is a bit faulty.
What happens when you hold the button down and keep it held, nothing should change until it is released.
Do a sketch just to read it and print out the value.
This sort of thing happens if the input is floating.
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Also add this to the setup() function:-
Code: [Select]
  pinMode ( buttonInt, INPUT);
All pins are input by default but it might not be the case on your processor.

niekwoo10


What happens when you hold the button down and keep it held, nothing should change until it is released.
...
Also add this to the setup() function:-
Code: [Select]
  pinMode ( buttonInt, INPUT);


Added
Code: [Select]
  pinMode ( buttonInt, INPUT); to the code and when i hold the button down, the green led stops fading. when i release the button, the green and red led start fading alternately again.

my setup: one side of the button connected to pin 2 and a 10K resistor that goes to ground and the other side connected to 5V.

What's wrong?

Grumpy_Mike

Quote
when i release the button, the green and red led start fading

And does that happen when you are touching none of the wires as well?

Replace:-
Code: [Select]
int selectedLED = greenLED;
with
Code: [Select]
int selectedLED = redLED;
Now it should be that the behavior is the same, the red LED will be fine at first. If not then you have wired up the LEDs wrong.

But basically you have some sort of hardware fault. I think it is not wired up as you think it is.

niekwoo10

Quote

Quote
when i release the button, the green and red led start fading

And does that happen when you are touching none of the wires as well?


Nope. but on your site you are using 3K pulldown resistors, i'm using 10K. Does that matter?

Grumpy_Mike

Well I would not use pull down resistors at all. I would connect the button between input and ground and attach a pull up resistor of 10K.
Then you have to invert the logic inside the code by putting a ! in front of the digital read.
Did you do the experiment I asked you to do, putting the start to a different colour?

niekwoo10

Yep i did that.

I also changed the swap() to
Code: [Select]

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


and now it kind of works but the green LED keeps blinking when it's supposed to be set..

niekwoo10

I'll try the pull-up resistor.

Coding Badly


The LED on the Uno (digital 2 on the ATtiny45; digital 13 on the Uno) is interfering.  Move the button to digital 3.

Do NOT try to access invalid pins.  The Tiny Core (like the Arduino Core) DOES NOT VALIDATE PIN NUMBERS...

Code: [Select]
static const int nullLED = 16;
...
pinMode (nullLED, OUTPUT);  // <--- Bad news.  Don't do this.


The following code works.  I used the internal pullup on the button pin; the button will have to be wired to ground the pin when pressed.  The debounce technique works but is crude.

Code: [Select]
//Setup the Button

// This is a constant and should be marked as such...
static const int buttonInt = 3; // 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);
 
  pinMode (buttonInt, INPUT);
  digitalWrite (buttonInt, HIGH);
}


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()
{
  if ( selectedLED == nullLED )
  {
    while ( digitalRead(buttonInt) );
  }
  else
  {
    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(30);
  while ( ! digitalRead(buttonInt) );
  swap();
}

niekwoo10

Quote

Move the button to digital 3.

why?

Quote

Code: [Select]
...


Used the code and moved the button; now it just does exactly the same as 2 posts before:
-Green LED starts fading
-When i press the button the green and red LED start fading alternately
etc.

Go Up