Manchester (England England)
Offline
Brattain Member
Karma: 272
Posts: 25466
Solder is electric glue
|
 |
« Reply #15 on: December 16, 2011, 12:04:44 pm » |
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
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #16 on: December 16, 2011, 12:16:47 pm » |
I don't know, what would be the easiest?
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 272
Posts: 25466
Solder is electric glue
|
 |
« Reply #17 on: December 16, 2011, 12:27:47 pm » |
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
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #18 on: December 16, 2011, 01:59:45 pm » |
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
Brattain Member
Karma: 272
Posts: 25466
Solder is electric glue
|
 |
« Reply #19 on: December 16, 2011, 03:01:24 pm » |
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;} }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #20 on: December 16, 2011, 03:10:24 pm » |
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
Brattain Member
Karma: 272
Posts: 25466
Solder is electric glue
|
 |
« Reply #21 on: December 16, 2011, 03:18:04 pm » |
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.htmlAlso add this to the setup() function:- pinMode ( buttonInt, INPUT); All pins are input by default but it might not be the case on your processor.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #22 on: December 16, 2011, 03:26:26 pm » |
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:- pinMode ( buttonInt, INPUT); Added 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
Brattain Member
Karma: 272
Posts: 25466
Solder is electric glue
|
 |
« Reply #23 on: December 16, 2011, 04:03:48 pm » |
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:- int selectedLED = greenLED; with 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
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #24 on: December 17, 2011, 02:41:15 am » |
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
Brattain Member
Karma: 272
Posts: 25466
Solder is electric glue
|
 |
« Reply #25 on: December 17, 2011, 02:48:35 am » |
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
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #26 on: December 17, 2011, 02:50:08 am » |
Yep i did that. I also changed the swap() to 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
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #27 on: December 17, 2011, 02:51:09 am » |
I'll try the pull-up resistor.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 116
Posts: 10144
|
 |
« Reply #28 on: December 17, 2011, 03:35:10 am » |
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... 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. //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
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #29 on: December 17, 2011, 04:23:11 am » |
Move the button to digital 3.
why? 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
|
|
|
|
|
|