I want to change the brightness of and LED when i press one button and dimm it when i press another button. However when i take my finger off the button it will stop to were it has been dimmed to. Is it possible to do this and were can i find resources to help me with this.
that is easily doable, would you mind uploading your sketch so we may see it?
in Poseudo code you would have to do it like this:
int brightness = 0; //off
int ledpin = 13; //or any other having PWM support
void setup()
{
//declare pins as input and outputs.
analogWrite(ledpin, brightness);
}
void loop()
{
if(digitalRead(PIN_FOR_DIMMER) == 1)
{
brightness--;
//check if brightness drops below 0 and set it to it
}
if(digitalRead(PIN_FOR_BRIGHTER) == 1)
{
brightness++;
//check if brightness succeedes 1024 and set it to 1024
}
analogWrite(ledpin, brightness);
}
second though:
Do you want to keep it dimming or brightening depending on what you pressed last?
in that case you could do the following:
int brightness = 0; //off
int ledpin = 13; //or any other having PWM support
dimming = false;
brightening = false;
void setup()
{
//declare pins as input and outputs.
analogWrite(ledpin, brightness);
}
void loop()
{
if(digitalRead(PIN_FOR_DIMMER) == 1)
{
dimming = !dimming; //That way a second press will stop it
}
if(digitalRead(PIN_FOR_BRIGHTER) == 1)
{
brightening = !brightening; //That way a second press will stop it
}
if(dimming==true)
{
brightness--;
}
if (brightening==true)
{
brightness++;
}
analogWrite(ledpin, brightness);
}
I would like the LED to stop dimming or getting brighter when the button is not pressed. I would get a scetch but i have no idea if there is a circuit program that has arduino support the free version of yenka i have doesn't have a arduino on it.
Cheers Nachtwind
so in that case my first code would be what you look for.
The sketch is the code of your program, nothing else ,0)
You might need a delay or time counter in there, if you want the dimming to go slower. This might be important if your trying to control it physically.
Ok cool is there any way i can just get it to dimm really slowly.
Also as regards to the sketchs i kinda new so that term has been added to my word bank
as darudude already set, you should add a delay somewhere before or after the analogWrite(). Try something like delay(50) or delay(75)
int ledPin = 13;
int buttonPin = 2;
int buttonPin=4;
void setup()
{pinMode(ledPin,OUTPUT); pinMode(buttonPin, INPUT;}
if(buttonState 2 == HIGH)
{for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
analogWrite(ledPin, fadeValue);
delay(50);}
if(buttonState 4 == HIGH)
{for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(ledPin, fadeValue);
delay(50);}
does this work if not can you give me some pointers on where im going wrong( wanted to have ago myself other wise i wont learn anything and thats the point)
You've only put the code for "setup()" there.
"setup" only executes once, so you won't see anything that happened on the switch after the first few fractions of a second after reset.
You should put the stuff after the "pinMode" functions into "loop()" which gets called repeatedly.
const int ledPin = 13;
const int buttonPin = 2;
const int buttonPin=4;
void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(buttonPin, INPUT;
}
void loop () {
if(buttonState 2 == HIGH) {
//etc
ok cheers
int ledPin = 13;
int buttonPin = 2;
int buttonPin=4;
void setup()
{pinMode(ledPin,OUTPUT); pinMode(buttonPin, INPUT;}
void loop()
{if(buttonState 2 == HIGH)
{for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
analogWrite(ledPin, fadeValue);
delay(50);}
if(buttonState 4 == HIGH)
{for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(ledPin, fadeValue);
delay(50);} }
ok i have made modifications, does anybody see anything wrong with this before i up load it. bearing in mind it should do what the title descibes
a) it probably won't compile if(buttonState 2 == HIGH)
buttonState isn't declared, and buttonPin is declared twice.
b) it won't do what you want - you don't read buttonState anywhere
(uncompiled, untested)
const int ledPin = 13;
const int buttonPin0 = 2;
const int buttonPin1 = 4;
void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(buttonPin0, INPUT);
pinMode(buttonPin1, INPUT);
// You may wish to enable pull-ups here
// but be aware it may change the logic of the digitalReads below
}
void loop()
{
if(digitalRead (buttonPin0)) {
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
analogWrite(ledPin, fadeValue);
delay(50);
}
}
if(digitalRead (buttonPin1)) {
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(ledPin, fadeValue);
delay(50);
}
}
}
const int ledPin = 13;
const int buttonPin0 = 2;
const int buttonPin1 = 4;
void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(buttonPin0, INPUT);
pinMode(buttonPin1, INPUT);
}
void loop()
{buttonState = digitalRead(buttonPin0);
{if(buttonState == HIGH)
{for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
analogWrite(ledPin, fadeValue);
delay(50);}
buttonState = digitalRead(buttonPin1);
if(buttonState == HIGH)
{for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(ledPin, fadeValue);
delay(50);} }
Right i think i have got the program to read the buttons. if there is still something wrong with this sketch. Could somebody please help me out. thankyou for all your time
Maybe you can spot the errors yourself, if you see the code 'properly' formatted:
const int ledPin = 13;
const int buttonPin0 = 2;
const int buttonPin1 = 4;
void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(buttonPin0, INPUT);
pinMode(buttonPin1, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin0);
{
if (buttonState == HIGH)
{
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
analogWrite(ledPin, fadeValue);
delay(50);
}
buttonState = digitalRead(buttonPin1);
if (buttonState == HIGH)
{
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(ledPin, fadeValue);
delay(50);
}
}
cheers i think i get it now but it should still do what i want it to do? thanks again guys Also i cant spot error beacuse i only just about get basic code and ifr its formatted wrong or something else i have no clue.
have i got the {} brakets wrong beacuse that would be my first guess
AlphaBeta has already highlighted the problem; you're only looking at the second button if the first button is HIGH.
You need to sort out the braces, and using consistent indentation and formatting helps keep track of what is going on.
could you please use laymans terms. Is the code wrong or :-/ is it the way i put it together
From AlphaBeta's last post:
Each time through "loop" you read buttonPin0.
If it is HIGH, you fade up an LED from zero to 255 in steps of 5, with a 50ms delay between each new value (should take 250 /5 * 50ms = 2.5 seconds). Then you read the value of buttonPin1 and if it is high, you fade the same LED down over the same time period, but you ONLY do this if buttonPin0 was HIGH as well, because all of this code is in the first "if" clause.
Is this wrong?
I don't know, because I don't know if that is what you intended.
If you want to start a fade up or down, hit a button and pause the brightness at that point, then the code needs to be restructured so that during the fade, you look at the button, and if it is pressed, then you exit the loop, and then make sure that you don't try to do the same thing the next time "loop" gets called.
thankyou so much man. i will make the corrections. Thanks again for dumbing it down to my level big help both of you