Pages: 1 [2] 3 4   Go Down
Author Topic: Weird glitchy stuff  (Read 3037 times)
0 Members and 1 Guest are viewing this topic.
Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:-

Code:
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;}
}
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  pinMode ( buttonInt, INPUT);
All pins are input by default but it might not be the case on your processor.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  pinMode ( buttonInt, INPUT);

Added
Code:
  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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int selectedLED = greenLED;
with
Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep i did that.

I also changed the swap() to
Code:
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..
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll try the pull-up resistor.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
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:
//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();
}
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Move the button to digital 3.
why?

Quote
Code:
...

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.
Logged

Pages: 1 [2] 3 4   Go Up
Jump to: